blob: 7fb510e545488841262774a5c5f2eb78ae33afe1 [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>
shli@kernel.org46d5b782014-12-15 12:57:02 +110057#include <linux/flex_array.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110058#include <trace/events/block.h>
59
NeilBrown43b2e5d2009-03-31 14:33:13 +110060#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110061#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110062#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110063#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070064
Shaohua Li851c30c2013-08-28 14:30:16 +080065#define cpu_to_group(cpu) cpu_to_node(cpu)
66#define ANY_GROUP NUMA_NO_NODE
67
NeilBrown8e0e99b2014-10-02 13:45:00 +100068static bool devices_handle_discard_safely = false;
69module_param(devices_handle_discard_safely, bool, 0644);
70MODULE_PARM_DESC(devices_handle_discard_safely,
71 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080072static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
74 * Stripe cache
75 */
76
77#define NR_STRIPES 256
78#define STRIPE_SIZE PAGE_SIZE
79#define STRIPE_SHIFT (PAGE_SHIFT - 9)
80#define STRIPE_SECTORS (STRIPE_SIZE>>9)
81#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070082#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080083#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define HASH_MASK (NR_HASH - 1)
Shaohua Libfc90cb2013-08-29 15:40:32 +080085#define MAX_STRIPE_BATCH 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
NeilBrownd1688a62011-10-11 16:49:52 +110087static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110088{
89 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
90 return &conf->stripe_hashtbl[hash];
91}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Shaohua Li566c09c2013-11-14 15:16:17 +110093static inline int stripe_hash_locks_hash(sector_t sect)
94{
95 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
96}
97
98static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
99{
100 spin_lock_irq(conf->hash_locks + hash);
101 spin_lock(&conf->device_lock);
102}
103
104static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
105{
106 spin_unlock(&conf->device_lock);
107 spin_unlock_irq(conf->hash_locks + hash);
108}
109
110static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
111{
112 int i;
113 local_irq_disable();
114 spin_lock(conf->hash_locks);
115 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
116 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
117 spin_lock(&conf->device_lock);
118}
119
120static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
121{
122 int i;
123 spin_unlock(&conf->device_lock);
124 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
125 spin_unlock(conf->hash_locks + i - 1);
126 local_irq_enable();
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* bio's attached to a stripe+device for I/O are linked together in bi_sector
130 * order without overlap. There may be several bio's per stripe+device, and
131 * a bio could span several devices.
132 * When walking this list for a particular stripe+device, we must never proceed
133 * beyond a bio that extends past this device, as the next bio might no longer
134 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +1100135 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * of the current stripe+device
137 */
NeilBrowndb298e12011-10-07 14:23:00 +1100138static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
139{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800140 int sectors = bio_sectors(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700141 if (bio->bi_iter.bi_sector + sectors < sector + STRIPE_SECTORS)
NeilBrowndb298e12011-10-07 14:23:00 +1100142 return bio->bi_next;
143 else
144 return NULL;
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jens Axboe960e7392008-08-15 10:41:18 +0200147/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200148 * We maintain a biased count of active stripes in the bottom 16 bits of
149 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200150 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000151static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200152{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000153 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
154 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200155}
156
Shaohua Lie7836bd62012-07-19 16:01:31 +1000157static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200158{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000159 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
160 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200161}
162
Shaohua Lie7836bd62012-07-19 16:01:31 +1000163static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200164{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000165 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
166 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200167}
168
Shaohua Lie7836bd62012-07-19 16:01:31 +1000169static inline void raid5_set_bi_processed_stripes(struct bio *bio,
170 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200171{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000172 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
173 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200174
Shaohua Lie7836bd62012-07-19 16:01:31 +1000175 do {
176 old = atomic_read(segments);
177 new = (old & 0xffff) | (cnt << 16);
178 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200179}
180
Shaohua Lie7836bd62012-07-19 16:01:31 +1000181static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200182{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000183 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
184 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200185}
186
NeilBrownd0dabf72009-03-31 14:39:38 +1100187/* Find first data disk in a raid6 stripe */
188static inline int raid6_d0(struct stripe_head *sh)
189{
NeilBrown67cc2b82009-03-31 14:39:38 +1100190 if (sh->ddf_layout)
191 /* ddf always start from first device */
192 return 0;
193 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100194 if (sh->qd_idx == sh->disks - 1)
195 return 0;
196 else
197 return sh->qd_idx + 1;
198}
NeilBrown16a53ec2006-06-26 00:27:38 -0700199static inline int raid6_next_disk(int disk, int raid_disks)
200{
201 disk++;
202 return (disk < raid_disks) ? disk : 0;
203}
Dan Williamsa4456852007-07-09 11:56:43 -0700204
NeilBrownd0dabf72009-03-31 14:39:38 +1100205/* When walking through the disks in a raid5, starting at raid6_d0,
206 * We need to map each disk to a 'slot', where the data disks are slot
207 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
208 * is raid_disks-1. This help does that mapping.
209 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100210static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
211 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100212{
Dan Williams66295422009-10-19 18:09:32 -0700213 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100214
NeilBrowne4424fe2009-10-16 16:27:34 +1100215 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700216 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100217 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100218 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100219 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100220 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100221 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700222 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100223 return slot;
224}
225
Dan Williamsa4456852007-07-09 11:56:43 -0700226static void return_io(struct bio *return_bi)
227{
228 struct bio *bi = return_bi;
229 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700230
231 return_bi = bi->bi_next;
232 bi->bi_next = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700233 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700234 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
235 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000236 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700237 bi = return_bi;
238 }
239}
240
NeilBrownd1688a62011-10-11 16:49:52 +1100241static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Dan Williams600aa102008-06-28 08:32:05 +1000243static int stripe_operations_active(struct stripe_head *sh)
244{
245 return sh->check_state || sh->reconstruct_state ||
246 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
247 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
248}
249
Shaohua Li851c30c2013-08-28 14:30:16 +0800250static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
251{
252 struct r5conf *conf = sh->raid_conf;
253 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800254 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800255 int i, cpu = sh->cpu;
256
257 if (!cpu_online(cpu)) {
258 cpu = cpumask_any(cpu_online_mask);
259 sh->cpu = cpu;
260 }
261
262 if (list_empty(&sh->lru)) {
263 struct r5worker_group *group;
264 group = conf->worker_groups + cpu_to_group(cpu);
265 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800266 group->stripes_cnt++;
267 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800268 }
269
270 if (conf->worker_cnt_per_group == 0) {
271 md_wakeup_thread(conf->mddev->thread);
272 return;
273 }
274
275 group = conf->worker_groups + cpu_to_group(sh->cpu);
276
Shaohua Libfc90cb2013-08-29 15:40:32 +0800277 group->workers[0].working = true;
278 /* at least one worker should run to avoid race */
279 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
280
281 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
282 /* wakeup more workers */
283 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
284 if (group->workers[i].working == false) {
285 group->workers[i].working = true;
286 queue_work_on(sh->cpu, raid5_wq,
287 &group->workers[i].work);
288 thread_cnt--;
289 }
290 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800291}
292
Shaohua Li566c09c2013-11-14 15:16:17 +1100293static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
294 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000296 BUG_ON(!list_empty(&sh->lru));
297 BUG_ON(atomic_read(&conf->active_stripes)==0);
298 if (test_bit(STRIPE_HANDLE, &sh->state)) {
299 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500300 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000301 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500302 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000303 sh->bm_seq - conf->seq_write > 0)
304 list_add_tail(&sh->lru, &conf->bitmap_list);
305 else {
306 clear_bit(STRIPE_DELAYED, &sh->state);
307 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800308 if (conf->worker_cnt_per_group == 0) {
309 list_add_tail(&sh->lru, &conf->handle_list);
310 } else {
311 raid5_wakeup_stripe_thread(sh);
312 return;
313 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000314 }
315 md_wakeup_thread(conf->mddev->thread);
316 } else {
317 BUG_ON(stripe_operations_active(sh));
318 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
319 if (atomic_dec_return(&conf->preread_active_stripes)
320 < IO_THRESHOLD)
321 md_wakeup_thread(conf->mddev->thread);
322 atomic_dec(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100323 if (!test_bit(STRIPE_EXPANDING, &sh->state))
324 list_add_tail(&sh->lru, temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326}
NeilBrownd0dabf72009-03-31 14:39:38 +1100327
Shaohua Li566c09c2013-11-14 15:16:17 +1100328static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
329 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000330{
331 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100332 do_release_stripe(conf, sh, temp_inactive_list);
333}
334
335/*
336 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
337 *
338 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
339 * given time. Adding stripes only takes device lock, while deleting stripes
340 * only takes hash lock.
341 */
342static void release_inactive_stripe_list(struct r5conf *conf,
343 struct list_head *temp_inactive_list,
344 int hash)
345{
346 int size;
347 bool do_wakeup = false;
348 unsigned long flags;
349
350 if (hash == NR_STRIPE_HASH_LOCKS) {
351 size = NR_STRIPE_HASH_LOCKS;
352 hash = NR_STRIPE_HASH_LOCKS - 1;
353 } else
354 size = 1;
355 while (size) {
356 struct list_head *list = &temp_inactive_list[size - 1];
357
358 /*
359 * We don't hold any lock here yet, get_active_stripe() might
360 * remove stripes from the list
361 */
362 if (!list_empty_careful(list)) {
363 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100364 if (list_empty(conf->inactive_list + hash) &&
365 !list_empty(list))
366 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100367 list_splice_tail_init(list, conf->inactive_list + hash);
368 do_wakeup = true;
369 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
370 }
371 size--;
372 hash--;
373 }
374
375 if (do_wakeup) {
376 wake_up(&conf->wait_for_stripe);
377 if (conf->retry_read_aligned)
378 md_wakeup_thread(conf->mddev->thread);
379 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000380}
381
Shaohua Li773ca822013-08-27 17:50:39 +0800382/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100383static int release_stripe_list(struct r5conf *conf,
384 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800385{
386 struct stripe_head *sh;
387 int count = 0;
388 struct llist_node *head;
389
390 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800391 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800392 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100393 int hash;
394
Shaohua Li773ca822013-08-27 17:50:39 +0800395 sh = llist_entry(head, struct stripe_head, release_list);
396 head = llist_next(head);
397 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
398 smp_mb();
399 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
400 /*
401 * Don't worry the bit is set here, because if the bit is set
402 * again, the count is always > 1. This is true for
403 * STRIPE_ON_UNPLUG_LIST bit too.
404 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100405 hash = sh->hash_lock_index;
406 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800407 count++;
408 }
409
410 return count;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static void release_stripe(struct stripe_head *sh)
414{
NeilBrownd1688a62011-10-11 16:49:52 +1100415 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100417 struct list_head list;
418 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800419 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700420
Eivind Sartocf170f32014-05-28 13:39:23 +1000421 /* Avoid release_list until the last reference.
422 */
423 if (atomic_add_unless(&sh->count, -1, 1))
424 return;
425
majianpengad4068d2013-11-14 15:16:15 +1100426 if (unlikely(!conf->mddev->thread) ||
427 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800428 goto slow_path;
429 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
430 if (wakeup)
431 md_wakeup_thread(conf->mddev->thread);
432 return;
433slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000434 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800435 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000436 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100437 INIT_LIST_HEAD(&list);
438 hash = sh->hash_lock_index;
439 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000440 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100441 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000442 }
443 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
NeilBrownfccddba2006-01-06 00:20:33 -0800446static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Dan Williams45b42332007-07-09 11:56:43 -0700448 pr_debug("remove_hash(), stripe %llu\n",
449 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
NeilBrownfccddba2006-01-06 00:20:33 -0800451 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
NeilBrownd1688a62011-10-11 16:49:52 +1100454static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
NeilBrownfccddba2006-01-06 00:20:33 -0800456 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Dan Williams45b42332007-07-09 11:56:43 -0700458 pr_debug("insert_hash(), stripe %llu\n",
459 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
NeilBrownfccddba2006-01-06 00:20:33 -0800461 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100465static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct stripe_head *sh = NULL;
468 struct list_head *first;
469
Shaohua Li566c09c2013-11-14 15:16:17 +1100470 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100472 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 sh = list_entry(first, struct stripe_head, lru);
474 list_del_init(first);
475 remove_hash(sh);
476 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100477 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100478 if (list_empty(conf->inactive_list + hash))
479 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480out:
481 return sh;
482}
483
NeilBrowne4e11e32010-06-16 16:45:16 +1000484static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct page *p;
487 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000488 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
NeilBrowne4e11e32010-06-16 16:45:16 +1000490 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800491 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 p = sh->dev[i].page;
493 if (!p)
494 continue;
495 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800496 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498}
499
NeilBrowne4e11e32010-06-16 16:45:16 +1000500static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000503 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
NeilBrowne4e11e32010-06-16 16:45:16 +1000505 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct page *page;
507
508 if (!(page = alloc_page(GFP_KERNEL))) {
509 return 1;
510 }
511 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800512 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 return 0;
515}
516
NeilBrown784052e2009-03-31 15:19:07 +1100517static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100518static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100519 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
NeilBrownb5663ba2009-03-31 14:39:38 +1100521static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
NeilBrownd1688a62011-10-11 16:49:52 +1100523 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100524 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200526 BUG_ON(atomic_read(&sh->count) != 0);
527 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000528 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700529
Dan Williams45b42332007-07-09 11:56:43 -0700530 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000531 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100532retry:
533 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100534 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100535 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100537 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 sh->state = 0;
539
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800540 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct r5dev *dev = &sh->dev[i];
542
Dan Williamsd84e0f12007-01-02 13:52:30 -0700543 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700545 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700547 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000549 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100552 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100554 if (read_seqcount_retry(&conf->gen_lock, seq))
555 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800557 sh->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
NeilBrownd1688a62011-10-11 16:49:52 +1100560static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100561 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct stripe_head *sh;
564
Dan Williams45b42332007-07-09 11:56:43 -0700565 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800566 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100567 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700569 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return NULL;
571}
572
NeilBrown674806d2010-06-16 17:17:53 +1000573/*
574 * Need to check if array has failed when deciding whether to:
575 * - start an array
576 * - remove non-faulty devices
577 * - add a spare
578 * - allow a reshape
579 * This determination is simple when no reshape is happening.
580 * However if there is a reshape, we need to carefully check
581 * both the before and after sections.
582 * This is because some failed devices may only affect one
583 * of the two sections, and some non-in_sync devices may
584 * be insync in the section most affected by failed devices.
585 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100586static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000587{
NeilBrown908f4fb2011-12-23 10:17:50 +1100588 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000589 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000590
591 rcu_read_lock();
592 degraded = 0;
593 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100594 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000595 if (rdev && test_bit(Faulty, &rdev->flags))
596 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000597 if (!rdev || test_bit(Faulty, &rdev->flags))
598 degraded++;
599 else if (test_bit(In_sync, &rdev->flags))
600 ;
601 else
602 /* not in-sync or faulty.
603 * If the reshape increases the number of devices,
604 * this is being recovered by the reshape, so
605 * this 'previous' section is not in_sync.
606 * If the number of devices is being reduced however,
607 * the device can only be part of the array if
608 * we are reverting a reshape, so this section will
609 * be in-sync.
610 */
611 if (conf->raid_disks >= conf->previous_raid_disks)
612 degraded++;
613 }
614 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100615 if (conf->raid_disks == conf->previous_raid_disks)
616 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000617 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100618 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000619 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100620 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000621 if (rdev && test_bit(Faulty, &rdev->flags))
622 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000623 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100624 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000625 else if (test_bit(In_sync, &rdev->flags))
626 ;
627 else
628 /* not in-sync or faulty.
629 * If reshape increases the number of devices, this
630 * section has already been recovered, else it
631 * almost certainly hasn't.
632 */
633 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100634 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000635 }
636 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100637 if (degraded2 > degraded)
638 return degraded2;
639 return degraded;
640}
641
642static int has_failed(struct r5conf *conf)
643{
644 int degraded;
645
646 if (conf->mddev->reshape_position == MaxSector)
647 return conf->mddev->degraded > conf->max_degraded;
648
649 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000650 if (degraded > conf->max_degraded)
651 return 1;
652 return 0;
653}
654
NeilBrownb5663ba2009-03-31 14:39:38 +1100655static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100656get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000657 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100660 int hash = stripe_hash_locks_hash(sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Dan Williams45b42332007-07-09 11:56:43 -0700662 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Shaohua Li566c09c2013-11-14 15:16:17 +1100664 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 do {
NeilBrown72626682005-09-09 16:23:54 -0700667 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000668 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100669 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100670 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!sh) {
672 if (!conf->inactive_blocked)
Shaohua Li566c09c2013-11-14 15:16:17 +1100673 sh = get_free_stripe(conf, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (noblock && sh == NULL)
675 break;
676 if (!sh) {
677 conf->inactive_blocked = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +1100678 wait_event_lock_irq(
679 conf->wait_for_stripe,
680 !list_empty(conf->inactive_list + hash) &&
681 (atomic_read(&conf->active_stripes)
682 < (conf->max_nr_stripes * 3 / 4)
683 || !conf->inactive_blocked),
684 *(conf->hash_locks + hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 conf->inactive_blocked = 0;
NeilBrown7da9d452014-01-22 11:45:03 +1100686 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100687 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100688 atomic_inc(&sh->count);
689 }
Shaohua Lie240c182014-04-09 11:27:42 +0800690 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100691 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800692 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!test_bit(STRIPE_HANDLE, &sh->state))
694 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100695 BUG_ON(list_empty(&sh->lru) &&
696 !test_bit(STRIPE_EXPANDING, &sh->state));
NeilBrown16a53ec2006-06-26 00:27:38 -0700697 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800698 if (sh->group) {
699 sh->group->stripes_cnt--;
700 sh->group = NULL;
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
NeilBrown7da9d452014-01-22 11:45:03 +1100703 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100704 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 } while (sh == NULL);
707
Shaohua Li566c09c2013-11-14 15:16:17 +1100708 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return sh;
710}
711
NeilBrown05616be2012-05-21 09:27:00 +1000712/* Determine if 'data_offset' or 'new_data_offset' should be used
713 * in this stripe_head.
714 */
715static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
716{
717 sector_t progress = conf->reshape_progress;
718 /* Need a memory barrier to make sure we see the value
719 * of conf->generation, or ->data_offset that was set before
720 * reshape_progress was updated.
721 */
722 smp_rmb();
723 if (progress == MaxSector)
724 return 0;
725 if (sh->generation == conf->generation - 1)
726 return 0;
727 /* We are in a reshape, and this is a new-generation stripe,
728 * so use new_data_offset.
729 */
730 return 1;
731}
732
NeilBrown6712ecf2007-09-27 12:47:43 +0200733static void
734raid5_end_read_request(struct bio *bi, int error);
735static void
736raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700737
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000738static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700739{
NeilBrownd1688a62011-10-11 16:49:52 +1100740 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700741 int i, disks = sh->disks;
742
743 might_sleep();
744
745 for (i = disks; i--; ) {
746 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100747 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100748 struct bio *bi, *rbi;
749 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200750 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
751 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
752 rw = WRITE_FUA;
753 else
754 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100755 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100756 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200757 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700758 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100759 else if (test_and_clear_bit(R5_WantReplace,
760 &sh->dev[i].flags)) {
761 rw = WRITE;
762 replace_only = 1;
763 } else
Dan Williams91c00922007-01-02 13:52:30 -0700764 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000765 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
766 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700767
768 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100769 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700770
Dan Williams91c00922007-01-02 13:52:30 -0700771 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100772 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100773 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
774 rdev = rcu_dereference(conf->disks[i].rdev);
775 if (!rdev) {
776 rdev = rrdev;
777 rrdev = NULL;
778 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100779 if (rw & WRITE) {
780 if (replace_only)
781 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100782 if (rdev == rrdev)
783 /* We raced and saw duplicates */
784 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100785 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100786 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100787 rdev = rrdev;
788 rrdev = NULL;
789 }
NeilBrown977df362011-12-23 10:17:53 +1100790
Dan Williams91c00922007-01-02 13:52:30 -0700791 if (rdev && test_bit(Faulty, &rdev->flags))
792 rdev = NULL;
793 if (rdev)
794 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100795 if (rrdev && test_bit(Faulty, &rrdev->flags))
796 rrdev = NULL;
797 if (rrdev)
798 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700799 rcu_read_unlock();
800
NeilBrown73e92e52011-07-28 11:39:22 +1000801 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100802 * need to check for writes. We never accept write errors
803 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000804 */
805 while ((rw & WRITE) && rdev &&
806 test_bit(WriteErrorSeen, &rdev->flags)) {
807 sector_t first_bad;
808 int bad_sectors;
809 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
810 &first_bad, &bad_sectors);
811 if (!bad)
812 break;
813
814 if (bad < 0) {
815 set_bit(BlockedBadBlocks, &rdev->flags);
816 if (!conf->mddev->external &&
817 conf->mddev->flags) {
818 /* It is very unlikely, but we might
819 * still need to write out the
820 * bad block log - better give it
821 * a chance*/
822 md_check_recovery(conf->mddev);
823 }
majianpeng18507532012-07-03 12:11:54 +1000824 /*
825 * Because md_wait_for_blocked_rdev
826 * will dec nr_pending, we must
827 * increment it first.
828 */
829 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000830 md_wait_for_blocked_rdev(rdev, conf->mddev);
831 } else {
832 /* Acknowledged bad block - skip the write */
833 rdev_dec_pending(rdev, conf->mddev);
834 rdev = NULL;
835 }
836 }
837
Dan Williams91c00922007-01-02 13:52:30 -0700838 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100839 if (s->syncing || s->expanding || s->expanded
840 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700841 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
842
Dan Williams2b7497f2008-06-28 08:31:52 +1000843 set_bit(STRIPE_IO_STARTED, &sh->state);
844
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700845 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700846 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700847 bi->bi_rw = rw;
848 bi->bi_end_io = (rw & WRITE)
849 ? raid5_end_write_request
850 : raid5_end_read_request;
851 bi->bi_private = sh;
852
Dan Williams91c00922007-01-02 13:52:30 -0700853 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700854 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700855 bi->bi_rw, i);
856 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000857 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700858 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000859 + rdev->new_data_offset);
860 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700861 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000862 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000863 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
majianpenge59aa232013-11-14 15:16:19 +1100864 bi->bi_rw |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +1000865
Shaohua Lid592a992014-05-21 17:57:44 +0800866 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
867 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
868 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +0200869 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700870 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
871 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700872 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800873 /*
874 * If this is discard request, set bi_vcnt 0. We don't
875 * want to confuse SCSI because SCSI will replace payload
876 */
877 if (rw & REQ_DISCARD)
878 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +1100879 if (rrdev)
880 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600881
882 if (conf->mddev->gendisk)
883 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
884 bi, disk_devt(conf->mddev->gendisk),
885 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700886 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100887 }
888 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100889 if (s->syncing || s->expanding || s->expanded
890 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100891 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
892
893 set_bit(STRIPE_IO_STARTED, &sh->state);
894
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700895 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100896 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700897 rbi->bi_rw = rw;
898 BUG_ON(!(rw & WRITE));
899 rbi->bi_end_io = raid5_end_write_request;
900 rbi->bi_private = sh;
901
NeilBrown977df362011-12-23 10:17:53 +1100902 pr_debug("%s: for %llu schedule op %ld on "
903 "replacement disc %d\n",
904 __func__, (unsigned long long)sh->sector,
905 rbi->bi_rw, i);
906 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000907 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700908 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000909 + rrdev->new_data_offset);
910 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700911 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000912 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +0800913 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
914 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
915 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +0200916 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100917 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
918 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700919 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800920 /*
921 * If this is discard request, set bi_vcnt 0. We don't
922 * want to confuse SCSI because SCSI will replace payload
923 */
924 if (rw & REQ_DISCARD)
925 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600926 if (conf->mddev->gendisk)
927 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
928 rbi, disk_devt(conf->mddev->gendisk),
929 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100930 generic_make_request(rbi);
931 }
932 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000933 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700934 set_bit(STRIPE_DEGRADED, &sh->state);
935 pr_debug("skip op %ld on disc %d for sector %llu\n",
936 bi->bi_rw, i, (unsigned long long)sh->sector);
937 clear_bit(R5_LOCKED, &sh->dev[i].flags);
938 set_bit(STRIPE_HANDLE, &sh->state);
939 }
940 }
941}
942
943static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +0800944async_copy_data(int frombio, struct bio *bio, struct page **page,
945 sector_t sector, struct dma_async_tx_descriptor *tx,
946 struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -0700947{
Kent Overstreet79886132013-11-23 17:19:00 -0800948 struct bio_vec bvl;
949 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -0700950 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -0700951 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700952 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700953 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700954
Kent Overstreet4f024f32013-10-11 15:44:27 -0700955 if (bio->bi_iter.bi_sector >= sector)
956 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -0700957 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700958 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700959
Dan Williams0403e382009-09-08 17:42:50 -0700960 if (frombio)
961 flags |= ASYNC_TX_FENCE;
962 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
963
Kent Overstreet79886132013-11-23 17:19:00 -0800964 bio_for_each_segment(bvl, bio, iter) {
965 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700966 int clen;
967 int b_offset = 0;
968
969 if (page_offset < 0) {
970 b_offset = -page_offset;
971 page_offset += b_offset;
972 len -= b_offset;
973 }
974
975 if (len > 0 && page_offset + len > STRIPE_SIZE)
976 clen = STRIPE_SIZE - page_offset;
977 else
978 clen = len;
979
980 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -0800981 b_offset += bvl.bv_offset;
982 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +0800983 if (frombio) {
984 if (sh->raid_conf->skip_copy &&
985 b_offset == 0 && page_offset == 0 &&
986 clen == STRIPE_SIZE)
987 *page = bio_page;
988 else
989 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700990 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +0800991 } else
992 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700993 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700994 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700995 /* chain the operations */
996 submit.depend_tx = tx;
997
Dan Williams91c00922007-01-02 13:52:30 -0700998 if (clen < len) /* hit end of page */
999 break;
1000 page_offset += len;
1001 }
1002
1003 return tx;
1004}
1005
1006static void ops_complete_biofill(void *stripe_head_ref)
1007{
1008 struct stripe_head *sh = stripe_head_ref;
1009 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -07001010 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001011
Harvey Harrisone46b2722008-04-28 02:15:50 -07001012 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001013 (unsigned long long)sh->sector);
1014
1015 /* clear completed biofills */
1016 for (i = sh->disks; i--; ) {
1017 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001018
1019 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001020 /* and check if we need to reply to a read request,
1021 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001022 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001023 */
1024 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001025 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001026
Dan Williams91c00922007-01-02 13:52:30 -07001027 BUG_ON(!dev->read);
1028 rbi = dev->read;
1029 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001030 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001031 dev->sector + STRIPE_SECTORS) {
1032 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10001033 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -07001034 rbi->bi_next = return_bi;
1035 return_bi = rbi;
1036 }
Dan Williams91c00922007-01-02 13:52:30 -07001037 rbi = rbi2;
1038 }
1039 }
1040 }
Dan Williams83de75c2008-06-28 08:31:58 +10001041 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001042
1043 return_io(return_bi);
1044
Dan Williamse4d84902007-09-24 10:06:13 -07001045 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001046 release_stripe(sh);
1047}
1048
1049static void ops_run_biofill(struct stripe_head *sh)
1050{
1051 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001052 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001053 int i;
1054
Harvey Harrisone46b2722008-04-28 02:15:50 -07001055 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001056 (unsigned long long)sh->sector);
1057
1058 for (i = sh->disks; i--; ) {
1059 struct r5dev *dev = &sh->dev[i];
1060 if (test_bit(R5_Wantfill, &dev->flags)) {
1061 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001062 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001063 dev->read = rbi = dev->toread;
1064 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001065 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001066 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001067 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001068 tx = async_copy_data(0, rbi, &dev->page,
1069 dev->sector, tx, sh);
Dan Williams91c00922007-01-02 13:52:30 -07001070 rbi = r5_next_bio(rbi, dev->sector);
1071 }
1072 }
1073 }
1074
1075 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001076 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1077 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001078}
1079
Dan Williams4e7d2c02009-08-29 19:13:11 -07001080static void mark_target_uptodate(struct stripe_head *sh, int target)
1081{
1082 struct r5dev *tgt;
1083
1084 if (target < 0)
1085 return;
1086
1087 tgt = &sh->dev[target];
1088 set_bit(R5_UPTODATE, &tgt->flags);
1089 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1090 clear_bit(R5_Wantcompute, &tgt->flags);
1091}
1092
Dan Williamsac6b53b2009-07-14 13:40:19 -07001093static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001094{
1095 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001096
Harvey Harrisone46b2722008-04-28 02:15:50 -07001097 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001098 (unsigned long long)sh->sector);
1099
Dan Williamsac6b53b2009-07-14 13:40:19 -07001100 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001101 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001102 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001103
Dan Williamsecc65c92008-06-28 08:31:57 +10001104 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1105 if (sh->check_state == check_state_compute_run)
1106 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001107 set_bit(STRIPE_HANDLE, &sh->state);
1108 release_stripe(sh);
1109}
1110
Dan Williamsd6f38f32009-07-14 11:50:52 -07001111/* return a pointer to the address conversion region of the scribble buffer */
1112static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001113 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001114{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001115 void *addr;
1116
1117 addr = flex_array_get(percpu->scribble, i);
1118 return addr + sizeof(struct page *) * (sh->disks + 2);
1119}
1120
1121/* return a pointer to the address conversion region of the scribble buffer */
1122static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1123{
1124 void *addr;
1125
1126 addr = flex_array_get(percpu->scribble, i);
1127 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001128}
1129
1130static struct dma_async_tx_descriptor *
1131ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1132{
Dan Williams91c00922007-01-02 13:52:30 -07001133 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001134 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001135 int target = sh->ops.target;
1136 struct r5dev *tgt = &sh->dev[target];
1137 struct page *xor_dest = tgt->page;
1138 int count = 0;
1139 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001140 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001141 int i;
1142
1143 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001144 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001145 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1146
1147 for (i = disks; i--; )
1148 if (i != target)
1149 xor_srcs[count++] = sh->dev[i].page;
1150
1151 atomic_inc(&sh->count);
1152
Dan Williams0403e382009-09-08 17:42:50 -07001153 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001154 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001155 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001156 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001157 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001158 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001159
Dan Williams91c00922007-01-02 13:52:30 -07001160 return tx;
1161}
1162
Dan Williamsac6b53b2009-07-14 13:40:19 -07001163/* set_syndrome_sources - populate source buffers for gen_syndrome
1164 * @srcs - (struct page *) array of size sh->disks
1165 * @sh - stripe_head to parse
1166 *
1167 * Populates srcs in proper layout order for the stripe and returns the
1168 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1169 * destination buffer is recorded in srcs[count] and the Q destination
1170 * is recorded in srcs[count+1]].
1171 */
1172static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1173{
1174 int disks = sh->disks;
1175 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1176 int d0_idx = raid6_d0(sh);
1177 int count;
1178 int i;
1179
1180 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001181 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001182
1183 count = 0;
1184 i = d0_idx;
1185 do {
1186 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1187
1188 srcs[slot] = sh->dev[i].page;
1189 i = raid6_next_disk(i, disks);
1190 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001191
NeilBrowne4424fe2009-10-16 16:27:34 +11001192 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001193}
1194
1195static struct dma_async_tx_descriptor *
1196ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1197{
1198 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001199 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001200 int target;
1201 int qd_idx = sh->qd_idx;
1202 struct dma_async_tx_descriptor *tx;
1203 struct async_submit_ctl submit;
1204 struct r5dev *tgt;
1205 struct page *dest;
1206 int i;
1207 int count;
1208
1209 if (sh->ops.target < 0)
1210 target = sh->ops.target2;
1211 else if (sh->ops.target2 < 0)
1212 target = sh->ops.target;
1213 else
1214 /* we should only have one valid target */
1215 BUG();
1216 BUG_ON(target < 0);
1217 pr_debug("%s: stripe %llu block: %d\n",
1218 __func__, (unsigned long long)sh->sector, target);
1219
1220 tgt = &sh->dev[target];
1221 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1222 dest = tgt->page;
1223
1224 atomic_inc(&sh->count);
1225
1226 if (target == qd_idx) {
1227 count = set_syndrome_sources(blocks, sh);
1228 blocks[count] = NULL; /* regenerating p is not necessary */
1229 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001230 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1231 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001232 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001233 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1234 } else {
1235 /* Compute any data- or p-drive using XOR */
1236 count = 0;
1237 for (i = disks; i-- ; ) {
1238 if (i == target || i == qd_idx)
1239 continue;
1240 blocks[count++] = sh->dev[i].page;
1241 }
1242
Dan Williams0403e382009-09-08 17:42:50 -07001243 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1244 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001245 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001246 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1247 }
1248
1249 return tx;
1250}
1251
1252static struct dma_async_tx_descriptor *
1253ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1254{
1255 int i, count, disks = sh->disks;
1256 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1257 int d0_idx = raid6_d0(sh);
1258 int faila = -1, failb = -1;
1259 int target = sh->ops.target;
1260 int target2 = sh->ops.target2;
1261 struct r5dev *tgt = &sh->dev[target];
1262 struct r5dev *tgt2 = &sh->dev[target2];
1263 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001264 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001265 struct async_submit_ctl submit;
1266
1267 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1268 __func__, (unsigned long long)sh->sector, target, target2);
1269 BUG_ON(target < 0 || target2 < 0);
1270 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1271 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1272
Dan Williams6c910a72009-09-16 12:24:54 -07001273 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001274 * slot number conversion for 'faila' and 'failb'
1275 */
1276 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001277 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001278 count = 0;
1279 i = d0_idx;
1280 do {
1281 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1282
1283 blocks[slot] = sh->dev[i].page;
1284
1285 if (i == target)
1286 faila = slot;
1287 if (i == target2)
1288 failb = slot;
1289 i = raid6_next_disk(i, disks);
1290 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001291
1292 BUG_ON(faila == failb);
1293 if (failb < faila)
1294 swap(faila, failb);
1295 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1296 __func__, (unsigned long long)sh->sector, faila, failb);
1297
1298 atomic_inc(&sh->count);
1299
1300 if (failb == syndrome_disks+1) {
1301 /* Q disk is one of the missing disks */
1302 if (faila == syndrome_disks) {
1303 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001304 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1305 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001306 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001307 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001308 STRIPE_SIZE, &submit);
1309 } else {
1310 struct page *dest;
1311 int data_target;
1312 int qd_idx = sh->qd_idx;
1313
1314 /* Missing D+Q: recompute D from P, then recompute Q */
1315 if (target == qd_idx)
1316 data_target = target2;
1317 else
1318 data_target = target;
1319
1320 count = 0;
1321 for (i = disks; i-- ; ) {
1322 if (i == data_target || i == qd_idx)
1323 continue;
1324 blocks[count++] = sh->dev[i].page;
1325 }
1326 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001327 init_async_submit(&submit,
1328 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1329 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001330 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001331 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1332 &submit);
1333
1334 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001335 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1336 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001337 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001338 return async_gen_syndrome(blocks, 0, count+2,
1339 STRIPE_SIZE, &submit);
1340 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001341 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001342 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1343 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001344 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001345 if (failb == syndrome_disks) {
1346 /* We're missing D+P. */
1347 return async_raid6_datap_recov(syndrome_disks+2,
1348 STRIPE_SIZE, faila,
1349 blocks, &submit);
1350 } else {
1351 /* We're missing D+D. */
1352 return async_raid6_2data_recov(syndrome_disks+2,
1353 STRIPE_SIZE, faila, failb,
1354 blocks, &submit);
1355 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356 }
1357}
1358
Dan Williams91c00922007-01-02 13:52:30 -07001359static void ops_complete_prexor(void *stripe_head_ref)
1360{
1361 struct stripe_head *sh = stripe_head_ref;
1362
Harvey Harrisone46b2722008-04-28 02:15:50 -07001363 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001364 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001365}
1366
1367static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001368ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1369 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001370{
Dan Williams91c00922007-01-02 13:52:30 -07001371 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001372 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001373 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001374 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001375
1376 /* existing parity data subtracted */
1377 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1378
Harvey Harrisone46b2722008-04-28 02:15:50 -07001379 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001380 (unsigned long long)sh->sector);
1381
1382 for (i = disks; i--; ) {
1383 struct r5dev *dev = &sh->dev[i];
1384 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001385 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001386 xor_srcs[count++] = dev->page;
1387 }
1388
Dan Williams0403e382009-09-08 17:42:50 -07001389 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001390 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001391 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001392
1393 return tx;
1394}
1395
1396static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001397ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001398{
1399 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001400 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001401
Harvey Harrisone46b2722008-04-28 02:15:50 -07001402 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001403 (unsigned long long)sh->sector);
1404
1405 for (i = disks; i--; ) {
1406 struct r5dev *dev = &sh->dev[i];
1407 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001408
Dan Williamsd8ee0722008-06-28 08:32:06 +10001409 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001410 struct bio *wbi;
1411
Shaohua Lib17459c2012-07-19 16:01:31 +10001412 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001413 chosen = dev->towrite;
1414 dev->towrite = NULL;
1415 BUG_ON(dev->written);
1416 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001417 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001418 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001419
Kent Overstreet4f024f32013-10-11 15:44:27 -07001420 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001421 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001422 if (wbi->bi_rw & REQ_FUA)
1423 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001424 if (wbi->bi_rw & REQ_SYNC)
1425 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001426 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001427 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001428 else {
1429 tx = async_copy_data(1, wbi, &dev->page,
1430 dev->sector, tx, sh);
1431 if (dev->page != dev->orig_page) {
1432 set_bit(R5_SkipCopy, &dev->flags);
1433 clear_bit(R5_UPTODATE, &dev->flags);
1434 clear_bit(R5_OVERWRITE, &dev->flags);
1435 }
1436 }
Dan Williams91c00922007-01-02 13:52:30 -07001437 wbi = r5_next_bio(wbi, dev->sector);
1438 }
1439 }
1440 }
1441
1442 return tx;
1443}
1444
Dan Williamsac6b53b2009-07-14 13:40:19 -07001445static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001446{
1447 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001448 int disks = sh->disks;
1449 int pd_idx = sh->pd_idx;
1450 int qd_idx = sh->qd_idx;
1451 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001452 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001453
Harvey Harrisone46b2722008-04-28 02:15:50 -07001454 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001455 (unsigned long long)sh->sector);
1456
Shaohua Libc0934f2012-05-22 13:55:05 +10001457 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001458 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001459 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001460 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001461 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001462
Dan Williams91c00922007-01-02 13:52:30 -07001463 for (i = disks; i--; ) {
1464 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001465
Tejun Heoe9c74692010-09-03 11:56:18 +02001466 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001467 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001468 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001469 if (fua)
1470 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001471 if (sync)
1472 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001473 }
Dan Williams91c00922007-01-02 13:52:30 -07001474 }
1475
Dan Williamsd8ee0722008-06-28 08:32:06 +10001476 if (sh->reconstruct_state == reconstruct_state_drain_run)
1477 sh->reconstruct_state = reconstruct_state_drain_result;
1478 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1479 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1480 else {
1481 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1482 sh->reconstruct_state = reconstruct_state_result;
1483 }
Dan Williams91c00922007-01-02 13:52:30 -07001484
1485 set_bit(STRIPE_HANDLE, &sh->state);
1486 release_stripe(sh);
1487}
1488
1489static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001490ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1491 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001492{
Dan Williams91c00922007-01-02 13:52:30 -07001493 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001494 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williamsa08abd82009-06-03 11:43:59 -07001495 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001496 int count = 0, pd_idx = sh->pd_idx, i;
1497 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001498 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001499 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001500
Harvey Harrisone46b2722008-04-28 02:15:50 -07001501 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001502 (unsigned long long)sh->sector);
1503
Shaohua Li620125f2012-10-11 13:49:05 +11001504 for (i = 0; i < sh->disks; i++) {
1505 if (pd_idx == i)
1506 continue;
1507 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1508 break;
1509 }
1510 if (i >= sh->disks) {
1511 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001512 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1513 ops_complete_reconstruct(sh);
1514 return;
1515 }
Dan Williams91c00922007-01-02 13:52:30 -07001516 /* check if prexor is active which means only process blocks
1517 * that are part of a read-modify-write (written)
1518 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001519 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1520 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001521 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1522 for (i = disks; i--; ) {
1523 struct r5dev *dev = &sh->dev[i];
1524 if (dev->written)
1525 xor_srcs[count++] = dev->page;
1526 }
1527 } else {
1528 xor_dest = sh->dev[pd_idx].page;
1529 for (i = disks; i--; ) {
1530 struct r5dev *dev = &sh->dev[i];
1531 if (i != pd_idx)
1532 xor_srcs[count++] = dev->page;
1533 }
1534 }
1535
Dan Williams91c00922007-01-02 13:52:30 -07001536 /* 1/ if we prexor'd then the dest is reused as a source
1537 * 2/ if we did not prexor then we are redoing the parity
1538 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1539 * for the synchronous xor case
1540 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001541 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001542 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1543
1544 atomic_inc(&sh->count);
1545
Dan Williamsac6b53b2009-07-14 13:40:19 -07001546 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001547 to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001548 if (unlikely(count == 1))
1549 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1550 else
1551 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001552}
1553
Dan Williamsac6b53b2009-07-14 13:40:19 -07001554static void
1555ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1556 struct dma_async_tx_descriptor *tx)
1557{
1558 struct async_submit_ctl submit;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001559 struct page **blocks = to_addr_page(percpu, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11001560 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001561
1562 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1563
Shaohua Li620125f2012-10-11 13:49:05 +11001564 for (i = 0; i < sh->disks; i++) {
1565 if (sh->pd_idx == i || sh->qd_idx == i)
1566 continue;
1567 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1568 break;
1569 }
1570 if (i >= sh->disks) {
1571 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001572 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1573 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1574 ops_complete_reconstruct(sh);
1575 return;
1576 }
1577
Dan Williamsac6b53b2009-07-14 13:40:19 -07001578 count = set_syndrome_sources(blocks, sh);
1579
1580 atomic_inc(&sh->count);
1581
1582 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001583 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001584 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001585}
1586
1587static void ops_complete_check(void *stripe_head_ref)
1588{
1589 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001590
Harvey Harrisone46b2722008-04-28 02:15:50 -07001591 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001592 (unsigned long long)sh->sector);
1593
Dan Williamsecc65c92008-06-28 08:31:57 +10001594 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001595 set_bit(STRIPE_HANDLE, &sh->state);
1596 release_stripe(sh);
1597}
1598
Dan Williamsac6b53b2009-07-14 13:40:19 -07001599static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001600{
Dan Williams91c00922007-01-02 13:52:30 -07001601 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001602 int pd_idx = sh->pd_idx;
1603 int qd_idx = sh->qd_idx;
1604 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001605 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001606 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001607 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001608 int count;
1609 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001610
Harvey Harrisone46b2722008-04-28 02:15:50 -07001611 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001612 (unsigned long long)sh->sector);
1613
Dan Williamsac6b53b2009-07-14 13:40:19 -07001614 count = 0;
1615 xor_dest = sh->dev[pd_idx].page;
1616 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001617 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001618 if (i == pd_idx || i == qd_idx)
1619 continue;
1620 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001621 }
1622
Dan Williamsd6f38f32009-07-14 11:50:52 -07001623 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001624 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001625 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001626 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001627
Dan Williams91c00922007-01-02 13:52:30 -07001628 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001629 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1630 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001631}
1632
Dan Williamsac6b53b2009-07-14 13:40:19 -07001633static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1634{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001635 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001636 struct async_submit_ctl submit;
1637 int count;
1638
1639 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1640 (unsigned long long)sh->sector, checkp);
1641
1642 count = set_syndrome_sources(srcs, sh);
1643 if (!checkp)
1644 srcs[count] = NULL;
1645
1646 atomic_inc(&sh->count);
1647 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001648 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001649 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1650 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1651}
1652
NeilBrown51acbce2013-02-28 09:08:34 +11001653static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001654{
1655 int overlap_clear = 0, i, disks = sh->disks;
1656 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001657 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001658 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001659 struct raid5_percpu *percpu;
1660 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001661
Dan Williamsd6f38f32009-07-14 11:50:52 -07001662 cpu = get_cpu();
1663 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001664 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001665 ops_run_biofill(sh);
1666 overlap_clear++;
1667 }
1668
Dan Williams7b3a8712008-06-28 08:32:09 +10001669 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001670 if (level < 6)
1671 tx = ops_run_compute5(sh, percpu);
1672 else {
1673 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1674 tx = ops_run_compute6_1(sh, percpu);
1675 else
1676 tx = ops_run_compute6_2(sh, percpu);
1677 }
1678 /* terminate the chain if reconstruct is not set to be run */
1679 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001680 async_tx_ack(tx);
1681 }
Dan Williams91c00922007-01-02 13:52:30 -07001682
Dan Williams600aa102008-06-28 08:32:05 +10001683 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001684 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001685
Dan Williams600aa102008-06-28 08:32:05 +10001686 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001687 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001688 overlap_clear++;
1689 }
1690
Dan Williamsac6b53b2009-07-14 13:40:19 -07001691 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1692 if (level < 6)
1693 ops_run_reconstruct5(sh, percpu, tx);
1694 else
1695 ops_run_reconstruct6(sh, percpu, tx);
1696 }
Dan Williams91c00922007-01-02 13:52:30 -07001697
Dan Williamsac6b53b2009-07-14 13:40:19 -07001698 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1699 if (sh->check_state == check_state_run)
1700 ops_run_check_p(sh, percpu);
1701 else if (sh->check_state == check_state_run_q)
1702 ops_run_check_pq(sh, percpu, 0);
1703 else if (sh->check_state == check_state_run_pq)
1704 ops_run_check_pq(sh, percpu, 1);
1705 else
1706 BUG();
1707 }
Dan Williams91c00922007-01-02 13:52:30 -07001708
Dan Williams91c00922007-01-02 13:52:30 -07001709 if (overlap_clear)
1710 for (i = disks; i--; ) {
1711 struct r5dev *dev = &sh->dev[i];
1712 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1713 wake_up(&sh->raid_conf->wait_for_overlap);
1714 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001715 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001716}
1717
Shaohua Li566c09c2013-11-14 15:16:17 +11001718static int grow_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001721 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001722 if (!sh)
1723 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001724
NeilBrown3f294f42005-11-08 21:39:25 -08001725 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001726
Shaohua Lib17459c2012-07-19 16:01:31 +10001727 spin_lock_init(&sh->stripe_lock);
1728
NeilBrowne4e11e32010-06-16 16:45:16 +10001729 if (grow_buffers(sh)) {
1730 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001731 kmem_cache_free(conf->slab_cache, sh);
1732 return 0;
1733 }
Shaohua Li566c09c2013-11-14 15:16:17 +11001734 sh->hash_lock_index = hash;
NeilBrown3f294f42005-11-08 21:39:25 -08001735 /* we just created an active stripe so... */
1736 atomic_set(&sh->count, 1);
1737 atomic_inc(&conf->active_stripes);
1738 INIT_LIST_HEAD(&sh->lru);
1739 release_stripe(sh);
1740 return 1;
1741}
1742
NeilBrownd1688a62011-10-11 16:49:52 +11001743static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001744{
Christoph Lametere18b8902006-12-06 20:33:20 -08001745 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001746 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Shaohua Li566c09c2013-11-14 15:16:17 +11001747 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
NeilBrownf4be6b42010-06-01 19:37:25 +10001749 if (conf->mddev->gendisk)
1750 sprintf(conf->cache_name[0],
1751 "raid%d-%s", conf->level, mdname(conf->mddev));
1752 else
1753 sprintf(conf->cache_name[0],
1754 "raid%d-%p", conf->level, conf->mddev);
1755 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1756
NeilBrownad01c9e2006-03-27 01:18:07 -08001757 conf->active_name = 0;
1758 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001760 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (!sc)
1762 return 1;
1763 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001764 conf->pool_size = devs;
Shaohua Li566c09c2013-11-14 15:16:17 +11001765 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
1766 while (num--) {
1767 if (!grow_one_stripe(conf, hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11001769 conf->max_nr_stripes++;
1770 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return 0;
1773}
NeilBrown29269552006-03-27 01:18:10 -08001774
Dan Williamsd6f38f32009-07-14 11:50:52 -07001775/**
1776 * scribble_len - return the required size of the scribble region
1777 * @num - total number of disks in the array
1778 *
1779 * The size must be enough to contain:
1780 * 1/ a struct page pointer for each device in the array +2
1781 * 2/ room to convert each entry in (1) to its corresponding dma
1782 * (dma_map_page()) or page (page_address()) address.
1783 *
1784 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1785 * calculate over all devices (not just the data blocks), using zeros in place
1786 * of the P and Q blocks.
1787 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11001788static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07001789{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001790 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001791 size_t len;
1792
1793 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11001794 ret = flex_array_alloc(len, cnt, flags);
1795 if (!ret)
1796 return NULL;
1797 /* always prealloc all elements, so no locking is required */
1798 if (flex_array_prealloc(ret, 0, cnt, flags)) {
1799 flex_array_free(ret);
1800 return NULL;
1801 }
1802 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001803}
1804
NeilBrownd1688a62011-10-11 16:49:52 +11001805static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001806{
1807 /* Make all the stripes able to hold 'newsize' devices.
1808 * New slots in each stripe get 'page' set to a new page.
1809 *
1810 * This happens in stages:
1811 * 1/ create a new kmem_cache and allocate the required number of
1812 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001813 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001814 * to the new stripe_heads. This will have the side effect of
1815 * freezing the array as once all stripe_heads have been collected,
1816 * no IO will be possible. Old stripe heads are freed once their
1817 * pages have been transferred over, and the old kmem_cache is
1818 * freed when all stripes are done.
1819 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1820 * we simple return a failre status - no need to clean anything up.
1821 * 4/ allocate new pages for the new slots in the new stripe_heads.
1822 * If this fails, we don't bother trying the shrink the
1823 * stripe_heads down again, we just leave them as they are.
1824 * As each stripe_head is processed the new one is released into
1825 * active service.
1826 *
1827 * Once step2 is started, we cannot afford to wait for a write,
1828 * so we use GFP_NOIO allocations.
1829 */
1830 struct stripe_head *osh, *nsh;
1831 LIST_HEAD(newstripes);
1832 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001833 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001834 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001835 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001836 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11001837 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08001838
1839 if (newsize <= conf->pool_size)
1840 return 0; /* never bother to shrink */
1841
Dan Williamsb5470dc2008-06-27 21:44:04 -07001842 err = md_allow_write(conf->mddev);
1843 if (err)
1844 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001845
NeilBrownad01c9e2006-03-27 01:18:07 -08001846 /* Step 1 */
1847 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1848 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001849 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001850 if (!sc)
1851 return -ENOMEM;
1852
1853 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001854 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001855 if (!nsh)
1856 break;
1857
NeilBrownad01c9e2006-03-27 01:18:07 -08001858 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001859 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001860
1861 list_add(&nsh->lru, &newstripes);
1862 }
1863 if (i) {
1864 /* didn't get enough, give up */
1865 while (!list_empty(&newstripes)) {
1866 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1867 list_del(&nsh->lru);
1868 kmem_cache_free(sc, nsh);
1869 }
1870 kmem_cache_destroy(sc);
1871 return -ENOMEM;
1872 }
1873 /* Step 2 - Must use GFP_NOIO now.
1874 * OK, we have enough stripes, start collecting inactive
1875 * stripes and copying them over
1876 */
Shaohua Li566c09c2013-11-14 15:16:17 +11001877 hash = 0;
1878 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08001879 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11001880 lock_device_hash_lock(conf, hash);
1881 wait_event_cmd(conf->wait_for_stripe,
1882 !list_empty(conf->inactive_list + hash),
1883 unlock_device_hash_lock(conf, hash),
1884 lock_device_hash_lock(conf, hash));
1885 osh = get_free_stripe(conf, hash);
1886 unlock_device_hash_lock(conf, hash);
NeilBrownad01c9e2006-03-27 01:18:07 -08001887 atomic_set(&nsh->count, 1);
Shaohua Lid592a992014-05-21 17:57:44 +08001888 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08001889 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08001890 nsh->dev[i].orig_page = osh->dev[i].page;
1891 }
NeilBrownad01c9e2006-03-27 01:18:07 -08001892 for( ; i<newsize; i++)
1893 nsh->dev[i].page = NULL;
Shaohua Li566c09c2013-11-14 15:16:17 +11001894 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08001895 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11001896 cnt++;
1897 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
1898 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
1899 hash++;
1900 cnt = 0;
1901 }
NeilBrownad01c9e2006-03-27 01:18:07 -08001902 }
1903 kmem_cache_destroy(conf->slab_cache);
1904
1905 /* Step 3.
1906 * At this point, we are holding all the stripes so the array
1907 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001908 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001909 */
1910 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1911 if (ndisks) {
1912 for (i=0; i<conf->raid_disks; i++)
1913 ndisks[i] = conf->disks[i];
1914 kfree(conf->disks);
1915 conf->disks = ndisks;
1916 } else
1917 err = -ENOMEM;
1918
Dan Williamsd6f38f32009-07-14 11:50:52 -07001919 get_online_cpus();
Dan Williamsd6f38f32009-07-14 11:50:52 -07001920 for_each_present_cpu(cpu) {
1921 struct raid5_percpu *percpu;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001922 struct flex_array *scribble;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001923
1924 percpu = per_cpu_ptr(conf->percpu, cpu);
shli@kernel.org46d5b782014-12-15 12:57:02 +11001925 scribble = scribble_alloc(newsize, conf->chunk_sectors /
1926 STRIPE_SECTORS, GFP_NOIO);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001927
1928 if (scribble) {
shli@kernel.org46d5b782014-12-15 12:57:02 +11001929 flex_array_free(percpu->scribble);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001930 percpu->scribble = scribble;
1931 } else {
1932 err = -ENOMEM;
1933 break;
1934 }
1935 }
1936 put_online_cpus();
1937
NeilBrownad01c9e2006-03-27 01:18:07 -08001938 /* Step 4, return new stripes to service */
1939 while(!list_empty(&newstripes)) {
1940 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1941 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001942
NeilBrownad01c9e2006-03-27 01:18:07 -08001943 for (i=conf->raid_disks; i < newsize; i++)
1944 if (nsh->dev[i].page == NULL) {
1945 struct page *p = alloc_page(GFP_NOIO);
1946 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08001947 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08001948 if (!p)
1949 err = -ENOMEM;
1950 }
1951 release_stripe(nsh);
1952 }
1953 /* critical section pass, GFP_NOIO no longer needed */
1954
1955 conf->slab_cache = sc;
1956 conf->active_name = 1-conf->active_name;
1957 conf->pool_size = newsize;
1958 return err;
1959}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Shaohua Li566c09c2013-11-14 15:16:17 +11001961static int drop_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct stripe_head *sh;
1964
Shaohua Li566c09c2013-11-14 15:16:17 +11001965 spin_lock_irq(conf->hash_locks + hash);
1966 sh = get_free_stripe(conf, hash);
1967 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08001968 if (!sh)
1969 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001970 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001971 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001972 kmem_cache_free(conf->slab_cache, sh);
1973 atomic_dec(&conf->active_stripes);
1974 return 1;
1975}
1976
NeilBrownd1688a62011-10-11 16:49:52 +11001977static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001978{
Shaohua Li566c09c2013-11-14 15:16:17 +11001979 int hash;
1980 for (hash = 0; hash < NR_STRIPE_HASH_LOCKS; hash++)
1981 while (drop_one_stripe(conf, hash))
1982 ;
NeilBrown3f294f42005-11-08 21:39:25 -08001983
NeilBrown29fc7e32006-02-03 03:03:41 -08001984 if (conf->slab_cache)
1985 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 conf->slab_cache = NULL;
1987}
1988
NeilBrown6712ecf2007-09-27 12:47:43 +02001989static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
NeilBrown99c0fb52009-03-31 14:39:38 +11001991 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001992 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001993 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001995 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001996 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001997 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 for (i=0 ; i<disks; i++)
2000 if (bi == &sh->dev[i].req)
2001 break;
2002
Dan Williams45b42332007-07-09 11:56:43 -07002003 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
2004 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 uptodate);
2006 if (i == disks) {
2007 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
NeilBrown14a75d32011-12-23 10:17:52 +11002010 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002011 /* If replacement finished while this request was outstanding,
2012 * 'replacement' might be NULL already.
2013 * In that case it moved down to 'rdev'.
2014 * rdev is not removed until all requests are finished.
2015 */
NeilBrown14a75d32011-12-23 10:17:52 +11002016 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002017 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002018 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
NeilBrown05616be2012-05-21 09:27:00 +10002020 if (use_new_offset(conf, sh))
2021 s = sh->sector + rdev->new_data_offset;
2022 else
2023 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002026 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002027 /* Note that this cannot happen on a
2028 * replacement device. We just fail those on
2029 * any error
2030 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10002031 printk_ratelimited(
2032 KERN_INFO
2033 "md/raid:%s: read error corrected"
2034 " (%lu sectors at %llu on %s)\n",
2035 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002036 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002037 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002038 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002039 clear_bit(R5_ReadError, &sh->dev[i].flags);
2040 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002041 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2042 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2043
NeilBrown14a75d32011-12-23 10:17:52 +11002044 if (atomic_read(&rdev->read_errors))
2045 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002047 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002048 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002049 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002052 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002053 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2054 printk_ratelimited(
2055 KERN_WARNING
2056 "md/raid:%s: read error on replacement device "
2057 "(sector %llu on %s).\n",
2058 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002059 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002060 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002061 else if (conf->mddev->degraded >= conf->max_degraded) {
2062 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002063 printk_ratelimited(
2064 KERN_WARNING
2065 "md/raid:%s: read error not correctable "
2066 "(sector %llu on %s).\n",
2067 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002068 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002069 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002070 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002071 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002072 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002073 printk_ratelimited(
2074 KERN_WARNING
2075 "md/raid:%s: read error NOT corrected!! "
2076 "(sector %llu on %s).\n",
2077 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002078 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002079 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002080 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002081 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002082 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002083 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002084 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002085 else
2086 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002087 if (set_bad && test_bit(In_sync, &rdev->flags)
2088 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2089 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002090 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002091 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2092 set_bit(R5_ReadError, &sh->dev[i].flags);
2093 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2094 } else
2095 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002096 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002097 clear_bit(R5_ReadError, &sh->dev[i].flags);
2098 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002099 if (!(set_bad
2100 && test_bit(In_sync, &rdev->flags)
2101 && rdev_set_badblocks(
2102 rdev, sh->sector, STRIPE_SECTORS, 0)))
2103 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
NeilBrown14a75d32011-12-23 10:17:52 +11002106 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2108 set_bit(STRIPE_HANDLE, &sh->state);
2109 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
NeilBrownd710e132008-10-13 11:55:12 +11002112static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
NeilBrown99c0fb52009-03-31 14:39:38 +11002114 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002115 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002116 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002117 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10002119 sector_t first_bad;
2120 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002121 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
NeilBrown977df362011-12-23 10:17:53 +11002123 for (i = 0 ; i < disks; i++) {
2124 if (bi == &sh->dev[i].req) {
2125 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 break;
NeilBrown977df362011-12-23 10:17:53 +11002127 }
2128 if (bi == &sh->dev[i].rreq) {
2129 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002130 if (rdev)
2131 replacement = 1;
2132 else
2133 /* rdev was removed and 'replacement'
2134 * replaced it. rdev is not removed
2135 * until all requests are finished.
2136 */
2137 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002138 break;
2139 }
2140 }
Dan Williams45b42332007-07-09 11:56:43 -07002141 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2143 uptodate);
2144 if (i == disks) {
2145 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002146 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
NeilBrown977df362011-12-23 10:17:53 +11002149 if (replacement) {
2150 if (!uptodate)
2151 md_error(conf->mddev, rdev);
2152 else if (is_badblock(rdev, sh->sector,
2153 STRIPE_SECTORS,
2154 &first_bad, &bad_sectors))
2155 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2156 } else {
2157 if (!uptodate) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002158 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002159 set_bit(WriteErrorSeen, &rdev->flags);
2160 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002161 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2162 set_bit(MD_RECOVERY_NEEDED,
2163 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002164 } else if (is_badblock(rdev, sh->sector,
2165 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002166 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002167 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002168 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2169 /* That was a successful write so make
2170 * sure it looks like we already did
2171 * a re-write.
2172 */
2173 set_bit(R5_ReWrite, &sh->dev[i].flags);
2174 }
NeilBrown977df362011-12-23 10:17:53 +11002175 }
2176 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
NeilBrown977df362011-12-23 10:17:53 +11002178 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2179 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07002181 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
NeilBrown784052e2009-03-31 15:19:07 +11002184static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Shaohua Lid592a992014-05-21 17:57:44 +08002185
NeilBrown784052e2009-03-31 15:19:07 +11002186static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct r5dev *dev = &sh->dev[i];
2189
2190 bio_init(&dev->req);
2191 dev->req.bi_io_vec = &dev->vec;
Shaohua Lid592a992014-05-21 17:57:44 +08002192 dev->req.bi_max_vecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 dev->req.bi_private = sh;
2194
NeilBrown977df362011-12-23 10:17:53 +11002195 bio_init(&dev->rreq);
2196 dev->rreq.bi_io_vec = &dev->rvec;
Shaohua Lid592a992014-05-21 17:57:44 +08002197 dev->rreq.bi_max_vecs = 1;
NeilBrown977df362011-12-23 10:17:53 +11002198 dev->rreq.bi_private = sh;
NeilBrown977df362011-12-23 10:17:53 +11002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002201 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
NeilBrownfd01b882011-10-11 16:47:53 +11002204static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
2206 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002207 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002208 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002209 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
NeilBrown908f4fb2011-12-23 10:17:50 +11002211 spin_lock_irqsave(&conf->device_lock, flags);
2212 clear_bit(In_sync, &rdev->flags);
2213 mddev->degraded = calc_degraded(conf);
2214 spin_unlock_irqrestore(&conf->device_lock, flags);
2215 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2216
NeilBrownde393cd2011-07-28 11:31:48 +10002217 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002218 set_bit(Faulty, &rdev->flags);
2219 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2220 printk(KERN_ALERT
2221 "md/raid:%s: Disk failure on %s, disabling device.\n"
2222 "md/raid:%s: Operation continuing on %d devices.\n",
2223 mdname(mddev),
2224 bdevname(rdev->bdev, b),
2225 mdname(mddev),
2226 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229/*
2230 * Input: a 'big' sector number,
2231 * Output: index of the data and parity disk, and the sector # in them.
2232 */
NeilBrownd1688a62011-10-11 16:49:52 +11002233static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002234 int previous, int *dd_idx,
2235 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002237 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002238 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002240 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002241 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002243 int algorithm = previous ? conf->prev_algo
2244 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002245 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2246 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002247 int raid_disks = previous ? conf->previous_raid_disks
2248 : conf->raid_disks;
2249 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 /* First compute the information on this sector */
2252
2253 /*
2254 * Compute the chunk number and the sector offset inside the chunk
2255 */
2256 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2257 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /*
2260 * Compute the stripe number
2261 */
NeilBrown35f2a592010-04-20 14:13:34 +10002262 stripe = chunk_number;
2263 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002264 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /*
2266 * Select the parity disk based on the user selected algorithm.
2267 */
NeilBrown84789552011-07-27 11:00:36 +10002268 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002269 switch(conf->level) {
2270 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002271 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002272 break;
2273 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002274 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002276 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002277 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 (*dd_idx)++;
2279 break;
2280 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002281 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002282 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 (*dd_idx)++;
2284 break;
2285 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002286 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002287 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 break;
2289 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002290 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002291 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002293 case ALGORITHM_PARITY_0:
2294 pd_idx = 0;
2295 (*dd_idx)++;
2296 break;
2297 case ALGORITHM_PARITY_N:
2298 pd_idx = data_disks;
2299 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002301 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002302 }
2303 break;
2304 case 6:
2305
NeilBrowne183eae2009-03-31 15:20:22 +11002306 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002307 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002308 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002309 qd_idx = pd_idx + 1;
2310 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002311 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002312 qd_idx = 0;
2313 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002314 (*dd_idx) += 2; /* D D P Q D */
2315 break;
2316 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002317 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002318 qd_idx = pd_idx + 1;
2319 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002320 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002321 qd_idx = 0;
2322 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002323 (*dd_idx) += 2; /* D D P Q D */
2324 break;
2325 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002326 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002327 qd_idx = (pd_idx + 1) % raid_disks;
2328 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002329 break;
2330 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002331 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002332 qd_idx = (pd_idx + 1) % raid_disks;
2333 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002334 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002335
2336 case ALGORITHM_PARITY_0:
2337 pd_idx = 0;
2338 qd_idx = 1;
2339 (*dd_idx) += 2;
2340 break;
2341 case ALGORITHM_PARITY_N:
2342 pd_idx = data_disks;
2343 qd_idx = data_disks + 1;
2344 break;
2345
2346 case ALGORITHM_ROTATING_ZERO_RESTART:
2347 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2348 * of blocks for computing Q is different.
2349 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002350 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002351 qd_idx = pd_idx + 1;
2352 if (pd_idx == raid_disks-1) {
2353 (*dd_idx)++; /* Q D D D P */
2354 qd_idx = 0;
2355 } else if (*dd_idx >= pd_idx)
2356 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002357 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002358 break;
2359
2360 case ALGORITHM_ROTATING_N_RESTART:
2361 /* Same a left_asymmetric, by first stripe is
2362 * D D D P Q rather than
2363 * Q D D D P
2364 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002365 stripe2 += 1;
2366 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002367 qd_idx = pd_idx + 1;
2368 if (pd_idx == raid_disks-1) {
2369 (*dd_idx)++; /* Q D D D P */
2370 qd_idx = 0;
2371 } else if (*dd_idx >= pd_idx)
2372 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002373 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002374 break;
2375
2376 case ALGORITHM_ROTATING_N_CONTINUE:
2377 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002378 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002379 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2380 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002381 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002382 break;
2383
2384 case ALGORITHM_LEFT_ASYMMETRIC_6:
2385 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002386 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002387 if (*dd_idx >= pd_idx)
2388 (*dd_idx)++;
2389 qd_idx = raid_disks - 1;
2390 break;
2391
2392 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002393 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002394 if (*dd_idx >= pd_idx)
2395 (*dd_idx)++;
2396 qd_idx = raid_disks - 1;
2397 break;
2398
2399 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002400 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002401 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2402 qd_idx = raid_disks - 1;
2403 break;
2404
2405 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002406 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002407 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2408 qd_idx = raid_disks - 1;
2409 break;
2410
2411 case ALGORITHM_PARITY_0_6:
2412 pd_idx = 0;
2413 (*dd_idx)++;
2414 qd_idx = raid_disks - 1;
2415 break;
2416
NeilBrown16a53ec2006-06-26 00:27:38 -07002417 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002418 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002419 }
2420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422
NeilBrown911d4ee2009-03-31 14:39:38 +11002423 if (sh) {
2424 sh->pd_idx = pd_idx;
2425 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002426 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /*
2429 * Finally, compute the new sector number
2430 */
2431 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2432 return new_sector;
2433}
2434
NeilBrown784052e2009-03-31 15:19:07 +11002435static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
NeilBrownd1688a62011-10-11 16:49:52 +11002437 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002438 int raid_disks = sh->disks;
2439 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002441 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2442 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002443 int algorithm = previous ? conf->prev_algo
2444 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 sector_t stripe;
2446 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002447 sector_t chunk_number;
2448 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002450 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2453 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
NeilBrown16a53ec2006-06-26 00:27:38 -07002455 if (i == sh->pd_idx)
2456 return 0;
2457 switch(conf->level) {
2458 case 4: break;
2459 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002460 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 case ALGORITHM_LEFT_ASYMMETRIC:
2462 case ALGORITHM_RIGHT_ASYMMETRIC:
2463 if (i > sh->pd_idx)
2464 i--;
2465 break;
2466 case ALGORITHM_LEFT_SYMMETRIC:
2467 case ALGORITHM_RIGHT_SYMMETRIC:
2468 if (i < sh->pd_idx)
2469 i += raid_disks;
2470 i -= (sh->pd_idx + 1);
2471 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002472 case ALGORITHM_PARITY_0:
2473 i -= 1;
2474 break;
2475 case ALGORITHM_PARITY_N:
2476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002478 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002479 }
2480 break;
2481 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002482 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002483 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002484 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002485 case ALGORITHM_LEFT_ASYMMETRIC:
2486 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002487 case ALGORITHM_ROTATING_ZERO_RESTART:
2488 case ALGORITHM_ROTATING_N_RESTART:
2489 if (sh->pd_idx == raid_disks-1)
2490 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002491 else if (i > sh->pd_idx)
2492 i -= 2; /* D D P Q D */
2493 break;
2494 case ALGORITHM_LEFT_SYMMETRIC:
2495 case ALGORITHM_RIGHT_SYMMETRIC:
2496 if (sh->pd_idx == raid_disks-1)
2497 i--; /* Q D D D P */
2498 else {
2499 /* D D P Q D */
2500 if (i < sh->pd_idx)
2501 i += raid_disks;
2502 i -= (sh->pd_idx + 2);
2503 }
2504 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002505 case ALGORITHM_PARITY_0:
2506 i -= 2;
2507 break;
2508 case ALGORITHM_PARITY_N:
2509 break;
2510 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002511 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002512 if (sh->pd_idx == 0)
2513 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002514 else {
2515 /* D D Q P D */
2516 if (i < sh->pd_idx)
2517 i += raid_disks;
2518 i -= (sh->pd_idx + 1);
2519 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002520 break;
2521 case ALGORITHM_LEFT_ASYMMETRIC_6:
2522 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2523 if (i > sh->pd_idx)
2524 i--;
2525 break;
2526 case ALGORITHM_LEFT_SYMMETRIC_6:
2527 case ALGORITHM_RIGHT_SYMMETRIC_6:
2528 if (i < sh->pd_idx)
2529 i += data_disks + 1;
2530 i -= (sh->pd_idx + 1);
2531 break;
2532 case ALGORITHM_PARITY_0_6:
2533 i -= 1;
2534 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002535 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002536 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002537 }
2538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
2540
2541 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002542 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
NeilBrown112bf892009-03-31 14:39:38 +11002544 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002545 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002546 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2547 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002548 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2549 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 return 0;
2551 }
2552 return r_sector;
2553}
2554
Dan Williams600aa102008-06-28 08:32:05 +10002555static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002556schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002557 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002558{
2559 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002560 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002561 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002562
Dan Williamse33129d2007-01-02 13:52:30 -07002563 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002564
2565 for (i = disks; i--; ) {
2566 struct r5dev *dev = &sh->dev[i];
2567
2568 if (dev->towrite) {
2569 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002570 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002571 if (!expand)
2572 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002573 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002574 }
2575 }
NeilBrownce7d3632013-03-04 12:37:14 +11002576 /* if we are not expanding this is a proper write request, and
2577 * there will be bios with new data to be drained into the
2578 * stripe cache
2579 */
2580 if (!expand) {
2581 if (!s->locked)
2582 /* False alarm, nothing to do */
2583 return;
2584 sh->reconstruct_state = reconstruct_state_drain_run;
2585 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2586 } else
2587 sh->reconstruct_state = reconstruct_state_run;
2588
2589 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2590
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002591 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002592 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002593 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002594 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002595 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002596 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2597 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2598
Dan Williamse33129d2007-01-02 13:52:30 -07002599 for (i = disks; i--; ) {
2600 struct r5dev *dev = &sh->dev[i];
2601 if (i == pd_idx)
2602 continue;
2603
Dan Williamse33129d2007-01-02 13:52:30 -07002604 if (dev->towrite &&
2605 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002606 test_bit(R5_Wantcompute, &dev->flags))) {
2607 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002608 set_bit(R5_LOCKED, &dev->flags);
2609 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002610 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002611 }
2612 }
NeilBrownce7d3632013-03-04 12:37:14 +11002613 if (!s->locked)
2614 /* False alarm - nothing to do */
2615 return;
2616 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2617 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2618 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2619 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002620 }
2621
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002622 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002623 * are in flight
2624 */
2625 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2626 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002627 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002628
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002629 if (level == 6) {
2630 int qd_idx = sh->qd_idx;
2631 struct r5dev *dev = &sh->dev[qd_idx];
2632
2633 set_bit(R5_LOCKED, &dev->flags);
2634 clear_bit(R5_UPTODATE, &dev->flags);
2635 s->locked++;
2636 }
2637
Dan Williams600aa102008-06-28 08:32:05 +10002638 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002639 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002640 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002641}
NeilBrown16a53ec2006-06-26 00:27:38 -07002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643/*
2644 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002645 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 * The bi_next chain must be in order.
2647 */
2648static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2649{
2650 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002651 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002652 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
NeilBrowncbe47ec2011-07-26 11:20:35 +10002654 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002655 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 (unsigned long long)sh->sector);
2657
Shaohua Lib17459c2012-07-19 16:01:31 +10002658 /*
2659 * If several bio share a stripe. The bio bi_phys_segments acts as a
2660 * reference count to avoid race. The reference count should already be
2661 * increased before this function is called (for example, in
2662 * make_request()), so other bio sharing this stripe will not free the
2663 * stripe. If a stripe is owned by one stripe, the stripe lock will
2664 * protect it.
2665 */
2666 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002667 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002669 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002670 firstwrite = 1;
2671 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002673 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
2674 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 goto overlap;
2676 bip = & (*bip)->bi_next;
2677 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07002678 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 goto overlap;
2680
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002681 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (*bip)
2683 bi->bi_next = *bip;
2684 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002685 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (forwrite) {
2688 /* check if page is covered */
2689 sector_t sector = sh->dev[dd_idx].sector;
2690 for (bi=sh->dev[dd_idx].towrite;
2691 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07002692 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002694 if (bio_end_sector(bi) >= sector)
2695 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 }
2697 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2698 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2699 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002700
2701 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002702 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10002703 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002704 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002705
2706 if (conf->mddev->bitmap && firstwrite) {
2707 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2708 STRIPE_SECTORS, 0);
2709 sh->bm_seq = conf->seq_flush+1;
2710 set_bit(STRIPE_BIT_DELAY, &sh->state);
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return 1;
2713
2714 overlap:
2715 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002716 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return 0;
2718}
2719
NeilBrownd1688a62011-10-11 16:49:52 +11002720static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002721
NeilBrownd1688a62011-10-11 16:49:52 +11002722static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002723 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002724{
NeilBrown784052e2009-03-31 15:19:07 +11002725 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002726 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002727 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002728 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002729 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002730
NeilBrown112bf892009-03-31 14:39:38 +11002731 raid5_compute_sector(conf,
2732 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002733 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002734 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002735 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002736}
2737
Dan Williamsa4456852007-07-09 11:56:43 -07002738static void
NeilBrownd1688a62011-10-11 16:49:52 +11002739handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002740 struct stripe_head_state *s, int disks,
2741 struct bio **return_bi)
2742{
2743 int i;
2744 for (i = disks; i--; ) {
2745 struct bio *bi;
2746 int bitmap_end = 0;
2747
2748 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002749 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002750 rcu_read_lock();
2751 rdev = rcu_dereference(conf->disks[i].rdev);
2752 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002753 atomic_inc(&rdev->nr_pending);
2754 else
2755 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002756 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002757 if (rdev) {
2758 if (!rdev_set_badblocks(
2759 rdev,
2760 sh->sector,
2761 STRIPE_SECTORS, 0))
2762 md_error(conf->mddev, rdev);
2763 rdev_dec_pending(rdev, conf->mddev);
2764 }
Dan Williamsa4456852007-07-09 11:56:43 -07002765 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002766 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002767 /* fail all writes first */
2768 bi = sh->dev[i].towrite;
2769 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002770 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002771 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002772 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002773
2774 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2775 wake_up(&conf->wait_for_overlap);
2776
Kent Overstreet4f024f32013-10-11 15:44:27 -07002777 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002778 sh->dev[i].sector + STRIPE_SECTORS) {
2779 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2780 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002781 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002782 md_write_end(conf->mddev);
2783 bi->bi_next = *return_bi;
2784 *return_bi = bi;
2785 }
2786 bi = nextbi;
2787 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002788 if (bitmap_end)
2789 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2790 STRIPE_SECTORS, 0, 0);
2791 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002792 /* and fail all 'written' */
2793 bi = sh->dev[i].written;
2794 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08002795 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
2796 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
2797 sh->dev[i].page = sh->dev[i].orig_page;
2798 }
2799
Dan Williamsa4456852007-07-09 11:56:43 -07002800 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002801 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002802 sh->dev[i].sector + STRIPE_SECTORS) {
2803 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2804 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002805 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002806 md_write_end(conf->mddev);
2807 bi->bi_next = *return_bi;
2808 *return_bi = bi;
2809 }
2810 bi = bi2;
2811 }
2812
Dan Williamsb5e98d62007-01-02 13:52:31 -07002813 /* fail any reads if this device is non-operational and
2814 * the data has not reached the cache yet.
2815 */
2816 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2817 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2818 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002819 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002820 bi = sh->dev[i].toread;
2821 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002822 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002823 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2824 wake_up(&conf->wait_for_overlap);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002825 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002826 sh->dev[i].sector + STRIPE_SECTORS) {
2827 struct bio *nextbi =
2828 r5_next_bio(bi, sh->dev[i].sector);
2829 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002830 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002831 bi->bi_next = *return_bi;
2832 *return_bi = bi;
2833 }
2834 bi = nextbi;
2835 }
2836 }
Dan Williamsa4456852007-07-09 11:56:43 -07002837 if (bitmap_end)
2838 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2839 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002840 /* If we were in the middle of a write the parity block might
2841 * still be locked - so just clear all R5_LOCKED flags
2842 */
2843 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002844 }
2845
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002846 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2847 if (atomic_dec_and_test(&conf->pending_full_writes))
2848 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002849}
2850
NeilBrown7f0da592011-07-28 11:39:22 +10002851static void
NeilBrownd1688a62011-10-11 16:49:52 +11002852handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002853 struct stripe_head_state *s)
2854{
2855 int abort = 0;
2856 int i;
2857
NeilBrown7f0da592011-07-28 11:39:22 +10002858 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002859 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2860 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002861 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002862 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002863 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002864 * Don't even need to abort as that is handled elsewhere
2865 * if needed, and not always wanted e.g. if there is a known
2866 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002867 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002868 * non-sync devices, or abort the recovery
2869 */
NeilBrown18b98372012-04-01 23:48:38 +10002870 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2871 /* During recovery devices cannot be removed, so
2872 * locking and refcounting of rdevs is not needed
2873 */
2874 for (i = 0; i < conf->raid_disks; i++) {
2875 struct md_rdev *rdev = conf->disks[i].rdev;
2876 if (rdev
2877 && !test_bit(Faulty, &rdev->flags)
2878 && !test_bit(In_sync, &rdev->flags)
2879 && !rdev_set_badblocks(rdev, sh->sector,
2880 STRIPE_SECTORS, 0))
2881 abort = 1;
2882 rdev = conf->disks[i].replacement;
2883 if (rdev
2884 && !test_bit(Faulty, &rdev->flags)
2885 && !test_bit(In_sync, &rdev->flags)
2886 && !rdev_set_badblocks(rdev, sh->sector,
2887 STRIPE_SECTORS, 0))
2888 abort = 1;
2889 }
2890 if (abort)
2891 conf->recovery_disabled =
2892 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002893 }
NeilBrown18b98372012-04-01 23:48:38 +10002894 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002895}
2896
NeilBrown9a3e1102011-12-23 10:17:53 +11002897static int want_replace(struct stripe_head *sh, int disk_idx)
2898{
2899 struct md_rdev *rdev;
2900 int rv = 0;
2901 /* Doing recovery so rcu locking not required */
2902 rdev = sh->raid_conf->disks[disk_idx].replacement;
2903 if (rdev
2904 && !test_bit(Faulty, &rdev->flags)
2905 && !test_bit(In_sync, &rdev->flags)
2906 && (rdev->recovery_offset <= sh->sector
2907 || rdev->mddev->recovery_cp <= sh->sector))
2908 rv = 1;
2909
2910 return rv;
2911}
2912
NeilBrown93b3dbc2011-07-27 11:00:36 +10002913/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002914 * to be read or computed to satisfy a request.
2915 *
2916 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002917 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002918 */
NeilBrown2c58f062015-02-02 11:32:23 +11002919
2920static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
2921 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002922{
2923 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002924 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2925 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11002926 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002927
NeilBrowna79cfe12015-02-02 11:37:59 +11002928
2929 if (test_bit(R5_LOCKED, &dev->flags) ||
2930 test_bit(R5_UPTODATE, &dev->flags))
2931 /* No point reading this as we already have it or have
2932 * decided to get it.
2933 */
2934 return 0;
2935
2936 if (dev->toread ||
2937 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
2938 /* We need this block to directly satisfy a request */
2939 return 1;
2940
2941 if (s->syncing || s->expanding ||
2942 (s->replacing && want_replace(sh, disk_idx)))
2943 /* When syncing, or expanding we read everything.
2944 * When replacing, we need the replaced block.
2945 */
2946 return 1;
2947
2948 if ((s->failed >= 1 && fdev[0]->toread) ||
2949 (s->failed >= 2 && fdev[1]->toread))
2950 /* If we want to read from a failed device, then
2951 * we need to actually read every other device.
2952 */
2953 return 1;
2954
NeilBrowna9d56952015-02-02 11:49:10 +11002955 /* Sometimes neither read-modify-write nor reconstruct-write
2956 * cycles can work. In those cases we read every block we
2957 * can. Then the parity-update is certain to have enough to
2958 * work with.
2959 * This can only be a problem when we need to write something,
2960 * and some device has failed. If either of those tests
2961 * fail we need look no further.
2962 */
2963 if (!s->failed || !s->to_write)
2964 return 0;
2965
2966 if (test_bit(R5_Insync, &dev->flags) &&
2967 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2968 /* Pre-reads at not permitted until after short delay
2969 * to gather multiple requests. However if this
2970 * device is no Insync, the block could only be be computed
2971 * and there is no need to delay that.
2972 */
2973 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11002974
2975 for (i = 0; i < s->failed; i++) {
2976 if (fdev[i]->towrite &&
2977 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
2978 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
2979 /* If we have a partial write to a failed
2980 * device, then we will need to reconstruct
2981 * the content of that device, so all other
2982 * devices must be read.
2983 */
2984 return 1;
2985 }
2986
2987 /* If we are forced to do a reconstruct-write, either because
2988 * the current RAID6 implementation only supports that, or
2989 * or because parity cannot be trusted and we are currently
2990 * recovering it, there is extra need to be careful.
2991 * If one of the devices that we would need to read, because
2992 * it is not being overwritten (and maybe not written at all)
2993 * is missing/faulty, then we need to read everything we can.
2994 */
2995 if (sh->raid_conf->level != 6 &&
2996 sh->sector < sh->raid_conf->mddev->recovery_cp)
2997 /* reconstruct-write isn't being forced */
2998 return 0;
2999 for (i = 0; i < s->failed; i++) {
3000 if (!test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3001 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3002 return 1;
3003 }
3004
NeilBrown2c58f062015-02-02 11:32:23 +11003005 return 0;
3006}
3007
3008static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3009 int disk_idx, int disks)
3010{
3011 struct r5dev *dev = &sh->dev[disk_idx];
3012
3013 /* is the data in this block needed, and can we get it? */
3014 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003015 /* we would like to get this block, possibly by computing it,
3016 * otherwise read it if the backing disk is insync
3017 */
3018 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3019 BUG_ON(test_bit(R5_Wantread, &dev->flags));
3020 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003021 (s->failed && (disk_idx == s->failed_num[0] ||
3022 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003023 /* have disk failed, and we're requested to fetch it;
3024 * do compute it
3025 */
3026 pr_debug("Computing stripe %llu block %d\n",
3027 (unsigned long long)sh->sector, disk_idx);
3028 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3029 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3030 set_bit(R5_Wantcompute, &dev->flags);
3031 sh->ops.target = disk_idx;
3032 sh->ops.target2 = -1; /* no 2nd target */
3033 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003034 /* Careful: from this point on 'uptodate' is in the eye
3035 * of raid_run_ops which services 'compute' operations
3036 * before writes. R5_Wantcompute flags a block that will
3037 * be R5_UPTODATE by the time it is needed for a
3038 * subsequent operation.
3039 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003040 s->uptodate++;
3041 return 1;
3042 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3043 /* Computing 2-failure is *very* expensive; only
3044 * do it if failed >= 2
3045 */
3046 int other;
3047 for (other = disks; other--; ) {
3048 if (other == disk_idx)
3049 continue;
3050 if (!test_bit(R5_UPTODATE,
3051 &sh->dev[other].flags))
3052 break;
3053 }
3054 BUG_ON(other < 0);
3055 pr_debug("Computing stripe %llu blocks %d,%d\n",
3056 (unsigned long long)sh->sector,
3057 disk_idx, other);
3058 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3059 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3060 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3061 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3062 sh->ops.target = disk_idx;
3063 sh->ops.target2 = other;
3064 s->uptodate += 2;
3065 s->req_compute = 1;
3066 return 1;
3067 } else if (test_bit(R5_Insync, &dev->flags)) {
3068 set_bit(R5_LOCKED, &dev->flags);
3069 set_bit(R5_Wantread, &dev->flags);
3070 s->locked++;
3071 pr_debug("Reading block %d (sync=%d)\n",
3072 disk_idx, s->syncing);
3073 }
3074 }
3075
3076 return 0;
3077}
3078
3079/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003080 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003081 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003082static void handle_stripe_fill(struct stripe_head *sh,
3083 struct stripe_head_state *s,
3084 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003085{
3086 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003087
3088 /* look for blocks to read/compute, skip this if a compute
3089 * is already in flight, or if the stripe contents are in the
3090 * midst of changing due to a write
3091 */
3092 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
3093 !sh->reconstruct_state)
3094 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003095 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003096 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003097 set_bit(STRIPE_HANDLE, &sh->state);
3098}
3099
Dan Williams1fe797e2008-06-28 09:16:30 +10003100/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003101 * any written block on an uptodate or failed drive can be returned.
3102 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3103 * never LOCKED, so we don't need to test 'failed' directly.
3104 */
NeilBrownd1688a62011-10-11 16:49:52 +11003105static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07003106 struct stripe_head *sh, int disks, struct bio **return_bi)
3107{
3108 int i;
3109 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003110 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003111
3112 for (i = disks; i--; )
3113 if (sh->dev[i].written) {
3114 dev = &sh->dev[i];
3115 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003116 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003117 test_bit(R5_Discard, &dev->flags) ||
3118 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003119 /* We can return any write requests */
3120 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003121 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003122 if (test_and_clear_bit(R5_Discard, &dev->flags))
3123 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003124 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3125 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
3126 dev->page = dev->orig_page;
3127 }
Dan Williamsa4456852007-07-09 11:56:43 -07003128 wbi = dev->written;
3129 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003130 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003131 dev->sector + STRIPE_SECTORS) {
3132 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003133 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003134 md_write_end(conf->mddev);
3135 wbi->bi_next = *return_bi;
3136 *return_bi = wbi;
3137 }
3138 wbi = wbi2;
3139 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003140 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3141 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003142 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003143 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003144 } else if (test_bit(R5_Discard, &dev->flags))
3145 discard_pending = 1;
Shaohua Lid592a992014-05-21 17:57:44 +08003146 WARN_ON(test_bit(R5_SkipCopy, &dev->flags));
3147 WARN_ON(dev->page != dev->orig_page);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003148 }
3149 if (!discard_pending &&
3150 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3151 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3152 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3153 if (sh->qd_idx >= 0) {
3154 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3155 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3156 }
3157 /* now that discard is done we can proceed with any sync */
3158 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003159 /*
3160 * SCSI discard will change some bio fields and the stripe has
3161 * no updated data, so remove it from hash list and the stripe
3162 * will be reinitialized
3163 */
3164 spin_lock_irq(&conf->device_lock);
3165 remove_hash(sh);
3166 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003167 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3168 set_bit(STRIPE_HANDLE, &sh->state);
3169
3170 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003171
3172 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3173 if (atomic_dec_and_test(&conf->pending_full_writes))
3174 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003175}
3176
NeilBrownd1688a62011-10-11 16:49:52 +11003177static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003178 struct stripe_head *sh,
3179 struct stripe_head_state *s,
3180 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003181{
3182 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003183 sector_t recovery_cp = conf->mddev->recovery_cp;
3184
3185 /* RAID6 requires 'rcw' in current implementation.
3186 * Otherwise, check whether resync is now happening or should start.
3187 * If yes, then the array is dirty (after unclean shutdown or
3188 * initial creation), so parity in some stripes might be inconsistent.
3189 * In this case, we need to always do reconstruct-write, to ensure
3190 * that in case of drive failure or read-error correction, we
3191 * generate correct data from the parity.
3192 */
3193 if (conf->max_degraded == 2 ||
NeilBrown26ac1072015-02-18 11:35:14 +11003194 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3195 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003196 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003197 * look like rcw is cheaper
3198 */
3199 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003200 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
3201 conf->max_degraded, (unsigned long long)recovery_cp,
3202 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003203 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003204 /* would I have to read this buffer for read_modify_write */
3205 struct r5dev *dev = &sh->dev[i];
3206 if ((dev->towrite || i == sh->pd_idx) &&
3207 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003208 !(test_bit(R5_UPTODATE, &dev->flags) ||
3209 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003210 if (test_bit(R5_Insync, &dev->flags))
3211 rmw++;
3212 else
3213 rmw += 2*disks; /* cannot read it */
3214 }
3215 /* Would I have to read this buffer for reconstruct_write */
3216 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
3217 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003218 !(test_bit(R5_UPTODATE, &dev->flags) ||
3219 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003220 if (test_bit(R5_Insync, &dev->flags))
3221 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003222 else
3223 rcw += 2*disks;
3224 }
3225 }
Dan Williams45b42332007-07-09 11:56:43 -07003226 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003227 (unsigned long long)sh->sector, rmw, rcw);
3228 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11003229 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003230 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003231 if (conf->mddev->queue)
3232 blk_add_trace_msg(conf->mddev->queue,
3233 "raid5 rmw %llu %d",
3234 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003235 for (i = disks; i--; ) {
3236 struct r5dev *dev = &sh->dev[i];
3237 if ((dev->towrite || i == sh->pd_idx) &&
3238 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003239 !(test_bit(R5_UPTODATE, &dev->flags) ||
3240 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003241 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003242 if (test_bit(STRIPE_PREREAD_ACTIVE,
3243 &sh->state)) {
3244 pr_debug("Read_old block %d for r-m-w\n",
3245 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003246 set_bit(R5_LOCKED, &dev->flags);
3247 set_bit(R5_Wantread, &dev->flags);
3248 s->locked++;
3249 } else {
3250 set_bit(STRIPE_DELAYED, &sh->state);
3251 set_bit(STRIPE_HANDLE, &sh->state);
3252 }
3253 }
3254 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003255 }
NeilBrownc8ac1802011-07-27 11:00:36 +10003256 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003257 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003258 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003259 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003260 for (i = disks; i--; ) {
3261 struct r5dev *dev = &sh->dev[i];
3262 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003263 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003264 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003265 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003266 test_bit(R5_Wantcompute, &dev->flags))) {
3267 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003268 if (test_bit(R5_Insync, &dev->flags) &&
3269 test_bit(STRIPE_PREREAD_ACTIVE,
3270 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003271 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003272 "%d for Reconstruct\n", i);
3273 set_bit(R5_LOCKED, &dev->flags);
3274 set_bit(R5_Wantread, &dev->flags);
3275 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003276 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003277 } else {
3278 set_bit(STRIPE_DELAYED, &sh->state);
3279 set_bit(STRIPE_HANDLE, &sh->state);
3280 }
3281 }
3282 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003283 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003284 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3285 (unsigned long long)sh->sector,
3286 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003287 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003288
3289 if (rcw > disks && rmw > disks &&
3290 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3291 set_bit(STRIPE_DELAYED, &sh->state);
3292
Dan Williamsa4456852007-07-09 11:56:43 -07003293 /* now if nothing is locked, and if we have enough data,
3294 * we can start a write request
3295 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003296 /* since handle_stripe can be called at any time we need to handle the
3297 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003298 * subsequent call wants to start a write request. raid_run_ops only
3299 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003300 * simultaneously. If this is not the case then new writes need to be
3301 * held off until the compute completes.
3302 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003303 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3304 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3305 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003306 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003307}
3308
NeilBrownd1688a62011-10-11 16:49:52 +11003309static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003310 struct stripe_head_state *s, int disks)
3311{
Dan Williamsecc65c92008-06-28 08:31:57 +10003312 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003313
Dan Williamsbd2ab672008-04-10 21:29:27 -07003314 set_bit(STRIPE_HANDLE, &sh->state);
3315
Dan Williamsecc65c92008-06-28 08:31:57 +10003316 switch (sh->check_state) {
3317 case check_state_idle:
3318 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003319 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003320 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003321 sh->check_state = check_state_run;
3322 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003323 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003324 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003325 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003326 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003327 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003328 /* fall through */
3329 case check_state_compute_result:
3330 sh->check_state = check_state_idle;
3331 if (!dev)
3332 dev = &sh->dev[sh->pd_idx];
3333
3334 /* check that a write has not made the stripe insync */
3335 if (test_bit(STRIPE_INSYNC, &sh->state))
3336 break;
3337
3338 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003339 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3340 BUG_ON(s->uptodate != disks);
3341
3342 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003343 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003344 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003345
Dan Williamsa4456852007-07-09 11:56:43 -07003346 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003347 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003348 break;
3349 case check_state_run:
3350 break; /* we will be called again upon completion */
3351 case check_state_check_result:
3352 sh->check_state = check_state_idle;
3353
3354 /* if a failure occurred during the check operation, leave
3355 * STRIPE_INSYNC not set and let the stripe be handled again
3356 */
3357 if (s->failed)
3358 break;
3359
3360 /* handle a successful check operation, if parity is correct
3361 * we are done. Otherwise update the mismatch count and repair
3362 * parity if !MD_RECOVERY_CHECK
3363 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003364 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003365 /* parity is correct (on disc,
3366 * not in buffer any more)
3367 */
3368 set_bit(STRIPE_INSYNC, &sh->state);
3369 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003370 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003371 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3372 /* don't try to repair!! */
3373 set_bit(STRIPE_INSYNC, &sh->state);
3374 else {
3375 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003376 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003377 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3378 set_bit(R5_Wantcompute,
3379 &sh->dev[sh->pd_idx].flags);
3380 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003381 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003382 s->uptodate++;
3383 }
3384 }
3385 break;
3386 case check_state_compute_run:
3387 break;
3388 default:
3389 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3390 __func__, sh->check_state,
3391 (unsigned long long) sh->sector);
3392 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003393 }
3394}
3395
NeilBrownd1688a62011-10-11 16:49:52 +11003396static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003397 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003398 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003399{
Dan Williamsa4456852007-07-09 11:56:43 -07003400 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003401 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003402 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003403
3404 set_bit(STRIPE_HANDLE, &sh->state);
3405
3406 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003407
Dan Williamsa4456852007-07-09 11:56:43 -07003408 /* Want to check and possibly repair P and Q.
3409 * However there could be one 'failed' device, in which
3410 * case we can only check one of them, possibly using the
3411 * other to generate missing data
3412 */
3413
Dan Williamsd82dfee2009-07-14 13:40:57 -07003414 switch (sh->check_state) {
3415 case check_state_idle:
3416 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003417 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003418 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003419 * makes sense to check P (If anything else were failed,
3420 * we would have used P to recreate it).
3421 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003422 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003423 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003424 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003425 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003426 * anything, so it makes sense to check it
3427 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003428 if (sh->check_state == check_state_run)
3429 sh->check_state = check_state_run_pq;
3430 else
3431 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003432 }
Dan Williams36d1c642009-07-14 11:48:22 -07003433
Dan Williamsd82dfee2009-07-14 13:40:57 -07003434 /* discard potentially stale zero_sum_result */
3435 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003436
Dan Williamsd82dfee2009-07-14 13:40:57 -07003437 if (sh->check_state == check_state_run) {
3438 /* async_xor_zero_sum destroys the contents of P */
3439 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3440 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003441 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003442 if (sh->check_state >= check_state_run &&
3443 sh->check_state <= check_state_run_pq) {
3444 /* async_syndrome_zero_sum preserves P and Q, so
3445 * no need to mark them !uptodate here
3446 */
3447 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3448 break;
3449 }
Dan Williams36d1c642009-07-14 11:48:22 -07003450
Dan Williamsd82dfee2009-07-14 13:40:57 -07003451 /* we have 2-disk failure */
3452 BUG_ON(s->failed != 2);
3453 /* fall through */
3454 case check_state_compute_result:
3455 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003456
Dan Williamsd82dfee2009-07-14 13:40:57 -07003457 /* check that a write has not made the stripe insync */
3458 if (test_bit(STRIPE_INSYNC, &sh->state))
3459 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003460
3461 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003462 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003463 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003464 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003465 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003466 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003467 s->locked++;
3468 set_bit(R5_LOCKED, &dev->flags);
3469 set_bit(R5_Wantwrite, &dev->flags);
3470 }
3471 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003472 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003473 s->locked++;
3474 set_bit(R5_LOCKED, &dev->flags);
3475 set_bit(R5_Wantwrite, &dev->flags);
3476 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003477 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003478 dev = &sh->dev[pd_idx];
3479 s->locked++;
3480 set_bit(R5_LOCKED, &dev->flags);
3481 set_bit(R5_Wantwrite, &dev->flags);
3482 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003483 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003484 dev = &sh->dev[qd_idx];
3485 s->locked++;
3486 set_bit(R5_LOCKED, &dev->flags);
3487 set_bit(R5_Wantwrite, &dev->flags);
3488 }
3489 clear_bit(STRIPE_DEGRADED, &sh->state);
3490
3491 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003492 break;
3493 case check_state_run:
3494 case check_state_run_q:
3495 case check_state_run_pq:
3496 break; /* we will be called again upon completion */
3497 case check_state_check_result:
3498 sh->check_state = check_state_idle;
3499
3500 /* handle a successful check operation, if parity is correct
3501 * we are done. Otherwise update the mismatch count and repair
3502 * parity if !MD_RECOVERY_CHECK
3503 */
3504 if (sh->ops.zero_sum_result == 0) {
3505 /* both parities are correct */
3506 if (!s->failed)
3507 set_bit(STRIPE_INSYNC, &sh->state);
3508 else {
3509 /* in contrast to the raid5 case we can validate
3510 * parity, but still have a failure to write
3511 * back
3512 */
3513 sh->check_state = check_state_compute_result;
3514 /* Returning at this point means that we may go
3515 * off and bring p and/or q uptodate again so
3516 * we make sure to check zero_sum_result again
3517 * to verify if p or q need writeback
3518 */
3519 }
3520 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003521 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003522 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3523 /* don't try to repair!! */
3524 set_bit(STRIPE_INSYNC, &sh->state);
3525 else {
3526 int *target = &sh->ops.target;
3527
3528 sh->ops.target = -1;
3529 sh->ops.target2 = -1;
3530 sh->check_state = check_state_compute_run;
3531 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3532 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3533 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3534 set_bit(R5_Wantcompute,
3535 &sh->dev[pd_idx].flags);
3536 *target = pd_idx;
3537 target = &sh->ops.target2;
3538 s->uptodate++;
3539 }
3540 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3541 set_bit(R5_Wantcompute,
3542 &sh->dev[qd_idx].flags);
3543 *target = qd_idx;
3544 s->uptodate++;
3545 }
3546 }
3547 }
3548 break;
3549 case check_state_compute_run:
3550 break;
3551 default:
3552 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3553 __func__, sh->check_state,
3554 (unsigned long long) sh->sector);
3555 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003556 }
3557}
3558
NeilBrownd1688a62011-10-11 16:49:52 +11003559static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003560{
3561 int i;
3562
3563 /* We have read all the blocks in this stripe and now we need to
3564 * copy some of them into a target stripe for expand.
3565 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003566 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003567 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3568 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003569 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003570 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003571 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003572 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003573
NeilBrown784052e2009-03-31 15:19:07 +11003574 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003575 sector_t s = raid5_compute_sector(conf, bn, 0,
3576 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003577 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003578 if (sh2 == NULL)
3579 /* so far only the early blocks of this stripe
3580 * have been requested. When later blocks
3581 * get requested, we will try again
3582 */
3583 continue;
3584 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3585 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3586 /* must have already done this block */
3587 release_stripe(sh2);
3588 continue;
3589 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003590
3591 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003592 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003593 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003594 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003595 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003596
Dan Williamsa4456852007-07-09 11:56:43 -07003597 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3598 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3599 for (j = 0; j < conf->raid_disks; j++)
3600 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003601 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003602 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3603 break;
3604 if (j == conf->raid_disks) {
3605 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3606 set_bit(STRIPE_HANDLE, &sh2->state);
3607 }
3608 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003609
Dan Williamsa4456852007-07-09 11:56:43 -07003610 }
NeilBrowna2e08552007-09-11 15:23:36 -07003611 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003612 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
3615/*
3616 * handle_stripe - do things to a stripe.
3617 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003618 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3619 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003621 * return some read requests which now have data
3622 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 * schedule a read on some buffers
3624 * schedule a write of some buffers
3625 * return confirmation of parity correctness
3626 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 */
Dan Williamsa4456852007-07-09 11:56:43 -07003628
NeilBrownacfe7262011-07-27 11:00:36 +10003629static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003630{
NeilBrownd1688a62011-10-11 16:49:52 +11003631 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003632 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003633 struct r5dev *dev;
3634 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003635 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003636
NeilBrownacfe7262011-07-27 11:00:36 +10003637 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003638
NeilBrownacfe7262011-07-27 11:00:36 +10003639 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3640 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3641 s->failed_num[0] = -1;
3642 s->failed_num[1] = -1;
3643
3644 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003645 rcu_read_lock();
3646 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003647 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003648 sector_t first_bad;
3649 int bad_sectors;
3650 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003651
NeilBrown16a53ec2006-06-26 00:27:38 -07003652 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003653
Dan Williams45b42332007-07-09 11:56:43 -07003654 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003655 i, dev->flags,
3656 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003657 /* maybe we can reply to a read
3658 *
3659 * new wantfill requests are only permitted while
3660 * ops_complete_biofill is guaranteed to be inactive
3661 */
3662 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3663 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3664 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003665
3666 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003667 if (test_bit(R5_LOCKED, &dev->flags))
3668 s->locked++;
3669 if (test_bit(R5_UPTODATE, &dev->flags))
3670 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003671 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003672 s->compute++;
3673 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003674 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003675
NeilBrownacfe7262011-07-27 11:00:36 +10003676 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003677 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003678 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003679 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003680 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003681 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003682 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003683 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003684 }
Dan Williamsa4456852007-07-09 11:56:43 -07003685 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003686 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003687 /* Prefer to use the replacement for reads, but only
3688 * if it is recovered enough and has no bad blocks.
3689 */
3690 rdev = rcu_dereference(conf->disks[i].replacement);
3691 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3692 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3693 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3694 &first_bad, &bad_sectors))
3695 set_bit(R5_ReadRepl, &dev->flags);
3696 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003697 if (rdev)
3698 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003699 rdev = rcu_dereference(conf->disks[i].rdev);
3700 clear_bit(R5_ReadRepl, &dev->flags);
3701 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003702 if (rdev && test_bit(Faulty, &rdev->flags))
3703 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003704 if (rdev) {
3705 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3706 &first_bad, &bad_sectors);
3707 if (s->blocked_rdev == NULL
3708 && (test_bit(Blocked, &rdev->flags)
3709 || is_bad < 0)) {
3710 if (is_bad < 0)
3711 set_bit(BlockedBadBlocks,
3712 &rdev->flags);
3713 s->blocked_rdev = rdev;
3714 atomic_inc(&rdev->nr_pending);
3715 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003716 }
NeilBrown415e72d2010-06-17 17:25:21 +10003717 clear_bit(R5_Insync, &dev->flags);
3718 if (!rdev)
3719 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003720 else if (is_bad) {
3721 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003722 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3723 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003724 /* treat as in-sync, but with a read error
3725 * which we can now try to correct
3726 */
3727 set_bit(R5_Insync, &dev->flags);
3728 set_bit(R5_ReadError, &dev->flags);
3729 }
3730 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003731 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003732 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003733 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003734 set_bit(R5_Insync, &dev->flags);
3735 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3736 test_bit(R5_Expanded, &dev->flags))
3737 /* If we've reshaped into here, we assume it is Insync.
3738 * We will shortly update recovery_offset to make
3739 * it official.
3740 */
3741 set_bit(R5_Insync, &dev->flags);
3742
NeilBrown1cc03eb2014-01-06 13:19:42 +11003743 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003744 /* This flag does not apply to '.replacement'
3745 * only to .rdev, so make sure to check that*/
3746 struct md_rdev *rdev2 = rcu_dereference(
3747 conf->disks[i].rdev);
3748 if (rdev2 == rdev)
3749 clear_bit(R5_Insync, &dev->flags);
3750 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003751 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003752 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003753 } else
3754 clear_bit(R5_WriteError, &dev->flags);
3755 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11003756 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003757 /* This flag does not apply to '.replacement'
3758 * only to .rdev, so make sure to check that*/
3759 struct md_rdev *rdev2 = rcu_dereference(
3760 conf->disks[i].rdev);
3761 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003762 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003763 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003764 } else
3765 clear_bit(R5_MadeGood, &dev->flags);
3766 }
NeilBrown977df362011-12-23 10:17:53 +11003767 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3768 struct md_rdev *rdev2 = rcu_dereference(
3769 conf->disks[i].replacement);
3770 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3771 s->handle_bad_blocks = 1;
3772 atomic_inc(&rdev2->nr_pending);
3773 } else
3774 clear_bit(R5_MadeGoodRepl, &dev->flags);
3775 }
NeilBrown415e72d2010-06-17 17:25:21 +10003776 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003777 /* The ReadError flag will just be confusing now */
3778 clear_bit(R5_ReadError, &dev->flags);
3779 clear_bit(R5_ReWrite, &dev->flags);
3780 }
NeilBrown415e72d2010-06-17 17:25:21 +10003781 if (test_bit(R5_ReadError, &dev->flags))
3782 clear_bit(R5_Insync, &dev->flags);
3783 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003784 if (s->failed < 2)
3785 s->failed_num[s->failed] = i;
3786 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003787 if (rdev && !test_bit(Faulty, &rdev->flags))
3788 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003789 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003790 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003791 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3792 /* If there is a failed device being replaced,
3793 * we must be recovering.
3794 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003795 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003796 * else we can only be replacing
3797 * sync and recovery both need to read all devices, and so
3798 * use the same flag.
3799 */
3800 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003801 sh->sector >= conf->mddev->recovery_cp ||
3802 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003803 s->syncing = 1;
3804 else
3805 s->replacing = 1;
3806 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003807 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003808}
NeilBrownf4168852007-02-28 20:11:53 -08003809
NeilBrowncc940152011-07-26 11:35:35 +10003810static void handle_stripe(struct stripe_head *sh)
3811{
3812 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003813 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003814 int i;
NeilBrown84789552011-07-27 11:00:36 +10003815 int prexor;
3816 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003817 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003818
3819 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003820 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003821 /* already being handled, ensure it gets handled
3822 * again when current action finishes */
3823 set_bit(STRIPE_HANDLE, &sh->state);
3824 return;
3825 }
3826
NeilBrownf8dfcff2013-03-12 12:18:06 +11003827 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3828 spin_lock(&sh->stripe_lock);
3829 /* Cannot process 'sync' concurrently with 'discard' */
3830 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3831 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3832 set_bit(STRIPE_SYNCING, &sh->state);
3833 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003834 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003835 }
3836 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003837 }
3838 clear_bit(STRIPE_DELAYED, &sh->state);
3839
3840 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3841 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3842 (unsigned long long)sh->sector, sh->state,
3843 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3844 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003845
NeilBrownacfe7262011-07-27 11:00:36 +10003846 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003847
NeilBrownbc2607f2011-07-28 11:39:22 +10003848 if (s.handle_bad_blocks) {
3849 set_bit(STRIPE_HANDLE, &sh->state);
3850 goto finish;
3851 }
3852
NeilBrown474af965fe2011-07-27 11:00:36 +10003853 if (unlikely(s.blocked_rdev)) {
3854 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003855 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003856 set_bit(STRIPE_HANDLE, &sh->state);
3857 goto finish;
3858 }
3859 /* There is nothing for the blocked_rdev to block */
3860 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3861 s.blocked_rdev = NULL;
3862 }
3863
3864 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3865 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3866 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3867 }
3868
3869 pr_debug("locked=%d uptodate=%d to_read=%d"
3870 " to_write=%d failed=%d failed_num=%d,%d\n",
3871 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3872 s.failed_num[0], s.failed_num[1]);
3873 /* check if the array has lost more than max_degraded devices and,
3874 * if so, some requests might need to be failed.
3875 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003876 if (s.failed > conf->max_degraded) {
3877 sh->check_state = 0;
3878 sh->reconstruct_state = 0;
3879 if (s.to_read+s.to_write+s.written)
3880 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003881 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003882 handle_failed_sync(conf, sh, &s);
3883 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003884
NeilBrown84789552011-07-27 11:00:36 +10003885 /* Now we check to see if any write operations have recently
3886 * completed
3887 */
3888 prexor = 0;
3889 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3890 prexor = 1;
3891 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3892 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3893 sh->reconstruct_state = reconstruct_state_idle;
3894
3895 /* All the 'written' buffers and the parity block are ready to
3896 * be written back to disk
3897 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003898 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3899 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003900 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003901 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3902 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003903 for (i = disks; i--; ) {
3904 struct r5dev *dev = &sh->dev[i];
3905 if (test_bit(R5_LOCKED, &dev->flags) &&
3906 (i == sh->pd_idx || i == sh->qd_idx ||
3907 dev->written)) {
3908 pr_debug("Writing block %d\n", i);
3909 set_bit(R5_Wantwrite, &dev->flags);
3910 if (prexor)
3911 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10003912 if (s.failed > 1)
3913 continue;
NeilBrown84789552011-07-27 11:00:36 +10003914 if (!test_bit(R5_Insync, &dev->flags) ||
3915 ((i == sh->pd_idx || i == sh->qd_idx) &&
3916 s.failed == 0))
3917 set_bit(STRIPE_INSYNC, &sh->state);
3918 }
3919 }
3920 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3921 s.dec_preread_active = 1;
3922 }
3923
NeilBrownef5b7c62012-11-22 09:13:36 +11003924 /*
3925 * might be able to return some write requests if the parity blocks
3926 * are safe, or on a failed drive
3927 */
3928 pdev = &sh->dev[sh->pd_idx];
3929 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3930 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3931 qdev = &sh->dev[sh->qd_idx];
3932 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3933 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3934 || conf->level < 6;
3935
3936 if (s.written &&
3937 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3938 && !test_bit(R5_LOCKED, &pdev->flags)
3939 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3940 test_bit(R5_Discard, &pdev->flags))))) &&
3941 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3942 && !test_bit(R5_LOCKED, &qdev->flags)
3943 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3944 test_bit(R5_Discard, &qdev->flags))))))
3945 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3946
3947 /* Now we might consider reading some blocks, either to check/generate
3948 * parity, or to satisfy requests
3949 * or to load a block that is being partially written.
3950 */
3951 if (s.to_read || s.non_overwrite
3952 || (conf->level == 6 && s.to_write && s.failed)
3953 || (s.syncing && (s.uptodate + s.compute < disks))
3954 || s.replacing
3955 || s.expanding)
3956 handle_stripe_fill(sh, &s, disks);
3957
NeilBrown84789552011-07-27 11:00:36 +10003958 /* Now to consider new write requests and what else, if anything
3959 * should be read. We do not handle new writes when:
3960 * 1/ A 'write' operation (copy+xor) is already in flight.
3961 * 2/ A 'check' operation is in flight, as it may clobber the parity
3962 * block.
3963 */
3964 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3965 handle_stripe_dirtying(conf, sh, &s, disks);
3966
3967 /* maybe we need to check and possibly fix the parity for this stripe
3968 * Any reads will already have been scheduled, so we just see if enough
3969 * data is available. The parity check is held off while parity
3970 * dependent operations are in flight.
3971 */
3972 if (sh->check_state ||
3973 (s.syncing && s.locked == 0 &&
3974 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3975 !test_bit(STRIPE_INSYNC, &sh->state))) {
3976 if (conf->level == 6)
3977 handle_parity_checks6(conf, sh, &s, disks);
3978 else
3979 handle_parity_checks5(conf, sh, &s, disks);
3980 }
NeilBrownc5a31002011-07-27 11:00:36 +10003981
NeilBrownf94c0b62013-07-22 12:57:21 +10003982 if ((s.replacing || s.syncing) && s.locked == 0
3983 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3984 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003985 /* Write out to replacement devices where possible */
3986 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003987 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3988 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003989 set_bit(R5_WantReplace, &sh->dev[i].flags);
3990 set_bit(R5_LOCKED, &sh->dev[i].flags);
3991 s.locked++;
3992 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003993 if (s.replacing)
3994 set_bit(STRIPE_INSYNC, &sh->state);
3995 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003996 }
3997 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003998 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003999 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004000 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4001 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004002 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4003 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004004 }
4005
4006 /* If the failed drives are just a ReadError, then we might need
4007 * to progress the repair/check process
4008 */
4009 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4010 for (i = 0; i < s.failed; i++) {
4011 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4012 if (test_bit(R5_ReadError, &dev->flags)
4013 && !test_bit(R5_LOCKED, &dev->flags)
4014 && test_bit(R5_UPTODATE, &dev->flags)
4015 ) {
4016 if (!test_bit(R5_ReWrite, &dev->flags)) {
4017 set_bit(R5_Wantwrite, &dev->flags);
4018 set_bit(R5_ReWrite, &dev->flags);
4019 set_bit(R5_LOCKED, &dev->flags);
4020 s.locked++;
4021 } else {
4022 /* let's read it back */
4023 set_bit(R5_Wantread, &dev->flags);
4024 set_bit(R5_LOCKED, &dev->flags);
4025 s.locked++;
4026 }
4027 }
4028 }
4029
NeilBrown3687c062011-07-27 11:00:36 +10004030 /* Finish reconstruct operations initiated by the expansion process */
4031 if (sh->reconstruct_state == reconstruct_state_result) {
4032 struct stripe_head *sh_src
4033 = get_active_stripe(conf, sh->sector, 1, 1, 1);
4034 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4035 /* sh cannot be written until sh_src has been read.
4036 * so arrange for sh to be delayed a little
4037 */
4038 set_bit(STRIPE_DELAYED, &sh->state);
4039 set_bit(STRIPE_HANDLE, &sh->state);
4040 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4041 &sh_src->state))
4042 atomic_inc(&conf->preread_active_stripes);
4043 release_stripe(sh_src);
4044 goto finish;
4045 }
4046 if (sh_src)
4047 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004048
NeilBrown3687c062011-07-27 11:00:36 +10004049 sh->reconstruct_state = reconstruct_state_idle;
4050 clear_bit(STRIPE_EXPANDING, &sh->state);
4051 for (i = conf->raid_disks; i--; ) {
4052 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4053 set_bit(R5_LOCKED, &sh->dev[i].flags);
4054 s.locked++;
4055 }
4056 }
4057
4058 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4059 !sh->reconstruct_state) {
4060 /* Need to write out all blocks after computing parity */
4061 sh->disks = conf->raid_disks;
4062 stripe_set_idx(sh->sector, conf, 0, sh);
4063 schedule_reconstruction(sh, &s, 1, 1);
4064 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4065 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4066 atomic_dec(&conf->reshape_stripes);
4067 wake_up(&conf->wait_for_overlap);
4068 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4069 }
4070
4071 if (s.expanding && s.locked == 0 &&
4072 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4073 handle_stripe_expansion(conf, sh);
4074
4075finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004076 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004077 if (unlikely(s.blocked_rdev)) {
4078 if (conf->mddev->external)
4079 md_wait_for_blocked_rdev(s.blocked_rdev,
4080 conf->mddev);
4081 else
4082 /* Internal metadata will immediately
4083 * be written by raid5d, so we don't
4084 * need to wait here.
4085 */
4086 rdev_dec_pending(s.blocked_rdev,
4087 conf->mddev);
4088 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004089
NeilBrownbc2607f2011-07-28 11:39:22 +10004090 if (s.handle_bad_blocks)
4091 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004092 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004093 struct r5dev *dev = &sh->dev[i];
4094 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4095 /* We own a safe reference to the rdev */
4096 rdev = conf->disks[i].rdev;
4097 if (!rdev_set_badblocks(rdev, sh->sector,
4098 STRIPE_SECTORS, 0))
4099 md_error(conf->mddev, rdev);
4100 rdev_dec_pending(rdev, conf->mddev);
4101 }
NeilBrownb84db562011-07-28 11:39:23 +10004102 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4103 rdev = conf->disks[i].rdev;
4104 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004105 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004106 rdev_dec_pending(rdev, conf->mddev);
4107 }
NeilBrown977df362011-12-23 10:17:53 +11004108 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4109 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004110 if (!rdev)
4111 /* rdev have been moved down */
4112 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004113 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004114 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004115 rdev_dec_pending(rdev, conf->mddev);
4116 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004117 }
4118
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004119 if (s.ops_request)
4120 raid_run_ops(sh, s.ops_request);
4121
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004122 ops_run_io(sh, &s);
4123
NeilBrownc5709ef2011-07-26 11:35:20 +10004124 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004125 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004126 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004127 * have actually been submitted.
4128 */
4129 atomic_dec(&conf->preread_active_stripes);
4130 if (atomic_read(&conf->preread_active_stripes) <
4131 IO_THRESHOLD)
4132 md_wakeup_thread(conf->mddev->thread);
4133 }
4134
NeilBrownc5709ef2011-07-26 11:35:20 +10004135 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07004136
Dan Williams257a4b42011-11-08 16:22:06 +11004137 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004138}
4139
NeilBrownd1688a62011-10-11 16:49:52 +11004140static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
4142 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4143 while (!list_empty(&conf->delayed_list)) {
4144 struct list_head *l = conf->delayed_list.next;
4145 struct stripe_head *sh;
4146 sh = list_entry(l, struct stripe_head, lru);
4147 list_del_init(l);
4148 clear_bit(STRIPE_DELAYED, &sh->state);
4149 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4150 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004151 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004152 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
NeilBrown482c0832011-04-18 18:25:42 +10004154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
Shaohua Li566c09c2013-11-14 15:16:17 +11004157static void activate_bit_delay(struct r5conf *conf,
4158 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004159{
4160 /* device_lock is held */
4161 struct list_head head;
4162 list_add(&head, &conf->bitmap_list);
4163 list_del_init(&conf->bitmap_list);
4164 while (!list_empty(&head)) {
4165 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004166 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004167 list_del_init(&sh->lru);
4168 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004169 hash = sh->hash_lock_index;
4170 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004171 }
4172}
4173
NeilBrown5c675f82014-12-15 12:56:56 +11004174static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004175{
NeilBrownd1688a62011-10-11 16:49:52 +11004176 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004177
4178 /* No difference between reads and writes. Just check
4179 * how busy the stripe_cache is
4180 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004181
NeilBrownf022b2f2006-10-03 01:15:56 -07004182 if (conf->inactive_blocked)
4183 return 1;
4184 if (conf->quiesce)
4185 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004186 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004187 return 1;
4188
4189 return 0;
4190}
4191
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004192/* We want read requests to align with chunks where possible,
4193 * but write requests don't need to.
4194 */
NeilBrown64590f42014-12-15 12:56:57 +11004195static int raid5_mergeable_bvec(struct mddev *mddev,
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004196 struct bvec_merge_data *bvm,
4197 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004198{
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004199 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004200 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004201 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004202 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004203
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004204 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004205 return biovec->bv_len; /* always allow writes to be mergeable */
4206
Andre Noll664e7c42009-06-18 08:45:27 +10004207 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4208 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004209 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
4210 if (max < 0) max = 0;
4211 if (max <= biovec->bv_len && bio_sectors == 0)
4212 return biovec->bv_len;
4213 else
4214 return max;
4215}
4216
NeilBrownfd01b882011-10-11 16:47:53 +11004217static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004218{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004219 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10004220 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004221 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004222
Andre Noll664e7c42009-06-18 08:45:27 +10004223 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4224 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004225 return chunk_sectors >=
4226 ((sector & (chunk_sectors - 1)) + bio_sectors);
4227}
4228
4229/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004230 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4231 * later sampled by raid5d.
4232 */
NeilBrownd1688a62011-10-11 16:49:52 +11004233static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004234{
4235 unsigned long flags;
4236
4237 spin_lock_irqsave(&conf->device_lock, flags);
4238
4239 bi->bi_next = conf->retry_read_aligned_list;
4240 conf->retry_read_aligned_list = bi;
4241
4242 spin_unlock_irqrestore(&conf->device_lock, flags);
4243 md_wakeup_thread(conf->mddev->thread);
4244}
4245
NeilBrownd1688a62011-10-11 16:49:52 +11004246static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004247{
4248 struct bio *bi;
4249
4250 bi = conf->retry_read_aligned;
4251 if (bi) {
4252 conf->retry_read_aligned = NULL;
4253 return bi;
4254 }
4255 bi = conf->retry_read_aligned_list;
4256 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004257 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004258 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004259 /*
4260 * this sets the active strip count to 1 and the processed
4261 * strip count to zero (upper 8 bits)
4262 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004263 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004264 }
4265
4266 return bi;
4267}
4268
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004269/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004270 * The "raid5_align_endio" should check if the read succeeded and if it
4271 * did, call bio_endio on the original bio (having bio_put the new bio
4272 * first).
4273 * If the read failed..
4274 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004275static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004276{
4277 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004278 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004279 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004280 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004281 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004282
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004283 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004284
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004285 rdev = (void*)raid_bi->bi_next;
4286 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004287 mddev = rdev->mddev;
4288 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004289
4290 rdev_dec_pending(rdev, conf->mddev);
4291
4292 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004293 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4294 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004295 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004296 if (atomic_dec_and_test(&conf->active_aligned_reads))
4297 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004298 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004299 }
4300
Dan Williams45b42332007-07-09 11:56:43 -07004301 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004302
4303 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004304}
4305
Neil Brown387bb172007-02-08 14:20:29 -08004306static int bio_fits_rdev(struct bio *bi)
4307{
Jens Axboe165125e2007-07-24 09:28:11 +02004308 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004309
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004310 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004311 return 0;
4312 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004313 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004314 return 0;
4315
4316 if (q->merge_bvec_fn)
4317 /* it's too hard to apply the merge_bvec_fn at this stage,
4318 * just just give up
4319 */
4320 return 0;
4321
4322 return 1;
4323}
4324
NeilBrownfd01b882011-10-11 16:47:53 +11004325static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004326{
NeilBrownd1688a62011-10-11 16:49:52 +11004327 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11004328 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004329 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004330 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004331 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004332
4333 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004334 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004335 return 0;
4336 }
4337 /*
NeilBrowna167f662010-10-26 18:31:13 +11004338 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004339 */
NeilBrowna167f662010-10-26 18:31:13 +11004340 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004341 if (!align_bi)
4342 return 0;
4343 /*
4344 * set bi_end_io to a new function, and set bi_private to the
4345 * original bio.
4346 */
4347 align_bi->bi_end_io = raid5_align_endio;
4348 align_bi->bi_private = raid_bio;
4349 /*
4350 * compute position
4351 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004352 align_bi->bi_iter.bi_sector =
4353 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
4354 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004355
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004356 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004357 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004358 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4359 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4360 rdev->recovery_offset < end_sector) {
4361 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4362 if (rdev &&
4363 (test_bit(Faulty, &rdev->flags) ||
4364 !(test_bit(In_sync, &rdev->flags) ||
4365 rdev->recovery_offset >= end_sector)))
4366 rdev = NULL;
4367 }
4368 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004369 sector_t first_bad;
4370 int bad_sectors;
4371
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004372 atomic_inc(&rdev->nr_pending);
4373 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004374 raid_bio->bi_next = (void*)rdev;
4375 align_bi->bi_bdev = rdev->bdev;
NeilBrown3fd83712014-08-23 20:19:26 +10004376 __clear_bit(BIO_SEG_VALID, &align_bi->bi_flags);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004377
NeilBrown31c176e2011-07-28 11:39:22 +10004378 if (!bio_fits_rdev(align_bi) ||
Kent Overstreet4f024f32013-10-11 15:44:27 -07004379 is_badblock(rdev, align_bi->bi_iter.bi_sector,
4380 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004381 &first_bad, &bad_sectors)) {
4382 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004383 bio_put(align_bi);
4384 rdev_dec_pending(rdev, mddev);
4385 return 0;
4386 }
4387
majianpeng6c0544e2012-06-12 08:31:10 +08004388 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004389 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08004390
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004391 spin_lock_irq(&conf->device_lock);
4392 wait_event_lock_irq(conf->wait_for_stripe,
4393 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004394 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004395 atomic_inc(&conf->active_aligned_reads);
4396 spin_unlock_irq(&conf->device_lock);
4397
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004398 if (mddev->gendisk)
4399 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4400 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07004401 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004402 generic_make_request(align_bi);
4403 return 1;
4404 } else {
4405 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004406 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004407 return 0;
4408 }
4409}
4410
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004411/* __get_priority_stripe - get the next stripe to process
4412 *
4413 * Full stripe writes are allowed to pass preread active stripes up until
4414 * the bypass_threshold is exceeded. In general the bypass_count
4415 * increments when the handle_list is handled before the hold_list; however, it
4416 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4417 * stripe with in flight i/o. The bypass_count will be reset when the
4418 * head of the hold_list has changed, i.e. the head was promoted to the
4419 * handle_list.
4420 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004421static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004422{
Shaohua Li851c30c2013-08-28 14:30:16 +08004423 struct stripe_head *sh = NULL, *tmp;
4424 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004425 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004426
4427 if (conf->worker_cnt_per_group == 0) {
4428 handle_list = &conf->handle_list;
4429 } else if (group != ANY_GROUP) {
4430 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004431 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004432 } else {
4433 int i;
4434 for (i = 0; i < conf->group_cnt; i++) {
4435 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004436 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004437 if (!list_empty(handle_list))
4438 break;
4439 }
4440 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004441
4442 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4443 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004444 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004445 list_empty(&conf->hold_list) ? "empty" : "busy",
4446 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4447
Shaohua Li851c30c2013-08-28 14:30:16 +08004448 if (!list_empty(handle_list)) {
4449 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004450
4451 if (list_empty(&conf->hold_list))
4452 conf->bypass_count = 0;
4453 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4454 if (conf->hold_list.next == conf->last_hold)
4455 conf->bypass_count++;
4456 else {
4457 conf->last_hold = conf->hold_list.next;
4458 conf->bypass_count -= conf->bypass_threshold;
4459 if (conf->bypass_count < 0)
4460 conf->bypass_count = 0;
4461 }
4462 }
4463 } else if (!list_empty(&conf->hold_list) &&
4464 ((conf->bypass_threshold &&
4465 conf->bypass_count > conf->bypass_threshold) ||
4466 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004467
4468 list_for_each_entry(tmp, &conf->hold_list, lru) {
4469 if (conf->worker_cnt_per_group == 0 ||
4470 group == ANY_GROUP ||
4471 !cpu_online(tmp->cpu) ||
4472 cpu_to_group(tmp->cpu) == group) {
4473 sh = tmp;
4474 break;
4475 }
4476 }
4477
4478 if (sh) {
4479 conf->bypass_count -= conf->bypass_threshold;
4480 if (conf->bypass_count < 0)
4481 conf->bypass_count = 0;
4482 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004483 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004484 }
4485
4486 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004487 return NULL;
4488
Shaohua Libfc90cb2013-08-29 15:40:32 +08004489 if (wg) {
4490 wg->stripes_cnt--;
4491 sh->group = NULL;
4492 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004493 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08004494 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004495 return sh;
4496}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004497
Shaohua Li8811b592012-08-02 08:33:00 +10004498struct raid5_plug_cb {
4499 struct blk_plug_cb cb;
4500 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11004501 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10004502};
4503
4504static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4505{
4506 struct raid5_plug_cb *cb = container_of(
4507 blk_cb, struct raid5_plug_cb, cb);
4508 struct stripe_head *sh;
4509 struct mddev *mddev = cb->cb.data;
4510 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004511 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11004512 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10004513
4514 if (cb->list.next && !list_empty(&cb->list)) {
4515 spin_lock_irq(&conf->device_lock);
4516 while (!list_empty(&cb->list)) {
4517 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4518 list_del_init(&sh->lru);
4519 /*
4520 * avoid race release_stripe_plug() sees
4521 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4522 * is still in our list
4523 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01004524 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10004525 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004526 /*
4527 * STRIPE_ON_RELEASE_LIST could be set here. In that
4528 * case, the count is always > 1 here
4529 */
Shaohua Li566c09c2013-11-14 15:16:17 +11004530 hash = sh->hash_lock_index;
4531 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11004532 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004533 }
4534 spin_unlock_irq(&conf->device_lock);
4535 }
Shaohua Li566c09c2013-11-14 15:16:17 +11004536 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4537 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004538 if (mddev->queue)
4539 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004540 kfree(cb);
4541}
4542
4543static void release_stripe_plug(struct mddev *mddev,
4544 struct stripe_head *sh)
4545{
4546 struct blk_plug_cb *blk_cb = blk_check_plugged(
4547 raid5_unplug, mddev,
4548 sizeof(struct raid5_plug_cb));
4549 struct raid5_plug_cb *cb;
4550
4551 if (!blk_cb) {
4552 release_stripe(sh);
4553 return;
4554 }
4555
4556 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4557
Shaohua Li566c09c2013-11-14 15:16:17 +11004558 if (cb->list.next == NULL) {
4559 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10004560 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11004561 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
4562 INIT_LIST_HEAD(cb->temp_inactive_list + i);
4563 }
Shaohua Li8811b592012-08-02 08:33:00 +10004564
4565 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4566 list_add_tail(&sh->lru, &cb->list);
4567 else
4568 release_stripe(sh);
4569}
4570
Shaohua Li620125f2012-10-11 13:49:05 +11004571static void make_discard_request(struct mddev *mddev, struct bio *bi)
4572{
4573 struct r5conf *conf = mddev->private;
4574 sector_t logical_sector, last_sector;
4575 struct stripe_head *sh;
4576 int remaining;
4577 int stripe_sectors;
4578
4579 if (mddev->reshape_position != MaxSector)
4580 /* Skip discard while reshape is happening */
4581 return;
4582
Kent Overstreet4f024f32013-10-11 15:44:27 -07004583 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4584 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11004585
4586 bi->bi_next = NULL;
4587 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4588
4589 stripe_sectors = conf->chunk_sectors *
4590 (conf->raid_disks - conf->max_degraded);
4591 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4592 stripe_sectors);
4593 sector_div(last_sector, stripe_sectors);
4594
4595 logical_sector *= conf->chunk_sectors;
4596 last_sector *= conf->chunk_sectors;
4597
4598 for (; logical_sector < last_sector;
4599 logical_sector += STRIPE_SECTORS) {
4600 DEFINE_WAIT(w);
4601 int d;
4602 again:
4603 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4604 prepare_to_wait(&conf->wait_for_overlap, &w,
4605 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004606 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4607 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4608 release_stripe(sh);
4609 schedule();
4610 goto again;
4611 }
4612 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004613 spin_lock_irq(&sh->stripe_lock);
4614 for (d = 0; d < conf->raid_disks; d++) {
4615 if (d == sh->pd_idx || d == sh->qd_idx)
4616 continue;
4617 if (sh->dev[d].towrite || sh->dev[d].toread) {
4618 set_bit(R5_Overlap, &sh->dev[d].flags);
4619 spin_unlock_irq(&sh->stripe_lock);
4620 release_stripe(sh);
4621 schedule();
4622 goto again;
4623 }
4624 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004625 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004626 finish_wait(&conf->wait_for_overlap, &w);
4627 for (d = 0; d < conf->raid_disks; d++) {
4628 if (d == sh->pd_idx || d == sh->qd_idx)
4629 continue;
4630 sh->dev[d].towrite = bi;
4631 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4632 raid5_inc_bi_active_stripes(bi);
4633 }
4634 spin_unlock_irq(&sh->stripe_lock);
4635 if (conf->mddev->bitmap) {
4636 for (d = 0;
4637 d < conf->raid_disks - conf->max_degraded;
4638 d++)
4639 bitmap_startwrite(mddev->bitmap,
4640 sh->sector,
4641 STRIPE_SECTORS,
4642 0);
4643 sh->bm_seq = conf->seq_flush + 1;
4644 set_bit(STRIPE_BIT_DELAY, &sh->state);
4645 }
4646
4647 set_bit(STRIPE_HANDLE, &sh->state);
4648 clear_bit(STRIPE_DELAYED, &sh->state);
4649 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4650 atomic_inc(&conf->preread_active_stripes);
4651 release_stripe_plug(mddev, sh);
4652 }
4653
4654 remaining = raid5_dec_bi_active_stripes(bi);
4655 if (remaining == 0) {
4656 md_write_end(mddev);
4657 bio_endio(bi, 0);
4658 }
4659}
4660
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004661static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
NeilBrownd1688a62011-10-11 16:49:52 +11004663 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004664 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 sector_t new_sector;
4666 sector_t logical_sector, last_sector;
4667 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004668 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004669 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08004670 DEFINE_WAIT(w);
4671 bool do_prepare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Tejun Heoe9c74692010-09-03 11:56:18 +02004673 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4674 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004675 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004676 }
4677
NeilBrown3d310eb2005-06-21 17:17:26 -07004678 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004679
NeilBrown802ba062006-12-13 00:34:13 -08004680 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004681 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004682 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004683 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004684
Shaohua Li620125f2012-10-11 13:49:05 +11004685 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4686 make_discard_request(mddev, bi);
4687 return;
4688 }
4689
Kent Overstreet4f024f32013-10-11 15:44:27 -07004690 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004691 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 bi->bi_next = NULL;
4693 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004694
Shaohua Li27c0f682014-04-09 11:25:47 +08004695 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004697 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10004698 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08004699
Shaohua Li27c0f682014-04-09 11:25:47 +08004700 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004701 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10004702 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11004703 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08004704 if (do_prepare)
4705 prepare_to_wait(&conf->wait_for_overlap, &w,
4706 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004707 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004708 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004709 * 64bit on a 32bit platform, and so it might be
4710 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004711 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004712 * the lock is dropped, so once we get a reference
4713 * to the stripe that we think it is, we will have
4714 * to check again.
4715 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004716 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004717 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004718 ? logical_sector < conf->reshape_progress
4719 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004720 previous = 1;
4721 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004722 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004723 ? logical_sector < conf->reshape_safe
4724 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004725 spin_unlock_irq(&conf->device_lock);
4726 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004727 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08004728 goto retry;
4729 }
4730 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004731 spin_unlock_irq(&conf->device_lock);
4732 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004733
NeilBrown112bf892009-03-31 14:39:38 +11004734 new_sector = raid5_compute_sector(conf, logical_sector,
4735 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004736 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004737 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10004738 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 (unsigned long long)logical_sector);
4740
NeilBrownb5663ba2009-03-31 14:39:38 +11004741 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004742 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004744 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004745 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004746 * stripe, so we must do the range check again.
4747 * Expansion could still move past after this
4748 * test, but as we are holding a reference to
4749 * 'sh', we know that if that happens,
4750 * STRIPE_EXPANDING will get set and the expansion
4751 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004752 */
4753 int must_retry = 0;
4754 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004755 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004756 ? logical_sector >= conf->reshape_progress
4757 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004758 /* mismatch, need to try again */
4759 must_retry = 1;
4760 spin_unlock_irq(&conf->device_lock);
4761 if (must_retry) {
4762 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004763 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004764 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004765 goto retry;
4766 }
4767 }
NeilBrownc46501b2013-08-27 15:52:13 +10004768 if (read_seqcount_retry(&conf->gen_lock, seq)) {
4769 /* Might have got the wrong stripe_head
4770 * by accident
4771 */
4772 release_stripe(sh);
4773 goto retry;
4774 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004775
Namhyung Kimffd96e32011-07-18 17:38:51 +10004776 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004777 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004778 logical_sector < mddev->suspend_hi) {
4779 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004780 /* As the suspend_* range is controlled by
4781 * userspace, we want an interruptible
4782 * wait.
4783 */
4784 flush_signals(current);
4785 prepare_to_wait(&conf->wait_for_overlap,
4786 &w, TASK_INTERRUPTIBLE);
4787 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08004788 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10004789 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004790 do_prepare = true;
4791 }
NeilBrowne464eaf2006-03-27 01:18:14 -08004792 goto retry;
4793 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004794
4795 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004796 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004797 /* Stripe is busy expanding or
4798 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 * and wait a while
4800 */
NeilBrown482c0832011-04-18 18:25:42 +10004801 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 release_stripe(sh);
4803 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004804 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 goto retry;
4806 }
NeilBrown6ed30032008-02-06 01:40:00 -08004807 set_bit(STRIPE_HANDLE, &sh->state);
4808 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004809 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004810 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4811 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004812 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 } else {
4814 /* cannot get stripe for read-ahead, just give-up */
4815 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 break;
4817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
Shaohua Li27c0f682014-04-09 11:25:47 +08004819 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10004820
Shaohua Lie7836bd62012-07-19 16:01:31 +10004821 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004822 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
NeilBrown16a53ec2006-06-26 00:27:38 -07004824 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004826
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004827 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4828 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004829 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831}
4832
NeilBrownfd01b882011-10-11 16:47:53 +11004833static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004834
NeilBrownfd01b882011-10-11 16:47:53 +11004835static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
NeilBrown52c03292006-06-26 00:27:43 -07004837 /* reshaping is quite different to recovery/resync so it is
4838 * handled quite separately ... here.
4839 *
4840 * On each call to sync_request, we gather one chunk worth of
4841 * destination stripes and flag them as expanding.
4842 * Then we find all the source stripes and request reads.
4843 * As the reads complete, handle_stripe will copy the data
4844 * into the destination stripe and release that stripe.
4845 */
NeilBrownd1688a62011-10-11 16:49:52 +11004846 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004848 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004849 int raid_disks = conf->previous_raid_disks;
4850 int data_disks = raid_disks - conf->max_degraded;
4851 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004852 int i;
4853 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004854 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004855 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004856 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004857 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004858
NeilBrownfef9c612009-03-31 15:16:46 +11004859 if (sector_nr == 0) {
4860 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004861 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004862 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4863 sector_nr = raid5_size(mddev, 0, 0)
4864 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004865 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004866 conf->reshape_progress > 0)
4867 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004868 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004869 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004870 mddev->curr_resync_completed = sector_nr;
4871 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004872 *skipped = 1;
4873 return sector_nr;
4874 }
NeilBrown52c03292006-06-26 00:27:43 -07004875 }
4876
NeilBrown7a661382009-03-31 15:21:40 +11004877 /* We need to process a full chunk at a time.
4878 * If old and new chunk sizes differ, we need to process the
4879 * largest of these
4880 */
Andre Noll664e7c42009-06-18 08:45:27 +10004881 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4882 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004883 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004884 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004885
NeilBrownb5254dd2012-05-21 09:27:01 +10004886 /* We update the metadata at least every 10 seconds, or when
4887 * the data about to be copied would over-write the source of
4888 * the data at the front of the range. i.e. one new_stripe
4889 * along from reshape_progress new_maps to after where
4890 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004891 */
NeilBrownfef9c612009-03-31 15:16:46 +11004892 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004893 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004894 readpos = conf->reshape_progress;
4895 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004896 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004897 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004898 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004899 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004900 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004901 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004902 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004903 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004904 readpos -= min_t(sector_t, reshape_sectors, readpos);
4905 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004906 }
NeilBrown52c03292006-06-26 00:27:43 -07004907
NeilBrownb5254dd2012-05-21 09:27:01 +10004908 /* Having calculated the 'writepos' possibly use it
4909 * to set 'stripe_addr' which is where we will write to.
4910 */
4911 if (mddev->reshape_backwards) {
4912 BUG_ON(conf->reshape_progress == 0);
4913 stripe_addr = writepos;
4914 BUG_ON((mddev->dev_sectors &
4915 ~((sector_t)reshape_sectors - 1))
4916 - reshape_sectors - stripe_addr
4917 != sector_nr);
4918 } else {
4919 BUG_ON(writepos != sector_nr + reshape_sectors);
4920 stripe_addr = sector_nr;
4921 }
4922
NeilBrownc8f517c2009-03-31 15:28:40 +11004923 /* 'writepos' is the most advanced device address we might write.
4924 * 'readpos' is the least advanced device address we might read.
4925 * 'safepos' is the least address recorded in the metadata as having
4926 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004927 * If there is a min_offset_diff, these are adjusted either by
4928 * increasing the safepos/readpos if diff is negative, or
4929 * increasing writepos if diff is positive.
4930 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004931 * ensure safety in the face of a crash - that must be done by userspace
4932 * making a backup of the data. So in that case there is no particular
4933 * rush to update metadata.
4934 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4935 * update the metadata to advance 'safepos' to match 'readpos' so that
4936 * we can be safe in the event of a crash.
4937 * So we insist on updating metadata if safepos is behind writepos and
4938 * readpos is beyond writepos.
4939 * In any case, update the metadata every 10 seconds.
4940 * Maybe that number should be configurable, but I'm not sure it is
4941 * worth it.... maybe it could be a multiple of safemode_delay???
4942 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004943 if (conf->min_offset_diff < 0) {
4944 safepos += -conf->min_offset_diff;
4945 readpos += -conf->min_offset_diff;
4946 } else
4947 writepos += conf->min_offset_diff;
4948
NeilBrown2c810cd2012-05-21 09:27:00 +10004949 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004950 ? (safepos > writepos && readpos < writepos)
4951 : (safepos < writepos && readpos > writepos)) ||
4952 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004953 /* Cannot proceed until we've updated the superblock... */
4954 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11004955 atomic_read(&conf->reshape_stripes)==0
4956 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4957 if (atomic_read(&conf->reshape_stripes) != 0)
4958 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11004959 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004960 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004961 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004962 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004963 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004964 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004965 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4966 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4967 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07004968 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004969 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004970 spin_unlock_irq(&conf->device_lock);
4971 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004972 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004973 }
4974
NeilBrownab69ae12009-03-31 15:26:47 +11004975 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004976 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004977 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004978 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004979 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004980 set_bit(STRIPE_EXPANDING, &sh->state);
4981 atomic_inc(&conf->reshape_stripes);
4982 /* If any of this stripe is beyond the end of the old
4983 * array, then we need to zero those blocks
4984 */
4985 for (j=sh->disks; j--;) {
4986 sector_t s;
4987 if (j == sh->pd_idx)
4988 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004989 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004990 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004991 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004992 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004993 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004994 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004995 continue;
4996 }
4997 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4998 set_bit(R5_Expanded, &sh->dev[j].flags);
4999 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5000 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005001 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005002 set_bit(STRIPE_EXPAND_READY, &sh->state);
5003 set_bit(STRIPE_HANDLE, &sh->state);
5004 }
NeilBrownab69ae12009-03-31 15:26:47 +11005005 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005006 }
5007 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005008 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005009 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005010 else
NeilBrown7a661382009-03-31 15:21:40 +11005011 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005012 spin_unlock_irq(&conf->device_lock);
5013 /* Ok, those stripe are ready. We can start scheduling
5014 * reads on the source stripes.
5015 * The source stripes are determined by mapping the first and last
5016 * block on the destination stripes.
5017 */
NeilBrown52c03292006-06-26 00:27:43 -07005018 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005019 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005020 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005021 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005022 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005023 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005024 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005025 if (last_sector >= mddev->dev_sectors)
5026 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005027 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005028 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005029 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5030 set_bit(STRIPE_HANDLE, &sh->state);
5031 release_stripe(sh);
5032 first_sector += STRIPE_SECTORS;
5033 }
NeilBrownab69ae12009-03-31 15:26:47 +11005034 /* Now that the sources are clearly marked, we can release
5035 * the destination stripes
5036 */
5037 while (!list_empty(&stripes)) {
5038 sh = list_entry(stripes.next, struct stripe_head, lru);
5039 list_del_init(&sh->lru);
5040 release_stripe(sh);
5041 }
NeilBrownc6207272008-02-06 01:39:52 -08005042 /* If this takes us to the resync_max point where we have to pause,
5043 * then we need to write out the superblock.
5044 */
NeilBrown7a661382009-03-31 15:21:40 +11005045 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10005046 if ((sector_nr - mddev->curr_resync_completed) * 2
5047 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005048 /* Cannot proceed until we've updated the superblock... */
5049 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005050 atomic_read(&conf->reshape_stripes) == 0
5051 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5052 if (atomic_read(&conf->reshape_stripes) != 0)
5053 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005054 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005055 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005056 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08005057 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5058 md_wakeup_thread(mddev->thread);
5059 wait_event(mddev->sb_wait,
5060 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005061 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5062 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5063 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005064 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005065 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005066 spin_unlock_irq(&conf->device_lock);
5067 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005068 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005069 }
NeilBrownc91abf52013-11-19 12:02:01 +11005070ret:
NeilBrown7a661382009-03-31 15:21:40 +11005071 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07005072}
5073
NeilBrown09314792015-02-19 16:04:40 +11005074static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005075{
NeilBrownd1688a62011-10-11 16:49:52 +11005076 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005077 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005078 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005079 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005080 int still_degraded = 0;
5081 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
NeilBrown72626682005-09-09 16:23:54 -07005083 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005085
NeilBrown29269552006-03-27 01:18:10 -08005086 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5087 end_reshape(conf);
5088 return 0;
5089 }
NeilBrown72626682005-09-09 16:23:54 -07005090
5091 if (mddev->curr_resync < max_sector) /* aborted */
5092 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5093 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005094 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005095 conf->fullsync = 0;
5096 bitmap_close_sync(mddev->bitmap);
5097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 return 0;
5099 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005100
NeilBrown64bd6602009-08-03 10:59:58 +10005101 /* Allow raid5_quiesce to complete */
5102 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5103
NeilBrown52c03292006-06-26 00:27:43 -07005104 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5105 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005106
NeilBrownc6207272008-02-06 01:39:52 -08005107 /* No need to check resync_max as we never do more than one
5108 * stripe, and as resync_max will always be on a chunk boundary,
5109 * if the check in md_do_sync didn't fire, there is no chance
5110 * of overstepping resync_max here
5111 */
5112
NeilBrown16a53ec2006-06-26 00:27:38 -07005113 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 * to resync, then assert that we are finished, because there is
5115 * nothing we can do.
5116 */
NeilBrown3285edf2006-06-26 00:27:55 -07005117 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005118 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005119 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005120 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return rv;
5122 }
majianpeng6f608042013-04-24 11:42:41 +10005123 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5124 !conf->fullsync &&
5125 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5126 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005127 /* we can skip this block, and probably more */
5128 sync_blocks /= STRIPE_SECTORS;
5129 *skipped = 1;
5130 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
NeilBrownb47490c2008-02-06 01:39:50 -08005133 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
5134
NeilBrowna8c906c2009-06-09 14:39:59 +10005135 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005137 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005139 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005141 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005143 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005144 * Note in case of > 1 drive failures it's possible we're rebuilding
5145 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005146 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005147 rcu_read_lock();
5148 for (i = 0; i < conf->raid_disks; i++) {
5149 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5150
5151 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005152 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005153 }
5154 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005155
5156 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5157
NeilBrown83206d62011-07-26 11:19:49 +10005158 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005159 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 release_stripe(sh);
5162
5163 return STRIPE_SECTORS;
5164}
5165
NeilBrownd1688a62011-10-11 16:49:52 +11005166static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005167{
5168 /* We may not be able to submit a whole bio at once as there
5169 * may not be enough stripe_heads available.
5170 * We cannot pre-allocate enough stripe_heads as we may need
5171 * more than exist in the cache (if we allow ever large chunks).
5172 * So we do one stripe head at a time and record in
5173 * ->bi_hw_segments how many have been done.
5174 *
5175 * We *know* that this entire raid_bio is in one chunk, so
5176 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5177 */
5178 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005179 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005180 sector_t sector, logical_sector, last_sector;
5181 int scnt = 0;
5182 int remaining;
5183 int handled = 0;
5184
Kent Overstreet4f024f32013-10-11 15:44:27 -07005185 logical_sector = raid_bio->bi_iter.bi_sector &
5186 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005187 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005188 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005189 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005190
5191 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005192 logical_sector += STRIPE_SECTORS,
5193 sector += STRIPE_SECTORS,
5194 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005195
Shaohua Lie7836bd62012-07-19 16:01:31 +10005196 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005197 /* already done this stripe */
5198 continue;
5199
hui jiao2844dc32014-06-05 11:34:24 +08005200 sh = get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005201
5202 if (!sh) {
5203 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005204 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005205 conf->retry_read_aligned = raid_bio;
5206 return handled;
5207 }
5208
Neil Brown387bb172007-02-08 14:20:29 -08005209 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
5210 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005211 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005212 conf->retry_read_aligned = raid_bio;
5213 return handled;
5214 }
5215
majianpeng3f9e7c12012-07-31 10:04:21 +10005216 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005217 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005218 release_stripe(sh);
5219 handled++;
5220 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005221 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005222 if (remaining == 0) {
5223 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5224 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10005225 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005226 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005227 if (atomic_dec_and_test(&conf->active_aligned_reads))
5228 wake_up(&conf->wait_for_stripe);
5229 return handled;
5230}
5231
Shaohua Libfc90cb2013-08-29 15:40:32 +08005232static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005233 struct r5worker *worker,
5234 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005235{
5236 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005237 int i, batch_size = 0, hash;
5238 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005239
5240 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005241 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005242 batch[batch_size++] = sh;
5243
Shaohua Li566c09c2013-11-14 15:16:17 +11005244 if (batch_size == 0) {
5245 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5246 if (!list_empty(temp_inactive_list + i))
5247 break;
5248 if (i == NR_STRIPE_HASH_LOCKS)
5249 return batch_size;
5250 release_inactive = true;
5251 }
Shaohua Li46a06402012-08-02 08:33:15 +10005252 spin_unlock_irq(&conf->device_lock);
5253
Shaohua Li566c09c2013-11-14 15:16:17 +11005254 release_inactive_stripe_list(conf, temp_inactive_list,
5255 NR_STRIPE_HASH_LOCKS);
5256
5257 if (release_inactive) {
5258 spin_lock_irq(&conf->device_lock);
5259 return 0;
5260 }
5261
Shaohua Li46a06402012-08-02 08:33:15 +10005262 for (i = 0; i < batch_size; i++)
5263 handle_stripe(batch[i]);
5264
5265 cond_resched();
5266
5267 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005268 for (i = 0; i < batch_size; i++) {
5269 hash = batch[i]->hash_lock_index;
5270 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5271 }
Shaohua Li46a06402012-08-02 08:33:15 +10005272 return batch_size;
5273}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005274
Shaohua Li851c30c2013-08-28 14:30:16 +08005275static void raid5_do_work(struct work_struct *work)
5276{
5277 struct r5worker *worker = container_of(work, struct r5worker, work);
5278 struct r5worker_group *group = worker->group;
5279 struct r5conf *conf = group->conf;
5280 int group_id = group - conf->worker_groups;
5281 int handled;
5282 struct blk_plug plug;
5283
5284 pr_debug("+++ raid5worker active\n");
5285
5286 blk_start_plug(&plug);
5287 handled = 0;
5288 spin_lock_irq(&conf->device_lock);
5289 while (1) {
5290 int batch_size, released;
5291
Shaohua Li566c09c2013-11-14 15:16:17 +11005292 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005293
Shaohua Li566c09c2013-11-14 15:16:17 +11005294 batch_size = handle_active_stripes(conf, group_id, worker,
5295 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005296 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005297 if (!batch_size && !released)
5298 break;
5299 handled += batch_size;
5300 }
5301 pr_debug("%d stripes handled\n", handled);
5302
5303 spin_unlock_irq(&conf->device_lock);
5304 blk_finish_plug(&plug);
5305
5306 pr_debug("--- raid5worker inactive\n");
5307}
5308
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309/*
5310 * This is our raid5 kernel thread.
5311 *
5312 * We scan the hash table for stripes which can be handled now.
5313 * During the scan, completed stripes are saved for us by the interrupt
5314 * handler, so that they will not have to wait for our next wakeup.
5315 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005316static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317{
Shaohua Li4ed87312012-10-11 13:34:00 +11005318 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005319 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005321 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
Dan Williams45b42332007-07-09 11:56:43 -07005323 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
5325 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005327 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 handled = 0;
5329 spin_lock_irq(&conf->device_lock);
5330 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005331 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005332 int batch_size, released;
5333
Shaohua Li566c09c2013-11-14 15:16:17 +11005334 released = release_stripe_list(conf, conf->temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
NeilBrown0021b7b2012-07-31 09:08:14 +02005336 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005337 !list_empty(&conf->bitmap_list)) {
5338 /* Now is a good time to flush some bitmap updates */
5339 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005340 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005341 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005342 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005343 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005344 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005345 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005346 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005347
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005348 while ((bio = remove_bio_from_retry(conf))) {
5349 int ok;
5350 spin_unlock_irq(&conf->device_lock);
5351 ok = retry_aligned_read(conf, bio);
5352 spin_lock_irq(&conf->device_lock);
5353 if (!ok)
5354 break;
5355 handled++;
5356 }
5357
Shaohua Li566c09c2013-11-14 15:16:17 +11005358 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5359 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005360 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005362 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Shaohua Li46a06402012-08-02 08:33:15 +10005364 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5365 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005366 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005367 spin_lock_irq(&conf->device_lock);
5368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
Dan Williams45b42332007-07-09 11:56:43 -07005370 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
5372 spin_unlock_irq(&conf->device_lock);
5373
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005374 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005375 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
Dan Williams45b42332007-07-09 11:56:43 -07005377 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
NeilBrown3f294f42005-11-08 21:39:25 -08005380static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005381raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005382{
NeilBrown7b1485b2014-12-15 12:56:59 +11005383 struct r5conf *conf;
5384 int ret = 0;
5385 spin_lock(&mddev->lock);
5386 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005387 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11005388 ret = sprintf(page, "%d\n", conf->max_nr_stripes);
5389 spin_unlock(&mddev->lock);
5390 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08005391}
5392
NeilBrownc41d4ac2010-06-01 19:37:24 +10005393int
NeilBrownfd01b882011-10-11 16:47:53 +11005394raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005395{
NeilBrownd1688a62011-10-11 16:49:52 +11005396 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005397 int err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005398 int hash;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005399
5400 if (size <= 16 || size > 32768)
5401 return -EINVAL;
Shaohua Li566c09c2013-11-14 15:16:17 +11005402 hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005403 while (size < conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005404 if (drop_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005405 conf->max_nr_stripes--;
5406 else
5407 break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005408 hash--;
5409 if (hash < 0)
5410 hash = NR_STRIPE_HASH_LOCKS - 1;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005411 }
5412 err = md_allow_write(mddev);
5413 if (err)
5414 return err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005415 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005416 while (size > conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005417 if (grow_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005418 conf->max_nr_stripes++;
5419 else break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005420 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005421 }
5422 return 0;
5423}
5424EXPORT_SYMBOL(raid5_set_cache_size);
5425
NeilBrown3f294f42005-11-08 21:39:25 -08005426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005427raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005428{
NeilBrown67918752014-12-15 12:57:01 +11005429 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07005430 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005431 int err;
5432
NeilBrown3f294f42005-11-08 21:39:25 -08005433 if (len >= PAGE_SIZE)
5434 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005435 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005436 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005437 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005438 if (err)
5439 return err;
NeilBrown67918752014-12-15 12:57:01 +11005440 conf = mddev->private;
5441 if (!conf)
5442 err = -ENODEV;
5443 else
5444 err = raid5_set_cache_size(mddev, new);
5445 mddev_unlock(mddev);
5446
5447 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08005448}
NeilBrown007583c2005-11-08 21:39:30 -08005449
NeilBrown96de1e62005-11-08 21:39:39 -08005450static struct md_sysfs_entry
5451raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5452 raid5_show_stripe_cache_size,
5453 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005454
5455static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005456raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005457{
NeilBrown7b1485b2014-12-15 12:56:59 +11005458 struct r5conf *conf;
5459 int ret = 0;
5460 spin_lock(&mddev->lock);
5461 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005462 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11005463 ret = sprintf(page, "%d\n", conf->bypass_threshold);
5464 spin_unlock(&mddev->lock);
5465 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005466}
5467
5468static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005469raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005470{
NeilBrown67918752014-12-15 12:57:01 +11005471 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07005472 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11005473 int err;
5474
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005475 if (len >= PAGE_SIZE)
5476 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005477 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005478 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005479
5480 err = mddev_lock(mddev);
5481 if (err)
5482 return err;
5483 conf = mddev->private;
5484 if (!conf)
5485 err = -ENODEV;
5486 else if (new > conf->max_nr_stripes)
5487 err = -EINVAL;
5488 else
5489 conf->bypass_threshold = new;
5490 mddev_unlock(mddev);
5491 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005492}
5493
5494static struct md_sysfs_entry
5495raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5496 S_IRUGO | S_IWUSR,
5497 raid5_show_preread_threshold,
5498 raid5_store_preread_threshold);
5499
5500static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08005501raid5_show_skip_copy(struct mddev *mddev, char *page)
5502{
NeilBrown7b1485b2014-12-15 12:56:59 +11005503 struct r5conf *conf;
5504 int ret = 0;
5505 spin_lock(&mddev->lock);
5506 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08005507 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11005508 ret = sprintf(page, "%d\n", conf->skip_copy);
5509 spin_unlock(&mddev->lock);
5510 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08005511}
5512
5513static ssize_t
5514raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
5515{
NeilBrown67918752014-12-15 12:57:01 +11005516 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08005517 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11005518 int err;
5519
Shaohua Lid592a992014-05-21 17:57:44 +08005520 if (len >= PAGE_SIZE)
5521 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08005522 if (kstrtoul(page, 10, &new))
5523 return -EINVAL;
5524 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08005525
NeilBrown67918752014-12-15 12:57:01 +11005526 err = mddev_lock(mddev);
5527 if (err)
5528 return err;
5529 conf = mddev->private;
5530 if (!conf)
5531 err = -ENODEV;
5532 else if (new != conf->skip_copy) {
5533 mddev_suspend(mddev);
5534 conf->skip_copy = new;
5535 if (new)
5536 mddev->queue->backing_dev_info.capabilities |=
5537 BDI_CAP_STABLE_WRITES;
5538 else
5539 mddev->queue->backing_dev_info.capabilities &=
5540 ~BDI_CAP_STABLE_WRITES;
5541 mddev_resume(mddev);
5542 }
5543 mddev_unlock(mddev);
5544 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08005545}
5546
5547static struct md_sysfs_entry
5548raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
5549 raid5_show_skip_copy,
5550 raid5_store_skip_copy);
5551
Shaohua Lid592a992014-05-21 17:57:44 +08005552static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005553stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005554{
NeilBrownd1688a62011-10-11 16:49:52 +11005555 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005556 if (conf)
5557 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5558 else
5559 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005560}
5561
NeilBrown96de1e62005-11-08 21:39:39 -08005562static struct md_sysfs_entry
5563raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005564
Shaohua Lib7214202013-08-27 17:50:42 +08005565static ssize_t
5566raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5567{
NeilBrown7b1485b2014-12-15 12:56:59 +11005568 struct r5conf *conf;
5569 int ret = 0;
5570 spin_lock(&mddev->lock);
5571 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08005572 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11005573 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
5574 spin_unlock(&mddev->lock);
5575 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08005576}
5577
majianpeng60aaf932013-11-14 15:16:20 +11005578static int alloc_thread_groups(struct r5conf *conf, int cnt,
5579 int *group_cnt,
5580 int *worker_cnt_per_group,
5581 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08005582static ssize_t
5583raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5584{
NeilBrown67918752014-12-15 12:57:01 +11005585 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08005586 unsigned long new;
5587 int err;
majianpeng60aaf932013-11-14 15:16:20 +11005588 struct r5worker_group *new_groups, *old_groups;
5589 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08005590
5591 if (len >= PAGE_SIZE)
5592 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08005593 if (kstrtoul(page, 10, &new))
5594 return -EINVAL;
5595
NeilBrown67918752014-12-15 12:57:01 +11005596 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08005597 if (err)
5598 return err;
NeilBrown67918752014-12-15 12:57:01 +11005599 conf = mddev->private;
5600 if (!conf)
5601 err = -ENODEV;
5602 else if (new != conf->worker_cnt_per_group) {
5603 mddev_suspend(mddev);
5604
5605 old_groups = conf->worker_groups;
5606 if (old_groups)
5607 flush_workqueue(raid5_wq);
5608
5609 err = alloc_thread_groups(conf, new,
5610 &group_cnt, &worker_cnt_per_group,
5611 &new_groups);
5612 if (!err) {
5613 spin_lock_irq(&conf->device_lock);
5614 conf->group_cnt = group_cnt;
5615 conf->worker_cnt_per_group = worker_cnt_per_group;
5616 conf->worker_groups = new_groups;
5617 spin_unlock_irq(&conf->device_lock);
5618
5619 if (old_groups)
5620 kfree(old_groups[0].workers);
5621 kfree(old_groups);
5622 }
5623 mddev_resume(mddev);
5624 }
5625 mddev_unlock(mddev);
5626
5627 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08005628}
5629
5630static struct md_sysfs_entry
5631raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5632 raid5_show_group_thread_cnt,
5633 raid5_store_group_thread_cnt);
5634
NeilBrown007583c2005-11-08 21:39:30 -08005635static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005636 &raid5_stripecache_size.attr,
5637 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005638 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08005639 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08005640 &raid5_skip_copy.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005641 NULL,
5642};
NeilBrown007583c2005-11-08 21:39:30 -08005643static struct attribute_group raid5_attrs_group = {
5644 .name = NULL,
5645 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005646};
5647
majianpeng60aaf932013-11-14 15:16:20 +11005648static int alloc_thread_groups(struct r5conf *conf, int cnt,
5649 int *group_cnt,
5650 int *worker_cnt_per_group,
5651 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08005652{
Shaohua Li566c09c2013-11-14 15:16:17 +11005653 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08005654 ssize_t size;
5655 struct r5worker *workers;
5656
majianpeng60aaf932013-11-14 15:16:20 +11005657 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08005658 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11005659 *group_cnt = 0;
5660 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005661 return 0;
5662 }
majianpeng60aaf932013-11-14 15:16:20 +11005663 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08005664 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11005665 workers = kzalloc(size * *group_cnt, GFP_NOIO);
5666 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
5667 *group_cnt, GFP_NOIO);
5668 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005669 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11005670 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08005671 return -ENOMEM;
5672 }
5673
majianpeng60aaf932013-11-14 15:16:20 +11005674 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005675 struct r5worker_group *group;
5676
NeilBrown0c775d52013-11-25 11:12:43 +11005677 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005678 INIT_LIST_HEAD(&group->handle_list);
5679 group->conf = conf;
5680 group->workers = workers + i * cnt;
5681
5682 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005683 struct r5worker *worker = group->workers + j;
5684 worker->group = group;
5685 INIT_WORK(&worker->work, raid5_do_work);
5686
5687 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
5688 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08005689 }
5690 }
5691
5692 return 0;
5693}
5694
5695static void free_thread_groups(struct r5conf *conf)
5696{
5697 if (conf->worker_groups)
5698 kfree(conf->worker_groups[0].workers);
5699 kfree(conf->worker_groups);
5700 conf->worker_groups = NULL;
5701}
5702
Dan Williams80c3a6c2009-03-17 18:10:40 -07005703static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005704raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005705{
NeilBrownd1688a62011-10-11 16:49:52 +11005706 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005707
5708 if (!sectors)
5709 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005710 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005711 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005712 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005713
Andre Noll9d8f0362009-06-18 08:45:01 +10005714 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005715 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005716 return sectors * (raid_disks - conf->max_degraded);
5717}
5718
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305719static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5720{
5721 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11005722 if (percpu->scribble)
5723 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305724 percpu->spare_page = NULL;
5725 percpu->scribble = NULL;
5726}
5727
5728static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5729{
5730 if (conf->level == 6 && !percpu->spare_page)
5731 percpu->spare_page = alloc_page(GFP_KERNEL);
5732 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11005733 percpu->scribble = scribble_alloc(max(conf->raid_disks,
5734 conf->previous_raid_disks), conf->chunk_sectors /
5735 STRIPE_SECTORS, GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305736
5737 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
5738 free_scratch_buffer(conf, percpu);
5739 return -ENOMEM;
5740 }
5741
5742 return 0;
5743}
5744
NeilBrownd1688a62011-10-11 16:49:52 +11005745static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005746{
Dan Williams36d1c642009-07-14 11:48:22 -07005747 unsigned long cpu;
5748
5749 if (!conf->percpu)
5750 return;
5751
Dan Williams36d1c642009-07-14 11:48:22 -07005752#ifdef CONFIG_HOTPLUG_CPU
5753 unregister_cpu_notifier(&conf->cpu_notify);
5754#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305755
5756 get_online_cpus();
5757 for_each_possible_cpu(cpu)
5758 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005759 put_online_cpus();
5760
5761 free_percpu(conf->percpu);
5762}
5763
NeilBrownd1688a62011-10-11 16:49:52 +11005764static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005765{
Shaohua Li851c30c2013-08-28 14:30:16 +08005766 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005767 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005768 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005769 kfree(conf->disks);
5770 kfree(conf->stripe_hashtbl);
5771 kfree(conf);
5772}
5773
Dan Williams36d1c642009-07-14 11:48:22 -07005774#ifdef CONFIG_HOTPLUG_CPU
5775static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5776 void *hcpu)
5777{
NeilBrownd1688a62011-10-11 16:49:52 +11005778 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005779 long cpu = (long)hcpu;
5780 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5781
5782 switch (action) {
5783 case CPU_UP_PREPARE:
5784 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305785 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07005786 pr_err("%s: failed memory allocation for cpu%ld\n",
5787 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005788 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005789 }
5790 break;
5791 case CPU_DEAD:
5792 case CPU_DEAD_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305793 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005794 break;
5795 default:
5796 break;
5797 }
5798 return NOTIFY_OK;
5799}
5800#endif
5801
NeilBrownd1688a62011-10-11 16:49:52 +11005802static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005803{
5804 unsigned long cpu;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305805 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07005806
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305807 conf->percpu = alloc_percpu(struct raid5_percpu);
5808 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07005809 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07005810
Dan Williams36d1c642009-07-14 11:48:22 -07005811#ifdef CONFIG_HOTPLUG_CPU
5812 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5813 conf->cpu_notify.priority = 0;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305814 err = register_cpu_notifier(&conf->cpu_notify);
5815 if (err)
5816 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07005817#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305818
5819 get_online_cpus();
5820 for_each_present_cpu(cpu) {
5821 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5822 if (err) {
5823 pr_err("%s: failed memory allocation for cpu%ld\n",
5824 __func__, cpu);
5825 break;
5826 }
5827 }
Dan Williams36d1c642009-07-14 11:48:22 -07005828 put_online_cpus();
5829
5830 return err;
5831}
5832
NeilBrownd1688a62011-10-11 16:49:52 +11005833static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
NeilBrownd1688a62011-10-11 16:49:52 +11005835 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005836 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005837 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005839 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11005840 int i;
majianpeng60aaf932013-11-14 15:16:20 +11005841 int group_cnt, worker_cnt_per_group;
5842 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrown91adb562009-03-31 14:39:39 +11005844 if (mddev->new_level != 5
5845 && mddev->new_level != 4
5846 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005847 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005848 mdname(mddev), mddev->new_level);
5849 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 }
NeilBrown91adb562009-03-31 14:39:39 +11005851 if ((mddev->new_level == 5
5852 && !algorithm_valid_raid5(mddev->new_layout)) ||
5853 (mddev->new_level == 6
5854 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005855 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005856 mdname(mddev), mddev->new_layout);
5857 return ERR_PTR(-EIO);
5858 }
5859 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005860 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005861 mdname(mddev), mddev->raid_disks);
5862 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Andre Noll664e7c42009-06-18 08:45:27 +10005865 if (!mddev->new_chunk_sectors ||
5866 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5867 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005868 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5869 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005870 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005871 }
5872
NeilBrownd1688a62011-10-11 16:49:52 +11005873 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005874 if (conf == NULL)
5875 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08005876 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11005877 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
5878 &new_group)) {
5879 conf->group_cnt = group_cnt;
5880 conf->worker_cnt_per_group = worker_cnt_per_group;
5881 conf->worker_groups = new_group;
5882 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08005883 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005884 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10005885 seqcount_init(&conf->gen_lock);
Dan Williamsf5efd452009-10-16 15:55:38 +11005886 init_waitqueue_head(&conf->wait_for_stripe);
5887 init_waitqueue_head(&conf->wait_for_overlap);
5888 INIT_LIST_HEAD(&conf->handle_list);
5889 INIT_LIST_HEAD(&conf->hold_list);
5890 INIT_LIST_HEAD(&conf->delayed_list);
5891 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005892 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005893 atomic_set(&conf->active_stripes, 0);
5894 atomic_set(&conf->preread_active_stripes, 0);
5895 atomic_set(&conf->active_aligned_reads, 0);
5896 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005897 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005898
5899 conf->raid_disks = mddev->raid_disks;
5900 if (mddev->reshape_position == MaxSector)
5901 conf->previous_raid_disks = mddev->raid_disks;
5902 else
5903 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005904 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005905
NeilBrown5e5e3e72009-10-16 16:35:30 +11005906 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005907 GFP_KERNEL);
5908 if (!conf->disks)
5909 goto abort;
5910
5911 conf->mddev = mddev;
5912
5913 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5914 goto abort;
5915
Shaohua Li566c09c2013-11-14 15:16:17 +11005916 /* We init hash_locks[0] separately to that it can be used
5917 * as the reference lock in the spin_lock_nest_lock() call
5918 * in lock_all_device_hash_locks_irq in order to convince
5919 * lockdep that we know what we are doing.
5920 */
5921 spin_lock_init(conf->hash_locks);
5922 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
5923 spin_lock_init(conf->hash_locks + i);
5924
5925 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5926 INIT_LIST_HEAD(conf->inactive_list + i);
5927
5928 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5929 INIT_LIST_HEAD(conf->temp_inactive_list + i);
5930
Dan Williams36d1c642009-07-14 11:48:22 -07005931 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11005932 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07005933 if (raid5_alloc_percpu(conf) != 0)
5934 goto abort;
5935
NeilBrown0c55e022010-05-03 14:09:02 +10005936 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005937
NeilBrowndafb20f2012-03-19 12:46:39 +11005938 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005939 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005940 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005941 || raid_disk < 0)
5942 continue;
5943 disk = conf->disks + raid_disk;
5944
NeilBrown17045f52011-12-23 10:17:53 +11005945 if (test_bit(Replacement, &rdev->flags)) {
5946 if (disk->replacement)
5947 goto abort;
5948 disk->replacement = rdev;
5949 } else {
5950 if (disk->rdev)
5951 goto abort;
5952 disk->rdev = rdev;
5953 }
NeilBrown91adb562009-03-31 14:39:39 +11005954
5955 if (test_bit(In_sync, &rdev->flags)) {
5956 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005957 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5958 " disk %d\n",
5959 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005960 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005961 /* Cannot rely on bitmap to complete recovery */
5962 conf->fullsync = 1;
5963 }
5964
NeilBrown91adb562009-03-31 14:39:39 +11005965 conf->level = mddev->new_level;
5966 if (conf->level == 6)
5967 conf->max_degraded = 2;
5968 else
5969 conf->max_degraded = 1;
5970 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005971 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005972 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005973 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005974 conf->prev_algo = mddev->layout;
5975 }
NeilBrown91adb562009-03-31 14:39:39 +11005976
5977 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005978 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11005979 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
Shaohua Li566c09c2013-11-14 15:16:17 +11005980 if (grow_stripes(conf, NR_STRIPES)) {
NeilBrown91adb562009-03-31 14:39:39 +11005981 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005982 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5983 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005984 goto abort;
5985 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005986 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5987 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005988
NeilBrown02326052012-07-03 15:56:52 +10005989 sprintf(pers_name, "raid%d", mddev->new_level);
5990 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005991 if (!conf->thread) {
5992 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005993 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005994 mdname(mddev));
5995 goto abort;
5996 }
5997
5998 return conf;
5999
6000 abort:
6001 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006002 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006003 return ERR_PTR(-EIO);
6004 } else
6005 return ERR_PTR(-ENOMEM);
6006}
6007
NeilBrownc148ffd2009-11-13 17:47:00 +11006008static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6009{
6010 switch (algo) {
6011 case ALGORITHM_PARITY_0:
6012 if (raid_disk < max_degraded)
6013 return 1;
6014 break;
6015 case ALGORITHM_PARITY_N:
6016 if (raid_disk >= raid_disks - max_degraded)
6017 return 1;
6018 break;
6019 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006020 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006021 raid_disk == raid_disks - 1)
6022 return 1;
6023 break;
6024 case ALGORITHM_LEFT_ASYMMETRIC_6:
6025 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6026 case ALGORITHM_LEFT_SYMMETRIC_6:
6027 case ALGORITHM_RIGHT_SYMMETRIC_6:
6028 if (raid_disk == raid_disks - 1)
6029 return 1;
6030 }
6031 return 0;
6032}
6033
NeilBrownfd01b882011-10-11 16:47:53 +11006034static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11006035{
NeilBrownd1688a62011-10-11 16:49:52 +11006036 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10006037 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11006038 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006039 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11006040 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11006041 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10006042 long long min_offset_diff = 0;
6043 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11006044
Andre Noll8c6ac862009-06-18 08:48:06 +10006045 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10006046 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10006047 " -- starting background reconstruction\n",
6048 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10006049
6050 rdev_for_each(rdev, mddev) {
6051 long long diff;
6052 if (rdev->raid_disk < 0)
6053 continue;
6054 diff = (rdev->new_data_offset - rdev->data_offset);
6055 if (first) {
6056 min_offset_diff = diff;
6057 first = 0;
6058 } else if (mddev->reshape_backwards &&
6059 diff < min_offset_diff)
6060 min_offset_diff = diff;
6061 else if (!mddev->reshape_backwards &&
6062 diff > min_offset_diff)
6063 min_offset_diff = diff;
6064 }
6065
NeilBrownf6705572006-03-27 01:18:11 -08006066 if (mddev->reshape_position != MaxSector) {
6067 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10006068 * Difficulties arise if the stripe we would write to
6069 * next is at or after the stripe we would read from next.
6070 * For a reshape that changes the number of devices, this
6071 * is only possible for a very short time, and mdadm makes
6072 * sure that time appears to have past before assembling
6073 * the array. So we fail if that time hasn't passed.
6074 * For a reshape that keeps the number of devices the same
6075 * mdadm must be monitoring the reshape can keeping the
6076 * critical areas read-only and backed up. It will start
6077 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08006078 */
6079 sector_t here_new, here_old;
6080 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11006081 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08006082
NeilBrown88ce4932009-03-31 15:24:23 +11006083 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10006084 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08006085 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08006086 mdname(mddev));
6087 return -EINVAL;
6088 }
NeilBrownf6705572006-03-27 01:18:11 -08006089 old_disks = mddev->raid_disks - mddev->delta_disks;
6090 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08006091 * further up in new geometry must map after here in old
6092 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08006093 */
6094 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10006095 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08006096 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10006097 printk(KERN_ERR "md/raid:%s: reshape_position not "
6098 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006099 return -EINVAL;
6100 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006101 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006102 /* here_new is the stripe we will write to */
6103 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10006104 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08006105 (old_disks-max_degraded));
6106 /* here_old is the first stripe that we might need to read
6107 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10006108 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10006109 if ((here_new * mddev->new_chunk_sectors !=
6110 here_old * mddev->chunk_sectors)) {
6111 printk(KERN_ERR "md/raid:%s: reshape position is"
6112 " confused - aborting\n", mdname(mddev));
6113 return -EINVAL;
6114 }
NeilBrown67ac6012009-08-13 10:06:24 +10006115 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10006116 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10006117 * and taking constant backups.
6118 * mdadm always starts a situation like this in
6119 * readonly mode so it can take control before
6120 * allowing any writes. So just check for that.
6121 */
NeilBrownb5254dd2012-05-21 09:27:01 +10006122 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
6123 abs(min_offset_diff) >= mddev->new_chunk_sectors)
6124 /* not really in-place - so OK */;
6125 else if (mddev->ro == 0) {
6126 printk(KERN_ERR "md/raid:%s: in-place reshape "
6127 "must be started in read-only mode "
6128 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10006129 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10006130 return -EINVAL;
6131 }
NeilBrown2c810cd2012-05-21 09:27:00 +10006132 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10006133 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10006134 here_old * mddev->chunk_sectors)
6135 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10006136 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08006137 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10006138 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
6139 "auto-recovery - aborting.\n",
6140 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006141 return -EINVAL;
6142 }
NeilBrown0c55e022010-05-03 14:09:02 +10006143 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
6144 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006145 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08006146 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006147 BUG_ON(mddev->level != mddev->new_level);
6148 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10006149 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11006150 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08006151 }
6152
NeilBrown245f46c2009-03-31 14:39:39 +11006153 if (mddev->private == NULL)
6154 conf = setup_conf(mddev);
6155 else
6156 conf = mddev->private;
6157
NeilBrown91adb562009-03-31 14:39:39 +11006158 if (IS_ERR(conf))
6159 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08006160
NeilBrownb5254dd2012-05-21 09:27:01 +10006161 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11006162 mddev->thread = conf->thread;
6163 conf->thread = NULL;
6164 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
NeilBrown17045f52011-12-23 10:17:53 +11006166 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
6167 i++) {
6168 rdev = conf->disks[i].rdev;
6169 if (!rdev && conf->disks[i].replacement) {
6170 /* The replacement is all we have yet */
6171 rdev = conf->disks[i].replacement;
6172 conf->disks[i].replacement = NULL;
6173 clear_bit(Replacement, &rdev->flags);
6174 conf->disks[i].rdev = rdev;
6175 }
6176 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11006177 continue;
NeilBrown17045f52011-12-23 10:17:53 +11006178 if (conf->disks[i].replacement &&
6179 conf->reshape_progress != MaxSector) {
6180 /* replacements and reshape simply do not mix. */
6181 printk(KERN_ERR "md: cannot handle concurrent "
6182 "replacement and reshape.\n");
6183 goto abort;
6184 }
NeilBrown2f115882010-06-17 17:41:03 +10006185 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11006186 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10006187 continue;
6188 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006189 /* This disc is not fully in-sync. However if it
6190 * just stored parity (beyond the recovery_offset),
6191 * when we don't need to be concerned about the
6192 * array being dirty.
6193 * When reshape goes 'backwards', we never have
6194 * partially completed devices, so we only need
6195 * to worry about reshape going forwards.
6196 */
6197 /* Hack because v0.91 doesn't store recovery_offset properly. */
6198 if (mddev->major_version == 0 &&
6199 mddev->minor_version > 90)
6200 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006201
NeilBrownc148ffd2009-11-13 17:47:00 +11006202 if (rdev->recovery_offset < reshape_offset) {
6203 /* We need to check old and new layout */
6204 if (!only_parity(rdev->raid_disk,
6205 conf->algorithm,
6206 conf->raid_disks,
6207 conf->max_degraded))
6208 continue;
6209 }
6210 if (!only_parity(rdev->raid_disk,
6211 conf->prev_algo,
6212 conf->previous_raid_disks,
6213 conf->max_degraded))
6214 continue;
6215 dirty_parity_disks++;
6216 }
NeilBrown91adb562009-03-31 14:39:39 +11006217
NeilBrown17045f52011-12-23 10:17:53 +11006218 /*
6219 * 0 for a fully functional array, 1 or 2 for a degraded array.
6220 */
NeilBrown908f4fb2011-12-23 10:17:50 +11006221 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
NeilBrown674806d2010-06-16 17:17:53 +10006223 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006224 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006226 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 goto abort;
6228 }
6229
NeilBrown91adb562009-03-31 14:39:39 +11006230 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006231 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006232 mddev->resync_max_sectors = mddev->dev_sectors;
6233
NeilBrownc148ffd2009-11-13 17:47:00 +11006234 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006236 if (mddev->ok_start_degraded)
6237 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006238 "md/raid:%s: starting dirty degraded array"
6239 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006240 mdname(mddev));
6241 else {
6242 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006243 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006244 mdname(mddev));
6245 goto abort;
6246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 }
6248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006250 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6251 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006252 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6253 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 else
NeilBrown0c55e022010-05-03 14:09:02 +10006255 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6256 " out of %d devices, algorithm %d\n",
6257 mdname(mddev), conf->level,
6258 mddev->raid_disks - mddev->degraded,
6259 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 print_raid5_conf(conf);
6262
NeilBrownfef9c612009-03-31 15:16:46 +11006263 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006264 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006265 atomic_set(&conf->reshape_stripes, 0);
6266 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6267 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6268 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6269 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6270 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006271 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006272 }
6273
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006275 if (mddev->to_remove == &raid5_attrs_group)
6276 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006277 else if (mddev->kobj.sd &&
6278 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006279 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006280 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006281 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006282 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6283
6284 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006285 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006286 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006287 /* read-ahead size must cover two whole stripes, which
6288 * is 2 * (datadisks) * chunksize where 'n' is the
6289 * number of raid devices
6290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6292 int stripe = data_disks *
6293 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6294 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6295 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006296
NeilBrown9f7c2222010-07-26 12:04:13 +10006297 chunk_size = mddev->chunk_sectors << 9;
6298 blk_queue_io_min(mddev->queue, chunk_size);
6299 blk_queue_io_opt(mddev->queue, chunk_size *
6300 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07006301 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006302 /*
6303 * We can only discard a whole stripe. It doesn't make sense to
6304 * discard data disk but write parity disk
6305 */
6306 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11006307 /* Round up to power of 2, as discard handling
6308 * currently assumes that */
6309 while ((stripe-1) & stripe)
6310 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006311 mddev->queue->limits.discard_alignment = stripe;
6312 mddev->queue->limits.discard_granularity = stripe;
6313 /*
6314 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10006315 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11006316 */
6317 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10006318
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006319 blk_queue_max_write_same_sectors(mddev->queue, 0);
6320
NeilBrown05616be2012-05-21 09:27:00 +10006321 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006322 disk_stack_limits(mddev->gendisk, rdev->bdev,
6323 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10006324 disk_stack_limits(mddev->gendisk, rdev->bdev,
6325 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11006326 /*
6327 * discard_zeroes_data is required, otherwise data
6328 * could be lost. Consider a scenario: discard a stripe
6329 * (the stripe could be inconsistent if
6330 * discard_zeroes_data is 0); write one disk of the
6331 * stripe (the stripe could be inconsistent again
6332 * depending on which disks are used to calculate
6333 * parity); the disk is broken; The stripe data of this
6334 * disk is lost.
6335 */
6336 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6337 !bdev_get_queue(rdev->bdev)->
6338 limits.discard_zeroes_data)
6339 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10006340 /* Unfortunately, discard_zeroes_data is not currently
6341 * a guarantee - just a hint. So we only allow DISCARD
6342 * if the sysadmin has confirmed that only safe devices
6343 * are in use by setting a module parameter.
6344 */
6345 if (!devices_handle_discard_safely) {
6346 if (discard_supported) {
6347 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
6348 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
6349 }
6350 discard_supported = false;
6351 }
NeilBrown05616be2012-05-21 09:27:00 +10006352 }
Shaohua Li620125f2012-10-11 13:49:05 +11006353
6354 if (discard_supported &&
6355 mddev->queue->limits.max_discard_sectors >= stripe &&
6356 mddev->queue->limits.discard_granularity >= stripe)
6357 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6358 mddev->queue);
6359 else
6360 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6361 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 }
6363
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 return 0;
6365abort:
NeilBrown01f96c02011-09-21 15:30:20 +10006366 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11006367 print_raid5_conf(conf);
6368 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10006370 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 return -EIO;
6372}
6373
NeilBrownafa0f552014-12-15 12:56:58 +11006374static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
NeilBrownafa0f552014-12-15 12:56:58 +11006376 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Dan Williams95fc17a2009-07-31 12:39:15 +10006378 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10006379 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380}
6381
NeilBrownfd01b882011-10-11 16:47:53 +11006382static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383{
NeilBrownd1688a62011-10-11 16:49:52 +11006384 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 int i;
6386
Andre Noll9d8f0362009-06-18 08:45:01 +10006387 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
6388 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07006389 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 for (i = 0; i < conf->raid_disks; i++)
6391 seq_printf (seq, "%s",
6392 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08006393 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395}
6396
NeilBrownd1688a62011-10-11 16:49:52 +11006397static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398{
6399 int i;
6400 struct disk_info *tmp;
6401
NeilBrown0c55e022010-05-03 14:09:02 +10006402 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 if (!conf) {
6404 printk("(conf==NULL)\n");
6405 return;
6406 }
NeilBrown0c55e022010-05-03 14:09:02 +10006407 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
6408 conf->raid_disks,
6409 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
6411 for (i = 0; i < conf->raid_disks; i++) {
6412 char b[BDEVNAME_SIZE];
6413 tmp = conf->disks + i;
6414 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10006415 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
6416 i, !test_bit(Faulty, &tmp->rdev->flags),
6417 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 }
6419}
6420
NeilBrownfd01b882011-10-11 16:47:53 +11006421static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422{
6423 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11006424 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10006426 int count = 0;
6427 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
6429 for (i = 0; i < conf->raid_disks; i++) {
6430 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11006431 if (tmp->replacement
6432 && tmp->replacement->recovery_offset == MaxSector
6433 && !test_bit(Faulty, &tmp->replacement->flags)
6434 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6435 /* Replacement has just become active. */
6436 if (!tmp->rdev
6437 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6438 count++;
6439 if (tmp->rdev) {
6440 /* Replaced device not technically faulty,
6441 * but we need to be sure it gets removed
6442 * and never re-added.
6443 */
6444 set_bit(Faulty, &tmp->rdev->flags);
6445 sysfs_notify_dirent_safe(
6446 tmp->rdev->sysfs_state);
6447 }
6448 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6449 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10006450 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08006451 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07006452 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10006453 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11006454 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 }
6456 }
NeilBrown6b965622010-08-18 11:56:59 +10006457 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006458 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006459 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006461 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462}
6463
NeilBrownb8321b62011-12-23 10:17:51 +11006464static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465{
NeilBrownd1688a62011-10-11 16:49:52 +11006466 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006468 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006469 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 struct disk_info *p = conf->disks + number;
6471
6472 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006473 if (rdev == p->rdev)
6474 rdevp = &p->rdev;
6475 else if (rdev == p->replacement)
6476 rdevp = &p->replacement;
6477 else
6478 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006479
NeilBrown657e3e42011-12-23 10:17:52 +11006480 if (number >= conf->raid_disks &&
6481 conf->reshape_progress == MaxSector)
6482 clear_bit(In_sync, &rdev->flags);
6483
6484 if (test_bit(In_sync, &rdev->flags) ||
6485 atomic_read(&rdev->nr_pending)) {
6486 err = -EBUSY;
6487 goto abort;
6488 }
6489 /* Only remove non-faulty devices if recovery
6490 * isn't possible.
6491 */
6492 if (!test_bit(Faulty, &rdev->flags) &&
6493 mddev->recovery_disabled != conf->recovery_disabled &&
6494 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006495 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006496 number < conf->raid_disks) {
6497 err = -EBUSY;
6498 goto abort;
6499 }
6500 *rdevp = NULL;
6501 synchronize_rcu();
6502 if (atomic_read(&rdev->nr_pending)) {
6503 /* lost the race, try later */
6504 err = -EBUSY;
6505 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006506 } else if (p->replacement) {
6507 /* We must have just cleared 'rdev' */
6508 p->rdev = p->replacement;
6509 clear_bit(Replacement, &p->replacement->flags);
6510 smp_mb(); /* Make sure other CPUs may see both as identical
6511 * but will never see neither - if they are careful
6512 */
6513 p->replacement = NULL;
6514 clear_bit(WantReplacement, &rdev->flags);
6515 } else
6516 /* We might have just removed the Replacement as faulty-
6517 * clear the bit just in case
6518 */
6519 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520abort:
6521
6522 print_raid5_conf(conf);
6523 return err;
6524}
6525
NeilBrownfd01b882011-10-11 16:47:53 +11006526static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527{
NeilBrownd1688a62011-10-11 16:49:52 +11006528 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10006529 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 int disk;
6531 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10006532 int first = 0;
6533 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
NeilBrown7f0da592011-07-28 11:39:22 +10006535 if (mddev->recovery_disabled == conf->recovery_disabled)
6536 return -EBUSY;
6537
NeilBrowndc10c642012-03-19 12:46:37 +11006538 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10006540 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541
Neil Brown6c2fce22008-06-28 08:31:31 +10006542 if (rdev->raid_disk >= 0)
6543 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
6545 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07006546 * find the disk ... but prefer rdev->saved_raid_disk
6547 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 */
NeilBrown16a53ec2006-06-26 00:27:38 -07006549 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10006550 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006551 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10006552 first = rdev->saved_raid_disk;
6553
6554 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11006555 p = conf->disks + disk;
6556 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08006557 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10006559 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07006560 if (rdev->saved_raid_disk != disk)
6561 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08006562 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10006563 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006565 }
6566 for (disk = first; disk <= last; disk++) {
6567 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11006568 if (test_bit(WantReplacement, &p->rdev->flags) &&
6569 p->replacement == NULL) {
6570 clear_bit(In_sync, &rdev->flags);
6571 set_bit(Replacement, &rdev->flags);
6572 rdev->raid_disk = disk;
6573 err = 0;
6574 conf->fullsync = 1;
6575 rcu_assign_pointer(p->replacement, rdev);
6576 break;
6577 }
6578 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006579out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10006581 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582}
6583
NeilBrownfd01b882011-10-11 16:47:53 +11006584static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585{
6586 /* no resync is happening, and there is enough space
6587 * on all devices, so we can resize.
6588 * We need to make sure resync covers any new space.
6589 * If the array is shrinking we should possibly wait until
6590 * any io in the removed space completes, but it hardly seems
6591 * worth it.
6592 */
NeilBrowna4a61252012-05-22 13:55:27 +10006593 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10006594 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10006595 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6596 if (mddev->external_size &&
6597 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11006598 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10006599 if (mddev->bitmap) {
6600 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6601 if (ret)
6602 return ret;
6603 }
6604 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10006605 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006606 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10006607 if (sectors > mddev->dev_sectors &&
6608 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006609 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6611 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006612 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07006613 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 return 0;
6615}
6616
NeilBrownfd01b882011-10-11 16:47:53 +11006617static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10006618{
6619 /* Can only proceed if there are plenty of stripe_heads.
6620 * We need a minimum of one full stripe,, and for sensible progress
6621 * it is best to have about 4 times that.
6622 * If we require 4 times, then the default 256 4K stripe_heads will
6623 * allow for chunk sizes up to 256K, which is probably OK.
6624 * If the chunk size is greater, user-space should request more
6625 * stripe_heads first.
6626 */
NeilBrownd1688a62011-10-11 16:49:52 +11006627 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10006628 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6629 > conf->max_nr_stripes ||
6630 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6631 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10006632 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6633 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10006634 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6635 / STRIPE_SIZE)*4);
6636 return 0;
6637 }
6638 return 1;
6639}
6640
NeilBrownfd01b882011-10-11 16:47:53 +11006641static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08006642{
NeilBrownd1688a62011-10-11 16:49:52 +11006643 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08006644
NeilBrown88ce4932009-03-31 15:24:23 +11006645 if (mddev->delta_disks == 0 &&
6646 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10006647 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10006648 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10006649 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11006650 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10006651 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11006652 /* We might be able to shrink, but the devices must
6653 * be made bigger first.
6654 * For raid6, 4 is the minimum size.
6655 * Otherwise 2 is the minimum
6656 */
6657 int min = 2;
6658 if (mddev->level == 6)
6659 min = 4;
6660 if (mddev->raid_disks + mddev->delta_disks < min)
6661 return -EINVAL;
6662 }
NeilBrown29269552006-03-27 01:18:10 -08006663
NeilBrown01ee22b2009-06-18 08:47:20 +10006664 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08006665 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08006666
NeilBrowne56108d62012-10-11 14:24:13 +11006667 return resize_stripes(conf, (conf->previous_raid_disks
6668 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08006669}
6670
NeilBrownfd01b882011-10-11 16:47:53 +11006671static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006672{
NeilBrownd1688a62011-10-11 16:49:52 +11006673 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006674 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006675 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006676 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006677
NeilBrownf4168852007-02-28 20:11:53 -08006678 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006679 return -EBUSY;
6680
NeilBrown01ee22b2009-06-18 08:47:20 +10006681 if (!check_stripe_cache(mddev))
6682 return -ENOSPC;
6683
NeilBrown30b67642012-05-22 13:55:28 +10006684 if (has_failed(conf))
6685 return -EINVAL;
6686
NeilBrownc6563a82012-05-21 09:27:00 +10006687 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006688 if (!test_bit(In_sync, &rdev->flags)
6689 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006690 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006691 }
NeilBrown63c70c42006-03-27 01:18:13 -08006692
NeilBrownf4168852007-02-28 20:11:53 -08006693 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006694 /* Not enough devices even to make a degraded array
6695 * of that size
6696 */
6697 return -EINVAL;
6698
NeilBrownec32a2b2009-03-31 15:17:38 +11006699 /* Refuse to reduce size of the array. Any reductions in
6700 * array size must be through explicit setting of array_size
6701 * attribute.
6702 */
6703 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6704 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006705 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006706 "before number of disks\n", mdname(mddev));
6707 return -EINVAL;
6708 }
6709
NeilBrownf6705572006-03-27 01:18:11 -08006710 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006711 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006712 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006713 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006714 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006715 conf->prev_chunk_sectors = conf->chunk_sectors;
6716 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006717 conf->prev_algo = conf->algorithm;
6718 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006719 conf->generation++;
6720 /* Code that selects data_offset needs to see the generation update
6721 * if reshape_progress has been set - so a memory barrier needed.
6722 */
6723 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006724 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006725 conf->reshape_progress = raid5_size(mddev, 0, 0);
6726 else
6727 conf->reshape_progress = 0;
6728 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10006729 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006730 spin_unlock_irq(&conf->device_lock);
6731
NeilBrown4d77e3b2013-08-27 15:57:47 +10006732 /* Now make sure any requests that proceeded on the assumption
6733 * the reshape wasn't running - like Discard or Read - have
6734 * completed.
6735 */
6736 mddev_suspend(mddev);
6737 mddev_resume(mddev);
6738
NeilBrown29269552006-03-27 01:18:10 -08006739 /* Add some new drives, as many as will fit.
6740 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006741 * Don't add devices if we are reducing the number of
6742 * devices in the array. This is because it is not possible
6743 * to correctly record the "partially reconstructed" state of
6744 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006745 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006746 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006747 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006748 if (rdev->raid_disk < 0 &&
6749 !test_bit(Faulty, &rdev->flags)) {
6750 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006751 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006752 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006753 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006754 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006755 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006756
6757 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006758 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006759 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006760 } else if (rdev->raid_disk >= conf->previous_raid_disks
6761 && !test_bit(Faulty, &rdev->flags)) {
6762 /* This is a spare that was manually added */
6763 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006764 }
NeilBrown29269552006-03-27 01:18:10 -08006765
NeilBrown87a8dec2011-01-31 11:57:43 +11006766 /* When a reshape changes the number of devices,
6767 * ->degraded is measured against the larger of the
6768 * pre and post number of devices.
6769 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006770 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006771 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006772 spin_unlock_irqrestore(&conf->device_lock, flags);
6773 }
NeilBrown63c70c42006-03-27 01:18:13 -08006774 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006775 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006776 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006777
NeilBrown29269552006-03-27 01:18:10 -08006778 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6779 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6780 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6781 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6782 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006783 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006784 if (!mddev->sync_thread) {
6785 mddev->recovery = 0;
6786 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11006787 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006788 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11006789 mddev->new_chunk_sectors =
6790 conf->chunk_sectors = conf->prev_chunk_sectors;
6791 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10006792 rdev_for_each(rdev, mddev)
6793 rdev->new_data_offset = rdev->data_offset;
6794 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11006795 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11006796 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006797 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11006798 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006799 spin_unlock_irq(&conf->device_lock);
6800 return -EAGAIN;
6801 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006802 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006803 md_wakeup_thread(mddev->sync_thread);
6804 md_new_event(mddev);
6805 return 0;
6806}
NeilBrown29269552006-03-27 01:18:10 -08006807
NeilBrownec32a2b2009-03-31 15:17:38 +11006808/* This is called from the reshape thread and should make any
6809 * changes needed in 'conf'
6810 */
NeilBrownd1688a62011-10-11 16:49:52 +11006811static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006812{
NeilBrown29269552006-03-27 01:18:10 -08006813
NeilBrownf6705572006-03-27 01:18:11 -08006814 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006815 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006816
NeilBrownf6705572006-03-27 01:18:11 -08006817 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006818 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006819 rdev_for_each(rdev, conf->mddev)
6820 rdev->data_offset = rdev->new_data_offset;
6821 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006822 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006823 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006824 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006825
6826 /* read-ahead size must cover two whole stripes, which is
6827 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6828 */
NeilBrown4a5add42010-06-01 19:37:28 +10006829 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006830 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006831 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006832 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006833 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6834 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6835 }
NeilBrown29269552006-03-27 01:18:10 -08006836 }
NeilBrown29269552006-03-27 01:18:10 -08006837}
6838
NeilBrownec32a2b2009-03-31 15:17:38 +11006839/* This is called from the raid5d thread with mddev_lock held.
6840 * It makes config changes to the device.
6841 */
NeilBrownfd01b882011-10-11 16:47:53 +11006842static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006843{
NeilBrownd1688a62011-10-11 16:49:52 +11006844 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006845
6846 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6847
NeilBrownec32a2b2009-03-31 15:17:38 +11006848 if (mddev->delta_disks > 0) {
6849 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6850 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006851 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006852 } else {
6853 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006854 spin_lock_irq(&conf->device_lock);
6855 mddev->degraded = calc_degraded(conf);
6856 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006857 for (d = conf->raid_disks ;
6858 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006859 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006860 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006861 if (rdev)
6862 clear_bit(In_sync, &rdev->flags);
6863 rdev = conf->disks[d].replacement;
6864 if (rdev)
6865 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006866 }
NeilBrowncea9c222009-03-31 15:15:05 +11006867 }
NeilBrown88ce4932009-03-31 15:24:23 +11006868 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006869 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006870 mddev->reshape_position = MaxSector;
6871 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006872 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006873 }
6874}
6875
NeilBrownfd01b882011-10-11 16:47:53 +11006876static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006877{
NeilBrownd1688a62011-10-11 16:49:52 +11006878 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006879
6880 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006881 case 2: /* resume for a suspend */
6882 wake_up(&conf->wait_for_overlap);
6883 break;
6884
NeilBrown72626682005-09-09 16:23:54 -07006885 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006886 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006887 /* '2' tells resync/reshape to pause so that all
6888 * active stripes can drain
6889 */
6890 conf->quiesce = 2;
Shaohua Li566c09c2013-11-14 15:16:17 +11006891 wait_event_cmd(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006892 atomic_read(&conf->active_stripes) == 0 &&
6893 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11006894 unlock_all_device_hash_locks_irq(conf),
6895 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10006896 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11006897 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006898 /* allow reshape to continue */
6899 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006900 break;
6901
6902 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006903 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006904 conf->quiesce = 0;
6905 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006906 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11006907 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006908 break;
6909 }
NeilBrown72626682005-09-09 16:23:54 -07006910}
NeilBrownb15c2e52006-01-06 00:20:16 -08006911
NeilBrownfd01b882011-10-11 16:47:53 +11006912static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006913{
NeilBrowne373ab12011-10-11 16:48:59 +11006914 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006915 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006916
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006917 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006918 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006919 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6920 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006921 return ERR_PTR(-EINVAL);
6922 }
6923
NeilBrowne373ab12011-10-11 16:48:59 +11006924 sectors = raid0_conf->strip_zone[0].zone_end;
6925 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006926 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006927 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006928 mddev->new_layout = ALGORITHM_PARITY_N;
6929 mddev->new_chunk_sectors = mddev->chunk_sectors;
6930 mddev->raid_disks += 1;
6931 mddev->delta_disks = 1;
6932 /* make sure it will be not marked as dirty */
6933 mddev->recovery_cp = MaxSector;
6934
6935 return setup_conf(mddev);
6936}
6937
NeilBrownfd01b882011-10-11 16:47:53 +11006938static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006939{
6940 int chunksect;
6941
6942 if (mddev->raid_disks != 2 ||
6943 mddev->degraded > 1)
6944 return ERR_PTR(-EINVAL);
6945
6946 /* Should check if there are write-behind devices? */
6947
6948 chunksect = 64*2; /* 64K by default */
6949
6950 /* The array must be an exact multiple of chunksize */
6951 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6952 chunksect >>= 1;
6953
6954 if ((chunksect<<9) < STRIPE_SIZE)
6955 /* array size does not allow a suitable chunk size */
6956 return ERR_PTR(-EINVAL);
6957
6958 mddev->new_level = 5;
6959 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006960 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006961
6962 return setup_conf(mddev);
6963}
6964
NeilBrownfd01b882011-10-11 16:47:53 +11006965static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006966{
6967 int new_layout;
6968
6969 switch (mddev->layout) {
6970 case ALGORITHM_LEFT_ASYMMETRIC_6:
6971 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6972 break;
6973 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6974 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6975 break;
6976 case ALGORITHM_LEFT_SYMMETRIC_6:
6977 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6978 break;
6979 case ALGORITHM_RIGHT_SYMMETRIC_6:
6980 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6981 break;
6982 case ALGORITHM_PARITY_0_6:
6983 new_layout = ALGORITHM_PARITY_0;
6984 break;
6985 case ALGORITHM_PARITY_N:
6986 new_layout = ALGORITHM_PARITY_N;
6987 break;
6988 default:
6989 return ERR_PTR(-EINVAL);
6990 }
6991 mddev->new_level = 5;
6992 mddev->new_layout = new_layout;
6993 mddev->delta_disks = -1;
6994 mddev->raid_disks -= 1;
6995 return setup_conf(mddev);
6996}
6997
NeilBrownfd01b882011-10-11 16:47:53 +11006998static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006999{
NeilBrown88ce4932009-03-31 15:24:23 +11007000 /* For a 2-drive array, the layout and chunk size can be changed
7001 * immediately as not restriping is needed.
7002 * For larger arrays we record the new value - after validation
7003 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11007004 */
NeilBrownd1688a62011-10-11 16:49:52 +11007005 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10007006 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11007007
NeilBrown597a7112009-06-18 08:47:42 +10007008 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11007009 return -EINVAL;
7010 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007011 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11007012 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007013 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11007014 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007015 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11007016 /* not factor of array size */
7017 return -EINVAL;
7018 }
7019
7020 /* They look valid */
7021
NeilBrown88ce4932009-03-31 15:24:23 +11007022 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10007023 /* can make the change immediately */
7024 if (mddev->new_layout >= 0) {
7025 conf->algorithm = mddev->new_layout;
7026 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11007027 }
7028 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10007029 conf->chunk_sectors = new_chunk ;
7030 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11007031 }
7032 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7033 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11007034 }
NeilBrown50ac1682009-06-18 08:47:55 +10007035 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11007036}
7037
NeilBrownfd01b882011-10-11 16:47:53 +11007038static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11007039{
NeilBrown597a7112009-06-18 08:47:42 +10007040 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10007041
NeilBrown597a7112009-06-18 08:47:42 +10007042 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11007043 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007044 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007045 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11007046 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007047 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11007048 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007049 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11007050 /* not factor of array size */
7051 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007052 }
NeilBrown88ce4932009-03-31 15:24:23 +11007053
7054 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10007055 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11007056}
7057
NeilBrownfd01b882011-10-11 16:47:53 +11007058static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007059{
7060 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007061 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007062 * raid1 - if there are two drives. We need to know the chunk size
7063 * raid4 - trivial - just use a raid4 layout.
7064 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007065 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007066 if (mddev->level == 0)
7067 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11007068 if (mddev->level == 1)
7069 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11007070 if (mddev->level == 4) {
7071 mddev->new_layout = ALGORITHM_PARITY_N;
7072 mddev->new_level = 5;
7073 return setup_conf(mddev);
7074 }
NeilBrownfc9739c2009-03-31 14:57:20 +11007075 if (mddev->level == 6)
7076 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11007077
7078 return ERR_PTR(-EINVAL);
7079}
7080
NeilBrownfd01b882011-10-11 16:47:53 +11007081static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11007082{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007083 /* raid4 can take over:
7084 * raid0 - if there is only one strip zone
7085 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11007086 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007087 if (mddev->level == 0)
7088 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11007089 if (mddev->level == 5 &&
7090 mddev->layout == ALGORITHM_PARITY_N) {
7091 mddev->new_layout = 0;
7092 mddev->new_level = 4;
7093 return setup_conf(mddev);
7094 }
7095 return ERR_PTR(-EINVAL);
7096}
NeilBrownd562b0c2009-03-31 14:39:39 +11007097
NeilBrown84fc4b52011-10-11 16:49:58 +11007098static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11007099
NeilBrownfd01b882011-10-11 16:47:53 +11007100static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11007101{
7102 /* Currently can only take over a raid5. We map the
7103 * personality to an equivalent raid6 personality
7104 * with the Q block at the end.
7105 */
7106 int new_layout;
7107
7108 if (mddev->pers != &raid5_personality)
7109 return ERR_PTR(-EINVAL);
7110 if (mddev->degraded > 1)
7111 return ERR_PTR(-EINVAL);
7112 if (mddev->raid_disks > 253)
7113 return ERR_PTR(-EINVAL);
7114 if (mddev->raid_disks < 3)
7115 return ERR_PTR(-EINVAL);
7116
7117 switch (mddev->layout) {
7118 case ALGORITHM_LEFT_ASYMMETRIC:
7119 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
7120 break;
7121 case ALGORITHM_RIGHT_ASYMMETRIC:
7122 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
7123 break;
7124 case ALGORITHM_LEFT_SYMMETRIC:
7125 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
7126 break;
7127 case ALGORITHM_RIGHT_SYMMETRIC:
7128 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
7129 break;
7130 case ALGORITHM_PARITY_0:
7131 new_layout = ALGORITHM_PARITY_0_6;
7132 break;
7133 case ALGORITHM_PARITY_N:
7134 new_layout = ALGORITHM_PARITY_N;
7135 break;
7136 default:
7137 return ERR_PTR(-EINVAL);
7138 }
7139 mddev->new_level = 6;
7140 mddev->new_layout = new_layout;
7141 mddev->delta_disks = 1;
7142 mddev->raid_disks += 1;
7143 return setup_conf(mddev);
7144}
7145
NeilBrown84fc4b52011-10-11 16:49:58 +11007146static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07007147{
7148 .name = "raid6",
7149 .level = 6,
7150 .owner = THIS_MODULE,
7151 .make_request = make_request,
7152 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007153 .free = raid5_free,
NeilBrown16a53ec2006-06-26 00:27:38 -07007154 .status = status,
7155 .error_handler = error,
7156 .hot_add_disk = raid5_add_disk,
7157 .hot_remove_disk= raid5_remove_disk,
7158 .spare_active = raid5_spare_active,
7159 .sync_request = sync_request,
7160 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007161 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10007162 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08007163 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007164 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07007165 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11007166 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007167 .congested = raid5_congested,
NeilBrown64590f42014-12-15 12:56:57 +11007168 .mergeable_bvec = raid5_mergeable_bvec,
NeilBrown16a53ec2006-06-26 00:27:38 -07007169};
NeilBrown84fc4b52011-10-11 16:49:58 +11007170static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171{
7172 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08007173 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 .owner = THIS_MODULE,
7175 .make_request = make_request,
7176 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007177 .free = raid5_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 .status = status,
7179 .error_handler = error,
7180 .hot_add_disk = raid5_add_disk,
7181 .hot_remove_disk= raid5_remove_disk,
7182 .spare_active = raid5_spare_active,
7183 .sync_request = sync_request,
7184 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007185 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08007186 .check_reshape = raid5_check_reshape,
7187 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007188 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07007189 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11007190 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007191 .congested = raid5_congested,
NeilBrown64590f42014-12-15 12:56:57 +11007192 .mergeable_bvec = raid5_mergeable_bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193};
7194
NeilBrown84fc4b52011-10-11 16:49:58 +11007195static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196{
NeilBrown2604b702006-01-06 00:20:36 -08007197 .name = "raid4",
7198 .level = 4,
7199 .owner = THIS_MODULE,
7200 .make_request = make_request,
7201 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007202 .free = raid5_free,
NeilBrown2604b702006-01-06 00:20:36 -08007203 .status = status,
7204 .error_handler = error,
7205 .hot_add_disk = raid5_add_disk,
7206 .hot_remove_disk= raid5_remove_disk,
7207 .spare_active = raid5_spare_active,
7208 .sync_request = sync_request,
7209 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007210 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08007211 .check_reshape = raid5_check_reshape,
7212 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007213 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08007214 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11007215 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007216 .congested = raid5_congested,
NeilBrown64590f42014-12-15 12:56:57 +11007217 .mergeable_bvec = raid5_mergeable_bvec,
NeilBrown2604b702006-01-06 00:20:36 -08007218};
7219
7220static int __init raid5_init(void)
7221{
Shaohua Li851c30c2013-08-28 14:30:16 +08007222 raid5_wq = alloc_workqueue("raid5wq",
7223 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7224 if (!raid5_wq)
7225 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07007226 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007227 register_md_personality(&raid5_personality);
7228 register_md_personality(&raid4_personality);
7229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230}
7231
NeilBrown2604b702006-01-06 00:20:36 -08007232static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233{
NeilBrown16a53ec2006-06-26 00:27:38 -07007234 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007235 unregister_md_personality(&raid5_personality);
7236 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08007237 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238}
7239
7240module_init(raid5_init);
7241module_exit(raid5_exit);
7242MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007243MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08007245MODULE_ALIAS("md-raid5");
7246MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08007247MODULE_ALIAS("md-level-5");
7248MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07007249MODULE_ALIAS("md-personality-8"); /* RAID6 */
7250MODULE_ALIAS("md-raid6");
7251MODULE_ALIAS("md-level-6");
7252
7253/* This used to be two separate modules, they were: */
7254MODULE_ALIAS("raid5");
7255MODULE_ALIAS("raid6");