blob: b177cc4c111eeaf13cd2f810422c47712cfc0b8b [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
NeilBrown8e0e99b2014-10-02 13:45:00 +100067static bool devices_handle_discard_safely = false;
68module_param(devices_handle_discard_safely, bool, 0644);
69MODULE_PARM_DESC(devices_handle_discard_safely,
70 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080071static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * Stripe cache
74 */
75
76#define NR_STRIPES 256
77#define STRIPE_SIZE PAGE_SIZE
78#define STRIPE_SHIFT (PAGE_SHIFT - 9)
79#define STRIPE_SECTORS (STRIPE_SIZE>>9)
80#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070081#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080082#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define HASH_MASK (NR_HASH - 1)
Shaohua Libfc90cb2013-08-29 15:40:32 +080084#define MAX_STRIPE_BATCH 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
NeilBrownd1688a62011-10-11 16:49:52 +110086static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110087{
88 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
89 return &conf->stripe_hashtbl[hash];
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Shaohua Li566c09c2013-11-14 15:16:17 +110092static inline int stripe_hash_locks_hash(sector_t sect)
93{
94 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
95}
96
97static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
98{
99 spin_lock_irq(conf->hash_locks + hash);
100 spin_lock(&conf->device_lock);
101}
102
103static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
104{
105 spin_unlock(&conf->device_lock);
106 spin_unlock_irq(conf->hash_locks + hash);
107}
108
109static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
110{
111 int i;
112 local_irq_disable();
113 spin_lock(conf->hash_locks);
114 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
115 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
116 spin_lock(&conf->device_lock);
117}
118
119static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
120{
121 int i;
122 spin_unlock(&conf->device_lock);
123 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
124 spin_unlock(conf->hash_locks + i - 1);
125 local_irq_enable();
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* bio's attached to a stripe+device for I/O are linked together in bi_sector
129 * order without overlap. There may be several bio's per stripe+device, and
130 * a bio could span several devices.
131 * When walking this list for a particular stripe+device, we must never proceed
132 * beyond a bio that extends past this device, as the next bio might no longer
133 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +1100134 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * of the current stripe+device
136 */
NeilBrowndb298e12011-10-07 14:23:00 +1100137static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
138{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800139 int sectors = bio_sectors(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700140 if (bio->bi_iter.bi_sector + sectors < sector + STRIPE_SECTORS)
NeilBrowndb298e12011-10-07 14:23:00 +1100141 return bio->bi_next;
142 else
143 return NULL;
144}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jens Axboe960e7392008-08-15 10:41:18 +0200146/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200147 * We maintain a biased count of active stripes in the bottom 16 bits of
148 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200149 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000150static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200151{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000152 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
153 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200154}
155
Shaohua Lie7836bd62012-07-19 16:01:31 +1000156static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200157{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000158 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
159 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200160}
161
Shaohua Lie7836bd62012-07-19 16:01:31 +1000162static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200163{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000164 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
165 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200166}
167
Shaohua Lie7836bd62012-07-19 16:01:31 +1000168static inline void raid5_set_bi_processed_stripes(struct bio *bio,
169 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200170{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000171 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
172 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200173
Shaohua Lie7836bd62012-07-19 16:01:31 +1000174 do {
175 old = atomic_read(segments);
176 new = (old & 0xffff) | (cnt << 16);
177 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200178}
179
Shaohua Lie7836bd62012-07-19 16:01:31 +1000180static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200181{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000182 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
183 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200184}
185
NeilBrownd0dabf72009-03-31 14:39:38 +1100186/* Find first data disk in a raid6 stripe */
187static inline int raid6_d0(struct stripe_head *sh)
188{
NeilBrown67cc2b82009-03-31 14:39:38 +1100189 if (sh->ddf_layout)
190 /* ddf always start from first device */
191 return 0;
192 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100193 if (sh->qd_idx == sh->disks - 1)
194 return 0;
195 else
196 return sh->qd_idx + 1;
197}
NeilBrown16a53ec2006-06-26 00:27:38 -0700198static inline int raid6_next_disk(int disk, int raid_disks)
199{
200 disk++;
201 return (disk < raid_disks) ? disk : 0;
202}
Dan Williamsa4456852007-07-09 11:56:43 -0700203
NeilBrownd0dabf72009-03-31 14:39:38 +1100204/* When walking through the disks in a raid5, starting at raid6_d0,
205 * We need to map each disk to a 'slot', where the data disks are slot
206 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
207 * is raid_disks-1. This help does that mapping.
208 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100209static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
210 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100211{
Dan Williams66295422009-10-19 18:09:32 -0700212 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100213
NeilBrowne4424fe2009-10-16 16:27:34 +1100214 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700215 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100216 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100217 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100218 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100219 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100220 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700221 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100222 return slot;
223}
224
Dan Williamsa4456852007-07-09 11:56:43 -0700225static void return_io(struct bio *return_bi)
226{
227 struct bio *bi = return_bi;
228 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700229
230 return_bi = bi->bi_next;
231 bi->bi_next = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700232 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700233 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
234 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000235 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700236 bi = return_bi;
237 }
238}
239
NeilBrownd1688a62011-10-11 16:49:52 +1100240static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Dan Williams600aa102008-06-28 08:32:05 +1000242static int stripe_operations_active(struct stripe_head *sh)
243{
244 return sh->check_state || sh->reconstruct_state ||
245 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
246 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
247}
248
Shaohua Li851c30c2013-08-28 14:30:16 +0800249static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
250{
251 struct r5conf *conf = sh->raid_conf;
252 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800253 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800254 int i, cpu = sh->cpu;
255
256 if (!cpu_online(cpu)) {
257 cpu = cpumask_any(cpu_online_mask);
258 sh->cpu = cpu;
259 }
260
261 if (list_empty(&sh->lru)) {
262 struct r5worker_group *group;
263 group = conf->worker_groups + cpu_to_group(cpu);
264 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800265 group->stripes_cnt++;
266 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800267 }
268
269 if (conf->worker_cnt_per_group == 0) {
270 md_wakeup_thread(conf->mddev->thread);
271 return;
272 }
273
274 group = conf->worker_groups + cpu_to_group(sh->cpu);
275
Shaohua Libfc90cb2013-08-29 15:40:32 +0800276 group->workers[0].working = true;
277 /* at least one worker should run to avoid race */
278 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
279
280 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
281 /* wakeup more workers */
282 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
283 if (group->workers[i].working == false) {
284 group->workers[i].working = true;
285 queue_work_on(sh->cpu, raid5_wq,
286 &group->workers[i].work);
287 thread_cnt--;
288 }
289 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800290}
291
Shaohua Li566c09c2013-11-14 15:16:17 +1100292static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
293 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000295 BUG_ON(!list_empty(&sh->lru));
296 BUG_ON(atomic_read(&conf->active_stripes)==0);
297 if (test_bit(STRIPE_HANDLE, &sh->state)) {
298 if (test_bit(STRIPE_DELAYED, &sh->state) &&
NeilBrown67f45542014-05-28 13:39:22 +1000299 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Shaohua Li4eb788d2012-07-19 16:01:31 +1000300 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown67f45542014-05-28 13:39:22 +1000301 if (atomic_read(&conf->preread_active_stripes)
302 < IO_THRESHOLD)
303 md_wakeup_thread(conf->mddev->thread);
304 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000305 sh->bm_seq - conf->seq_write > 0)
306 list_add_tail(&sh->lru, &conf->bitmap_list);
307 else {
308 clear_bit(STRIPE_DELAYED, &sh->state);
309 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800310 if (conf->worker_cnt_per_group == 0) {
311 list_add_tail(&sh->lru, &conf->handle_list);
312 } else {
313 raid5_wakeup_stripe_thread(sh);
314 return;
315 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000316 }
317 md_wakeup_thread(conf->mddev->thread);
318 } else {
319 BUG_ON(stripe_operations_active(sh));
320 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
321 if (atomic_dec_return(&conf->preread_active_stripes)
322 < IO_THRESHOLD)
323 md_wakeup_thread(conf->mddev->thread);
324 atomic_dec(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100325 if (!test_bit(STRIPE_EXPANDING, &sh->state))
326 list_add_tail(&sh->lru, temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328}
NeilBrownd0dabf72009-03-31 14:39:38 +1100329
Shaohua Li566c09c2013-11-14 15:16:17 +1100330static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
331 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000332{
333 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100334 do_release_stripe(conf, sh, temp_inactive_list);
335}
336
337/*
338 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
339 *
340 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
341 * given time. Adding stripes only takes device lock, while deleting stripes
342 * only takes hash lock.
343 */
344static void release_inactive_stripe_list(struct r5conf *conf,
345 struct list_head *temp_inactive_list,
346 int hash)
347{
348 int size;
349 bool do_wakeup = false;
350 unsigned long flags;
351
352 if (hash == NR_STRIPE_HASH_LOCKS) {
353 size = NR_STRIPE_HASH_LOCKS;
354 hash = NR_STRIPE_HASH_LOCKS - 1;
355 } else
356 size = 1;
357 while (size) {
358 struct list_head *list = &temp_inactive_list[size - 1];
359
360 /*
361 * We don't hold any lock here yet, get_active_stripe() might
362 * remove stripes from the list
363 */
364 if (!list_empty_careful(list)) {
365 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100366 if (list_empty(conf->inactive_list + hash) &&
367 !list_empty(list))
368 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100369 list_splice_tail_init(list, conf->inactive_list + hash);
370 do_wakeup = true;
371 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
372 }
373 size--;
374 hash--;
375 }
376
377 if (do_wakeup) {
378 wake_up(&conf->wait_for_stripe);
379 if (conf->retry_read_aligned)
380 md_wakeup_thread(conf->mddev->thread);
381 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000382}
383
Shaohua Li773ca822013-08-27 17:50:39 +0800384/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100385static int release_stripe_list(struct r5conf *conf,
386 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800387{
388 struct stripe_head *sh;
389 int count = 0;
390 struct llist_node *head;
391
392 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800393 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800394 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100395 int hash;
396
Shaohua Li773ca822013-08-27 17:50:39 +0800397 sh = llist_entry(head, struct stripe_head, release_list);
398 head = llist_next(head);
399 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
400 smp_mb();
401 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
402 /*
403 * Don't worry the bit is set here, because if the bit is set
404 * again, the count is always > 1. This is true for
405 * STRIPE_ON_UNPLUG_LIST bit too.
406 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100407 hash = sh->hash_lock_index;
408 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800409 count++;
410 }
411
412 return count;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static void release_stripe(struct stripe_head *sh)
416{
NeilBrownd1688a62011-10-11 16:49:52 +1100417 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100419 struct list_head list;
420 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800421 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700422
Eivind Sartocf170f32014-05-28 13:39:23 +1000423 /* Avoid release_list until the last reference.
424 */
425 if (atomic_add_unless(&sh->count, -1, 1))
426 return;
427
majianpengad4068d2013-11-14 15:16:15 +1100428 if (unlikely(!conf->mddev->thread) ||
429 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800430 goto slow_path;
431 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
432 if (wakeup)
433 md_wakeup_thread(conf->mddev->thread);
434 return;
435slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000436 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800437 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000438 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100439 INIT_LIST_HEAD(&list);
440 hash = sh->hash_lock_index;
441 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000442 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100443 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000444 }
445 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
NeilBrownfccddba2006-01-06 00:20:33 -0800448static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Dan Williams45b42332007-07-09 11:56:43 -0700450 pr_debug("remove_hash(), stripe %llu\n",
451 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
NeilBrownfccddba2006-01-06 00:20:33 -0800453 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
NeilBrownd1688a62011-10-11 16:49:52 +1100456static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
NeilBrownfccddba2006-01-06 00:20:33 -0800458 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Dan Williams45b42332007-07-09 11:56:43 -0700460 pr_debug("insert_hash(), stripe %llu\n",
461 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
NeilBrownfccddba2006-01-06 00:20:33 -0800463 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466
467/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100468static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct stripe_head *sh = NULL;
471 struct list_head *first;
472
Shaohua Li566c09c2013-11-14 15:16:17 +1100473 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100475 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sh = list_entry(first, struct stripe_head, lru);
477 list_del_init(first);
478 remove_hash(sh);
479 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100480 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100481 if (list_empty(conf->inactive_list + hash))
482 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483out:
484 return sh;
485}
486
NeilBrowne4e11e32010-06-16 16:45:16 +1000487static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct page *p;
490 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000491 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
NeilBrowne4e11e32010-06-16 16:45:16 +1000493 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800494 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 p = sh->dev[i].page;
496 if (!p)
497 continue;
498 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800499 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501}
502
NeilBrowne4e11e32010-06-16 16:45:16 +1000503static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000506 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
NeilBrowne4e11e32010-06-16 16:45:16 +1000508 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct page *page;
510
511 if (!(page = alloc_page(GFP_KERNEL))) {
512 return 1;
513 }
514 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800515 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 return 0;
518}
519
NeilBrown784052e2009-03-31 15:19:07 +1100520static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100521static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100522 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
NeilBrownb5663ba2009-03-31 14:39:38 +1100524static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
NeilBrownd1688a62011-10-11 16:49:52 +1100526 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100527 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200529 BUG_ON(atomic_read(&sh->count) != 0);
530 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000531 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700532
Dan Williams45b42332007-07-09 11:56:43 -0700533 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 (unsigned long long)sh->sector);
535
536 remove_hash(sh);
Shaohua Li566c09c2013-11-14 15:16:17 +1100537retry:
538 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100539 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100540 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100542 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sh->state = 0;
544
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800545
546 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct r5dev *dev = &sh->dev[i];
548
Dan Williamsd84e0f12007-01-02 13:52:30 -0700549 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700551 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700553 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000555 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100558 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100560 if (read_seqcount_retry(&conf->gen_lock, seq))
561 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800563 sh->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
NeilBrownd1688a62011-10-11 16:49:52 +1100566static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100567 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct stripe_head *sh;
570
Dan Williams45b42332007-07-09 11:56:43 -0700571 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800572 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100573 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700575 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NULL;
577}
578
NeilBrown674806d2010-06-16 17:17:53 +1000579/*
580 * Need to check if array has failed when deciding whether to:
581 * - start an array
582 * - remove non-faulty devices
583 * - add a spare
584 * - allow a reshape
585 * This determination is simple when no reshape is happening.
586 * However if there is a reshape, we need to carefully check
587 * both the before and after sections.
588 * This is because some failed devices may only affect one
589 * of the two sections, and some non-in_sync devices may
590 * be insync in the section most affected by failed devices.
591 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100592static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000593{
NeilBrown908f4fb2011-12-23 10:17:50 +1100594 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000595 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000596
597 rcu_read_lock();
598 degraded = 0;
599 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100600 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000601 if (rdev && test_bit(Faulty, &rdev->flags))
602 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000603 if (!rdev || test_bit(Faulty, &rdev->flags))
604 degraded++;
605 else if (test_bit(In_sync, &rdev->flags))
606 ;
607 else
608 /* not in-sync or faulty.
609 * If the reshape increases the number of devices,
610 * this is being recovered by the reshape, so
611 * this 'previous' section is not in_sync.
612 * If the number of devices is being reduced however,
613 * the device can only be part of the array if
614 * we are reverting a reshape, so this section will
615 * be in-sync.
616 */
617 if (conf->raid_disks >= conf->previous_raid_disks)
618 degraded++;
619 }
620 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100621 if (conf->raid_disks == conf->previous_raid_disks)
622 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000623 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100624 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000625 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100626 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000627 if (rdev && test_bit(Faulty, &rdev->flags))
628 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000629 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100630 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000631 else if (test_bit(In_sync, &rdev->flags))
632 ;
633 else
634 /* not in-sync or faulty.
635 * If reshape increases the number of devices, this
636 * section has already been recovered, else it
637 * almost certainly hasn't.
638 */
639 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100640 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000641 }
642 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100643 if (degraded2 > degraded)
644 return degraded2;
645 return degraded;
646}
647
648static int has_failed(struct r5conf *conf)
649{
650 int degraded;
651
652 if (conf->mddev->reshape_position == MaxSector)
653 return conf->mddev->degraded > conf->max_degraded;
654
655 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000656 if (degraded > conf->max_degraded)
657 return 1;
658 return 0;
659}
660
NeilBrownb5663ba2009-03-31 14:39:38 +1100661static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100662get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000663 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100666 int hash = stripe_hash_locks_hash(sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dan Williams45b42332007-07-09 11:56:43 -0700668 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Shaohua Li566c09c2013-11-14 15:16:17 +1100670 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 do {
NeilBrown72626682005-09-09 16:23:54 -0700673 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000674 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100675 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100676 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!sh) {
678 if (!conf->inactive_blocked)
Shaohua Li566c09c2013-11-14 15:16:17 +1100679 sh = get_free_stripe(conf, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (noblock && sh == NULL)
681 break;
682 if (!sh) {
683 conf->inactive_blocked = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +1100684 wait_event_lock_irq(
685 conf->wait_for_stripe,
686 !list_empty(conf->inactive_list + hash) &&
687 (atomic_read(&conf->active_stripes)
688 < (conf->max_nr_stripes * 3 / 4)
689 || !conf->inactive_blocked),
690 *(conf->hash_locks + hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 conf->inactive_blocked = 0;
NeilBrown7da9d452014-01-22 11:45:03 +1100692 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100693 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100694 atomic_inc(&sh->count);
695 }
Shaohua Lie240c182014-04-09 11:27:42 +0800696 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100697 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800698 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!test_bit(STRIPE_HANDLE, &sh->state))
700 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100701 BUG_ON(list_empty(&sh->lru) &&
702 !test_bit(STRIPE_EXPANDING, &sh->state));
NeilBrown16a53ec2006-06-26 00:27:38 -0700703 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800704 if (sh->group) {
705 sh->group->stripes_cnt--;
706 sh->group = NULL;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
NeilBrown7da9d452014-01-22 11:45:03 +1100709 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100710 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 } while (sh == NULL);
713
Shaohua Li566c09c2013-11-14 15:16:17 +1100714 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return sh;
716}
717
NeilBrown05616be2012-05-21 09:27:00 +1000718/* Determine if 'data_offset' or 'new_data_offset' should be used
719 * in this stripe_head.
720 */
721static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
722{
723 sector_t progress = conf->reshape_progress;
724 /* Need a memory barrier to make sure we see the value
725 * of conf->generation, or ->data_offset that was set before
726 * reshape_progress was updated.
727 */
728 smp_rmb();
729 if (progress == MaxSector)
730 return 0;
731 if (sh->generation == conf->generation - 1)
732 return 0;
733 /* We are in a reshape, and this is a new-generation stripe,
734 * so use new_data_offset.
735 */
736 return 1;
737}
738
NeilBrown6712ecf2007-09-27 12:47:43 +0200739static void
740raid5_end_read_request(struct bio *bi, int error);
741static void
742raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700743
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000744static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700745{
NeilBrownd1688a62011-10-11 16:49:52 +1100746 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700747 int i, disks = sh->disks;
748
749 might_sleep();
750
751 for (i = disks; i--; ) {
752 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100753 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100754 struct bio *bi, *rbi;
755 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200756 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
757 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
758 rw = WRITE_FUA;
759 else
760 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100761 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100762 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200763 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700764 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100765 else if (test_and_clear_bit(R5_WantReplace,
766 &sh->dev[i].flags)) {
767 rw = WRITE;
768 replace_only = 1;
769 } else
Dan Williams91c00922007-01-02 13:52:30 -0700770 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000771 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
772 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700773
774 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100775 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700776
Dan Williams91c00922007-01-02 13:52:30 -0700777 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100778 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100779 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
780 rdev = rcu_dereference(conf->disks[i].rdev);
781 if (!rdev) {
782 rdev = rrdev;
783 rrdev = NULL;
784 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100785 if (rw & WRITE) {
786 if (replace_only)
787 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100788 if (rdev == rrdev)
789 /* We raced and saw duplicates */
790 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100791 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100792 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100793 rdev = rrdev;
794 rrdev = NULL;
795 }
NeilBrown977df362011-12-23 10:17:53 +1100796
Dan Williams91c00922007-01-02 13:52:30 -0700797 if (rdev && test_bit(Faulty, &rdev->flags))
798 rdev = NULL;
799 if (rdev)
800 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100801 if (rrdev && test_bit(Faulty, &rrdev->flags))
802 rrdev = NULL;
803 if (rrdev)
804 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700805 rcu_read_unlock();
806
NeilBrown73e92e52011-07-28 11:39:22 +1000807 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100808 * need to check for writes. We never accept write errors
809 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000810 */
811 while ((rw & WRITE) && rdev &&
812 test_bit(WriteErrorSeen, &rdev->flags)) {
813 sector_t first_bad;
814 int bad_sectors;
815 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
816 &first_bad, &bad_sectors);
817 if (!bad)
818 break;
819
820 if (bad < 0) {
821 set_bit(BlockedBadBlocks, &rdev->flags);
822 if (!conf->mddev->external &&
823 conf->mddev->flags) {
824 /* It is very unlikely, but we might
825 * still need to write out the
826 * bad block log - better give it
827 * a chance*/
828 md_check_recovery(conf->mddev);
829 }
majianpeng18507532012-07-03 12:11:54 +1000830 /*
831 * Because md_wait_for_blocked_rdev
832 * will dec nr_pending, we must
833 * increment it first.
834 */
835 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000836 md_wait_for_blocked_rdev(rdev, conf->mddev);
837 } else {
838 /* Acknowledged bad block - skip the write */
839 rdev_dec_pending(rdev, conf->mddev);
840 rdev = NULL;
841 }
842 }
843
Dan Williams91c00922007-01-02 13:52:30 -0700844 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100845 if (s->syncing || s->expanding || s->expanded
846 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700847 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
848
Dan Williams2b7497f2008-06-28 08:31:52 +1000849 set_bit(STRIPE_IO_STARTED, &sh->state);
850
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700851 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700852 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700853 bi->bi_rw = rw;
854 bi->bi_end_io = (rw & WRITE)
855 ? raid5_end_write_request
856 : raid5_end_read_request;
857 bi->bi_private = sh;
858
Dan Williams91c00922007-01-02 13:52:30 -0700859 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700860 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700861 bi->bi_rw, i);
862 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000863 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700864 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000865 + rdev->new_data_offset);
866 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700867 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000868 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000869 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
majianpenge59aa232013-11-14 15:16:19 +1100870 bi->bi_rw |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +1000871
Shaohua Lid592a992014-05-21 17:57:44 +0800872 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
873 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
874 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +0200875 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700876 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
877 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700878 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800879 /*
880 * If this is discard request, set bi_vcnt 0. We don't
881 * want to confuse SCSI because SCSI will replace payload
882 */
883 if (rw & REQ_DISCARD)
884 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +1100885 if (rrdev)
886 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600887
888 if (conf->mddev->gendisk)
889 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
890 bi, disk_devt(conf->mddev->gendisk),
891 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700892 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100893 }
894 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100895 if (s->syncing || s->expanding || s->expanded
896 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100897 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
898
899 set_bit(STRIPE_IO_STARTED, &sh->state);
900
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700901 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100902 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700903 rbi->bi_rw = rw;
904 BUG_ON(!(rw & WRITE));
905 rbi->bi_end_io = raid5_end_write_request;
906 rbi->bi_private = sh;
907
NeilBrown977df362011-12-23 10:17:53 +1100908 pr_debug("%s: for %llu schedule op %ld on "
909 "replacement disc %d\n",
910 __func__, (unsigned long long)sh->sector,
911 rbi->bi_rw, i);
912 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000913 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700914 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000915 + rrdev->new_data_offset);
916 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700917 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000918 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +0800919 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
920 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
921 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +0200922 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100923 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
924 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700925 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800926 /*
927 * If this is discard request, set bi_vcnt 0. We don't
928 * want to confuse SCSI because SCSI will replace payload
929 */
930 if (rw & REQ_DISCARD)
931 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600932 if (conf->mddev->gendisk)
933 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
934 rbi, disk_devt(conf->mddev->gendisk),
935 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100936 generic_make_request(rbi);
937 }
938 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000939 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700940 set_bit(STRIPE_DEGRADED, &sh->state);
941 pr_debug("skip op %ld on disc %d for sector %llu\n",
942 bi->bi_rw, i, (unsigned long long)sh->sector);
943 clear_bit(R5_LOCKED, &sh->dev[i].flags);
944 set_bit(STRIPE_HANDLE, &sh->state);
945 }
946 }
947}
948
949static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +0800950async_copy_data(int frombio, struct bio *bio, struct page **page,
951 sector_t sector, struct dma_async_tx_descriptor *tx,
952 struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -0700953{
Kent Overstreet79886132013-11-23 17:19:00 -0800954 struct bio_vec bvl;
955 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -0700956 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -0700957 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700958 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700959 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700960
Kent Overstreet4f024f32013-10-11 15:44:27 -0700961 if (bio->bi_iter.bi_sector >= sector)
962 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -0700963 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700964 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700965
Dan Williams0403e382009-09-08 17:42:50 -0700966 if (frombio)
967 flags |= ASYNC_TX_FENCE;
968 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
969
Kent Overstreet79886132013-11-23 17:19:00 -0800970 bio_for_each_segment(bvl, bio, iter) {
971 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700972 int clen;
973 int b_offset = 0;
974
975 if (page_offset < 0) {
976 b_offset = -page_offset;
977 page_offset += b_offset;
978 len -= b_offset;
979 }
980
981 if (len > 0 && page_offset + len > STRIPE_SIZE)
982 clen = STRIPE_SIZE - page_offset;
983 else
984 clen = len;
985
986 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -0800987 b_offset += bvl.bv_offset;
988 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +0800989 if (frombio) {
990 if (sh->raid_conf->skip_copy &&
991 b_offset == 0 && page_offset == 0 &&
992 clen == STRIPE_SIZE)
993 *page = bio_page;
994 else
995 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700996 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +0800997 } else
998 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700999 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001000 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001001 /* chain the operations */
1002 submit.depend_tx = tx;
1003
Dan Williams91c00922007-01-02 13:52:30 -07001004 if (clen < len) /* hit end of page */
1005 break;
1006 page_offset += len;
1007 }
1008
1009 return tx;
1010}
1011
1012static void ops_complete_biofill(void *stripe_head_ref)
1013{
1014 struct stripe_head *sh = stripe_head_ref;
1015 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -07001016 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001017
Harvey Harrisone46b2722008-04-28 02:15:50 -07001018 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001019 (unsigned long long)sh->sector);
1020
1021 /* clear completed biofills */
1022 for (i = sh->disks; i--; ) {
1023 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001024
1025 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001026 /* and check if we need to reply to a read request,
1027 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001028 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001029 */
1030 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001031 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001032
Dan Williams91c00922007-01-02 13:52:30 -07001033 BUG_ON(!dev->read);
1034 rbi = dev->read;
1035 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001036 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001037 dev->sector + STRIPE_SECTORS) {
1038 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10001039 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -07001040 rbi->bi_next = return_bi;
1041 return_bi = rbi;
1042 }
Dan Williams91c00922007-01-02 13:52:30 -07001043 rbi = rbi2;
1044 }
1045 }
1046 }
Dan Williams83de75c2008-06-28 08:31:58 +10001047 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001048
1049 return_io(return_bi);
1050
Dan Williamse4d84902007-09-24 10:06:13 -07001051 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001052 release_stripe(sh);
1053}
1054
1055static void ops_run_biofill(struct stripe_head *sh)
1056{
1057 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001058 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001059 int i;
1060
Harvey Harrisone46b2722008-04-28 02:15:50 -07001061 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001062 (unsigned long long)sh->sector);
1063
1064 for (i = sh->disks; i--; ) {
1065 struct r5dev *dev = &sh->dev[i];
1066 if (test_bit(R5_Wantfill, &dev->flags)) {
1067 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001068 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001069 dev->read = rbi = dev->toread;
1070 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001071 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001072 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001073 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001074 tx = async_copy_data(0, rbi, &dev->page,
1075 dev->sector, tx, sh);
Dan Williams91c00922007-01-02 13:52:30 -07001076 rbi = r5_next_bio(rbi, dev->sector);
1077 }
1078 }
1079 }
1080
1081 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001082 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1083 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001084}
1085
Dan Williams4e7d2c02009-08-29 19:13:11 -07001086static void mark_target_uptodate(struct stripe_head *sh, int target)
1087{
1088 struct r5dev *tgt;
1089
1090 if (target < 0)
1091 return;
1092
1093 tgt = &sh->dev[target];
1094 set_bit(R5_UPTODATE, &tgt->flags);
1095 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1096 clear_bit(R5_Wantcompute, &tgt->flags);
1097}
1098
Dan Williamsac6b53b2009-07-14 13:40:19 -07001099static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001100{
1101 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001102
Harvey Harrisone46b2722008-04-28 02:15:50 -07001103 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001104 (unsigned long long)sh->sector);
1105
Dan Williamsac6b53b2009-07-14 13:40:19 -07001106 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001107 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001108 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001109
Dan Williamsecc65c92008-06-28 08:31:57 +10001110 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1111 if (sh->check_state == check_state_compute_run)
1112 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001113 set_bit(STRIPE_HANDLE, &sh->state);
1114 release_stripe(sh);
1115}
1116
Dan Williamsd6f38f32009-07-14 11:50:52 -07001117/* return a pointer to the address conversion region of the scribble buffer */
1118static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1119 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001120{
Dan Williamsd6f38f32009-07-14 11:50:52 -07001121 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
1122}
1123
1124static struct dma_async_tx_descriptor *
1125ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1126{
Dan Williams91c00922007-01-02 13:52:30 -07001127 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001128 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001129 int target = sh->ops.target;
1130 struct r5dev *tgt = &sh->dev[target];
1131 struct page *xor_dest = tgt->page;
1132 int count = 0;
1133 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001134 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001135 int i;
1136
1137 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001138 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001139 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1140
1141 for (i = disks; i--; )
1142 if (i != target)
1143 xor_srcs[count++] = sh->dev[i].page;
1144
1145 atomic_inc(&sh->count);
1146
Dan Williams0403e382009-09-08 17:42:50 -07001147 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001148 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -07001149 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001150 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001151 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001152 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001153
Dan Williams91c00922007-01-02 13:52:30 -07001154 return tx;
1155}
1156
Dan Williamsac6b53b2009-07-14 13:40:19 -07001157/* set_syndrome_sources - populate source buffers for gen_syndrome
1158 * @srcs - (struct page *) array of size sh->disks
1159 * @sh - stripe_head to parse
1160 *
1161 * Populates srcs in proper layout order for the stripe and returns the
1162 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1163 * destination buffer is recorded in srcs[count] and the Q destination
1164 * is recorded in srcs[count+1]].
1165 */
1166static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1167{
1168 int disks = sh->disks;
1169 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1170 int d0_idx = raid6_d0(sh);
1171 int count;
1172 int i;
1173
1174 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001175 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001176
1177 count = 0;
1178 i = d0_idx;
1179 do {
1180 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1181
1182 srcs[slot] = sh->dev[i].page;
1183 i = raid6_next_disk(i, disks);
1184 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001185
NeilBrowne4424fe2009-10-16 16:27:34 +11001186 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001187}
1188
1189static struct dma_async_tx_descriptor *
1190ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1191{
1192 int disks = sh->disks;
1193 struct page **blocks = percpu->scribble;
1194 int target;
1195 int qd_idx = sh->qd_idx;
1196 struct dma_async_tx_descriptor *tx;
1197 struct async_submit_ctl submit;
1198 struct r5dev *tgt;
1199 struct page *dest;
1200 int i;
1201 int count;
1202
1203 if (sh->ops.target < 0)
1204 target = sh->ops.target2;
1205 else if (sh->ops.target2 < 0)
1206 target = sh->ops.target;
1207 else
1208 /* we should only have one valid target */
1209 BUG();
1210 BUG_ON(target < 0);
1211 pr_debug("%s: stripe %llu block: %d\n",
1212 __func__, (unsigned long long)sh->sector, target);
1213
1214 tgt = &sh->dev[target];
1215 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1216 dest = tgt->page;
1217
1218 atomic_inc(&sh->count);
1219
1220 if (target == qd_idx) {
1221 count = set_syndrome_sources(blocks, sh);
1222 blocks[count] = NULL; /* regenerating p is not necessary */
1223 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001224 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1225 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001226 to_addr_conv(sh, percpu));
1227 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1228 } else {
1229 /* Compute any data- or p-drive using XOR */
1230 count = 0;
1231 for (i = disks; i-- ; ) {
1232 if (i == target || i == qd_idx)
1233 continue;
1234 blocks[count++] = sh->dev[i].page;
1235 }
1236
Dan Williams0403e382009-09-08 17:42:50 -07001237 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1238 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001239 to_addr_conv(sh, percpu));
1240 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1241 }
1242
1243 return tx;
1244}
1245
1246static struct dma_async_tx_descriptor *
1247ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1248{
1249 int i, count, disks = sh->disks;
1250 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1251 int d0_idx = raid6_d0(sh);
1252 int faila = -1, failb = -1;
1253 int target = sh->ops.target;
1254 int target2 = sh->ops.target2;
1255 struct r5dev *tgt = &sh->dev[target];
1256 struct r5dev *tgt2 = &sh->dev[target2];
1257 struct dma_async_tx_descriptor *tx;
1258 struct page **blocks = percpu->scribble;
1259 struct async_submit_ctl submit;
1260
1261 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1262 __func__, (unsigned long long)sh->sector, target, target2);
1263 BUG_ON(target < 0 || target2 < 0);
1264 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1265 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1266
Dan Williams6c910a72009-09-16 12:24:54 -07001267 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001268 * slot number conversion for 'faila' and 'failb'
1269 */
1270 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001271 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001272 count = 0;
1273 i = d0_idx;
1274 do {
1275 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1276
1277 blocks[slot] = sh->dev[i].page;
1278
1279 if (i == target)
1280 faila = slot;
1281 if (i == target2)
1282 failb = slot;
1283 i = raid6_next_disk(i, disks);
1284 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001285
1286 BUG_ON(faila == failb);
1287 if (failb < faila)
1288 swap(faila, failb);
1289 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1290 __func__, (unsigned long long)sh->sector, faila, failb);
1291
1292 atomic_inc(&sh->count);
1293
1294 if (failb == syndrome_disks+1) {
1295 /* Q disk is one of the missing disks */
1296 if (faila == syndrome_disks) {
1297 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001298 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1299 ops_complete_compute, sh,
1300 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001301 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001302 STRIPE_SIZE, &submit);
1303 } else {
1304 struct page *dest;
1305 int data_target;
1306 int qd_idx = sh->qd_idx;
1307
1308 /* Missing D+Q: recompute D from P, then recompute Q */
1309 if (target == qd_idx)
1310 data_target = target2;
1311 else
1312 data_target = target;
1313
1314 count = 0;
1315 for (i = disks; i-- ; ) {
1316 if (i == data_target || i == qd_idx)
1317 continue;
1318 blocks[count++] = sh->dev[i].page;
1319 }
1320 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001321 init_async_submit(&submit,
1322 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1323 NULL, NULL, NULL,
1324 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001325 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1326 &submit);
1327
1328 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001329 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1330 ops_complete_compute, sh,
1331 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001332 return async_gen_syndrome(blocks, 0, count+2,
1333 STRIPE_SIZE, &submit);
1334 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001335 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001336 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1337 ops_complete_compute, sh,
1338 to_addr_conv(sh, percpu));
1339 if (failb == syndrome_disks) {
1340 /* We're missing D+P. */
1341 return async_raid6_datap_recov(syndrome_disks+2,
1342 STRIPE_SIZE, faila,
1343 blocks, &submit);
1344 } else {
1345 /* We're missing D+D. */
1346 return async_raid6_2data_recov(syndrome_disks+2,
1347 STRIPE_SIZE, faila, failb,
1348 blocks, &submit);
1349 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001350 }
1351}
1352
1353
Dan Williams91c00922007-01-02 13:52:30 -07001354static void ops_complete_prexor(void *stripe_head_ref)
1355{
1356 struct stripe_head *sh = stripe_head_ref;
1357
Harvey Harrisone46b2722008-04-28 02:15:50 -07001358 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001359 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001360}
1361
1362static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001363ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1364 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001365{
Dan Williams91c00922007-01-02 13:52:30 -07001366 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001367 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001368 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001369 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001370
1371 /* existing parity data subtracted */
1372 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1373
Harvey Harrisone46b2722008-04-28 02:15:50 -07001374 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001375 (unsigned long long)sh->sector);
1376
1377 for (i = disks; i--; ) {
1378 struct r5dev *dev = &sh->dev[i];
1379 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001380 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001381 xor_srcs[count++] = dev->page;
1382 }
1383
Dan Williams0403e382009-09-08 17:42:50 -07001384 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001385 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001386 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001387
1388 return tx;
1389}
1390
1391static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001392ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001393{
1394 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001395 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001396
Harvey Harrisone46b2722008-04-28 02:15:50 -07001397 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001398 (unsigned long long)sh->sector);
1399
1400 for (i = disks; i--; ) {
1401 struct r5dev *dev = &sh->dev[i];
1402 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001403
Dan Williamsd8ee0722008-06-28 08:32:06 +10001404 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001405 struct bio *wbi;
1406
Shaohua Lib17459c2012-07-19 16:01:31 +10001407 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001408 chosen = dev->towrite;
1409 dev->towrite = NULL;
1410 BUG_ON(dev->written);
1411 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001412 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001413 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001414
Kent Overstreet4f024f32013-10-11 15:44:27 -07001415 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001416 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001417 if (wbi->bi_rw & REQ_FUA)
1418 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001419 if (wbi->bi_rw & REQ_SYNC)
1420 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001421 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001422 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001423 else {
1424 tx = async_copy_data(1, wbi, &dev->page,
1425 dev->sector, tx, sh);
1426 if (dev->page != dev->orig_page) {
1427 set_bit(R5_SkipCopy, &dev->flags);
1428 clear_bit(R5_UPTODATE, &dev->flags);
1429 clear_bit(R5_OVERWRITE, &dev->flags);
1430 }
1431 }
Dan Williams91c00922007-01-02 13:52:30 -07001432 wbi = r5_next_bio(wbi, dev->sector);
1433 }
1434 }
1435 }
1436
1437 return tx;
1438}
1439
Dan Williamsac6b53b2009-07-14 13:40:19 -07001440static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001441{
1442 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001443 int disks = sh->disks;
1444 int pd_idx = sh->pd_idx;
1445 int qd_idx = sh->qd_idx;
1446 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001447 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001448
Harvey Harrisone46b2722008-04-28 02:15:50 -07001449 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001450 (unsigned long long)sh->sector);
1451
Shaohua Libc0934f2012-05-22 13:55:05 +10001452 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001453 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001454 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001455 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001456 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001457
Dan Williams91c00922007-01-02 13:52:30 -07001458 for (i = disks; i--; ) {
1459 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001460
Tejun Heoe9c74692010-09-03 11:56:18 +02001461 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001462 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001463 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001464 if (fua)
1465 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001466 if (sync)
1467 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001468 }
Dan Williams91c00922007-01-02 13:52:30 -07001469 }
1470
Dan Williamsd8ee0722008-06-28 08:32:06 +10001471 if (sh->reconstruct_state == reconstruct_state_drain_run)
1472 sh->reconstruct_state = reconstruct_state_drain_result;
1473 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1474 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1475 else {
1476 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1477 sh->reconstruct_state = reconstruct_state_result;
1478 }
Dan Williams91c00922007-01-02 13:52:30 -07001479
1480 set_bit(STRIPE_HANDLE, &sh->state);
1481 release_stripe(sh);
1482}
1483
1484static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001485ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1486 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001487{
Dan Williams91c00922007-01-02 13:52:30 -07001488 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001489 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001490 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001491 int count = 0, pd_idx = sh->pd_idx, i;
1492 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001493 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001494 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001495
Harvey Harrisone46b2722008-04-28 02:15:50 -07001496 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001497 (unsigned long long)sh->sector);
1498
Shaohua Li620125f2012-10-11 13:49:05 +11001499 for (i = 0; i < sh->disks; i++) {
1500 if (pd_idx == i)
1501 continue;
1502 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1503 break;
1504 }
1505 if (i >= sh->disks) {
1506 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001507 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1508 ops_complete_reconstruct(sh);
1509 return;
1510 }
Dan Williams91c00922007-01-02 13:52:30 -07001511 /* check if prexor is active which means only process blocks
1512 * that are part of a read-modify-write (written)
1513 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001514 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1515 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001516 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1517 for (i = disks; i--; ) {
1518 struct r5dev *dev = &sh->dev[i];
1519 if (dev->written)
1520 xor_srcs[count++] = dev->page;
1521 }
1522 } else {
1523 xor_dest = sh->dev[pd_idx].page;
1524 for (i = disks; i--; ) {
1525 struct r5dev *dev = &sh->dev[i];
1526 if (i != pd_idx)
1527 xor_srcs[count++] = dev->page;
1528 }
1529 }
1530
Dan Williams91c00922007-01-02 13:52:30 -07001531 /* 1/ if we prexor'd then the dest is reused as a source
1532 * 2/ if we did not prexor then we are redoing the parity
1533 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1534 * for the synchronous xor case
1535 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001536 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001537 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1538
1539 atomic_inc(&sh->count);
1540
Dan Williamsac6b53b2009-07-14 13:40:19 -07001541 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001542 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001543 if (unlikely(count == 1))
1544 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1545 else
1546 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001547}
1548
Dan Williamsac6b53b2009-07-14 13:40:19 -07001549static void
1550ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1551 struct dma_async_tx_descriptor *tx)
1552{
1553 struct async_submit_ctl submit;
1554 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001555 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001556
1557 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1558
Shaohua Li620125f2012-10-11 13:49:05 +11001559 for (i = 0; i < sh->disks; i++) {
1560 if (sh->pd_idx == i || sh->qd_idx == i)
1561 continue;
1562 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1563 break;
1564 }
1565 if (i >= sh->disks) {
1566 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001567 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1568 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1569 ops_complete_reconstruct(sh);
1570 return;
1571 }
1572
Dan Williamsac6b53b2009-07-14 13:40:19 -07001573 count = set_syndrome_sources(blocks, sh);
1574
1575 atomic_inc(&sh->count);
1576
1577 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1578 sh, to_addr_conv(sh, percpu));
1579 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001580}
1581
1582static void ops_complete_check(void *stripe_head_ref)
1583{
1584 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001585
Harvey Harrisone46b2722008-04-28 02:15:50 -07001586 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001587 (unsigned long long)sh->sector);
1588
Dan Williamsecc65c92008-06-28 08:31:57 +10001589 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001590 set_bit(STRIPE_HANDLE, &sh->state);
1591 release_stripe(sh);
1592}
1593
Dan Williamsac6b53b2009-07-14 13:40:19 -07001594static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001595{
Dan Williams91c00922007-01-02 13:52:30 -07001596 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001597 int pd_idx = sh->pd_idx;
1598 int qd_idx = sh->qd_idx;
1599 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001600 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001601 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001602 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001603 int count;
1604 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001605
Harvey Harrisone46b2722008-04-28 02:15:50 -07001606 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001607 (unsigned long long)sh->sector);
1608
Dan Williamsac6b53b2009-07-14 13:40:19 -07001609 count = 0;
1610 xor_dest = sh->dev[pd_idx].page;
1611 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001612 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001613 if (i == pd_idx || i == qd_idx)
1614 continue;
1615 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001616 }
1617
Dan Williamsd6f38f32009-07-14 11:50:52 -07001618 init_async_submit(&submit, 0, NULL, NULL, NULL,
1619 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001620 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001621 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001622
Dan Williams91c00922007-01-02 13:52:30 -07001623 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001624 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1625 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001626}
1627
Dan Williamsac6b53b2009-07-14 13:40:19 -07001628static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1629{
1630 struct page **srcs = percpu->scribble;
1631 struct async_submit_ctl submit;
1632 int count;
1633
1634 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1635 (unsigned long long)sh->sector, checkp);
1636
1637 count = set_syndrome_sources(srcs, sh);
1638 if (!checkp)
1639 srcs[count] = NULL;
1640
1641 atomic_inc(&sh->count);
1642 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1643 sh, to_addr_conv(sh, percpu));
1644 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1645 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1646}
1647
NeilBrown51acbce2013-02-28 09:08:34 +11001648static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001649{
1650 int overlap_clear = 0, i, disks = sh->disks;
1651 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001652 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001653 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001654 struct raid5_percpu *percpu;
1655 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001656
Dan Williamsd6f38f32009-07-14 11:50:52 -07001657 cpu = get_cpu();
1658 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001659 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001660 ops_run_biofill(sh);
1661 overlap_clear++;
1662 }
1663
Dan Williams7b3a8712008-06-28 08:32:09 +10001664 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001665 if (level < 6)
1666 tx = ops_run_compute5(sh, percpu);
1667 else {
1668 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1669 tx = ops_run_compute6_1(sh, percpu);
1670 else
1671 tx = ops_run_compute6_2(sh, percpu);
1672 }
1673 /* terminate the chain if reconstruct is not set to be run */
1674 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001675 async_tx_ack(tx);
1676 }
Dan Williams91c00922007-01-02 13:52:30 -07001677
Dan Williams600aa102008-06-28 08:32:05 +10001678 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001679 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001680
Dan Williams600aa102008-06-28 08:32:05 +10001681 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001682 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001683 overlap_clear++;
1684 }
1685
Dan Williamsac6b53b2009-07-14 13:40:19 -07001686 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1687 if (level < 6)
1688 ops_run_reconstruct5(sh, percpu, tx);
1689 else
1690 ops_run_reconstruct6(sh, percpu, tx);
1691 }
Dan Williams91c00922007-01-02 13:52:30 -07001692
Dan Williamsac6b53b2009-07-14 13:40:19 -07001693 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1694 if (sh->check_state == check_state_run)
1695 ops_run_check_p(sh, percpu);
1696 else if (sh->check_state == check_state_run_q)
1697 ops_run_check_pq(sh, percpu, 0);
1698 else if (sh->check_state == check_state_run_pq)
1699 ops_run_check_pq(sh, percpu, 1);
1700 else
1701 BUG();
1702 }
Dan Williams91c00922007-01-02 13:52:30 -07001703
Dan Williams91c00922007-01-02 13:52:30 -07001704 if (overlap_clear)
1705 for (i = disks; i--; ) {
1706 struct r5dev *dev = &sh->dev[i];
1707 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1708 wake_up(&sh->raid_conf->wait_for_overlap);
1709 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001710 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001711}
1712
Shaohua Li566c09c2013-11-14 15:16:17 +11001713static int grow_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001716 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001717 if (!sh)
1718 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001719
NeilBrown3f294f42005-11-08 21:39:25 -08001720 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001721
Shaohua Lib17459c2012-07-19 16:01:31 +10001722 spin_lock_init(&sh->stripe_lock);
1723
NeilBrowne4e11e32010-06-16 16:45:16 +10001724 if (grow_buffers(sh)) {
1725 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001726 kmem_cache_free(conf->slab_cache, sh);
1727 return 0;
1728 }
Shaohua Li566c09c2013-11-14 15:16:17 +11001729 sh->hash_lock_index = hash;
NeilBrown3f294f42005-11-08 21:39:25 -08001730 /* we just created an active stripe so... */
1731 atomic_set(&sh->count, 1);
1732 atomic_inc(&conf->active_stripes);
1733 INIT_LIST_HEAD(&sh->lru);
1734 release_stripe(sh);
1735 return 1;
1736}
1737
NeilBrownd1688a62011-10-11 16:49:52 +11001738static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001739{
Christoph Lametere18b8902006-12-06 20:33:20 -08001740 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001741 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Shaohua Li566c09c2013-11-14 15:16:17 +11001742 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
NeilBrownf4be6b42010-06-01 19:37:25 +10001744 if (conf->mddev->gendisk)
1745 sprintf(conf->cache_name[0],
1746 "raid%d-%s", conf->level, mdname(conf->mddev));
1747 else
1748 sprintf(conf->cache_name[0],
1749 "raid%d-%p", conf->level, conf->mddev);
1750 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1751
NeilBrownad01c9e2006-03-27 01:18:07 -08001752 conf->active_name = 0;
1753 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001755 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (!sc)
1757 return 1;
1758 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001759 conf->pool_size = devs;
Shaohua Li566c09c2013-11-14 15:16:17 +11001760 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
1761 while (num--) {
1762 if (!grow_one_stripe(conf, hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11001764 conf->max_nr_stripes++;
1765 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768}
NeilBrown29269552006-03-27 01:18:10 -08001769
Dan Williamsd6f38f32009-07-14 11:50:52 -07001770/**
1771 * scribble_len - return the required size of the scribble region
1772 * @num - total number of disks in the array
1773 *
1774 * The size must be enough to contain:
1775 * 1/ a struct page pointer for each device in the array +2
1776 * 2/ room to convert each entry in (1) to its corresponding dma
1777 * (dma_map_page()) or page (page_address()) address.
1778 *
1779 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1780 * calculate over all devices (not just the data blocks), using zeros in place
1781 * of the P and Q blocks.
1782 */
1783static size_t scribble_len(int num)
1784{
1785 size_t len;
1786
1787 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1788
1789 return len;
1790}
1791
NeilBrownd1688a62011-10-11 16:49:52 +11001792static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001793{
1794 /* Make all the stripes able to hold 'newsize' devices.
1795 * New slots in each stripe get 'page' set to a new page.
1796 *
1797 * This happens in stages:
1798 * 1/ create a new kmem_cache and allocate the required number of
1799 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001800 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001801 * to the new stripe_heads. This will have the side effect of
1802 * freezing the array as once all stripe_heads have been collected,
1803 * no IO will be possible. Old stripe heads are freed once their
1804 * pages have been transferred over, and the old kmem_cache is
1805 * freed when all stripes are done.
1806 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1807 * we simple return a failre status - no need to clean anything up.
1808 * 4/ allocate new pages for the new slots in the new stripe_heads.
1809 * If this fails, we don't bother trying the shrink the
1810 * stripe_heads down again, we just leave them as they are.
1811 * As each stripe_head is processed the new one is released into
1812 * active service.
1813 *
1814 * Once step2 is started, we cannot afford to wait for a write,
1815 * so we use GFP_NOIO allocations.
1816 */
1817 struct stripe_head *osh, *nsh;
1818 LIST_HEAD(newstripes);
1819 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001820 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001821 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001822 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001823 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11001824 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08001825
1826 if (newsize <= conf->pool_size)
1827 return 0; /* never bother to shrink */
1828
Dan Williamsb5470dc2008-06-27 21:44:04 -07001829 err = md_allow_write(conf->mddev);
1830 if (err)
1831 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001832
NeilBrownad01c9e2006-03-27 01:18:07 -08001833 /* Step 1 */
1834 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1835 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001836 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001837 if (!sc)
1838 return -ENOMEM;
1839
1840 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001841 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001842 if (!nsh)
1843 break;
1844
NeilBrownad01c9e2006-03-27 01:18:07 -08001845 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001846 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001847
1848 list_add(&nsh->lru, &newstripes);
1849 }
1850 if (i) {
1851 /* didn't get enough, give up */
1852 while (!list_empty(&newstripes)) {
1853 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1854 list_del(&nsh->lru);
1855 kmem_cache_free(sc, nsh);
1856 }
1857 kmem_cache_destroy(sc);
1858 return -ENOMEM;
1859 }
1860 /* Step 2 - Must use GFP_NOIO now.
1861 * OK, we have enough stripes, start collecting inactive
1862 * stripes and copying them over
1863 */
Shaohua Li566c09c2013-11-14 15:16:17 +11001864 hash = 0;
1865 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08001866 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11001867 lock_device_hash_lock(conf, hash);
1868 wait_event_cmd(conf->wait_for_stripe,
1869 !list_empty(conf->inactive_list + hash),
1870 unlock_device_hash_lock(conf, hash),
1871 lock_device_hash_lock(conf, hash));
1872 osh = get_free_stripe(conf, hash);
1873 unlock_device_hash_lock(conf, hash);
NeilBrownad01c9e2006-03-27 01:18:07 -08001874 atomic_set(&nsh->count, 1);
Shaohua Lid592a992014-05-21 17:57:44 +08001875 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08001876 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08001877 nsh->dev[i].orig_page = osh->dev[i].page;
1878 }
NeilBrownad01c9e2006-03-27 01:18:07 -08001879 for( ; i<newsize; i++)
1880 nsh->dev[i].page = NULL;
Shaohua Li566c09c2013-11-14 15:16:17 +11001881 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08001882 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11001883 cnt++;
1884 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
1885 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
1886 hash++;
1887 cnt = 0;
1888 }
NeilBrownad01c9e2006-03-27 01:18:07 -08001889 }
1890 kmem_cache_destroy(conf->slab_cache);
1891
1892 /* Step 3.
1893 * At this point, we are holding all the stripes so the array
1894 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001895 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001896 */
1897 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1898 if (ndisks) {
1899 for (i=0; i<conf->raid_disks; i++)
1900 ndisks[i] = conf->disks[i];
1901 kfree(conf->disks);
1902 conf->disks = ndisks;
1903 } else
1904 err = -ENOMEM;
1905
Dan Williamsd6f38f32009-07-14 11:50:52 -07001906 get_online_cpus();
1907 conf->scribble_len = scribble_len(newsize);
1908 for_each_present_cpu(cpu) {
1909 struct raid5_percpu *percpu;
1910 void *scribble;
1911
1912 percpu = per_cpu_ptr(conf->percpu, cpu);
1913 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1914
1915 if (scribble) {
1916 kfree(percpu->scribble);
1917 percpu->scribble = scribble;
1918 } else {
1919 err = -ENOMEM;
1920 break;
1921 }
1922 }
1923 put_online_cpus();
1924
NeilBrownad01c9e2006-03-27 01:18:07 -08001925 /* Step 4, return new stripes to service */
1926 while(!list_empty(&newstripes)) {
1927 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1928 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001929
NeilBrownad01c9e2006-03-27 01:18:07 -08001930 for (i=conf->raid_disks; i < newsize; i++)
1931 if (nsh->dev[i].page == NULL) {
1932 struct page *p = alloc_page(GFP_NOIO);
1933 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08001934 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08001935 if (!p)
1936 err = -ENOMEM;
1937 }
1938 release_stripe(nsh);
1939 }
1940 /* critical section pass, GFP_NOIO no longer needed */
1941
1942 conf->slab_cache = sc;
1943 conf->active_name = 1-conf->active_name;
1944 conf->pool_size = newsize;
1945 return err;
1946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Shaohua Li566c09c2013-11-14 15:16:17 +11001948static int drop_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 struct stripe_head *sh;
1951
Shaohua Li566c09c2013-11-14 15:16:17 +11001952 spin_lock_irq(conf->hash_locks + hash);
1953 sh = get_free_stripe(conf, hash);
1954 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08001955 if (!sh)
1956 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001957 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001958 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001959 kmem_cache_free(conf->slab_cache, sh);
1960 atomic_dec(&conf->active_stripes);
1961 return 1;
1962}
1963
NeilBrownd1688a62011-10-11 16:49:52 +11001964static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001965{
Shaohua Li566c09c2013-11-14 15:16:17 +11001966 int hash;
1967 for (hash = 0; hash < NR_STRIPE_HASH_LOCKS; hash++)
1968 while (drop_one_stripe(conf, hash))
1969 ;
NeilBrown3f294f42005-11-08 21:39:25 -08001970
NeilBrown29fc7e32006-02-03 03:03:41 -08001971 if (conf->slab_cache)
1972 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 conf->slab_cache = NULL;
1974}
1975
NeilBrown6712ecf2007-09-27 12:47:43 +02001976static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
NeilBrown99c0fb52009-03-31 14:39:38 +11001978 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001979 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001980 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001982 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001983 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001984 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 for (i=0 ; i<disks; i++)
1987 if (bi == &sh->dev[i].req)
1988 break;
1989
Dan Williams45b42332007-07-09 11:56:43 -07001990 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1991 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 uptodate);
1993 if (i == disks) {
1994 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001995 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
NeilBrown14a75d32011-12-23 10:17:52 +11001997 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001998 /* If replacement finished while this request was outstanding,
1999 * 'replacement' might be NULL already.
2000 * In that case it moved down to 'rdev'.
2001 * rdev is not removed until all requests are finished.
2002 */
NeilBrown14a75d32011-12-23 10:17:52 +11002003 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002004 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002005 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
NeilBrown05616be2012-05-21 09:27:00 +10002007 if (use_new_offset(conf, sh))
2008 s = sh->sector + rdev->new_data_offset;
2009 else
2010 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002013 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002014 /* Note that this cannot happen on a
2015 * replacement device. We just fail those on
2016 * any error
2017 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10002018 printk_ratelimited(
2019 KERN_INFO
2020 "md/raid:%s: read error corrected"
2021 " (%lu sectors at %llu on %s)\n",
2022 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002023 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002024 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002025 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002026 clear_bit(R5_ReadError, &sh->dev[i].flags);
2027 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002028 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2029 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2030
NeilBrown14a75d32011-12-23 10:17:52 +11002031 if (atomic_read(&rdev->read_errors))
2032 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002034 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002035 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002036 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002039 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002040 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2041 printk_ratelimited(
2042 KERN_WARNING
2043 "md/raid:%s: read error on replacement device "
2044 "(sector %llu on %s).\n",
2045 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002046 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002047 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002048 else if (conf->mddev->degraded >= conf->max_degraded) {
2049 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002050 printk_ratelimited(
2051 KERN_WARNING
2052 "md/raid:%s: read error not correctable "
2053 "(sector %llu on %s).\n",
2054 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002055 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002056 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002057 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002058 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002059 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002060 printk_ratelimited(
2061 KERN_WARNING
2062 "md/raid:%s: read error NOT corrected!! "
2063 "(sector %llu on %s).\n",
2064 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002065 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002066 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002067 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002068 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002069 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002070 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002071 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002072 else
2073 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002074 if (set_bad && test_bit(In_sync, &rdev->flags)
2075 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2076 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002077 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002078 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2079 set_bit(R5_ReadError, &sh->dev[i].flags);
2080 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2081 } else
2082 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002083 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002084 clear_bit(R5_ReadError, &sh->dev[i].flags);
2085 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002086 if (!(set_bad
2087 && test_bit(In_sync, &rdev->flags)
2088 && rdev_set_badblocks(
2089 rdev, sh->sector, STRIPE_SECTORS, 0)))
2090 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
NeilBrown14a75d32011-12-23 10:17:52 +11002093 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2095 set_bit(STRIPE_HANDLE, &sh->state);
2096 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
NeilBrownd710e132008-10-13 11:55:12 +11002099static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
NeilBrown99c0fb52009-03-31 14:39:38 +11002101 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002102 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002103 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002104 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10002106 sector_t first_bad;
2107 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002108 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
NeilBrown977df362011-12-23 10:17:53 +11002110 for (i = 0 ; i < disks; i++) {
2111 if (bi == &sh->dev[i].req) {
2112 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 break;
NeilBrown977df362011-12-23 10:17:53 +11002114 }
2115 if (bi == &sh->dev[i].rreq) {
2116 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002117 if (rdev)
2118 replacement = 1;
2119 else
2120 /* rdev was removed and 'replacement'
2121 * replaced it. rdev is not removed
2122 * until all requests are finished.
2123 */
2124 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002125 break;
2126 }
2127 }
Dan Williams45b42332007-07-09 11:56:43 -07002128 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2130 uptodate);
2131 if (i == disks) {
2132 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002133 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135
NeilBrown977df362011-12-23 10:17:53 +11002136 if (replacement) {
2137 if (!uptodate)
2138 md_error(conf->mddev, rdev);
2139 else if (is_badblock(rdev, sh->sector,
2140 STRIPE_SECTORS,
2141 &first_bad, &bad_sectors))
2142 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2143 } else {
2144 if (!uptodate) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002145 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002146 set_bit(WriteErrorSeen, &rdev->flags);
2147 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002148 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2149 set_bit(MD_RECOVERY_NEEDED,
2150 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002151 } else if (is_badblock(rdev, sh->sector,
2152 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002153 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002154 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002155 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2156 /* That was a successful write so make
2157 * sure it looks like we already did
2158 * a re-write.
2159 */
2160 set_bit(R5_ReWrite, &sh->dev[i].flags);
2161 }
NeilBrown977df362011-12-23 10:17:53 +11002162 }
2163 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
NeilBrown977df362011-12-23 10:17:53 +11002165 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2166 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07002168 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
NeilBrown784052e2009-03-31 15:19:07 +11002171static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Shaohua Lid592a992014-05-21 17:57:44 +08002172
NeilBrown784052e2009-03-31 15:19:07 +11002173static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 struct r5dev *dev = &sh->dev[i];
2176
2177 bio_init(&dev->req);
2178 dev->req.bi_io_vec = &dev->vec;
Shaohua Lid592a992014-05-21 17:57:44 +08002179 dev->req.bi_max_vecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 dev->req.bi_private = sh;
2181
NeilBrown977df362011-12-23 10:17:53 +11002182 bio_init(&dev->rreq);
2183 dev->rreq.bi_io_vec = &dev->rvec;
Shaohua Lid592a992014-05-21 17:57:44 +08002184 dev->rreq.bi_max_vecs = 1;
NeilBrown977df362011-12-23 10:17:53 +11002185 dev->rreq.bi_private = sh;
NeilBrown977df362011-12-23 10:17:53 +11002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002188 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
NeilBrownfd01b882011-10-11 16:47:53 +11002191static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002194 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002195 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002196 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
NeilBrown908f4fb2011-12-23 10:17:50 +11002198 spin_lock_irqsave(&conf->device_lock, flags);
2199 clear_bit(In_sync, &rdev->flags);
2200 mddev->degraded = calc_degraded(conf);
2201 spin_unlock_irqrestore(&conf->device_lock, flags);
2202 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2203
NeilBrownde393cd2011-07-28 11:31:48 +10002204 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002205 set_bit(Faulty, &rdev->flags);
2206 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2207 printk(KERN_ALERT
2208 "md/raid:%s: Disk failure on %s, disabling device.\n"
2209 "md/raid:%s: Operation continuing on %d devices.\n",
2210 mdname(mddev),
2211 bdevname(rdev->bdev, b),
2212 mdname(mddev),
2213 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216/*
2217 * Input: a 'big' sector number,
2218 * Output: index of the data and parity disk, and the sector # in them.
2219 */
NeilBrownd1688a62011-10-11 16:49:52 +11002220static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002221 int previous, int *dd_idx,
2222 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002224 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002225 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002227 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002228 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002230 int algorithm = previous ? conf->prev_algo
2231 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002232 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2233 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002234 int raid_disks = previous ? conf->previous_raid_disks
2235 : conf->raid_disks;
2236 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 /* First compute the information on this sector */
2239
2240 /*
2241 * Compute the chunk number and the sector offset inside the chunk
2242 */
2243 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2244 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 /*
2247 * Compute the stripe number
2248 */
NeilBrown35f2a592010-04-20 14:13:34 +10002249 stripe = chunk_number;
2250 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002251 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /*
2253 * Select the parity disk based on the user selected algorithm.
2254 */
NeilBrown84789552011-07-27 11:00:36 +10002255 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002256 switch(conf->level) {
2257 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002258 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002259 break;
2260 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002261 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002263 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002264 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 (*dd_idx)++;
2266 break;
2267 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002268 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002269 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 (*dd_idx)++;
2271 break;
2272 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002273 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002274 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 break;
2276 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002277 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002278 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002280 case ALGORITHM_PARITY_0:
2281 pd_idx = 0;
2282 (*dd_idx)++;
2283 break;
2284 case ALGORITHM_PARITY_N:
2285 pd_idx = data_disks;
2286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002288 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002289 }
2290 break;
2291 case 6:
2292
NeilBrowne183eae2009-03-31 15:20:22 +11002293 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002294 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002295 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002296 qd_idx = pd_idx + 1;
2297 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002298 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002299 qd_idx = 0;
2300 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002301 (*dd_idx) += 2; /* D D P Q D */
2302 break;
2303 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002304 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002305 qd_idx = pd_idx + 1;
2306 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002307 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002308 qd_idx = 0;
2309 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002310 (*dd_idx) += 2; /* D D P Q D */
2311 break;
2312 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002313 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002314 qd_idx = (pd_idx + 1) % raid_disks;
2315 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002316 break;
2317 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002318 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002319 qd_idx = (pd_idx + 1) % raid_disks;
2320 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002321 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002322
2323 case ALGORITHM_PARITY_0:
2324 pd_idx = 0;
2325 qd_idx = 1;
2326 (*dd_idx) += 2;
2327 break;
2328 case ALGORITHM_PARITY_N:
2329 pd_idx = data_disks;
2330 qd_idx = data_disks + 1;
2331 break;
2332
2333 case ALGORITHM_ROTATING_ZERO_RESTART:
2334 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2335 * of blocks for computing Q is different.
2336 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002337 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002338 qd_idx = pd_idx + 1;
2339 if (pd_idx == raid_disks-1) {
2340 (*dd_idx)++; /* Q D D D P */
2341 qd_idx = 0;
2342 } else if (*dd_idx >= pd_idx)
2343 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002344 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002345 break;
2346
2347 case ALGORITHM_ROTATING_N_RESTART:
2348 /* Same a left_asymmetric, by first stripe is
2349 * D D D P Q rather than
2350 * Q D D D P
2351 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002352 stripe2 += 1;
2353 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002354 qd_idx = pd_idx + 1;
2355 if (pd_idx == raid_disks-1) {
2356 (*dd_idx)++; /* Q D D D P */
2357 qd_idx = 0;
2358 } else if (*dd_idx >= pd_idx)
2359 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002360 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002361 break;
2362
2363 case ALGORITHM_ROTATING_N_CONTINUE:
2364 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002365 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002366 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2367 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002368 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002369 break;
2370
2371 case ALGORITHM_LEFT_ASYMMETRIC_6:
2372 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002373 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002374 if (*dd_idx >= pd_idx)
2375 (*dd_idx)++;
2376 qd_idx = raid_disks - 1;
2377 break;
2378
2379 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002380 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002381 if (*dd_idx >= pd_idx)
2382 (*dd_idx)++;
2383 qd_idx = raid_disks - 1;
2384 break;
2385
2386 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002387 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002388 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2389 qd_idx = raid_disks - 1;
2390 break;
2391
2392 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002393 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002394 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2395 qd_idx = raid_disks - 1;
2396 break;
2397
2398 case ALGORITHM_PARITY_0_6:
2399 pd_idx = 0;
2400 (*dd_idx)++;
2401 qd_idx = raid_disks - 1;
2402 break;
2403
NeilBrown16a53ec2006-06-26 00:27:38 -07002404 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002405 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002406 }
2407 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409
NeilBrown911d4ee2009-03-31 14:39:38 +11002410 if (sh) {
2411 sh->pd_idx = pd_idx;
2412 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002413 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 /*
2416 * Finally, compute the new sector number
2417 */
2418 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2419 return new_sector;
2420}
2421
2422
NeilBrown784052e2009-03-31 15:19:07 +11002423static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
NeilBrownd1688a62011-10-11 16:49:52 +11002425 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002426 int raid_disks = sh->disks;
2427 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002429 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2430 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002431 int algorithm = previous ? conf->prev_algo
2432 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 sector_t stripe;
2434 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002435 sector_t chunk_number;
2436 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002438 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
NeilBrown16a53ec2006-06-26 00:27:38 -07002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2442 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
NeilBrown16a53ec2006-06-26 00:27:38 -07002444 if (i == sh->pd_idx)
2445 return 0;
2446 switch(conf->level) {
2447 case 4: break;
2448 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002449 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 case ALGORITHM_LEFT_ASYMMETRIC:
2451 case ALGORITHM_RIGHT_ASYMMETRIC:
2452 if (i > sh->pd_idx)
2453 i--;
2454 break;
2455 case ALGORITHM_LEFT_SYMMETRIC:
2456 case ALGORITHM_RIGHT_SYMMETRIC:
2457 if (i < sh->pd_idx)
2458 i += raid_disks;
2459 i -= (sh->pd_idx + 1);
2460 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002461 case ALGORITHM_PARITY_0:
2462 i -= 1;
2463 break;
2464 case ALGORITHM_PARITY_N:
2465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002467 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002468 }
2469 break;
2470 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002471 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002472 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002473 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002474 case ALGORITHM_LEFT_ASYMMETRIC:
2475 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002476 case ALGORITHM_ROTATING_ZERO_RESTART:
2477 case ALGORITHM_ROTATING_N_RESTART:
2478 if (sh->pd_idx == raid_disks-1)
2479 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002480 else if (i > sh->pd_idx)
2481 i -= 2; /* D D P Q D */
2482 break;
2483 case ALGORITHM_LEFT_SYMMETRIC:
2484 case ALGORITHM_RIGHT_SYMMETRIC:
2485 if (sh->pd_idx == raid_disks-1)
2486 i--; /* Q D D D P */
2487 else {
2488 /* D D P Q D */
2489 if (i < sh->pd_idx)
2490 i += raid_disks;
2491 i -= (sh->pd_idx + 2);
2492 }
2493 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002494 case ALGORITHM_PARITY_0:
2495 i -= 2;
2496 break;
2497 case ALGORITHM_PARITY_N:
2498 break;
2499 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002500 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002501 if (sh->pd_idx == 0)
2502 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002503 else {
2504 /* D D Q P D */
2505 if (i < sh->pd_idx)
2506 i += raid_disks;
2507 i -= (sh->pd_idx + 1);
2508 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002509 break;
2510 case ALGORITHM_LEFT_ASYMMETRIC_6:
2511 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2512 if (i > sh->pd_idx)
2513 i--;
2514 break;
2515 case ALGORITHM_LEFT_SYMMETRIC_6:
2516 case ALGORITHM_RIGHT_SYMMETRIC_6:
2517 if (i < sh->pd_idx)
2518 i += data_disks + 1;
2519 i -= (sh->pd_idx + 1);
2520 break;
2521 case ALGORITHM_PARITY_0_6:
2522 i -= 1;
2523 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002524 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002525 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002526 }
2527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529
2530 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002531 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
NeilBrown112bf892009-03-31 14:39:38 +11002533 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002534 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002535 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2536 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002537 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2538 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return 0;
2540 }
2541 return r_sector;
2542}
2543
2544
Dan Williams600aa102008-06-28 08:32:05 +10002545static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002546schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002547 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002548{
2549 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002550 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002551 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002552
Dan Williamse33129d2007-01-02 13:52:30 -07002553 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002554
2555 for (i = disks; i--; ) {
2556 struct r5dev *dev = &sh->dev[i];
2557
2558 if (dev->towrite) {
2559 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002560 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002561 if (!expand)
2562 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002563 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002564 }
2565 }
NeilBrownce7d3632013-03-04 12:37:14 +11002566 /* if we are not expanding this is a proper write request, and
2567 * there will be bios with new data to be drained into the
2568 * stripe cache
2569 */
2570 if (!expand) {
2571 if (!s->locked)
2572 /* False alarm, nothing to do */
2573 return;
2574 sh->reconstruct_state = reconstruct_state_drain_run;
2575 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2576 } else
2577 sh->reconstruct_state = reconstruct_state_run;
2578
2579 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2580
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002581 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002582 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002583 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002584 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002585 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002586 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2587 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2588
Dan Williamse33129d2007-01-02 13:52:30 -07002589 for (i = disks; i--; ) {
2590 struct r5dev *dev = &sh->dev[i];
2591 if (i == pd_idx)
2592 continue;
2593
Dan Williamse33129d2007-01-02 13:52:30 -07002594 if (dev->towrite &&
2595 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002596 test_bit(R5_Wantcompute, &dev->flags))) {
2597 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002598 set_bit(R5_LOCKED, &dev->flags);
2599 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002600 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002601 }
2602 }
NeilBrownce7d3632013-03-04 12:37:14 +11002603 if (!s->locked)
2604 /* False alarm - nothing to do */
2605 return;
2606 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2607 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2608 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2609 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002610 }
2611
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002612 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002613 * are in flight
2614 */
2615 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2616 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002617 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002618
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002619 if (level == 6) {
2620 int qd_idx = sh->qd_idx;
2621 struct r5dev *dev = &sh->dev[qd_idx];
2622
2623 set_bit(R5_LOCKED, &dev->flags);
2624 clear_bit(R5_UPTODATE, &dev->flags);
2625 s->locked++;
2626 }
2627
Dan Williams600aa102008-06-28 08:32:05 +10002628 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002629 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002630 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002631}
NeilBrown16a53ec2006-06-26 00:27:38 -07002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/*
2634 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002635 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 * The bi_next chain must be in order.
2637 */
2638static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2639{
2640 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002641 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002642 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
NeilBrowncbe47ec2011-07-26 11:20:35 +10002644 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002645 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 (unsigned long long)sh->sector);
2647
Shaohua Lib17459c2012-07-19 16:01:31 +10002648 /*
2649 * If several bio share a stripe. The bio bi_phys_segments acts as a
2650 * reference count to avoid race. The reference count should already be
2651 * increased before this function is called (for example, in
2652 * make_request()), so other bio sharing this stripe will not free the
2653 * stripe. If a stripe is owned by one stripe, the stripe lock will
2654 * protect it.
2655 */
2656 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002657 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002659 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002660 firstwrite = 1;
2661 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002663 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
2664 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 goto overlap;
2666 bip = & (*bip)->bi_next;
2667 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07002668 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 goto overlap;
2670
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002671 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (*bip)
2673 bi->bi_next = *bip;
2674 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002675 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if (forwrite) {
2678 /* check if page is covered */
2679 sector_t sector = sh->dev[dd_idx].sector;
2680 for (bi=sh->dev[dd_idx].towrite;
2681 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07002682 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002684 if (bio_end_sector(bi) >= sector)
2685 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
2687 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2688 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2689 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002690
2691 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002692 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10002693 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002694 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002695
2696 if (conf->mddev->bitmap && firstwrite) {
2697 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2698 STRIPE_SECTORS, 0);
2699 sh->bm_seq = conf->seq_flush+1;
2700 set_bit(STRIPE_BIT_DELAY, &sh->state);
2701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 return 1;
2703
2704 overlap:
2705 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002706 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return 0;
2708}
2709
NeilBrownd1688a62011-10-11 16:49:52 +11002710static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002711
NeilBrownd1688a62011-10-11 16:49:52 +11002712static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002713 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002714{
NeilBrown784052e2009-03-31 15:19:07 +11002715 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002716 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002717 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002718 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002719 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002720
NeilBrown112bf892009-03-31 14:39:38 +11002721 raid5_compute_sector(conf,
2722 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002723 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002724 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002725 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002726}
2727
Dan Williamsa4456852007-07-09 11:56:43 -07002728static void
NeilBrownd1688a62011-10-11 16:49:52 +11002729handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002730 struct stripe_head_state *s, int disks,
2731 struct bio **return_bi)
2732{
2733 int i;
2734 for (i = disks; i--; ) {
2735 struct bio *bi;
2736 int bitmap_end = 0;
2737
2738 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002739 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002740 rcu_read_lock();
2741 rdev = rcu_dereference(conf->disks[i].rdev);
2742 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002743 atomic_inc(&rdev->nr_pending);
2744 else
2745 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002746 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002747 if (rdev) {
2748 if (!rdev_set_badblocks(
2749 rdev,
2750 sh->sector,
2751 STRIPE_SECTORS, 0))
2752 md_error(conf->mddev, rdev);
2753 rdev_dec_pending(rdev, conf->mddev);
2754 }
Dan Williamsa4456852007-07-09 11:56:43 -07002755 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002756 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002757 /* fail all writes first */
2758 bi = sh->dev[i].towrite;
2759 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002760 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002761 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002762 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002763
2764 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2765 wake_up(&conf->wait_for_overlap);
2766
Kent Overstreet4f024f32013-10-11 15:44:27 -07002767 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002768 sh->dev[i].sector + STRIPE_SECTORS) {
2769 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2770 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002771 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002772 md_write_end(conf->mddev);
2773 bi->bi_next = *return_bi;
2774 *return_bi = bi;
2775 }
2776 bi = nextbi;
2777 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002778 if (bitmap_end)
2779 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2780 STRIPE_SECTORS, 0, 0);
2781 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002782 /* and fail all 'written' */
2783 bi = sh->dev[i].written;
2784 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08002785 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
2786 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
2787 sh->dev[i].page = sh->dev[i].orig_page;
2788 }
2789
Dan Williamsa4456852007-07-09 11:56:43 -07002790 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002791 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002792 sh->dev[i].sector + STRIPE_SECTORS) {
2793 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2794 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002795 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002796 md_write_end(conf->mddev);
2797 bi->bi_next = *return_bi;
2798 *return_bi = bi;
2799 }
2800 bi = bi2;
2801 }
2802
Dan Williamsb5e98d62007-01-02 13:52:31 -07002803 /* fail any reads if this device is non-operational and
2804 * the data has not reached the cache yet.
2805 */
2806 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2807 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2808 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002809 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002810 bi = sh->dev[i].toread;
2811 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002812 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002813 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2814 wake_up(&conf->wait_for_overlap);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002815 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002816 sh->dev[i].sector + STRIPE_SECTORS) {
2817 struct bio *nextbi =
2818 r5_next_bio(bi, sh->dev[i].sector);
2819 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002820 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002821 bi->bi_next = *return_bi;
2822 *return_bi = bi;
2823 }
2824 bi = nextbi;
2825 }
2826 }
Dan Williamsa4456852007-07-09 11:56:43 -07002827 if (bitmap_end)
2828 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2829 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002830 /* If we were in the middle of a write the parity block might
2831 * still be locked - so just clear all R5_LOCKED flags
2832 */
2833 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002834 }
2835
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002836 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2837 if (atomic_dec_and_test(&conf->pending_full_writes))
2838 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002839}
2840
NeilBrown7f0da592011-07-28 11:39:22 +10002841static void
NeilBrownd1688a62011-10-11 16:49:52 +11002842handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002843 struct stripe_head_state *s)
2844{
2845 int abort = 0;
2846 int i;
2847
NeilBrown7f0da592011-07-28 11:39:22 +10002848 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002849 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2850 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002851 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002852 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002853 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002854 * Don't even need to abort as that is handled elsewhere
2855 * if needed, and not always wanted e.g. if there is a known
2856 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002857 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002858 * non-sync devices, or abort the recovery
2859 */
NeilBrown18b98372012-04-01 23:48:38 +10002860 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2861 /* During recovery devices cannot be removed, so
2862 * locking and refcounting of rdevs is not needed
2863 */
2864 for (i = 0; i < conf->raid_disks; i++) {
2865 struct md_rdev *rdev = conf->disks[i].rdev;
2866 if (rdev
2867 && !test_bit(Faulty, &rdev->flags)
2868 && !test_bit(In_sync, &rdev->flags)
2869 && !rdev_set_badblocks(rdev, sh->sector,
2870 STRIPE_SECTORS, 0))
2871 abort = 1;
2872 rdev = conf->disks[i].replacement;
2873 if (rdev
2874 && !test_bit(Faulty, &rdev->flags)
2875 && !test_bit(In_sync, &rdev->flags)
2876 && !rdev_set_badblocks(rdev, sh->sector,
2877 STRIPE_SECTORS, 0))
2878 abort = 1;
2879 }
2880 if (abort)
2881 conf->recovery_disabled =
2882 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002883 }
NeilBrown18b98372012-04-01 23:48:38 +10002884 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002885}
2886
NeilBrown9a3e1102011-12-23 10:17:53 +11002887static int want_replace(struct stripe_head *sh, int disk_idx)
2888{
2889 struct md_rdev *rdev;
2890 int rv = 0;
2891 /* Doing recovery so rcu locking not required */
2892 rdev = sh->raid_conf->disks[disk_idx].replacement;
2893 if (rdev
2894 && !test_bit(Faulty, &rdev->flags)
2895 && !test_bit(In_sync, &rdev->flags)
2896 && (rdev->recovery_offset <= sh->sector
2897 || rdev->mddev->recovery_cp <= sh->sector))
2898 rv = 1;
2899
2900 return rv;
2901}
2902
NeilBrown93b3dbc2011-07-27 11:00:36 +10002903/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002904 * to be read or computed to satisfy a request.
2905 *
2906 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002907 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002908 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002909static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2910 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002911{
2912 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002913 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2914 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002915
Dan Williamsf38e1212007-01-02 13:52:30 -07002916 /* is the data in this block needed, and can we get it? */
2917 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002918 !test_bit(R5_UPTODATE, &dev->flags) &&
2919 (dev->toread ||
2920 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2921 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002922 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002923 (s->failed >= 1 && fdev[0]->toread) ||
2924 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002925 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
NeilBrown67f45542014-05-28 13:39:22 +10002926 (!test_bit(R5_Insync, &dev->flags) || test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) &&
NeilBrown93b3dbc2011-07-27 11:00:36 +10002927 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
NeilBrown67f45542014-05-28 13:39:22 +10002928 (sh->raid_conf->level == 6 && s->failed && s->to_write &&
NeilBrowna40687f2014-08-13 09:48:45 +10002929 s->to_write - s->non_overwrite < sh->raid_conf->raid_disks - 2 &&
NeilBrown67f45542014-05-28 13:39:22 +10002930 (!test_bit(R5_Insync, &dev->flags) || test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002931 /* we would like to get this block, possibly by computing it,
2932 * otherwise read it if the backing disk is insync
2933 */
2934 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2935 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2936 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002937 (s->failed && (disk_idx == s->failed_num[0] ||
2938 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002939 /* have disk failed, and we're requested to fetch it;
2940 * do compute it
2941 */
2942 pr_debug("Computing stripe %llu block %d\n",
2943 (unsigned long long)sh->sector, disk_idx);
2944 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2945 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2946 set_bit(R5_Wantcompute, &dev->flags);
2947 sh->ops.target = disk_idx;
2948 sh->ops.target2 = -1; /* no 2nd target */
2949 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002950 /* Careful: from this point on 'uptodate' is in the eye
2951 * of raid_run_ops which services 'compute' operations
2952 * before writes. R5_Wantcompute flags a block that will
2953 * be R5_UPTODATE by the time it is needed for a
2954 * subsequent operation.
2955 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002956 s->uptodate++;
2957 return 1;
2958 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2959 /* Computing 2-failure is *very* expensive; only
2960 * do it if failed >= 2
2961 */
2962 int other;
2963 for (other = disks; other--; ) {
2964 if (other == disk_idx)
2965 continue;
2966 if (!test_bit(R5_UPTODATE,
2967 &sh->dev[other].flags))
2968 break;
2969 }
2970 BUG_ON(other < 0);
2971 pr_debug("Computing stripe %llu blocks %d,%d\n",
2972 (unsigned long long)sh->sector,
2973 disk_idx, other);
2974 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2975 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2976 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2977 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2978 sh->ops.target = disk_idx;
2979 sh->ops.target2 = other;
2980 s->uptodate += 2;
2981 s->req_compute = 1;
2982 return 1;
2983 } else if (test_bit(R5_Insync, &dev->flags)) {
2984 set_bit(R5_LOCKED, &dev->flags);
2985 set_bit(R5_Wantread, &dev->flags);
2986 s->locked++;
2987 pr_debug("Reading block %d (sync=%d)\n",
2988 disk_idx, s->syncing);
2989 }
2990 }
2991
2992 return 0;
2993}
2994
2995/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002996 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002997 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002998static void handle_stripe_fill(struct stripe_head *sh,
2999 struct stripe_head_state *s,
3000 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003001{
3002 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003003
3004 /* look for blocks to read/compute, skip this if a compute
3005 * is already in flight, or if the stripe contents are in the
3006 * midst of changing due to a write
3007 */
3008 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
3009 !sh->reconstruct_state)
3010 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003011 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003012 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003013 set_bit(STRIPE_HANDLE, &sh->state);
3014}
3015
3016
Dan Williams1fe797e2008-06-28 09:16:30 +10003017/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003018 * any written block on an uptodate or failed drive can be returned.
3019 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3020 * never LOCKED, so we don't need to test 'failed' directly.
3021 */
NeilBrownd1688a62011-10-11 16:49:52 +11003022static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07003023 struct stripe_head *sh, int disks, struct bio **return_bi)
3024{
3025 int i;
3026 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003027 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003028
3029 for (i = disks; i--; )
3030 if (sh->dev[i].written) {
3031 dev = &sh->dev[i];
3032 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003033 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003034 test_bit(R5_Discard, &dev->flags) ||
3035 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003036 /* We can return any write requests */
3037 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003038 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003039 if (test_and_clear_bit(R5_Discard, &dev->flags))
3040 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003041 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3042 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
3043 dev->page = dev->orig_page;
3044 }
Dan Williamsa4456852007-07-09 11:56:43 -07003045 wbi = dev->written;
3046 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003047 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003048 dev->sector + STRIPE_SECTORS) {
3049 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003050 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003051 md_write_end(conf->mddev);
3052 wbi->bi_next = *return_bi;
3053 *return_bi = wbi;
3054 }
3055 wbi = wbi2;
3056 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003057 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3058 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003059 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003060 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003061 } else if (test_bit(R5_Discard, &dev->flags))
3062 discard_pending = 1;
Shaohua Lid592a992014-05-21 17:57:44 +08003063 WARN_ON(test_bit(R5_SkipCopy, &dev->flags));
3064 WARN_ON(dev->page != dev->orig_page);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003065 }
3066 if (!discard_pending &&
3067 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3068 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3069 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3070 if (sh->qd_idx >= 0) {
3071 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3072 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3073 }
3074 /* now that discard is done we can proceed with any sync */
3075 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003076 /*
3077 * SCSI discard will change some bio fields and the stripe has
3078 * no updated data, so remove it from hash list and the stripe
3079 * will be reinitialized
3080 */
3081 spin_lock_irq(&conf->device_lock);
3082 remove_hash(sh);
3083 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003084 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3085 set_bit(STRIPE_HANDLE, &sh->state);
3086
3087 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003088
3089 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3090 if (atomic_dec_and_test(&conf->pending_full_writes))
3091 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003092}
3093
NeilBrownd1688a62011-10-11 16:49:52 +11003094static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003095 struct stripe_head *sh,
3096 struct stripe_head_state *s,
3097 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003098{
3099 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003100 sector_t recovery_cp = conf->mddev->recovery_cp;
3101
3102 /* RAID6 requires 'rcw' in current implementation.
3103 * Otherwise, check whether resync is now happening or should start.
3104 * If yes, then the array is dirty (after unclean shutdown or
3105 * initial creation), so parity in some stripes might be inconsistent.
3106 * In this case, we need to always do reconstruct-write, to ensure
3107 * that in case of drive failure or read-error correction, we
3108 * generate correct data from the parity.
3109 */
3110 if (conf->max_degraded == 2 ||
3111 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
3112 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003113 * look like rcw is cheaper
3114 */
3115 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003116 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
3117 conf->max_degraded, (unsigned long long)recovery_cp,
3118 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003119 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003120 /* would I have to read this buffer for read_modify_write */
3121 struct r5dev *dev = &sh->dev[i];
3122 if ((dev->towrite || i == sh->pd_idx) &&
3123 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003124 !(test_bit(R5_UPTODATE, &dev->flags) ||
3125 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003126 if (test_bit(R5_Insync, &dev->flags))
3127 rmw++;
3128 else
3129 rmw += 2*disks; /* cannot read it */
3130 }
3131 /* Would I have to read this buffer for reconstruct_write */
3132 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
3133 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003134 !(test_bit(R5_UPTODATE, &dev->flags) ||
3135 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003136 if (test_bit(R5_Insync, &dev->flags))
3137 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003138 else
3139 rcw += 2*disks;
3140 }
3141 }
Dan Williams45b42332007-07-09 11:56:43 -07003142 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003143 (unsigned long long)sh->sector, rmw, rcw);
3144 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11003145 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003146 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003147 if (conf->mddev->queue)
3148 blk_add_trace_msg(conf->mddev->queue,
3149 "raid5 rmw %llu %d",
3150 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003151 for (i = disks; i--; ) {
3152 struct r5dev *dev = &sh->dev[i];
3153 if ((dev->towrite || i == sh->pd_idx) &&
3154 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003155 !(test_bit(R5_UPTODATE, &dev->flags) ||
3156 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003157 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003158 if (test_bit(STRIPE_PREREAD_ACTIVE,
3159 &sh->state)) {
3160 pr_debug("Read_old block %d for r-m-w\n",
3161 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003162 set_bit(R5_LOCKED, &dev->flags);
3163 set_bit(R5_Wantread, &dev->flags);
3164 s->locked++;
3165 } else {
3166 set_bit(STRIPE_DELAYED, &sh->state);
3167 set_bit(STRIPE_HANDLE, &sh->state);
3168 }
3169 }
3170 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003171 }
NeilBrownc8ac1802011-07-27 11:00:36 +10003172 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003173 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003174 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003175 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003176 for (i = disks; i--; ) {
3177 struct r5dev *dev = &sh->dev[i];
3178 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003179 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003180 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003181 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003182 test_bit(R5_Wantcompute, &dev->flags))) {
3183 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003184 if (test_bit(R5_Insync, &dev->flags) &&
3185 test_bit(STRIPE_PREREAD_ACTIVE,
3186 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003187 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003188 "%d for Reconstruct\n", i);
3189 set_bit(R5_LOCKED, &dev->flags);
3190 set_bit(R5_Wantread, &dev->flags);
3191 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003192 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003193 } else {
3194 set_bit(STRIPE_DELAYED, &sh->state);
3195 set_bit(STRIPE_HANDLE, &sh->state);
3196 }
3197 }
3198 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003199 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003200 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3201 (unsigned long long)sh->sector,
3202 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003203 }
Dan Williamsa4456852007-07-09 11:56:43 -07003204 /* now if nothing is locked, and if we have enough data,
3205 * we can start a write request
3206 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003207 /* since handle_stripe can be called at any time we need to handle the
3208 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003209 * subsequent call wants to start a write request. raid_run_ops only
3210 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003211 * simultaneously. If this is not the case then new writes need to be
3212 * held off until the compute completes.
3213 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003214 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3215 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3216 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003217 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003218}
3219
NeilBrownd1688a62011-10-11 16:49:52 +11003220static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003221 struct stripe_head_state *s, int disks)
3222{
Dan Williamsecc65c92008-06-28 08:31:57 +10003223 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003224
Dan Williamsbd2ab672008-04-10 21:29:27 -07003225 set_bit(STRIPE_HANDLE, &sh->state);
3226
Dan Williamsecc65c92008-06-28 08:31:57 +10003227 switch (sh->check_state) {
3228 case check_state_idle:
3229 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003230 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003231 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003232 sh->check_state = check_state_run;
3233 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003234 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003235 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003236 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003237 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003238 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003239 /* fall through */
3240 case check_state_compute_result:
3241 sh->check_state = check_state_idle;
3242 if (!dev)
3243 dev = &sh->dev[sh->pd_idx];
3244
3245 /* check that a write has not made the stripe insync */
3246 if (test_bit(STRIPE_INSYNC, &sh->state))
3247 break;
3248
3249 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003250 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3251 BUG_ON(s->uptodate != disks);
3252
3253 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003254 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003255 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003256
Dan Williamsa4456852007-07-09 11:56:43 -07003257 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003258 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003259 break;
3260 case check_state_run:
3261 break; /* we will be called again upon completion */
3262 case check_state_check_result:
3263 sh->check_state = check_state_idle;
3264
3265 /* if a failure occurred during the check operation, leave
3266 * STRIPE_INSYNC not set and let the stripe be handled again
3267 */
3268 if (s->failed)
3269 break;
3270
3271 /* handle a successful check operation, if parity is correct
3272 * we are done. Otherwise update the mismatch count and repair
3273 * parity if !MD_RECOVERY_CHECK
3274 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003275 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003276 /* parity is correct (on disc,
3277 * not in buffer any more)
3278 */
3279 set_bit(STRIPE_INSYNC, &sh->state);
3280 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003281 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003282 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3283 /* don't try to repair!! */
3284 set_bit(STRIPE_INSYNC, &sh->state);
3285 else {
3286 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003287 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003288 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3289 set_bit(R5_Wantcompute,
3290 &sh->dev[sh->pd_idx].flags);
3291 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003292 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003293 s->uptodate++;
3294 }
3295 }
3296 break;
3297 case check_state_compute_run:
3298 break;
3299 default:
3300 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3301 __func__, sh->check_state,
3302 (unsigned long long) sh->sector);
3303 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003304 }
3305}
3306
3307
NeilBrownd1688a62011-10-11 16:49:52 +11003308static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003309 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003310 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003311{
Dan Williamsa4456852007-07-09 11:56:43 -07003312 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003313 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003314 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003315
3316 set_bit(STRIPE_HANDLE, &sh->state);
3317
3318 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003319
Dan Williamsa4456852007-07-09 11:56:43 -07003320 /* Want to check and possibly repair P and Q.
3321 * However there could be one 'failed' device, in which
3322 * case we can only check one of them, possibly using the
3323 * other to generate missing data
3324 */
3325
Dan Williamsd82dfee2009-07-14 13:40:57 -07003326 switch (sh->check_state) {
3327 case check_state_idle:
3328 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003329 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003330 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003331 * makes sense to check P (If anything else were failed,
3332 * we would have used P to recreate it).
3333 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003334 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003335 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003336 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003337 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003338 * anything, so it makes sense to check it
3339 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003340 if (sh->check_state == check_state_run)
3341 sh->check_state = check_state_run_pq;
3342 else
3343 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003344 }
Dan Williams36d1c642009-07-14 11:48:22 -07003345
Dan Williamsd82dfee2009-07-14 13:40:57 -07003346 /* discard potentially stale zero_sum_result */
3347 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003348
Dan Williamsd82dfee2009-07-14 13:40:57 -07003349 if (sh->check_state == check_state_run) {
3350 /* async_xor_zero_sum destroys the contents of P */
3351 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3352 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003353 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003354 if (sh->check_state >= check_state_run &&
3355 sh->check_state <= check_state_run_pq) {
3356 /* async_syndrome_zero_sum preserves P and Q, so
3357 * no need to mark them !uptodate here
3358 */
3359 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3360 break;
3361 }
Dan Williams36d1c642009-07-14 11:48:22 -07003362
Dan Williamsd82dfee2009-07-14 13:40:57 -07003363 /* we have 2-disk failure */
3364 BUG_ON(s->failed != 2);
3365 /* fall through */
3366 case check_state_compute_result:
3367 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003368
Dan Williamsd82dfee2009-07-14 13:40:57 -07003369 /* check that a write has not made the stripe insync */
3370 if (test_bit(STRIPE_INSYNC, &sh->state))
3371 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003372
3373 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003374 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003375 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003376 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003377 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003378 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003379 s->locked++;
3380 set_bit(R5_LOCKED, &dev->flags);
3381 set_bit(R5_Wantwrite, &dev->flags);
3382 }
3383 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003384 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003385 s->locked++;
3386 set_bit(R5_LOCKED, &dev->flags);
3387 set_bit(R5_Wantwrite, &dev->flags);
3388 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003389 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003390 dev = &sh->dev[pd_idx];
3391 s->locked++;
3392 set_bit(R5_LOCKED, &dev->flags);
3393 set_bit(R5_Wantwrite, &dev->flags);
3394 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003395 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003396 dev = &sh->dev[qd_idx];
3397 s->locked++;
3398 set_bit(R5_LOCKED, &dev->flags);
3399 set_bit(R5_Wantwrite, &dev->flags);
3400 }
3401 clear_bit(STRIPE_DEGRADED, &sh->state);
3402
3403 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003404 break;
3405 case check_state_run:
3406 case check_state_run_q:
3407 case check_state_run_pq:
3408 break; /* we will be called again upon completion */
3409 case check_state_check_result:
3410 sh->check_state = check_state_idle;
3411
3412 /* handle a successful check operation, if parity is correct
3413 * we are done. Otherwise update the mismatch count and repair
3414 * parity if !MD_RECOVERY_CHECK
3415 */
3416 if (sh->ops.zero_sum_result == 0) {
3417 /* both parities are correct */
3418 if (!s->failed)
3419 set_bit(STRIPE_INSYNC, &sh->state);
3420 else {
3421 /* in contrast to the raid5 case we can validate
3422 * parity, but still have a failure to write
3423 * back
3424 */
3425 sh->check_state = check_state_compute_result;
3426 /* Returning at this point means that we may go
3427 * off and bring p and/or q uptodate again so
3428 * we make sure to check zero_sum_result again
3429 * to verify if p or q need writeback
3430 */
3431 }
3432 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003433 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003434 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3435 /* don't try to repair!! */
3436 set_bit(STRIPE_INSYNC, &sh->state);
3437 else {
3438 int *target = &sh->ops.target;
3439
3440 sh->ops.target = -1;
3441 sh->ops.target2 = -1;
3442 sh->check_state = check_state_compute_run;
3443 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3444 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3445 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3446 set_bit(R5_Wantcompute,
3447 &sh->dev[pd_idx].flags);
3448 *target = pd_idx;
3449 target = &sh->ops.target2;
3450 s->uptodate++;
3451 }
3452 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3453 set_bit(R5_Wantcompute,
3454 &sh->dev[qd_idx].flags);
3455 *target = qd_idx;
3456 s->uptodate++;
3457 }
3458 }
3459 }
3460 break;
3461 case check_state_compute_run:
3462 break;
3463 default:
3464 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3465 __func__, sh->check_state,
3466 (unsigned long long) sh->sector);
3467 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003468 }
3469}
3470
NeilBrownd1688a62011-10-11 16:49:52 +11003471static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003472{
3473 int i;
3474
3475 /* We have read all the blocks in this stripe and now we need to
3476 * copy some of them into a target stripe for expand.
3477 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003478 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003479 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3480 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003481 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003482 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003483 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003484 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003485
NeilBrown784052e2009-03-31 15:19:07 +11003486 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003487 sector_t s = raid5_compute_sector(conf, bn, 0,
3488 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003489 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003490 if (sh2 == NULL)
3491 /* so far only the early blocks of this stripe
3492 * have been requested. When later blocks
3493 * get requested, we will try again
3494 */
3495 continue;
3496 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3497 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3498 /* must have already done this block */
3499 release_stripe(sh2);
3500 continue;
3501 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003502
3503 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003504 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003505 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003506 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003507 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003508
Dan Williamsa4456852007-07-09 11:56:43 -07003509 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3510 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3511 for (j = 0; j < conf->raid_disks; j++)
3512 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003513 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003514 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3515 break;
3516 if (j == conf->raid_disks) {
3517 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3518 set_bit(STRIPE_HANDLE, &sh2->state);
3519 }
3520 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003521
Dan Williamsa4456852007-07-09 11:56:43 -07003522 }
NeilBrowna2e08552007-09-11 15:23:36 -07003523 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003524 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527/*
3528 * handle_stripe - do things to a stripe.
3529 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003530 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3531 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003533 * return some read requests which now have data
3534 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 * schedule a read on some buffers
3536 * schedule a write of some buffers
3537 * return confirmation of parity correctness
3538 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 */
Dan Williamsa4456852007-07-09 11:56:43 -07003540
NeilBrownacfe7262011-07-27 11:00:36 +10003541static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003542{
NeilBrownd1688a62011-10-11 16:49:52 +11003543 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003544 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003545 struct r5dev *dev;
3546 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003547 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003548
NeilBrownacfe7262011-07-27 11:00:36 +10003549 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003550
NeilBrownacfe7262011-07-27 11:00:36 +10003551 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3552 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3553 s->failed_num[0] = -1;
3554 s->failed_num[1] = -1;
3555
3556 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003557 rcu_read_lock();
3558 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003559 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003560 sector_t first_bad;
3561 int bad_sectors;
3562 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003563
NeilBrown16a53ec2006-06-26 00:27:38 -07003564 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003565
Dan Williams45b42332007-07-09 11:56:43 -07003566 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003567 i, dev->flags,
3568 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003569 /* maybe we can reply to a read
3570 *
3571 * new wantfill requests are only permitted while
3572 * ops_complete_biofill is guaranteed to be inactive
3573 */
3574 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3575 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3576 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003577
3578 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003579 if (test_bit(R5_LOCKED, &dev->flags))
3580 s->locked++;
3581 if (test_bit(R5_UPTODATE, &dev->flags))
3582 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003583 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003584 s->compute++;
3585 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003586 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003587
NeilBrownacfe7262011-07-27 11:00:36 +10003588 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003589 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003590 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003591 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003592 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003593 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003594 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003595 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003596 }
Dan Williamsa4456852007-07-09 11:56:43 -07003597 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003598 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003599 /* Prefer to use the replacement for reads, but only
3600 * if it is recovered enough and has no bad blocks.
3601 */
3602 rdev = rcu_dereference(conf->disks[i].replacement);
3603 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3604 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3605 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3606 &first_bad, &bad_sectors))
3607 set_bit(R5_ReadRepl, &dev->flags);
3608 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003609 if (rdev)
3610 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003611 rdev = rcu_dereference(conf->disks[i].rdev);
3612 clear_bit(R5_ReadRepl, &dev->flags);
3613 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003614 if (rdev && test_bit(Faulty, &rdev->flags))
3615 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003616 if (rdev) {
3617 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3618 &first_bad, &bad_sectors);
3619 if (s->blocked_rdev == NULL
3620 && (test_bit(Blocked, &rdev->flags)
3621 || is_bad < 0)) {
3622 if (is_bad < 0)
3623 set_bit(BlockedBadBlocks,
3624 &rdev->flags);
3625 s->blocked_rdev = rdev;
3626 atomic_inc(&rdev->nr_pending);
3627 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003628 }
NeilBrown415e72d2010-06-17 17:25:21 +10003629 clear_bit(R5_Insync, &dev->flags);
3630 if (!rdev)
3631 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003632 else if (is_bad) {
3633 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003634 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3635 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003636 /* treat as in-sync, but with a read error
3637 * which we can now try to correct
3638 */
3639 set_bit(R5_Insync, &dev->flags);
3640 set_bit(R5_ReadError, &dev->flags);
3641 }
3642 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003643 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003644 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003645 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003646 set_bit(R5_Insync, &dev->flags);
3647 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3648 test_bit(R5_Expanded, &dev->flags))
3649 /* If we've reshaped into here, we assume it is Insync.
3650 * We will shortly update recovery_offset to make
3651 * it official.
3652 */
3653 set_bit(R5_Insync, &dev->flags);
3654
NeilBrown1cc03eb2014-01-06 13:19:42 +11003655 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003656 /* This flag does not apply to '.replacement'
3657 * only to .rdev, so make sure to check that*/
3658 struct md_rdev *rdev2 = rcu_dereference(
3659 conf->disks[i].rdev);
3660 if (rdev2 == rdev)
3661 clear_bit(R5_Insync, &dev->flags);
3662 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003663 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003664 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003665 } else
3666 clear_bit(R5_WriteError, &dev->flags);
3667 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11003668 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003669 /* This flag does not apply to '.replacement'
3670 * only to .rdev, so make sure to check that*/
3671 struct md_rdev *rdev2 = rcu_dereference(
3672 conf->disks[i].rdev);
3673 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003674 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003675 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003676 } else
3677 clear_bit(R5_MadeGood, &dev->flags);
3678 }
NeilBrown977df362011-12-23 10:17:53 +11003679 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3680 struct md_rdev *rdev2 = rcu_dereference(
3681 conf->disks[i].replacement);
3682 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3683 s->handle_bad_blocks = 1;
3684 atomic_inc(&rdev2->nr_pending);
3685 } else
3686 clear_bit(R5_MadeGoodRepl, &dev->flags);
3687 }
NeilBrown415e72d2010-06-17 17:25:21 +10003688 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003689 /* The ReadError flag will just be confusing now */
3690 clear_bit(R5_ReadError, &dev->flags);
3691 clear_bit(R5_ReWrite, &dev->flags);
3692 }
NeilBrown415e72d2010-06-17 17:25:21 +10003693 if (test_bit(R5_ReadError, &dev->flags))
3694 clear_bit(R5_Insync, &dev->flags);
3695 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003696 if (s->failed < 2)
3697 s->failed_num[s->failed] = i;
3698 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003699 if (rdev && !test_bit(Faulty, &rdev->flags))
3700 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003701 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003702 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003703 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3704 /* If there is a failed device being replaced,
3705 * we must be recovering.
3706 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003707 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003708 * else we can only be replacing
3709 * sync and recovery both need to read all devices, and so
3710 * use the same flag.
3711 */
3712 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003713 sh->sector >= conf->mddev->recovery_cp ||
3714 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003715 s->syncing = 1;
3716 else
3717 s->replacing = 1;
3718 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003719 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003720}
NeilBrownf4168852007-02-28 20:11:53 -08003721
NeilBrowncc940152011-07-26 11:35:35 +10003722static void handle_stripe(struct stripe_head *sh)
3723{
3724 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003725 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003726 int i;
NeilBrown84789552011-07-27 11:00:36 +10003727 int prexor;
3728 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003729 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003730
3731 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003732 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003733 /* already being handled, ensure it gets handled
3734 * again when current action finishes */
3735 set_bit(STRIPE_HANDLE, &sh->state);
3736 return;
3737 }
3738
NeilBrownf8dfcff2013-03-12 12:18:06 +11003739 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3740 spin_lock(&sh->stripe_lock);
3741 /* Cannot process 'sync' concurrently with 'discard' */
3742 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3743 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3744 set_bit(STRIPE_SYNCING, &sh->state);
3745 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003746 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003747 }
3748 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003749 }
3750 clear_bit(STRIPE_DELAYED, &sh->state);
3751
3752 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3753 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3754 (unsigned long long)sh->sector, sh->state,
3755 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3756 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003757
NeilBrownacfe7262011-07-27 11:00:36 +10003758 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003759
NeilBrownbc2607f2011-07-28 11:39:22 +10003760 if (s.handle_bad_blocks) {
3761 set_bit(STRIPE_HANDLE, &sh->state);
3762 goto finish;
3763 }
3764
NeilBrown474af965fe2011-07-27 11:00:36 +10003765 if (unlikely(s.blocked_rdev)) {
3766 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003767 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003768 set_bit(STRIPE_HANDLE, &sh->state);
3769 goto finish;
3770 }
3771 /* There is nothing for the blocked_rdev to block */
3772 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3773 s.blocked_rdev = NULL;
3774 }
3775
3776 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3777 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3778 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3779 }
3780
3781 pr_debug("locked=%d uptodate=%d to_read=%d"
3782 " to_write=%d failed=%d failed_num=%d,%d\n",
3783 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3784 s.failed_num[0], s.failed_num[1]);
3785 /* check if the array has lost more than max_degraded devices and,
3786 * if so, some requests might need to be failed.
3787 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003788 if (s.failed > conf->max_degraded) {
3789 sh->check_state = 0;
3790 sh->reconstruct_state = 0;
3791 if (s.to_read+s.to_write+s.written)
3792 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003793 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003794 handle_failed_sync(conf, sh, &s);
3795 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003796
NeilBrown84789552011-07-27 11:00:36 +10003797 /* Now we check to see if any write operations have recently
3798 * completed
3799 */
3800 prexor = 0;
3801 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3802 prexor = 1;
3803 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3804 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3805 sh->reconstruct_state = reconstruct_state_idle;
3806
3807 /* All the 'written' buffers and the parity block are ready to
3808 * be written back to disk
3809 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003810 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3811 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003812 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003813 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3814 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003815 for (i = disks; i--; ) {
3816 struct r5dev *dev = &sh->dev[i];
3817 if (test_bit(R5_LOCKED, &dev->flags) &&
3818 (i == sh->pd_idx || i == sh->qd_idx ||
3819 dev->written)) {
3820 pr_debug("Writing block %d\n", i);
3821 set_bit(R5_Wantwrite, &dev->flags);
3822 if (prexor)
3823 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10003824 if (s.failed > 1)
3825 continue;
NeilBrown84789552011-07-27 11:00:36 +10003826 if (!test_bit(R5_Insync, &dev->flags) ||
3827 ((i == sh->pd_idx || i == sh->qd_idx) &&
3828 s.failed == 0))
3829 set_bit(STRIPE_INSYNC, &sh->state);
3830 }
3831 }
3832 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3833 s.dec_preread_active = 1;
3834 }
3835
NeilBrownef5b7c62012-11-22 09:13:36 +11003836 /*
3837 * might be able to return some write requests if the parity blocks
3838 * are safe, or on a failed drive
3839 */
3840 pdev = &sh->dev[sh->pd_idx];
3841 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3842 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3843 qdev = &sh->dev[sh->qd_idx];
3844 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3845 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3846 || conf->level < 6;
3847
3848 if (s.written &&
3849 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3850 && !test_bit(R5_LOCKED, &pdev->flags)
3851 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3852 test_bit(R5_Discard, &pdev->flags))))) &&
3853 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3854 && !test_bit(R5_LOCKED, &qdev->flags)
3855 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3856 test_bit(R5_Discard, &qdev->flags))))))
3857 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3858
3859 /* Now we might consider reading some blocks, either to check/generate
3860 * parity, or to satisfy requests
3861 * or to load a block that is being partially written.
3862 */
3863 if (s.to_read || s.non_overwrite
3864 || (conf->level == 6 && s.to_write && s.failed)
3865 || (s.syncing && (s.uptodate + s.compute < disks))
3866 || s.replacing
3867 || s.expanding)
3868 handle_stripe_fill(sh, &s, disks);
3869
NeilBrown84789552011-07-27 11:00:36 +10003870 /* Now to consider new write requests and what else, if anything
3871 * should be read. We do not handle new writes when:
3872 * 1/ A 'write' operation (copy+xor) is already in flight.
3873 * 2/ A 'check' operation is in flight, as it may clobber the parity
3874 * block.
3875 */
3876 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3877 handle_stripe_dirtying(conf, sh, &s, disks);
3878
3879 /* maybe we need to check and possibly fix the parity for this stripe
3880 * Any reads will already have been scheduled, so we just see if enough
3881 * data is available. The parity check is held off while parity
3882 * dependent operations are in flight.
3883 */
3884 if (sh->check_state ||
3885 (s.syncing && s.locked == 0 &&
3886 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3887 !test_bit(STRIPE_INSYNC, &sh->state))) {
3888 if (conf->level == 6)
3889 handle_parity_checks6(conf, sh, &s, disks);
3890 else
3891 handle_parity_checks5(conf, sh, &s, disks);
3892 }
NeilBrownc5a31002011-07-27 11:00:36 +10003893
NeilBrownf94c0b62013-07-22 12:57:21 +10003894 if ((s.replacing || s.syncing) && s.locked == 0
3895 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3896 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003897 /* Write out to replacement devices where possible */
3898 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003899 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3900 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003901 set_bit(R5_WantReplace, &sh->dev[i].flags);
3902 set_bit(R5_LOCKED, &sh->dev[i].flags);
3903 s.locked++;
3904 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003905 if (s.replacing)
3906 set_bit(STRIPE_INSYNC, &sh->state);
3907 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003908 }
3909 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003910 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003911 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003912 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3913 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003914 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3915 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003916 }
3917
3918 /* If the failed drives are just a ReadError, then we might need
3919 * to progress the repair/check process
3920 */
3921 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3922 for (i = 0; i < s.failed; i++) {
3923 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3924 if (test_bit(R5_ReadError, &dev->flags)
3925 && !test_bit(R5_LOCKED, &dev->flags)
3926 && test_bit(R5_UPTODATE, &dev->flags)
3927 ) {
3928 if (!test_bit(R5_ReWrite, &dev->flags)) {
3929 set_bit(R5_Wantwrite, &dev->flags);
3930 set_bit(R5_ReWrite, &dev->flags);
3931 set_bit(R5_LOCKED, &dev->flags);
3932 s.locked++;
3933 } else {
3934 /* let's read it back */
3935 set_bit(R5_Wantread, &dev->flags);
3936 set_bit(R5_LOCKED, &dev->flags);
3937 s.locked++;
3938 }
3939 }
3940 }
3941
3942
NeilBrown3687c062011-07-27 11:00:36 +10003943 /* Finish reconstruct operations initiated by the expansion process */
3944 if (sh->reconstruct_state == reconstruct_state_result) {
3945 struct stripe_head *sh_src
3946 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3947 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3948 /* sh cannot be written until sh_src has been read.
3949 * so arrange for sh to be delayed a little
3950 */
3951 set_bit(STRIPE_DELAYED, &sh->state);
3952 set_bit(STRIPE_HANDLE, &sh->state);
3953 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3954 &sh_src->state))
3955 atomic_inc(&conf->preread_active_stripes);
3956 release_stripe(sh_src);
3957 goto finish;
3958 }
3959 if (sh_src)
3960 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003961
NeilBrown3687c062011-07-27 11:00:36 +10003962 sh->reconstruct_state = reconstruct_state_idle;
3963 clear_bit(STRIPE_EXPANDING, &sh->state);
3964 for (i = conf->raid_disks; i--; ) {
3965 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3966 set_bit(R5_LOCKED, &sh->dev[i].flags);
3967 s.locked++;
3968 }
3969 }
3970
3971 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3972 !sh->reconstruct_state) {
3973 /* Need to write out all blocks after computing parity */
3974 sh->disks = conf->raid_disks;
3975 stripe_set_idx(sh->sector, conf, 0, sh);
3976 schedule_reconstruction(sh, &s, 1, 1);
3977 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3978 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3979 atomic_dec(&conf->reshape_stripes);
3980 wake_up(&conf->wait_for_overlap);
3981 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3982 }
3983
3984 if (s.expanding && s.locked == 0 &&
3985 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3986 handle_stripe_expansion(conf, sh);
3987
3988finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003989 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003990 if (unlikely(s.blocked_rdev)) {
3991 if (conf->mddev->external)
3992 md_wait_for_blocked_rdev(s.blocked_rdev,
3993 conf->mddev);
3994 else
3995 /* Internal metadata will immediately
3996 * be written by raid5d, so we don't
3997 * need to wait here.
3998 */
3999 rdev_dec_pending(s.blocked_rdev,
4000 conf->mddev);
4001 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004002
NeilBrownbc2607f2011-07-28 11:39:22 +10004003 if (s.handle_bad_blocks)
4004 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004005 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004006 struct r5dev *dev = &sh->dev[i];
4007 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4008 /* We own a safe reference to the rdev */
4009 rdev = conf->disks[i].rdev;
4010 if (!rdev_set_badblocks(rdev, sh->sector,
4011 STRIPE_SECTORS, 0))
4012 md_error(conf->mddev, rdev);
4013 rdev_dec_pending(rdev, conf->mddev);
4014 }
NeilBrownb84db562011-07-28 11:39:23 +10004015 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4016 rdev = conf->disks[i].rdev;
4017 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004018 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004019 rdev_dec_pending(rdev, conf->mddev);
4020 }
NeilBrown977df362011-12-23 10:17:53 +11004021 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4022 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004023 if (!rdev)
4024 /* rdev have been moved down */
4025 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004026 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004027 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004028 rdev_dec_pending(rdev, conf->mddev);
4029 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004030 }
4031
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004032 if (s.ops_request)
4033 raid_run_ops(sh, s.ops_request);
4034
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004035 ops_run_io(sh, &s);
4036
NeilBrownc5709ef2011-07-26 11:35:20 +10004037 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004038 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004039 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004040 * have actually been submitted.
4041 */
4042 atomic_dec(&conf->preread_active_stripes);
4043 if (atomic_read(&conf->preread_active_stripes) <
4044 IO_THRESHOLD)
4045 md_wakeup_thread(conf->mddev->thread);
4046 }
4047
NeilBrownc5709ef2011-07-26 11:35:20 +10004048 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07004049
Dan Williams257a4b42011-11-08 16:22:06 +11004050 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004051}
4052
NeilBrownd1688a62011-10-11 16:49:52 +11004053static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
4055 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4056 while (!list_empty(&conf->delayed_list)) {
4057 struct list_head *l = conf->delayed_list.next;
4058 struct stripe_head *sh;
4059 sh = list_entry(l, struct stripe_head, lru);
4060 list_del_init(l);
4061 clear_bit(STRIPE_DELAYED, &sh->state);
4062 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4063 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004064 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004065 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
NeilBrown482c0832011-04-18 18:25:42 +10004067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
Shaohua Li566c09c2013-11-14 15:16:17 +11004070static void activate_bit_delay(struct r5conf *conf,
4071 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004072{
4073 /* device_lock is held */
4074 struct list_head head;
4075 list_add(&head, &conf->bitmap_list);
4076 list_del_init(&conf->bitmap_list);
4077 while (!list_empty(&head)) {
4078 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004079 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004080 list_del_init(&sh->lru);
4081 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004082 hash = sh->hash_lock_index;
4083 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004084 }
4085}
4086
NeilBrownfd01b882011-10-11 16:47:53 +11004087int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004088{
NeilBrownd1688a62011-10-11 16:49:52 +11004089 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004090
4091 /* No difference between reads and writes. Just check
4092 * how busy the stripe_cache is
4093 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004094
NeilBrownf022b2f2006-10-03 01:15:56 -07004095 if (conf->inactive_blocked)
4096 return 1;
4097 if (conf->quiesce)
4098 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004099 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004100 return 1;
4101
4102 return 0;
4103}
NeilBrown11d8a6e2010-07-26 11:57:07 +10004104EXPORT_SYMBOL_GPL(md_raid5_congested);
4105
4106static int raid5_congested(void *data, int bits)
4107{
NeilBrownfd01b882011-10-11 16:47:53 +11004108 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10004109
4110 return mddev_congested(mddev, bits) ||
4111 md_raid5_congested(mddev, bits);
4112}
NeilBrownf022b2f2006-10-03 01:15:56 -07004113
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004114/* We want read requests to align with chunks where possible,
4115 * but write requests don't need to.
4116 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004117static int raid5_mergeable_bvec(struct request_queue *q,
4118 struct bvec_merge_data *bvm,
4119 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004120{
NeilBrownfd01b882011-10-11 16:47:53 +11004121 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004122 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004123 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004124 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004125 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004126
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004127 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004128 return biovec->bv_len; /* always allow writes to be mergeable */
4129
Andre Noll664e7c42009-06-18 08:45:27 +10004130 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4131 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004132 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
4133 if (max < 0) max = 0;
4134 if (max <= biovec->bv_len && bio_sectors == 0)
4135 return biovec->bv_len;
4136 else
4137 return max;
4138}
4139
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004140
NeilBrownfd01b882011-10-11 16:47:53 +11004141static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004142{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004143 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10004144 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004145 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004146
Andre Noll664e7c42009-06-18 08:45:27 +10004147 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4148 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004149 return chunk_sectors >=
4150 ((sector & (chunk_sectors - 1)) + bio_sectors);
4151}
4152
4153/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004154 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4155 * later sampled by raid5d.
4156 */
NeilBrownd1688a62011-10-11 16:49:52 +11004157static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004158{
4159 unsigned long flags;
4160
4161 spin_lock_irqsave(&conf->device_lock, flags);
4162
4163 bi->bi_next = conf->retry_read_aligned_list;
4164 conf->retry_read_aligned_list = bi;
4165
4166 spin_unlock_irqrestore(&conf->device_lock, flags);
4167 md_wakeup_thread(conf->mddev->thread);
4168}
4169
4170
NeilBrownd1688a62011-10-11 16:49:52 +11004171static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004172{
4173 struct bio *bi;
4174
4175 bi = conf->retry_read_aligned;
4176 if (bi) {
4177 conf->retry_read_aligned = NULL;
4178 return bi;
4179 }
4180 bi = conf->retry_read_aligned_list;
4181 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004182 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004183 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004184 /*
4185 * this sets the active strip count to 1 and the processed
4186 * strip count to zero (upper 8 bits)
4187 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004188 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004189 }
4190
4191 return bi;
4192}
4193
4194
4195/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004196 * The "raid5_align_endio" should check if the read succeeded and if it
4197 * did, call bio_endio on the original bio (having bio_put the new bio
4198 * first).
4199 * If the read failed..
4200 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004201static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004202{
4203 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004204 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004205 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004206 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004207 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004208
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004209 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004210
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004211 rdev = (void*)raid_bi->bi_next;
4212 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004213 mddev = rdev->mddev;
4214 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004215
4216 rdev_dec_pending(rdev, conf->mddev);
4217
4218 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004219 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4220 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004221 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004222 if (atomic_dec_and_test(&conf->active_aligned_reads))
4223 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004224 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004225 }
4226
4227
Dan Williams45b42332007-07-09 11:56:43 -07004228 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004229
4230 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004231}
4232
Neil Brown387bb172007-02-08 14:20:29 -08004233static int bio_fits_rdev(struct bio *bi)
4234{
Jens Axboe165125e2007-07-24 09:28:11 +02004235 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004236
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004237 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004238 return 0;
4239 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004240 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004241 return 0;
4242
4243 if (q->merge_bvec_fn)
4244 /* it's too hard to apply the merge_bvec_fn at this stage,
4245 * just just give up
4246 */
4247 return 0;
4248
4249 return 1;
4250}
4251
4252
NeilBrownfd01b882011-10-11 16:47:53 +11004253static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004254{
NeilBrownd1688a62011-10-11 16:49:52 +11004255 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004256 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004257 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004258 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004259 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004260
4261 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004262 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004263 return 0;
4264 }
4265 /*
NeilBrowna167f662010-10-26 18:31:13 +11004266 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004267 */
NeilBrowna167f662010-10-26 18:31:13 +11004268 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004269 if (!align_bi)
4270 return 0;
4271 /*
4272 * set bi_end_io to a new function, and set bi_private to the
4273 * original bio.
4274 */
4275 align_bi->bi_end_io = raid5_align_endio;
4276 align_bi->bi_private = raid_bio;
4277 /*
4278 * compute position
4279 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004280 align_bi->bi_iter.bi_sector =
4281 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
4282 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004283
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004284 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004285 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004286 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4287 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4288 rdev->recovery_offset < end_sector) {
4289 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4290 if (rdev &&
4291 (test_bit(Faulty, &rdev->flags) ||
4292 !(test_bit(In_sync, &rdev->flags) ||
4293 rdev->recovery_offset >= end_sector)))
4294 rdev = NULL;
4295 }
4296 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004297 sector_t first_bad;
4298 int bad_sectors;
4299
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004300 atomic_inc(&rdev->nr_pending);
4301 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004302 raid_bio->bi_next = (void*)rdev;
4303 align_bi->bi_bdev = rdev->bdev;
NeilBrown3fd83712014-08-23 20:19:26 +10004304 __clear_bit(BIO_SEG_VALID, &align_bi->bi_flags);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004305
NeilBrown31c176e2011-07-28 11:39:22 +10004306 if (!bio_fits_rdev(align_bi) ||
Kent Overstreet4f024f32013-10-11 15:44:27 -07004307 is_badblock(rdev, align_bi->bi_iter.bi_sector,
4308 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004309 &first_bad, &bad_sectors)) {
4310 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004311 bio_put(align_bi);
4312 rdev_dec_pending(rdev, mddev);
4313 return 0;
4314 }
4315
majianpeng6c0544e2012-06-12 08:31:10 +08004316 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004317 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08004318
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004319 spin_lock_irq(&conf->device_lock);
4320 wait_event_lock_irq(conf->wait_for_stripe,
4321 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004322 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004323 atomic_inc(&conf->active_aligned_reads);
4324 spin_unlock_irq(&conf->device_lock);
4325
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004326 if (mddev->gendisk)
4327 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4328 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07004329 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004330 generic_make_request(align_bi);
4331 return 1;
4332 } else {
4333 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004334 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004335 return 0;
4336 }
4337}
4338
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004339/* __get_priority_stripe - get the next stripe to process
4340 *
4341 * Full stripe writes are allowed to pass preread active stripes up until
4342 * the bypass_threshold is exceeded. In general the bypass_count
4343 * increments when the handle_list is handled before the hold_list; however, it
4344 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4345 * stripe with in flight i/o. The bypass_count will be reset when the
4346 * head of the hold_list has changed, i.e. the head was promoted to the
4347 * handle_list.
4348 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004349static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004350{
Shaohua Li851c30c2013-08-28 14:30:16 +08004351 struct stripe_head *sh = NULL, *tmp;
4352 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004353 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004354
4355 if (conf->worker_cnt_per_group == 0) {
4356 handle_list = &conf->handle_list;
4357 } else if (group != ANY_GROUP) {
4358 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004359 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004360 } else {
4361 int i;
4362 for (i = 0; i < conf->group_cnt; i++) {
4363 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004364 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004365 if (!list_empty(handle_list))
4366 break;
4367 }
4368 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004369
4370 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4371 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004372 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004373 list_empty(&conf->hold_list) ? "empty" : "busy",
4374 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4375
Shaohua Li851c30c2013-08-28 14:30:16 +08004376 if (!list_empty(handle_list)) {
4377 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004378
4379 if (list_empty(&conf->hold_list))
4380 conf->bypass_count = 0;
4381 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4382 if (conf->hold_list.next == conf->last_hold)
4383 conf->bypass_count++;
4384 else {
4385 conf->last_hold = conf->hold_list.next;
4386 conf->bypass_count -= conf->bypass_threshold;
4387 if (conf->bypass_count < 0)
4388 conf->bypass_count = 0;
4389 }
4390 }
4391 } else if (!list_empty(&conf->hold_list) &&
4392 ((conf->bypass_threshold &&
4393 conf->bypass_count > conf->bypass_threshold) ||
4394 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004395
4396 list_for_each_entry(tmp, &conf->hold_list, lru) {
4397 if (conf->worker_cnt_per_group == 0 ||
4398 group == ANY_GROUP ||
4399 !cpu_online(tmp->cpu) ||
4400 cpu_to_group(tmp->cpu) == group) {
4401 sh = tmp;
4402 break;
4403 }
4404 }
4405
4406 if (sh) {
4407 conf->bypass_count -= conf->bypass_threshold;
4408 if (conf->bypass_count < 0)
4409 conf->bypass_count = 0;
4410 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004411 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004412 }
4413
4414 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004415 return NULL;
4416
Shaohua Libfc90cb2013-08-29 15:40:32 +08004417 if (wg) {
4418 wg->stripes_cnt--;
4419 sh->group = NULL;
4420 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004421 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08004422 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004423 return sh;
4424}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004425
Shaohua Li8811b592012-08-02 08:33:00 +10004426struct raid5_plug_cb {
4427 struct blk_plug_cb cb;
4428 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11004429 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10004430};
4431
4432static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4433{
4434 struct raid5_plug_cb *cb = container_of(
4435 blk_cb, struct raid5_plug_cb, cb);
4436 struct stripe_head *sh;
4437 struct mddev *mddev = cb->cb.data;
4438 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004439 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11004440 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10004441
4442 if (cb->list.next && !list_empty(&cb->list)) {
4443 spin_lock_irq(&conf->device_lock);
4444 while (!list_empty(&cb->list)) {
4445 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4446 list_del_init(&sh->lru);
4447 /*
4448 * avoid race release_stripe_plug() sees
4449 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4450 * is still in our list
4451 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01004452 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10004453 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004454 /*
4455 * STRIPE_ON_RELEASE_LIST could be set here. In that
4456 * case, the count is always > 1 here
4457 */
Shaohua Li566c09c2013-11-14 15:16:17 +11004458 hash = sh->hash_lock_index;
4459 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11004460 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004461 }
4462 spin_unlock_irq(&conf->device_lock);
4463 }
Shaohua Li566c09c2013-11-14 15:16:17 +11004464 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4465 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004466 if (mddev->queue)
4467 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004468 kfree(cb);
4469}
4470
4471static void release_stripe_plug(struct mddev *mddev,
4472 struct stripe_head *sh)
4473{
4474 struct blk_plug_cb *blk_cb = blk_check_plugged(
4475 raid5_unplug, mddev,
4476 sizeof(struct raid5_plug_cb));
4477 struct raid5_plug_cb *cb;
4478
4479 if (!blk_cb) {
4480 release_stripe(sh);
4481 return;
4482 }
4483
4484 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4485
Shaohua Li566c09c2013-11-14 15:16:17 +11004486 if (cb->list.next == NULL) {
4487 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10004488 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11004489 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
4490 INIT_LIST_HEAD(cb->temp_inactive_list + i);
4491 }
Shaohua Li8811b592012-08-02 08:33:00 +10004492
4493 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4494 list_add_tail(&sh->lru, &cb->list);
4495 else
4496 release_stripe(sh);
4497}
4498
Shaohua Li620125f2012-10-11 13:49:05 +11004499static void make_discard_request(struct mddev *mddev, struct bio *bi)
4500{
4501 struct r5conf *conf = mddev->private;
4502 sector_t logical_sector, last_sector;
4503 struct stripe_head *sh;
4504 int remaining;
4505 int stripe_sectors;
4506
4507 if (mddev->reshape_position != MaxSector)
4508 /* Skip discard while reshape is happening */
4509 return;
4510
Kent Overstreet4f024f32013-10-11 15:44:27 -07004511 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4512 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11004513
4514 bi->bi_next = NULL;
4515 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4516
4517 stripe_sectors = conf->chunk_sectors *
4518 (conf->raid_disks - conf->max_degraded);
4519 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4520 stripe_sectors);
4521 sector_div(last_sector, stripe_sectors);
4522
4523 logical_sector *= conf->chunk_sectors;
4524 last_sector *= conf->chunk_sectors;
4525
4526 for (; logical_sector < last_sector;
4527 logical_sector += STRIPE_SECTORS) {
4528 DEFINE_WAIT(w);
4529 int d;
4530 again:
4531 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4532 prepare_to_wait(&conf->wait_for_overlap, &w,
4533 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004534 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4535 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4536 release_stripe(sh);
4537 schedule();
4538 goto again;
4539 }
4540 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004541 spin_lock_irq(&sh->stripe_lock);
4542 for (d = 0; d < conf->raid_disks; d++) {
4543 if (d == sh->pd_idx || d == sh->qd_idx)
4544 continue;
4545 if (sh->dev[d].towrite || sh->dev[d].toread) {
4546 set_bit(R5_Overlap, &sh->dev[d].flags);
4547 spin_unlock_irq(&sh->stripe_lock);
4548 release_stripe(sh);
4549 schedule();
4550 goto again;
4551 }
4552 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004553 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004554 finish_wait(&conf->wait_for_overlap, &w);
4555 for (d = 0; d < conf->raid_disks; d++) {
4556 if (d == sh->pd_idx || d == sh->qd_idx)
4557 continue;
4558 sh->dev[d].towrite = bi;
4559 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4560 raid5_inc_bi_active_stripes(bi);
4561 }
4562 spin_unlock_irq(&sh->stripe_lock);
4563 if (conf->mddev->bitmap) {
4564 for (d = 0;
4565 d < conf->raid_disks - conf->max_degraded;
4566 d++)
4567 bitmap_startwrite(mddev->bitmap,
4568 sh->sector,
4569 STRIPE_SECTORS,
4570 0);
4571 sh->bm_seq = conf->seq_flush + 1;
4572 set_bit(STRIPE_BIT_DELAY, &sh->state);
4573 }
4574
4575 set_bit(STRIPE_HANDLE, &sh->state);
4576 clear_bit(STRIPE_DELAYED, &sh->state);
4577 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4578 atomic_inc(&conf->preread_active_stripes);
4579 release_stripe_plug(mddev, sh);
4580 }
4581
4582 remaining = raid5_dec_bi_active_stripes(bi);
4583 if (remaining == 0) {
4584 md_write_end(mddev);
4585 bio_endio(bi, 0);
4586 }
4587}
4588
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004589static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590{
NeilBrownd1688a62011-10-11 16:49:52 +11004591 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004592 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 sector_t new_sector;
4594 sector_t logical_sector, last_sector;
4595 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004596 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004597 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08004598 DEFINE_WAIT(w);
4599 bool do_prepare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Tejun Heoe9c74692010-09-03 11:56:18 +02004601 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4602 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004603 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004604 }
4605
NeilBrown3d310eb2005-06-21 17:17:26 -07004606 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004607
NeilBrown802ba062006-12-13 00:34:13 -08004608 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004609 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004610 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004611 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004612
Shaohua Li620125f2012-10-11 13:49:05 +11004613 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4614 make_discard_request(mddev, bi);
4615 return;
4616 }
4617
Kent Overstreet4f024f32013-10-11 15:44:27 -07004618 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004619 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 bi->bi_next = NULL;
4621 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004622
Shaohua Li27c0f682014-04-09 11:25:47 +08004623 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004625 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10004626 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08004627
Shaohua Li27c0f682014-04-09 11:25:47 +08004628 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004629 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10004630 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11004631 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08004632 if (do_prepare)
4633 prepare_to_wait(&conf->wait_for_overlap, &w,
4634 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004635 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004636 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004637 * 64bit on a 32bit platform, and so it might be
4638 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004639 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004640 * the lock is dropped, so once we get a reference
4641 * to the stripe that we think it is, we will have
4642 * to check again.
4643 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004644 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004645 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004646 ? logical_sector < conf->reshape_progress
4647 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004648 previous = 1;
4649 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004650 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004651 ? logical_sector < conf->reshape_safe
4652 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004653 spin_unlock_irq(&conf->device_lock);
4654 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004655 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08004656 goto retry;
4657 }
4658 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004659 spin_unlock_irq(&conf->device_lock);
4660 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004661
NeilBrown112bf892009-03-31 14:39:38 +11004662 new_sector = raid5_compute_sector(conf, logical_sector,
4663 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004664 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004665 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10004666 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 (unsigned long long)logical_sector);
4668
NeilBrownb5663ba2009-03-31 14:39:38 +11004669 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004670 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004672 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004673 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004674 * stripe, so we must do the range check again.
4675 * Expansion could still move past after this
4676 * test, but as we are holding a reference to
4677 * 'sh', we know that if that happens,
4678 * STRIPE_EXPANDING will get set and the expansion
4679 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004680 */
4681 int must_retry = 0;
4682 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004683 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004684 ? logical_sector >= conf->reshape_progress
4685 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004686 /* mismatch, need to try again */
4687 must_retry = 1;
4688 spin_unlock_irq(&conf->device_lock);
4689 if (must_retry) {
4690 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004691 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004692 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004693 goto retry;
4694 }
4695 }
NeilBrownc46501b2013-08-27 15:52:13 +10004696 if (read_seqcount_retry(&conf->gen_lock, seq)) {
4697 /* Might have got the wrong stripe_head
4698 * by accident
4699 */
4700 release_stripe(sh);
4701 goto retry;
4702 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004703
Namhyung Kimffd96e32011-07-18 17:38:51 +10004704 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004705 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004706 logical_sector < mddev->suspend_hi) {
4707 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004708 /* As the suspend_* range is controlled by
4709 * userspace, we want an interruptible
4710 * wait.
4711 */
4712 flush_signals(current);
4713 prepare_to_wait(&conf->wait_for_overlap,
4714 &w, TASK_INTERRUPTIBLE);
4715 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08004716 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10004717 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004718 do_prepare = true;
4719 }
NeilBrowne464eaf2006-03-27 01:18:14 -08004720 goto retry;
4721 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004722
4723 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004724 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004725 /* Stripe is busy expanding or
4726 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 * and wait a while
4728 */
NeilBrown482c0832011-04-18 18:25:42 +10004729 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 release_stripe(sh);
4731 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004732 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 goto retry;
4734 }
NeilBrown6ed30032008-02-06 01:40:00 -08004735 set_bit(STRIPE_HANDLE, &sh->state);
4736 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004737 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004738 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4739 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004740 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 } else {
4742 /* cannot get stripe for read-ahead, just give-up */
4743 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 break;
4745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 }
Shaohua Li27c0f682014-04-09 11:25:47 +08004747 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10004748
Shaohua Lie7836bd62012-07-19 16:01:31 +10004749 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004750 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
NeilBrown16a53ec2006-06-26 00:27:38 -07004752 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004754
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004755 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4756 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004757 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
NeilBrownfd01b882011-10-11 16:47:53 +11004761static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004762
NeilBrownfd01b882011-10-11 16:47:53 +11004763static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
NeilBrown52c03292006-06-26 00:27:43 -07004765 /* reshaping is quite different to recovery/resync so it is
4766 * handled quite separately ... here.
4767 *
4768 * On each call to sync_request, we gather one chunk worth of
4769 * destination stripes and flag them as expanding.
4770 * Then we find all the source stripes and request reads.
4771 * As the reads complete, handle_stripe will copy the data
4772 * into the destination stripe and release that stripe.
4773 */
NeilBrownd1688a62011-10-11 16:49:52 +11004774 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004776 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004777 int raid_disks = conf->previous_raid_disks;
4778 int data_disks = raid_disks - conf->max_degraded;
4779 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004780 int i;
4781 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004782 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004783 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004784 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004785 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004786
NeilBrownfef9c612009-03-31 15:16:46 +11004787 if (sector_nr == 0) {
4788 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004789 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004790 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4791 sector_nr = raid5_size(mddev, 0, 0)
4792 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004793 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004794 conf->reshape_progress > 0)
4795 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004796 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004797 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004798 mddev->curr_resync_completed = sector_nr;
4799 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004800 *skipped = 1;
4801 return sector_nr;
4802 }
NeilBrown52c03292006-06-26 00:27:43 -07004803 }
4804
NeilBrown7a661382009-03-31 15:21:40 +11004805 /* We need to process a full chunk at a time.
4806 * If old and new chunk sizes differ, we need to process the
4807 * largest of these
4808 */
Andre Noll664e7c42009-06-18 08:45:27 +10004809 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4810 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004811 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004812 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004813
NeilBrownb5254dd2012-05-21 09:27:01 +10004814 /* We update the metadata at least every 10 seconds, or when
4815 * the data about to be copied would over-write the source of
4816 * the data at the front of the range. i.e. one new_stripe
4817 * along from reshape_progress new_maps to after where
4818 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004819 */
NeilBrownfef9c612009-03-31 15:16:46 +11004820 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004821 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004822 readpos = conf->reshape_progress;
4823 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004824 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004825 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004826 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004827 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004828 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004829 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004830 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004831 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004832 readpos -= min_t(sector_t, reshape_sectors, readpos);
4833 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004834 }
NeilBrown52c03292006-06-26 00:27:43 -07004835
NeilBrownb5254dd2012-05-21 09:27:01 +10004836 /* Having calculated the 'writepos' possibly use it
4837 * to set 'stripe_addr' which is where we will write to.
4838 */
4839 if (mddev->reshape_backwards) {
4840 BUG_ON(conf->reshape_progress == 0);
4841 stripe_addr = writepos;
4842 BUG_ON((mddev->dev_sectors &
4843 ~((sector_t)reshape_sectors - 1))
4844 - reshape_sectors - stripe_addr
4845 != sector_nr);
4846 } else {
4847 BUG_ON(writepos != sector_nr + reshape_sectors);
4848 stripe_addr = sector_nr;
4849 }
4850
NeilBrownc8f517c2009-03-31 15:28:40 +11004851 /* 'writepos' is the most advanced device address we might write.
4852 * 'readpos' is the least advanced device address we might read.
4853 * 'safepos' is the least address recorded in the metadata as having
4854 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004855 * If there is a min_offset_diff, these are adjusted either by
4856 * increasing the safepos/readpos if diff is negative, or
4857 * increasing writepos if diff is positive.
4858 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004859 * ensure safety in the face of a crash - that must be done by userspace
4860 * making a backup of the data. So in that case there is no particular
4861 * rush to update metadata.
4862 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4863 * update the metadata to advance 'safepos' to match 'readpos' so that
4864 * we can be safe in the event of a crash.
4865 * So we insist on updating metadata if safepos is behind writepos and
4866 * readpos is beyond writepos.
4867 * In any case, update the metadata every 10 seconds.
4868 * Maybe that number should be configurable, but I'm not sure it is
4869 * worth it.... maybe it could be a multiple of safemode_delay???
4870 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004871 if (conf->min_offset_diff < 0) {
4872 safepos += -conf->min_offset_diff;
4873 readpos += -conf->min_offset_diff;
4874 } else
4875 writepos += conf->min_offset_diff;
4876
NeilBrown2c810cd2012-05-21 09:27:00 +10004877 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004878 ? (safepos > writepos && readpos < writepos)
4879 : (safepos < writepos && readpos > writepos)) ||
4880 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004881 /* Cannot proceed until we've updated the superblock... */
4882 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11004883 atomic_read(&conf->reshape_stripes)==0
4884 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4885 if (atomic_read(&conf->reshape_stripes) != 0)
4886 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11004887 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004888 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004889 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004890 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004891 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004892 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004893 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4894 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4895 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07004896 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004897 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004898 spin_unlock_irq(&conf->device_lock);
4899 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004900 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004901 }
4902
NeilBrownab69ae12009-03-31 15:26:47 +11004903 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004904 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004905 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004906 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004907 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004908 set_bit(STRIPE_EXPANDING, &sh->state);
4909 atomic_inc(&conf->reshape_stripes);
4910 /* If any of this stripe is beyond the end of the old
4911 * array, then we need to zero those blocks
4912 */
4913 for (j=sh->disks; j--;) {
4914 sector_t s;
4915 if (j == sh->pd_idx)
4916 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004917 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004918 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004919 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004920 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004921 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004922 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004923 continue;
4924 }
4925 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4926 set_bit(R5_Expanded, &sh->dev[j].flags);
4927 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4928 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004929 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004930 set_bit(STRIPE_EXPAND_READY, &sh->state);
4931 set_bit(STRIPE_HANDLE, &sh->state);
4932 }
NeilBrownab69ae12009-03-31 15:26:47 +11004933 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004934 }
4935 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004936 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004937 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004938 else
NeilBrown7a661382009-03-31 15:21:40 +11004939 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004940 spin_unlock_irq(&conf->device_lock);
4941 /* Ok, those stripe are ready. We can start scheduling
4942 * reads on the source stripes.
4943 * The source stripes are determined by mapping the first and last
4944 * block on the destination stripes.
4945 */
NeilBrown52c03292006-06-26 00:27:43 -07004946 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004947 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004948 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004949 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004950 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004951 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004952 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004953 if (last_sector >= mddev->dev_sectors)
4954 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004955 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004956 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004957 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4958 set_bit(STRIPE_HANDLE, &sh->state);
4959 release_stripe(sh);
4960 first_sector += STRIPE_SECTORS;
4961 }
NeilBrownab69ae12009-03-31 15:26:47 +11004962 /* Now that the sources are clearly marked, we can release
4963 * the destination stripes
4964 */
4965 while (!list_empty(&stripes)) {
4966 sh = list_entry(stripes.next, struct stripe_head, lru);
4967 list_del_init(&sh->lru);
4968 release_stripe(sh);
4969 }
NeilBrownc6207272008-02-06 01:39:52 -08004970 /* If this takes us to the resync_max point where we have to pause,
4971 * then we need to write out the superblock.
4972 */
NeilBrown7a661382009-03-31 15:21:40 +11004973 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004974 if ((sector_nr - mddev->curr_resync_completed) * 2
4975 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004976 /* Cannot proceed until we've updated the superblock... */
4977 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11004978 atomic_read(&conf->reshape_stripes) == 0
4979 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4980 if (atomic_read(&conf->reshape_stripes) != 0)
4981 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11004982 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004983 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004984 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004985 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4986 md_wakeup_thread(mddev->thread);
4987 wait_event(mddev->sb_wait,
4988 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
NeilBrownc91abf52013-11-19 12:02:01 +11004989 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4990 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4991 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08004992 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004993 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004994 spin_unlock_irq(&conf->device_lock);
4995 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004996 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004997 }
NeilBrownc91abf52013-11-19 12:02:01 +11004998ret:
NeilBrown7a661382009-03-31 15:21:40 +11004999 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07005000}
5001
5002/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11005003static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07005004{
NeilBrownd1688a62011-10-11 16:49:52 +11005005 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005006 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005007 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005008 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005009 int still_degraded = 0;
5010 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
NeilBrown72626682005-09-09 16:23:54 -07005012 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005014
NeilBrown29269552006-03-27 01:18:10 -08005015 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5016 end_reshape(conf);
5017 return 0;
5018 }
NeilBrown72626682005-09-09 16:23:54 -07005019
5020 if (mddev->curr_resync < max_sector) /* aborted */
5021 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5022 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005023 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005024 conf->fullsync = 0;
5025 bitmap_close_sync(mddev->bitmap);
5026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 return 0;
5028 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005029
NeilBrown64bd6602009-08-03 10:59:58 +10005030 /* Allow raid5_quiesce to complete */
5031 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5032
NeilBrown52c03292006-06-26 00:27:43 -07005033 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5034 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005035
NeilBrownc6207272008-02-06 01:39:52 -08005036 /* No need to check resync_max as we never do more than one
5037 * stripe, and as resync_max will always be on a chunk boundary,
5038 * if the check in md_do_sync didn't fire, there is no chance
5039 * of overstepping resync_max here
5040 */
5041
NeilBrown16a53ec2006-06-26 00:27:38 -07005042 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 * to resync, then assert that we are finished, because there is
5044 * nothing we can do.
5045 */
NeilBrown3285edf2006-06-26 00:27:55 -07005046 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005047 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005048 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005049 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 return rv;
5051 }
majianpeng6f608042013-04-24 11:42:41 +10005052 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5053 !conf->fullsync &&
5054 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5055 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005056 /* we can skip this block, and probably more */
5057 sync_blocks /= STRIPE_SECTORS;
5058 *skipped = 1;
5059 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
NeilBrownb47490c2008-02-06 01:39:50 -08005062 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
5063
NeilBrowna8c906c2009-06-09 14:39:59 +10005064 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005066 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005068 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005070 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005072 /* Need to check if array will still be degraded after recovery/resync
5073 * We don't need to check the 'failed' flag as when that gets set,
5074 * recovery aborts.
5075 */
NeilBrownf001a702009-06-09 14:30:31 +10005076 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07005077 if (conf->disks[i].rdev == NULL)
5078 still_degraded = 1;
5079
5080 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5081
NeilBrown83206d62011-07-26 11:19:49 +10005082 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005083 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 release_stripe(sh);
5086
5087 return STRIPE_SECTORS;
5088}
5089
NeilBrownd1688a62011-10-11 16:49:52 +11005090static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005091{
5092 /* We may not be able to submit a whole bio at once as there
5093 * may not be enough stripe_heads available.
5094 * We cannot pre-allocate enough stripe_heads as we may need
5095 * more than exist in the cache (if we allow ever large chunks).
5096 * So we do one stripe head at a time and record in
5097 * ->bi_hw_segments how many have been done.
5098 *
5099 * We *know* that this entire raid_bio is in one chunk, so
5100 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5101 */
5102 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005103 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005104 sector_t sector, logical_sector, last_sector;
5105 int scnt = 0;
5106 int remaining;
5107 int handled = 0;
5108
Kent Overstreet4f024f32013-10-11 15:44:27 -07005109 logical_sector = raid_bio->bi_iter.bi_sector &
5110 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005111 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005112 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005113 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005114
5115 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005116 logical_sector += STRIPE_SECTORS,
5117 sector += STRIPE_SECTORS,
5118 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005119
Shaohua Lie7836bd62012-07-19 16:01:31 +10005120 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005121 /* already done this stripe */
5122 continue;
5123
hui jiao2844dc32014-06-05 11:34:24 +08005124 sh = get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005125
5126 if (!sh) {
5127 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005128 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005129 conf->retry_read_aligned = raid_bio;
5130 return handled;
5131 }
5132
Neil Brown387bb172007-02-08 14:20:29 -08005133 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
5134 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005135 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005136 conf->retry_read_aligned = raid_bio;
5137 return handled;
5138 }
5139
majianpeng3f9e7c12012-07-31 10:04:21 +10005140 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005141 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005142 release_stripe(sh);
5143 handled++;
5144 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005145 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005146 if (remaining == 0) {
5147 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5148 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10005149 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005150 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005151 if (atomic_dec_and_test(&conf->active_aligned_reads))
5152 wake_up(&conf->wait_for_stripe);
5153 return handled;
5154}
5155
Shaohua Libfc90cb2013-08-29 15:40:32 +08005156static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005157 struct r5worker *worker,
5158 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005159{
5160 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005161 int i, batch_size = 0, hash;
5162 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005163
5164 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005165 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005166 batch[batch_size++] = sh;
5167
Shaohua Li566c09c2013-11-14 15:16:17 +11005168 if (batch_size == 0) {
5169 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5170 if (!list_empty(temp_inactive_list + i))
5171 break;
5172 if (i == NR_STRIPE_HASH_LOCKS)
5173 return batch_size;
5174 release_inactive = true;
5175 }
Shaohua Li46a06402012-08-02 08:33:15 +10005176 spin_unlock_irq(&conf->device_lock);
5177
Shaohua Li566c09c2013-11-14 15:16:17 +11005178 release_inactive_stripe_list(conf, temp_inactive_list,
5179 NR_STRIPE_HASH_LOCKS);
5180
5181 if (release_inactive) {
5182 spin_lock_irq(&conf->device_lock);
5183 return 0;
5184 }
5185
Shaohua Li46a06402012-08-02 08:33:15 +10005186 for (i = 0; i < batch_size; i++)
5187 handle_stripe(batch[i]);
5188
5189 cond_resched();
5190
5191 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005192 for (i = 0; i < batch_size; i++) {
5193 hash = batch[i]->hash_lock_index;
5194 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5195 }
Shaohua Li46a06402012-08-02 08:33:15 +10005196 return batch_size;
5197}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005198
Shaohua Li851c30c2013-08-28 14:30:16 +08005199static void raid5_do_work(struct work_struct *work)
5200{
5201 struct r5worker *worker = container_of(work, struct r5worker, work);
5202 struct r5worker_group *group = worker->group;
5203 struct r5conf *conf = group->conf;
5204 int group_id = group - conf->worker_groups;
5205 int handled;
5206 struct blk_plug plug;
5207
5208 pr_debug("+++ raid5worker active\n");
5209
5210 blk_start_plug(&plug);
5211 handled = 0;
5212 spin_lock_irq(&conf->device_lock);
5213 while (1) {
5214 int batch_size, released;
5215
Shaohua Li566c09c2013-11-14 15:16:17 +11005216 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005217
Shaohua Li566c09c2013-11-14 15:16:17 +11005218 batch_size = handle_active_stripes(conf, group_id, worker,
5219 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005220 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005221 if (!batch_size && !released)
5222 break;
5223 handled += batch_size;
5224 }
5225 pr_debug("%d stripes handled\n", handled);
5226
5227 spin_unlock_irq(&conf->device_lock);
5228 blk_finish_plug(&plug);
5229
5230 pr_debug("--- raid5worker inactive\n");
5231}
5232
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233/*
5234 * This is our raid5 kernel thread.
5235 *
5236 * We scan the hash table for stripes which can be handled now.
5237 * During the scan, completed stripes are saved for us by the interrupt
5238 * handler, so that they will not have to wait for our next wakeup.
5239 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005240static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241{
Shaohua Li4ed87312012-10-11 13:34:00 +11005242 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005243 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005245 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
Dan Williams45b42332007-07-09 11:56:43 -07005247 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
5249 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005251 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 handled = 0;
5253 spin_lock_irq(&conf->device_lock);
5254 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005255 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005256 int batch_size, released;
5257
Shaohua Li566c09c2013-11-14 15:16:17 +11005258 released = release_stripe_list(conf, conf->temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
NeilBrown0021b7b2012-07-31 09:08:14 +02005260 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005261 !list_empty(&conf->bitmap_list)) {
5262 /* Now is a good time to flush some bitmap updates */
5263 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005264 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005265 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005266 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005267 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005268 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005269 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005270 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005271
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005272 while ((bio = remove_bio_from_retry(conf))) {
5273 int ok;
5274 spin_unlock_irq(&conf->device_lock);
5275 ok = retry_aligned_read(conf, bio);
5276 spin_lock_irq(&conf->device_lock);
5277 if (!ok)
5278 break;
5279 handled++;
5280 }
5281
Shaohua Li566c09c2013-11-14 15:16:17 +11005282 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5283 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005284 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005286 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Shaohua Li46a06402012-08-02 08:33:15 +10005288 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5289 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005290 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005291 spin_lock_irq(&conf->device_lock);
5292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 }
Dan Williams45b42332007-07-09 11:56:43 -07005294 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
5296 spin_unlock_irq(&conf->device_lock);
5297
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005298 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005299 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Dan Williams45b42332007-07-09 11:56:43 -07005301 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302}
5303
NeilBrown3f294f42005-11-08 21:39:25 -08005304static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005305raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005306{
NeilBrownd1688a62011-10-11 16:49:52 +11005307 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005308 if (conf)
5309 return sprintf(page, "%d\n", conf->max_nr_stripes);
5310 else
5311 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005312}
5313
NeilBrownc41d4ac2010-06-01 19:37:24 +10005314int
NeilBrownfd01b882011-10-11 16:47:53 +11005315raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005316{
NeilBrownd1688a62011-10-11 16:49:52 +11005317 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005318 int err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005319 int hash;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005320
5321 if (size <= 16 || size > 32768)
5322 return -EINVAL;
Shaohua Li566c09c2013-11-14 15:16:17 +11005323 hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005324 while (size < conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005325 if (drop_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005326 conf->max_nr_stripes--;
5327 else
5328 break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005329 hash--;
5330 if (hash < 0)
5331 hash = NR_STRIPE_HASH_LOCKS - 1;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005332 }
5333 err = md_allow_write(mddev);
5334 if (err)
5335 return err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005336 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005337 while (size > conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005338 if (grow_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005339 conf->max_nr_stripes++;
5340 else break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005341 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005342 }
5343 return 0;
5344}
5345EXPORT_SYMBOL(raid5_set_cache_size);
5346
NeilBrown3f294f42005-11-08 21:39:25 -08005347static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005348raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005349{
NeilBrownd1688a62011-10-11 16:49:52 +11005350 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005351 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005352 int err;
5353
NeilBrown3f294f42005-11-08 21:39:25 -08005354 if (len >= PAGE_SIZE)
5355 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08005356 if (!conf)
5357 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08005358
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005359 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005360 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005361 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005362 if (err)
5363 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08005364 return len;
5365}
NeilBrown007583c2005-11-08 21:39:30 -08005366
NeilBrown96de1e62005-11-08 21:39:39 -08005367static struct md_sysfs_entry
5368raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5369 raid5_show_stripe_cache_size,
5370 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005371
5372static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005373raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005374{
NeilBrownd1688a62011-10-11 16:49:52 +11005375 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005376 if (conf)
5377 return sprintf(page, "%d\n", conf->bypass_threshold);
5378 else
5379 return 0;
5380}
5381
5382static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005383raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005384{
NeilBrownd1688a62011-10-11 16:49:52 +11005385 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005386 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005387 if (len >= PAGE_SIZE)
5388 return -EINVAL;
5389 if (!conf)
5390 return -ENODEV;
5391
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005392 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005393 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005394 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005395 return -EINVAL;
5396 conf->bypass_threshold = new;
5397 return len;
5398}
5399
5400static struct md_sysfs_entry
5401raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5402 S_IRUGO | S_IWUSR,
5403 raid5_show_preread_threshold,
5404 raid5_store_preread_threshold);
5405
5406static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08005407raid5_show_skip_copy(struct mddev *mddev, char *page)
5408{
5409 struct r5conf *conf = mddev->private;
5410 if (conf)
5411 return sprintf(page, "%d\n", conf->skip_copy);
5412 else
5413 return 0;
5414}
5415
5416static ssize_t
5417raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
5418{
5419 struct r5conf *conf = mddev->private;
5420 unsigned long new;
5421 if (len >= PAGE_SIZE)
5422 return -EINVAL;
5423 if (!conf)
5424 return -ENODEV;
5425
5426 if (kstrtoul(page, 10, &new))
5427 return -EINVAL;
5428 new = !!new;
5429 if (new == conf->skip_copy)
5430 return len;
5431
5432 mddev_suspend(mddev);
5433 conf->skip_copy = new;
5434 if (new)
5435 mddev->queue->backing_dev_info.capabilities |=
5436 BDI_CAP_STABLE_WRITES;
5437 else
5438 mddev->queue->backing_dev_info.capabilities &=
5439 ~BDI_CAP_STABLE_WRITES;
5440 mddev_resume(mddev);
5441 return len;
5442}
5443
5444static struct md_sysfs_entry
5445raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
5446 raid5_show_skip_copy,
5447 raid5_store_skip_copy);
5448
5449
5450static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005451stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005452{
NeilBrownd1688a62011-10-11 16:49:52 +11005453 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005454 if (conf)
5455 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5456 else
5457 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005458}
5459
NeilBrown96de1e62005-11-08 21:39:39 -08005460static struct md_sysfs_entry
5461raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005462
Shaohua Lib7214202013-08-27 17:50:42 +08005463static ssize_t
5464raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5465{
5466 struct r5conf *conf = mddev->private;
5467 if (conf)
5468 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5469 else
5470 return 0;
5471}
5472
majianpeng60aaf932013-11-14 15:16:20 +11005473static int alloc_thread_groups(struct r5conf *conf, int cnt,
5474 int *group_cnt,
5475 int *worker_cnt_per_group,
5476 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08005477static ssize_t
5478raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5479{
5480 struct r5conf *conf = mddev->private;
5481 unsigned long new;
5482 int err;
majianpeng60aaf932013-11-14 15:16:20 +11005483 struct r5worker_group *new_groups, *old_groups;
5484 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08005485
5486 if (len >= PAGE_SIZE)
5487 return -EINVAL;
5488 if (!conf)
5489 return -ENODEV;
5490
5491 if (kstrtoul(page, 10, &new))
5492 return -EINVAL;
5493
5494 if (new == conf->worker_cnt_per_group)
5495 return len;
5496
5497 mddev_suspend(mddev);
5498
5499 old_groups = conf->worker_groups;
majianpengd206dcf2013-11-14 15:16:19 +11005500 if (old_groups)
5501 flush_workqueue(raid5_wq);
Shaohua Lib7214202013-08-27 17:50:42 +08005502
majianpeng60aaf932013-11-14 15:16:20 +11005503 err = alloc_thread_groups(conf, new,
5504 &group_cnt, &worker_cnt_per_group,
5505 &new_groups);
5506 if (!err) {
5507 spin_lock_irq(&conf->device_lock);
5508 conf->group_cnt = group_cnt;
5509 conf->worker_cnt_per_group = worker_cnt_per_group;
5510 conf->worker_groups = new_groups;
5511 spin_unlock_irq(&conf->device_lock);
5512
Shaohua Lib7214202013-08-27 17:50:42 +08005513 if (old_groups)
5514 kfree(old_groups[0].workers);
5515 kfree(old_groups);
5516 }
5517
5518 mddev_resume(mddev);
5519
5520 if (err)
5521 return err;
5522 return len;
5523}
5524
5525static struct md_sysfs_entry
5526raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5527 raid5_show_group_thread_cnt,
5528 raid5_store_group_thread_cnt);
5529
NeilBrown007583c2005-11-08 21:39:30 -08005530static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005531 &raid5_stripecache_size.attr,
5532 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005533 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08005534 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08005535 &raid5_skip_copy.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005536 NULL,
5537};
NeilBrown007583c2005-11-08 21:39:30 -08005538static struct attribute_group raid5_attrs_group = {
5539 .name = NULL,
5540 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005541};
5542
majianpeng60aaf932013-11-14 15:16:20 +11005543static int alloc_thread_groups(struct r5conf *conf, int cnt,
5544 int *group_cnt,
5545 int *worker_cnt_per_group,
5546 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08005547{
Shaohua Li566c09c2013-11-14 15:16:17 +11005548 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08005549 ssize_t size;
5550 struct r5worker *workers;
5551
majianpeng60aaf932013-11-14 15:16:20 +11005552 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08005553 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11005554 *group_cnt = 0;
5555 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005556 return 0;
5557 }
majianpeng60aaf932013-11-14 15:16:20 +11005558 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08005559 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11005560 workers = kzalloc(size * *group_cnt, GFP_NOIO);
5561 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
5562 *group_cnt, GFP_NOIO);
5563 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005564 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11005565 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08005566 return -ENOMEM;
5567 }
5568
majianpeng60aaf932013-11-14 15:16:20 +11005569 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005570 struct r5worker_group *group;
5571
NeilBrown0c775d52013-11-25 11:12:43 +11005572 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005573 INIT_LIST_HEAD(&group->handle_list);
5574 group->conf = conf;
5575 group->workers = workers + i * cnt;
5576
5577 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005578 struct r5worker *worker = group->workers + j;
5579 worker->group = group;
5580 INIT_WORK(&worker->work, raid5_do_work);
5581
5582 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
5583 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08005584 }
5585 }
5586
5587 return 0;
5588}
5589
5590static void free_thread_groups(struct r5conf *conf)
5591{
5592 if (conf->worker_groups)
5593 kfree(conf->worker_groups[0].workers);
5594 kfree(conf->worker_groups);
5595 conf->worker_groups = NULL;
5596}
5597
Dan Williams80c3a6c2009-03-17 18:10:40 -07005598static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005599raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005600{
NeilBrownd1688a62011-10-11 16:49:52 +11005601 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005602
5603 if (!sectors)
5604 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005605 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005606 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005607 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005608
Andre Noll9d8f0362009-06-18 08:45:01 +10005609 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005610 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005611 return sectors * (raid_disks - conf->max_degraded);
5612}
5613
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305614static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5615{
5616 safe_put_page(percpu->spare_page);
5617 kfree(percpu->scribble);
5618 percpu->spare_page = NULL;
5619 percpu->scribble = NULL;
5620}
5621
5622static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5623{
5624 if (conf->level == 6 && !percpu->spare_page)
5625 percpu->spare_page = alloc_page(GFP_KERNEL);
5626 if (!percpu->scribble)
5627 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5628
5629 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
5630 free_scratch_buffer(conf, percpu);
5631 return -ENOMEM;
5632 }
5633
5634 return 0;
5635}
5636
NeilBrownd1688a62011-10-11 16:49:52 +11005637static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005638{
Dan Williams36d1c642009-07-14 11:48:22 -07005639 unsigned long cpu;
5640
5641 if (!conf->percpu)
5642 return;
5643
Dan Williams36d1c642009-07-14 11:48:22 -07005644#ifdef CONFIG_HOTPLUG_CPU
5645 unregister_cpu_notifier(&conf->cpu_notify);
5646#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305647
5648 get_online_cpus();
5649 for_each_possible_cpu(cpu)
5650 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005651 put_online_cpus();
5652
5653 free_percpu(conf->percpu);
5654}
5655
NeilBrownd1688a62011-10-11 16:49:52 +11005656static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005657{
Shaohua Li851c30c2013-08-28 14:30:16 +08005658 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005659 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005660 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005661 kfree(conf->disks);
5662 kfree(conf->stripe_hashtbl);
5663 kfree(conf);
5664}
5665
Dan Williams36d1c642009-07-14 11:48:22 -07005666#ifdef CONFIG_HOTPLUG_CPU
5667static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5668 void *hcpu)
5669{
NeilBrownd1688a62011-10-11 16:49:52 +11005670 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005671 long cpu = (long)hcpu;
5672 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5673
5674 switch (action) {
5675 case CPU_UP_PREPARE:
5676 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305677 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07005678 pr_err("%s: failed memory allocation for cpu%ld\n",
5679 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005680 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005681 }
5682 break;
5683 case CPU_DEAD:
5684 case CPU_DEAD_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305685 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005686 break;
5687 default:
5688 break;
5689 }
5690 return NOTIFY_OK;
5691}
5692#endif
5693
NeilBrownd1688a62011-10-11 16:49:52 +11005694static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005695{
5696 unsigned long cpu;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305697 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07005698
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305699 conf->percpu = alloc_percpu(struct raid5_percpu);
5700 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07005701 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07005702
Dan Williams36d1c642009-07-14 11:48:22 -07005703#ifdef CONFIG_HOTPLUG_CPU
5704 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5705 conf->cpu_notify.priority = 0;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305706 err = register_cpu_notifier(&conf->cpu_notify);
5707 if (err)
5708 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07005709#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305710
5711 get_online_cpus();
5712 for_each_present_cpu(cpu) {
5713 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5714 if (err) {
5715 pr_err("%s: failed memory allocation for cpu%ld\n",
5716 __func__, cpu);
5717 break;
5718 }
5719 }
Dan Williams36d1c642009-07-14 11:48:22 -07005720 put_online_cpus();
5721
5722 return err;
5723}
5724
NeilBrownd1688a62011-10-11 16:49:52 +11005725static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
NeilBrownd1688a62011-10-11 16:49:52 +11005727 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005728 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005729 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005731 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11005732 int i;
majianpeng60aaf932013-11-14 15:16:20 +11005733 int group_cnt, worker_cnt_per_group;
5734 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
NeilBrown91adb562009-03-31 14:39:39 +11005736 if (mddev->new_level != 5
5737 && mddev->new_level != 4
5738 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005739 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005740 mdname(mddev), mddev->new_level);
5741 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 }
NeilBrown91adb562009-03-31 14:39:39 +11005743 if ((mddev->new_level == 5
5744 && !algorithm_valid_raid5(mddev->new_layout)) ||
5745 (mddev->new_level == 6
5746 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005747 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005748 mdname(mddev), mddev->new_layout);
5749 return ERR_PTR(-EIO);
5750 }
5751 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005752 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005753 mdname(mddev), mddev->raid_disks);
5754 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
Andre Noll664e7c42009-06-18 08:45:27 +10005757 if (!mddev->new_chunk_sectors ||
5758 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5759 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005760 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5761 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005762 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005763 }
5764
NeilBrownd1688a62011-10-11 16:49:52 +11005765 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005766 if (conf == NULL)
5767 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08005768 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11005769 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
5770 &new_group)) {
5771 conf->group_cnt = group_cnt;
5772 conf->worker_cnt_per_group = worker_cnt_per_group;
5773 conf->worker_groups = new_group;
5774 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08005775 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005776 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10005777 seqcount_init(&conf->gen_lock);
Dan Williamsf5efd452009-10-16 15:55:38 +11005778 init_waitqueue_head(&conf->wait_for_stripe);
5779 init_waitqueue_head(&conf->wait_for_overlap);
5780 INIT_LIST_HEAD(&conf->handle_list);
5781 INIT_LIST_HEAD(&conf->hold_list);
5782 INIT_LIST_HEAD(&conf->delayed_list);
5783 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005784 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005785 atomic_set(&conf->active_stripes, 0);
5786 atomic_set(&conf->preread_active_stripes, 0);
5787 atomic_set(&conf->active_aligned_reads, 0);
5788 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005789 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005790
5791 conf->raid_disks = mddev->raid_disks;
5792 if (mddev->reshape_position == MaxSector)
5793 conf->previous_raid_disks = mddev->raid_disks;
5794 else
5795 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005796 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5797 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005798
NeilBrown5e5e3e72009-10-16 16:35:30 +11005799 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005800 GFP_KERNEL);
5801 if (!conf->disks)
5802 goto abort;
5803
5804 conf->mddev = mddev;
5805
5806 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5807 goto abort;
5808
Shaohua Li566c09c2013-11-14 15:16:17 +11005809 /* We init hash_locks[0] separately to that it can be used
5810 * as the reference lock in the spin_lock_nest_lock() call
5811 * in lock_all_device_hash_locks_irq in order to convince
5812 * lockdep that we know what we are doing.
5813 */
5814 spin_lock_init(conf->hash_locks);
5815 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
5816 spin_lock_init(conf->hash_locks + i);
5817
5818 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5819 INIT_LIST_HEAD(conf->inactive_list + i);
5820
5821 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5822 INIT_LIST_HEAD(conf->temp_inactive_list + i);
5823
Dan Williams36d1c642009-07-14 11:48:22 -07005824 conf->level = mddev->new_level;
5825 if (raid5_alloc_percpu(conf) != 0)
5826 goto abort;
5827
NeilBrown0c55e022010-05-03 14:09:02 +10005828 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005829
NeilBrowndafb20f2012-03-19 12:46:39 +11005830 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005831 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005832 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005833 || raid_disk < 0)
5834 continue;
5835 disk = conf->disks + raid_disk;
5836
NeilBrown17045f52011-12-23 10:17:53 +11005837 if (test_bit(Replacement, &rdev->flags)) {
5838 if (disk->replacement)
5839 goto abort;
5840 disk->replacement = rdev;
5841 } else {
5842 if (disk->rdev)
5843 goto abort;
5844 disk->rdev = rdev;
5845 }
NeilBrown91adb562009-03-31 14:39:39 +11005846
5847 if (test_bit(In_sync, &rdev->flags)) {
5848 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005849 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5850 " disk %d\n",
5851 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005852 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005853 /* Cannot rely on bitmap to complete recovery */
5854 conf->fullsync = 1;
5855 }
5856
Andre Noll09c9e5f2009-06-18 08:45:55 +10005857 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005858 conf->level = mddev->new_level;
5859 if (conf->level == 6)
5860 conf->max_degraded = 2;
5861 else
5862 conf->max_degraded = 1;
5863 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005864 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005865 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005866 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005867 conf->prev_algo = mddev->layout;
5868 }
NeilBrown91adb562009-03-31 14:39:39 +11005869
5870 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005871 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11005872 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
Shaohua Li566c09c2013-11-14 15:16:17 +11005873 if (grow_stripes(conf, NR_STRIPES)) {
NeilBrown91adb562009-03-31 14:39:39 +11005874 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005875 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5876 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005877 goto abort;
5878 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005879 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5880 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005881
NeilBrown02326052012-07-03 15:56:52 +10005882 sprintf(pers_name, "raid%d", mddev->new_level);
5883 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005884 if (!conf->thread) {
5885 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005886 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005887 mdname(mddev));
5888 goto abort;
5889 }
5890
5891 return conf;
5892
5893 abort:
5894 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005895 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005896 return ERR_PTR(-EIO);
5897 } else
5898 return ERR_PTR(-ENOMEM);
5899}
5900
NeilBrownc148ffd2009-11-13 17:47:00 +11005901
5902static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5903{
5904 switch (algo) {
5905 case ALGORITHM_PARITY_0:
5906 if (raid_disk < max_degraded)
5907 return 1;
5908 break;
5909 case ALGORITHM_PARITY_N:
5910 if (raid_disk >= raid_disks - max_degraded)
5911 return 1;
5912 break;
5913 case ALGORITHM_PARITY_0_6:
5914 if (raid_disk == 0 ||
5915 raid_disk == raid_disks - 1)
5916 return 1;
5917 break;
5918 case ALGORITHM_LEFT_ASYMMETRIC_6:
5919 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5920 case ALGORITHM_LEFT_SYMMETRIC_6:
5921 case ALGORITHM_RIGHT_SYMMETRIC_6:
5922 if (raid_disk == raid_disks - 1)
5923 return 1;
5924 }
5925 return 0;
5926}
5927
NeilBrownfd01b882011-10-11 16:47:53 +11005928static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005929{
NeilBrownd1688a62011-10-11 16:49:52 +11005930 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005931 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005932 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005933 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005934 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005935 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005936 long long min_offset_diff = 0;
5937 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005938
Andre Noll8c6ac862009-06-18 08:48:06 +10005939 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005940 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005941 " -- starting background reconstruction\n",
5942 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005943
5944 rdev_for_each(rdev, mddev) {
5945 long long diff;
5946 if (rdev->raid_disk < 0)
5947 continue;
5948 diff = (rdev->new_data_offset - rdev->data_offset);
5949 if (first) {
5950 min_offset_diff = diff;
5951 first = 0;
5952 } else if (mddev->reshape_backwards &&
5953 diff < min_offset_diff)
5954 min_offset_diff = diff;
5955 else if (!mddev->reshape_backwards &&
5956 diff > min_offset_diff)
5957 min_offset_diff = diff;
5958 }
5959
NeilBrownf6705572006-03-27 01:18:11 -08005960 if (mddev->reshape_position != MaxSector) {
5961 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005962 * Difficulties arise if the stripe we would write to
5963 * next is at or after the stripe we would read from next.
5964 * For a reshape that changes the number of devices, this
5965 * is only possible for a very short time, and mdadm makes
5966 * sure that time appears to have past before assembling
5967 * the array. So we fail if that time hasn't passed.
5968 * For a reshape that keeps the number of devices the same
5969 * mdadm must be monitoring the reshape can keeping the
5970 * critical areas read-only and backed up. It will start
5971 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005972 */
5973 sector_t here_new, here_old;
5974 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005975 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005976
NeilBrown88ce4932009-03-31 15:24:23 +11005977 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005978 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005979 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005980 mdname(mddev));
5981 return -EINVAL;
5982 }
NeilBrownf6705572006-03-27 01:18:11 -08005983 old_disks = mddev->raid_disks - mddev->delta_disks;
5984 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005985 * further up in new geometry must map after here in old
5986 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005987 */
5988 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005989 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005990 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005991 printk(KERN_ERR "md/raid:%s: reshape_position not "
5992 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005993 return -EINVAL;
5994 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005995 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005996 /* here_new is the stripe we will write to */
5997 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005998 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005999 (old_disks-max_degraded));
6000 /* here_old is the first stripe that we might need to read
6001 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10006002 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10006003 if ((here_new * mddev->new_chunk_sectors !=
6004 here_old * mddev->chunk_sectors)) {
6005 printk(KERN_ERR "md/raid:%s: reshape position is"
6006 " confused - aborting\n", mdname(mddev));
6007 return -EINVAL;
6008 }
NeilBrown67ac6012009-08-13 10:06:24 +10006009 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10006010 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10006011 * and taking constant backups.
6012 * mdadm always starts a situation like this in
6013 * readonly mode so it can take control before
6014 * allowing any writes. So just check for that.
6015 */
NeilBrownb5254dd2012-05-21 09:27:01 +10006016 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
6017 abs(min_offset_diff) >= mddev->new_chunk_sectors)
6018 /* not really in-place - so OK */;
6019 else if (mddev->ro == 0) {
6020 printk(KERN_ERR "md/raid:%s: in-place reshape "
6021 "must be started in read-only mode "
6022 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10006023 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10006024 return -EINVAL;
6025 }
NeilBrown2c810cd2012-05-21 09:27:00 +10006026 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10006027 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10006028 here_old * mddev->chunk_sectors)
6029 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10006030 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08006031 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10006032 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
6033 "auto-recovery - aborting.\n",
6034 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006035 return -EINVAL;
6036 }
NeilBrown0c55e022010-05-03 14:09:02 +10006037 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
6038 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006039 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08006040 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006041 BUG_ON(mddev->level != mddev->new_level);
6042 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10006043 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11006044 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08006045 }
6046
NeilBrown245f46c2009-03-31 14:39:39 +11006047 if (mddev->private == NULL)
6048 conf = setup_conf(mddev);
6049 else
6050 conf = mddev->private;
6051
NeilBrown91adb562009-03-31 14:39:39 +11006052 if (IS_ERR(conf))
6053 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08006054
NeilBrownb5254dd2012-05-21 09:27:01 +10006055 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11006056 mddev->thread = conf->thread;
6057 conf->thread = NULL;
6058 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
NeilBrown17045f52011-12-23 10:17:53 +11006060 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
6061 i++) {
6062 rdev = conf->disks[i].rdev;
6063 if (!rdev && conf->disks[i].replacement) {
6064 /* The replacement is all we have yet */
6065 rdev = conf->disks[i].replacement;
6066 conf->disks[i].replacement = NULL;
6067 clear_bit(Replacement, &rdev->flags);
6068 conf->disks[i].rdev = rdev;
6069 }
6070 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11006071 continue;
NeilBrown17045f52011-12-23 10:17:53 +11006072 if (conf->disks[i].replacement &&
6073 conf->reshape_progress != MaxSector) {
6074 /* replacements and reshape simply do not mix. */
6075 printk(KERN_ERR "md: cannot handle concurrent "
6076 "replacement and reshape.\n");
6077 goto abort;
6078 }
NeilBrown2f115882010-06-17 17:41:03 +10006079 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11006080 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10006081 continue;
6082 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006083 /* This disc is not fully in-sync. However if it
6084 * just stored parity (beyond the recovery_offset),
6085 * when we don't need to be concerned about the
6086 * array being dirty.
6087 * When reshape goes 'backwards', we never have
6088 * partially completed devices, so we only need
6089 * to worry about reshape going forwards.
6090 */
6091 /* Hack because v0.91 doesn't store recovery_offset properly. */
6092 if (mddev->major_version == 0 &&
6093 mddev->minor_version > 90)
6094 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006095
NeilBrownc148ffd2009-11-13 17:47:00 +11006096 if (rdev->recovery_offset < reshape_offset) {
6097 /* We need to check old and new layout */
6098 if (!only_parity(rdev->raid_disk,
6099 conf->algorithm,
6100 conf->raid_disks,
6101 conf->max_degraded))
6102 continue;
6103 }
6104 if (!only_parity(rdev->raid_disk,
6105 conf->prev_algo,
6106 conf->previous_raid_disks,
6107 conf->max_degraded))
6108 continue;
6109 dirty_parity_disks++;
6110 }
NeilBrown91adb562009-03-31 14:39:39 +11006111
NeilBrown17045f52011-12-23 10:17:53 +11006112 /*
6113 * 0 for a fully functional array, 1 or 2 for a degraded array.
6114 */
NeilBrown908f4fb2011-12-23 10:17:50 +11006115 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
NeilBrown674806d2010-06-16 17:17:53 +10006117 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006118 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006120 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 goto abort;
6122 }
6123
NeilBrown91adb562009-03-31 14:39:39 +11006124 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006125 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006126 mddev->resync_max_sectors = mddev->dev_sectors;
6127
NeilBrownc148ffd2009-11-13 17:47:00 +11006128 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006130 if (mddev->ok_start_degraded)
6131 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006132 "md/raid:%s: starting dirty degraded array"
6133 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006134 mdname(mddev));
6135 else {
6136 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006137 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006138 mdname(mddev));
6139 goto abort;
6140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 }
6142
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006144 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6145 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006146 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6147 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 else
NeilBrown0c55e022010-05-03 14:09:02 +10006149 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6150 " out of %d devices, algorithm %d\n",
6151 mdname(mddev), conf->level,
6152 mddev->raid_disks - mddev->degraded,
6153 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
6155 print_raid5_conf(conf);
6156
NeilBrownfef9c612009-03-31 15:16:46 +11006157 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006158 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006159 atomic_set(&conf->reshape_stripes, 0);
6160 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6161 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6162 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6163 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6164 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006165 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006166 }
6167
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
6169 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006170 if (mddev->to_remove == &raid5_attrs_group)
6171 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006172 else if (mddev->kobj.sd &&
6173 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006174 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006175 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006176 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006177 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6178
6179 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006180 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006181 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006182 /* read-ahead size must cover two whole stripes, which
6183 * is 2 * (datadisks) * chunksize where 'n' is the
6184 * number of raid devices
6185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6187 int stripe = data_disks *
6188 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6189 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6190 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006191
6192 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006193
6194 mddev->queue->backing_dev_info.congested_data = mddev;
6195 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10006196
6197 chunk_size = mddev->chunk_sectors << 9;
6198 blk_queue_io_min(mddev->queue, chunk_size);
6199 blk_queue_io_opt(mddev->queue, chunk_size *
6200 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07006201 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006202 /*
6203 * We can only discard a whole stripe. It doesn't make sense to
6204 * discard data disk but write parity disk
6205 */
6206 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11006207 /* Round up to power of 2, as discard handling
6208 * currently assumes that */
6209 while ((stripe-1) & stripe)
6210 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006211 mddev->queue->limits.discard_alignment = stripe;
6212 mddev->queue->limits.discard_granularity = stripe;
6213 /*
6214 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10006215 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11006216 */
6217 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10006218
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006219 blk_queue_max_write_same_sectors(mddev->queue, 0);
6220
NeilBrown05616be2012-05-21 09:27:00 +10006221 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006222 disk_stack_limits(mddev->gendisk, rdev->bdev,
6223 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10006224 disk_stack_limits(mddev->gendisk, rdev->bdev,
6225 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11006226 /*
6227 * discard_zeroes_data is required, otherwise data
6228 * could be lost. Consider a scenario: discard a stripe
6229 * (the stripe could be inconsistent if
6230 * discard_zeroes_data is 0); write one disk of the
6231 * stripe (the stripe could be inconsistent again
6232 * depending on which disks are used to calculate
6233 * parity); the disk is broken; The stripe data of this
6234 * disk is lost.
6235 */
6236 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6237 !bdev_get_queue(rdev->bdev)->
6238 limits.discard_zeroes_data)
6239 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10006240 /* Unfortunately, discard_zeroes_data is not currently
6241 * a guarantee - just a hint. So we only allow DISCARD
6242 * if the sysadmin has confirmed that only safe devices
6243 * are in use by setting a module parameter.
6244 */
6245 if (!devices_handle_discard_safely) {
6246 if (discard_supported) {
6247 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
6248 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
6249 }
6250 discard_supported = false;
6251 }
NeilBrown05616be2012-05-21 09:27:00 +10006252 }
Shaohua Li620125f2012-10-11 13:49:05 +11006253
6254 if (discard_supported &&
6255 mddev->queue->limits.max_discard_sectors >= stripe &&
6256 mddev->queue->limits.discard_granularity >= stripe)
6257 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6258 mddev->queue);
6259 else
6260 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6261 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 }
6263
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 return 0;
6265abort:
NeilBrown01f96c02011-09-21 15:30:20 +10006266 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11006267 print_raid5_conf(conf);
6268 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10006270 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 return -EIO;
6272}
6273
NeilBrownfd01b882011-10-11 16:47:53 +11006274static int stop(struct mddev *mddev)
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
NeilBrown01f96c02011-09-21 15:30:20 +10006278 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006279 if (mddev->queue)
6280 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10006281 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10006282 mddev->private = NULL;
6283 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 return 0;
6285}
6286
NeilBrownfd01b882011-10-11 16:47:53 +11006287static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288{
NeilBrownd1688a62011-10-11 16:49:52 +11006289 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 int i;
6291
Andre Noll9d8f0362009-06-18 08:45:01 +10006292 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
6293 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07006294 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 for (i = 0; i < conf->raid_disks; i++)
6296 seq_printf (seq, "%s",
6297 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08006298 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300}
6301
NeilBrownd1688a62011-10-11 16:49:52 +11006302static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303{
6304 int i;
6305 struct disk_info *tmp;
6306
NeilBrown0c55e022010-05-03 14:09:02 +10006307 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 if (!conf) {
6309 printk("(conf==NULL)\n");
6310 return;
6311 }
NeilBrown0c55e022010-05-03 14:09:02 +10006312 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
6313 conf->raid_disks,
6314 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
6316 for (i = 0; i < conf->raid_disks; i++) {
6317 char b[BDEVNAME_SIZE];
6318 tmp = conf->disks + i;
6319 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10006320 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
6321 i, !test_bit(Faulty, &tmp->rdev->flags),
6322 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 }
6324}
6325
NeilBrownfd01b882011-10-11 16:47:53 +11006326static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327{
6328 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11006329 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10006331 int count = 0;
6332 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
6334 for (i = 0; i < conf->raid_disks; i++) {
6335 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11006336 if (tmp->replacement
6337 && tmp->replacement->recovery_offset == MaxSector
6338 && !test_bit(Faulty, &tmp->replacement->flags)
6339 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6340 /* Replacement has just become active. */
6341 if (!tmp->rdev
6342 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6343 count++;
6344 if (tmp->rdev) {
6345 /* Replaced device not technically faulty,
6346 * but we need to be sure it gets removed
6347 * and never re-added.
6348 */
6349 set_bit(Faulty, &tmp->rdev->flags);
6350 sysfs_notify_dirent_safe(
6351 tmp->rdev->sysfs_state);
6352 }
6353 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6354 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10006355 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08006356 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07006357 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10006358 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11006359 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 }
6361 }
NeilBrown6b965622010-08-18 11:56:59 +10006362 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006363 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006364 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006366 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367}
6368
NeilBrownb8321b62011-12-23 10:17:51 +11006369static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370{
NeilBrownd1688a62011-10-11 16:49:52 +11006371 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006373 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006374 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 struct disk_info *p = conf->disks + number;
6376
6377 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006378 if (rdev == p->rdev)
6379 rdevp = &p->rdev;
6380 else if (rdev == p->replacement)
6381 rdevp = &p->replacement;
6382 else
6383 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006384
NeilBrown657e3e42011-12-23 10:17:52 +11006385 if (number >= conf->raid_disks &&
6386 conf->reshape_progress == MaxSector)
6387 clear_bit(In_sync, &rdev->flags);
6388
6389 if (test_bit(In_sync, &rdev->flags) ||
6390 atomic_read(&rdev->nr_pending)) {
6391 err = -EBUSY;
6392 goto abort;
6393 }
6394 /* Only remove non-faulty devices if recovery
6395 * isn't possible.
6396 */
6397 if (!test_bit(Faulty, &rdev->flags) &&
6398 mddev->recovery_disabled != conf->recovery_disabled &&
6399 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006400 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006401 number < conf->raid_disks) {
6402 err = -EBUSY;
6403 goto abort;
6404 }
6405 *rdevp = NULL;
6406 synchronize_rcu();
6407 if (atomic_read(&rdev->nr_pending)) {
6408 /* lost the race, try later */
6409 err = -EBUSY;
6410 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006411 } else if (p->replacement) {
6412 /* We must have just cleared 'rdev' */
6413 p->rdev = p->replacement;
6414 clear_bit(Replacement, &p->replacement->flags);
6415 smp_mb(); /* Make sure other CPUs may see both as identical
6416 * but will never see neither - if they are careful
6417 */
6418 p->replacement = NULL;
6419 clear_bit(WantReplacement, &rdev->flags);
6420 } else
6421 /* We might have just removed the Replacement as faulty-
6422 * clear the bit just in case
6423 */
6424 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425abort:
6426
6427 print_raid5_conf(conf);
6428 return err;
6429}
6430
NeilBrownfd01b882011-10-11 16:47:53 +11006431static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432{
NeilBrownd1688a62011-10-11 16:49:52 +11006433 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10006434 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 int disk;
6436 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10006437 int first = 0;
6438 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
NeilBrown7f0da592011-07-28 11:39:22 +10006440 if (mddev->recovery_disabled == conf->recovery_disabled)
6441 return -EBUSY;
6442
NeilBrowndc10c642012-03-19 12:46:37 +11006443 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10006445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
Neil Brown6c2fce22008-06-28 08:31:31 +10006447 if (rdev->raid_disk >= 0)
6448 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
6450 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07006451 * find the disk ... but prefer rdev->saved_raid_disk
6452 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 */
NeilBrown16a53ec2006-06-26 00:27:38 -07006454 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10006455 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006456 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10006457 first = rdev->saved_raid_disk;
6458
6459 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11006460 p = conf->disks + disk;
6461 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08006462 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10006464 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07006465 if (rdev->saved_raid_disk != disk)
6466 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08006467 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10006468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006470 }
6471 for (disk = first; disk <= last; disk++) {
6472 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11006473 if (test_bit(WantReplacement, &p->rdev->flags) &&
6474 p->replacement == NULL) {
6475 clear_bit(In_sync, &rdev->flags);
6476 set_bit(Replacement, &rdev->flags);
6477 rdev->raid_disk = disk;
6478 err = 0;
6479 conf->fullsync = 1;
6480 rcu_assign_pointer(p->replacement, rdev);
6481 break;
6482 }
6483 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006484out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10006486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487}
6488
NeilBrownfd01b882011-10-11 16:47:53 +11006489static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490{
6491 /* no resync is happening, and there is enough space
6492 * on all devices, so we can resize.
6493 * We need to make sure resync covers any new space.
6494 * If the array is shrinking we should possibly wait until
6495 * any io in the removed space completes, but it hardly seems
6496 * worth it.
6497 */
NeilBrowna4a61252012-05-22 13:55:27 +10006498 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10006499 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10006500 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6501 if (mddev->external_size &&
6502 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11006503 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10006504 if (mddev->bitmap) {
6505 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6506 if (ret)
6507 return ret;
6508 }
6509 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10006510 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006511 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10006512 if (sectors > mddev->dev_sectors &&
6513 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006514 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6516 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006517 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07006518 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 return 0;
6520}
6521
NeilBrownfd01b882011-10-11 16:47:53 +11006522static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10006523{
6524 /* Can only proceed if there are plenty of stripe_heads.
6525 * We need a minimum of one full stripe,, and for sensible progress
6526 * it is best to have about 4 times that.
6527 * If we require 4 times, then the default 256 4K stripe_heads will
6528 * allow for chunk sizes up to 256K, which is probably OK.
6529 * If the chunk size is greater, user-space should request more
6530 * stripe_heads first.
6531 */
NeilBrownd1688a62011-10-11 16:49:52 +11006532 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10006533 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6534 > conf->max_nr_stripes ||
6535 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6536 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10006537 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6538 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10006539 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6540 / STRIPE_SIZE)*4);
6541 return 0;
6542 }
6543 return 1;
6544}
6545
NeilBrownfd01b882011-10-11 16:47:53 +11006546static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08006547{
NeilBrownd1688a62011-10-11 16:49:52 +11006548 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08006549
NeilBrown88ce4932009-03-31 15:24:23 +11006550 if (mddev->delta_disks == 0 &&
6551 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10006552 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10006553 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10006554 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11006555 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10006556 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11006557 /* We might be able to shrink, but the devices must
6558 * be made bigger first.
6559 * For raid6, 4 is the minimum size.
6560 * Otherwise 2 is the minimum
6561 */
6562 int min = 2;
6563 if (mddev->level == 6)
6564 min = 4;
6565 if (mddev->raid_disks + mddev->delta_disks < min)
6566 return -EINVAL;
6567 }
NeilBrown29269552006-03-27 01:18:10 -08006568
NeilBrown01ee22b2009-06-18 08:47:20 +10006569 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08006570 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08006571
NeilBrowne56108d62012-10-11 14:24:13 +11006572 return resize_stripes(conf, (conf->previous_raid_disks
6573 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08006574}
6575
NeilBrownfd01b882011-10-11 16:47:53 +11006576static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006577{
NeilBrownd1688a62011-10-11 16:49:52 +11006578 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006579 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006580 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006581 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006582
NeilBrownf4168852007-02-28 20:11:53 -08006583 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006584 return -EBUSY;
6585
NeilBrown01ee22b2009-06-18 08:47:20 +10006586 if (!check_stripe_cache(mddev))
6587 return -ENOSPC;
6588
NeilBrown30b67642012-05-22 13:55:28 +10006589 if (has_failed(conf))
6590 return -EINVAL;
6591
NeilBrownc6563a82012-05-21 09:27:00 +10006592 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006593 if (!test_bit(In_sync, &rdev->flags)
6594 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006595 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006596 }
NeilBrown63c70c42006-03-27 01:18:13 -08006597
NeilBrownf4168852007-02-28 20:11:53 -08006598 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006599 /* Not enough devices even to make a degraded array
6600 * of that size
6601 */
6602 return -EINVAL;
6603
NeilBrownec32a2b2009-03-31 15:17:38 +11006604 /* Refuse to reduce size of the array. Any reductions in
6605 * array size must be through explicit setting of array_size
6606 * attribute.
6607 */
6608 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6609 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006610 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006611 "before number of disks\n", mdname(mddev));
6612 return -EINVAL;
6613 }
6614
NeilBrownf6705572006-03-27 01:18:11 -08006615 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006616 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006617 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006618 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006619 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006620 conf->prev_chunk_sectors = conf->chunk_sectors;
6621 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006622 conf->prev_algo = conf->algorithm;
6623 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006624 conf->generation++;
6625 /* Code that selects data_offset needs to see the generation update
6626 * if reshape_progress has been set - so a memory barrier needed.
6627 */
6628 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006629 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006630 conf->reshape_progress = raid5_size(mddev, 0, 0);
6631 else
6632 conf->reshape_progress = 0;
6633 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10006634 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006635 spin_unlock_irq(&conf->device_lock);
6636
NeilBrown4d77e3b2013-08-27 15:57:47 +10006637 /* Now make sure any requests that proceeded on the assumption
6638 * the reshape wasn't running - like Discard or Read - have
6639 * completed.
6640 */
6641 mddev_suspend(mddev);
6642 mddev_resume(mddev);
6643
NeilBrown29269552006-03-27 01:18:10 -08006644 /* Add some new drives, as many as will fit.
6645 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006646 * Don't add devices if we are reducing the number of
6647 * devices in the array. This is because it is not possible
6648 * to correctly record the "partially reconstructed" state of
6649 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006650 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006651 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006652 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006653 if (rdev->raid_disk < 0 &&
6654 !test_bit(Faulty, &rdev->flags)) {
6655 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006656 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006657 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006658 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006659 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006660 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006661
6662 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006663 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006664 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006665 } else if (rdev->raid_disk >= conf->previous_raid_disks
6666 && !test_bit(Faulty, &rdev->flags)) {
6667 /* This is a spare that was manually added */
6668 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006669 }
NeilBrown29269552006-03-27 01:18:10 -08006670
NeilBrown87a8dec2011-01-31 11:57:43 +11006671 /* When a reshape changes the number of devices,
6672 * ->degraded is measured against the larger of the
6673 * pre and post number of devices.
6674 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006675 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006676 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006677 spin_unlock_irqrestore(&conf->device_lock, flags);
6678 }
NeilBrown63c70c42006-03-27 01:18:13 -08006679 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006680 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006681 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006682
NeilBrown29269552006-03-27 01:18:10 -08006683 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6684 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6685 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6686 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6687 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006688 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006689 if (!mddev->sync_thread) {
6690 mddev->recovery = 0;
6691 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11006692 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006693 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11006694 mddev->new_chunk_sectors =
6695 conf->chunk_sectors = conf->prev_chunk_sectors;
6696 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10006697 rdev_for_each(rdev, mddev)
6698 rdev->new_data_offset = rdev->data_offset;
6699 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11006700 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11006701 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006702 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11006703 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006704 spin_unlock_irq(&conf->device_lock);
6705 return -EAGAIN;
6706 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006707 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006708 md_wakeup_thread(mddev->sync_thread);
6709 md_new_event(mddev);
6710 return 0;
6711}
NeilBrown29269552006-03-27 01:18:10 -08006712
NeilBrownec32a2b2009-03-31 15:17:38 +11006713/* This is called from the reshape thread and should make any
6714 * changes needed in 'conf'
6715 */
NeilBrownd1688a62011-10-11 16:49:52 +11006716static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006717{
NeilBrown29269552006-03-27 01:18:10 -08006718
NeilBrownf6705572006-03-27 01:18:11 -08006719 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006720 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006721
NeilBrownf6705572006-03-27 01:18:11 -08006722 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006723 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006724 rdev_for_each(rdev, conf->mddev)
6725 rdev->data_offset = rdev->new_data_offset;
6726 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006727 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006728 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006729 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006730
6731 /* read-ahead size must cover two whole stripes, which is
6732 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6733 */
NeilBrown4a5add42010-06-01 19:37:28 +10006734 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006735 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006736 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006737 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006738 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6739 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6740 }
NeilBrown29269552006-03-27 01:18:10 -08006741 }
NeilBrown29269552006-03-27 01:18:10 -08006742}
6743
NeilBrownec32a2b2009-03-31 15:17:38 +11006744/* This is called from the raid5d thread with mddev_lock held.
6745 * It makes config changes to the device.
6746 */
NeilBrownfd01b882011-10-11 16:47:53 +11006747static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006748{
NeilBrownd1688a62011-10-11 16:49:52 +11006749 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006750
6751 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6752
NeilBrownec32a2b2009-03-31 15:17:38 +11006753 if (mddev->delta_disks > 0) {
6754 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6755 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006756 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006757 } else {
6758 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006759 spin_lock_irq(&conf->device_lock);
6760 mddev->degraded = calc_degraded(conf);
6761 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006762 for (d = conf->raid_disks ;
6763 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006764 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006765 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006766 if (rdev)
6767 clear_bit(In_sync, &rdev->flags);
6768 rdev = conf->disks[d].replacement;
6769 if (rdev)
6770 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006771 }
NeilBrowncea9c222009-03-31 15:15:05 +11006772 }
NeilBrown88ce4932009-03-31 15:24:23 +11006773 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006774 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006775 mddev->reshape_position = MaxSector;
6776 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006777 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006778 }
6779}
6780
NeilBrownfd01b882011-10-11 16:47:53 +11006781static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006782{
NeilBrownd1688a62011-10-11 16:49:52 +11006783 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006784
6785 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006786 case 2: /* resume for a suspend */
6787 wake_up(&conf->wait_for_overlap);
6788 break;
6789
NeilBrown72626682005-09-09 16:23:54 -07006790 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006791 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006792 /* '2' tells resync/reshape to pause so that all
6793 * active stripes can drain
6794 */
6795 conf->quiesce = 2;
Shaohua Li566c09c2013-11-14 15:16:17 +11006796 wait_event_cmd(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006797 atomic_read(&conf->active_stripes) == 0 &&
6798 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11006799 unlock_all_device_hash_locks_irq(conf),
6800 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10006801 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11006802 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006803 /* allow reshape to continue */
6804 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006805 break;
6806
6807 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006808 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006809 conf->quiesce = 0;
6810 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006811 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11006812 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006813 break;
6814 }
NeilBrown72626682005-09-09 16:23:54 -07006815}
NeilBrownb15c2e52006-01-06 00:20:16 -08006816
NeilBrownd562b0c2009-03-31 14:39:39 +11006817
NeilBrownfd01b882011-10-11 16:47:53 +11006818static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006819{
NeilBrowne373ab12011-10-11 16:48:59 +11006820 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006821 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006822
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006823 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006824 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006825 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6826 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006827 return ERR_PTR(-EINVAL);
6828 }
6829
NeilBrowne373ab12011-10-11 16:48:59 +11006830 sectors = raid0_conf->strip_zone[0].zone_end;
6831 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006832 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006833 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006834 mddev->new_layout = ALGORITHM_PARITY_N;
6835 mddev->new_chunk_sectors = mddev->chunk_sectors;
6836 mddev->raid_disks += 1;
6837 mddev->delta_disks = 1;
6838 /* make sure it will be not marked as dirty */
6839 mddev->recovery_cp = MaxSector;
6840
6841 return setup_conf(mddev);
6842}
6843
6844
NeilBrownfd01b882011-10-11 16:47:53 +11006845static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006846{
6847 int chunksect;
6848
6849 if (mddev->raid_disks != 2 ||
6850 mddev->degraded > 1)
6851 return ERR_PTR(-EINVAL);
6852
6853 /* Should check if there are write-behind devices? */
6854
6855 chunksect = 64*2; /* 64K by default */
6856
6857 /* The array must be an exact multiple of chunksize */
6858 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6859 chunksect >>= 1;
6860
6861 if ((chunksect<<9) < STRIPE_SIZE)
6862 /* array size does not allow a suitable chunk size */
6863 return ERR_PTR(-EINVAL);
6864
6865 mddev->new_level = 5;
6866 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006867 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006868
6869 return setup_conf(mddev);
6870}
6871
NeilBrownfd01b882011-10-11 16:47:53 +11006872static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006873{
6874 int new_layout;
6875
6876 switch (mddev->layout) {
6877 case ALGORITHM_LEFT_ASYMMETRIC_6:
6878 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6879 break;
6880 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6881 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6882 break;
6883 case ALGORITHM_LEFT_SYMMETRIC_6:
6884 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6885 break;
6886 case ALGORITHM_RIGHT_SYMMETRIC_6:
6887 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6888 break;
6889 case ALGORITHM_PARITY_0_6:
6890 new_layout = ALGORITHM_PARITY_0;
6891 break;
6892 case ALGORITHM_PARITY_N:
6893 new_layout = ALGORITHM_PARITY_N;
6894 break;
6895 default:
6896 return ERR_PTR(-EINVAL);
6897 }
6898 mddev->new_level = 5;
6899 mddev->new_layout = new_layout;
6900 mddev->delta_disks = -1;
6901 mddev->raid_disks -= 1;
6902 return setup_conf(mddev);
6903}
6904
NeilBrownd562b0c2009-03-31 14:39:39 +11006905
NeilBrownfd01b882011-10-11 16:47:53 +11006906static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006907{
NeilBrown88ce4932009-03-31 15:24:23 +11006908 /* For a 2-drive array, the layout and chunk size can be changed
6909 * immediately as not restriping is needed.
6910 * For larger arrays we record the new value - after validation
6911 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006912 */
NeilBrownd1688a62011-10-11 16:49:52 +11006913 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006914 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006915
NeilBrown597a7112009-06-18 08:47:42 +10006916 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006917 return -EINVAL;
6918 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006919 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006920 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006921 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006922 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006923 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006924 /* not factor of array size */
6925 return -EINVAL;
6926 }
6927
6928 /* They look valid */
6929
NeilBrown88ce4932009-03-31 15:24:23 +11006930 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006931 /* can make the change immediately */
6932 if (mddev->new_layout >= 0) {
6933 conf->algorithm = mddev->new_layout;
6934 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006935 }
6936 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006937 conf->chunk_sectors = new_chunk ;
6938 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006939 }
6940 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6941 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006942 }
NeilBrown50ac1682009-06-18 08:47:55 +10006943 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006944}
6945
NeilBrownfd01b882011-10-11 16:47:53 +11006946static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006947{
NeilBrown597a7112009-06-18 08:47:42 +10006948 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006949
NeilBrown597a7112009-06-18 08:47:42 +10006950 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006951 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006952 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006953 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006954 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006955 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006956 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006957 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006958 /* not factor of array size */
6959 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006960 }
NeilBrown88ce4932009-03-31 15:24:23 +11006961
6962 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006963 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006964}
6965
NeilBrownfd01b882011-10-11 16:47:53 +11006966static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006967{
6968 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006969 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006970 * raid1 - if there are two drives. We need to know the chunk size
6971 * raid4 - trivial - just use a raid4 layout.
6972 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006973 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006974 if (mddev->level == 0)
6975 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006976 if (mddev->level == 1)
6977 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006978 if (mddev->level == 4) {
6979 mddev->new_layout = ALGORITHM_PARITY_N;
6980 mddev->new_level = 5;
6981 return setup_conf(mddev);
6982 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006983 if (mddev->level == 6)
6984 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006985
6986 return ERR_PTR(-EINVAL);
6987}
6988
NeilBrownfd01b882011-10-11 16:47:53 +11006989static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006990{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006991 /* raid4 can take over:
6992 * raid0 - if there is only one strip zone
6993 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006994 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006995 if (mddev->level == 0)
6996 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006997 if (mddev->level == 5 &&
6998 mddev->layout == ALGORITHM_PARITY_N) {
6999 mddev->new_layout = 0;
7000 mddev->new_level = 4;
7001 return setup_conf(mddev);
7002 }
7003 return ERR_PTR(-EINVAL);
7004}
NeilBrownd562b0c2009-03-31 14:39:39 +11007005
NeilBrown84fc4b52011-10-11 16:49:58 +11007006static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11007007
NeilBrownfd01b882011-10-11 16:47:53 +11007008static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11007009{
7010 /* Currently can only take over a raid5. We map the
7011 * personality to an equivalent raid6 personality
7012 * with the Q block at the end.
7013 */
7014 int new_layout;
7015
7016 if (mddev->pers != &raid5_personality)
7017 return ERR_PTR(-EINVAL);
7018 if (mddev->degraded > 1)
7019 return ERR_PTR(-EINVAL);
7020 if (mddev->raid_disks > 253)
7021 return ERR_PTR(-EINVAL);
7022 if (mddev->raid_disks < 3)
7023 return ERR_PTR(-EINVAL);
7024
7025 switch (mddev->layout) {
7026 case ALGORITHM_LEFT_ASYMMETRIC:
7027 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
7028 break;
7029 case ALGORITHM_RIGHT_ASYMMETRIC:
7030 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
7031 break;
7032 case ALGORITHM_LEFT_SYMMETRIC:
7033 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
7034 break;
7035 case ALGORITHM_RIGHT_SYMMETRIC:
7036 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
7037 break;
7038 case ALGORITHM_PARITY_0:
7039 new_layout = ALGORITHM_PARITY_0_6;
7040 break;
7041 case ALGORITHM_PARITY_N:
7042 new_layout = ALGORITHM_PARITY_N;
7043 break;
7044 default:
7045 return ERR_PTR(-EINVAL);
7046 }
7047 mddev->new_level = 6;
7048 mddev->new_layout = new_layout;
7049 mddev->delta_disks = 1;
7050 mddev->raid_disks += 1;
7051 return setup_conf(mddev);
7052}
7053
7054
NeilBrown84fc4b52011-10-11 16:49:58 +11007055static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07007056{
7057 .name = "raid6",
7058 .level = 6,
7059 .owner = THIS_MODULE,
7060 .make_request = make_request,
7061 .run = run,
7062 .stop = stop,
7063 .status = status,
7064 .error_handler = error,
7065 .hot_add_disk = raid5_add_disk,
7066 .hot_remove_disk= raid5_remove_disk,
7067 .spare_active = raid5_spare_active,
7068 .sync_request = sync_request,
7069 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007070 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10007071 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08007072 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007073 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07007074 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11007075 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07007076};
NeilBrown84fc4b52011-10-11 16:49:58 +11007077static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078{
7079 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08007080 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 .owner = THIS_MODULE,
7082 .make_request = make_request,
7083 .run = run,
7084 .stop = stop,
7085 .status = status,
7086 .error_handler = error,
7087 .hot_add_disk = raid5_add_disk,
7088 .hot_remove_disk= raid5_remove_disk,
7089 .spare_active = raid5_spare_active,
7090 .sync_request = sync_request,
7091 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007092 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08007093 .check_reshape = raid5_check_reshape,
7094 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007095 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07007096 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11007097 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098};
7099
NeilBrown84fc4b52011-10-11 16:49:58 +11007100static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101{
NeilBrown2604b702006-01-06 00:20:36 -08007102 .name = "raid4",
7103 .level = 4,
7104 .owner = THIS_MODULE,
7105 .make_request = make_request,
7106 .run = run,
7107 .stop = stop,
7108 .status = status,
7109 .error_handler = error,
7110 .hot_add_disk = raid5_add_disk,
7111 .hot_remove_disk= raid5_remove_disk,
7112 .spare_active = raid5_spare_active,
7113 .sync_request = sync_request,
7114 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007115 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08007116 .check_reshape = raid5_check_reshape,
7117 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007118 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08007119 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11007120 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08007121};
7122
7123static int __init raid5_init(void)
7124{
Shaohua Li851c30c2013-08-28 14:30:16 +08007125 raid5_wq = alloc_workqueue("raid5wq",
7126 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7127 if (!raid5_wq)
7128 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07007129 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007130 register_md_personality(&raid5_personality);
7131 register_md_personality(&raid4_personality);
7132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133}
7134
NeilBrown2604b702006-01-06 00:20:36 -08007135static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136{
NeilBrown16a53ec2006-06-26 00:27:38 -07007137 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007138 unregister_md_personality(&raid5_personality);
7139 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08007140 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141}
7142
7143module_init(raid5_init);
7144module_exit(raid5_exit);
7145MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007146MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08007148MODULE_ALIAS("md-raid5");
7149MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08007150MODULE_ALIAS("md-level-5");
7151MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07007152MODULE_ALIAS("md-personality-8"); /* RAID6 */
7153MODULE_ALIAS("md-raid6");
7154MODULE_ALIAS("md-level-6");
7155
7156/* This used to be two separate modules, they were: */
7157MODULE_ALIAS("raid5");
7158MODULE_ALIAS("raid6");