blob: b7cd32e7f29ecfb18c7bfd03c184797c43661c85 [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
NeilBrowna9683a72015-02-25 12:02:51 +1100500static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
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
NeilBrowna9683a72015-02-25 12:02:51 +1100508 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 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));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100529 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700530
Dan Williams45b42332007-07-09 11:56:43 -0700531 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000532 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100533retry:
534 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100535 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100536 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100538 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 sh->state = 0;
540
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800541 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct r5dev *dev = &sh->dev[i];
543
Dan Williamsd84e0f12007-01-02 13:52:30 -0700544 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700546 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700548 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000550 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100553 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100555 if (read_seqcount_retry(&conf->gen_lock, seq))
556 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100557 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800559 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100560 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
NeilBrownd1688a62011-10-11 16:49:52 +1100563static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100564 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct stripe_head *sh;
567
Dan Williams45b42332007-07-09 11:56:43 -0700568 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800569 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100570 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700572 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return NULL;
574}
575
NeilBrown674806d2010-06-16 17:17:53 +1000576/*
577 * Need to check if array has failed when deciding whether to:
578 * - start an array
579 * - remove non-faulty devices
580 * - add a spare
581 * - allow a reshape
582 * This determination is simple when no reshape is happening.
583 * However if there is a reshape, we need to carefully check
584 * both the before and after sections.
585 * This is because some failed devices may only affect one
586 * of the two sections, and some non-in_sync devices may
587 * be insync in the section most affected by failed devices.
588 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100589static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000590{
NeilBrown908f4fb2011-12-23 10:17:50 +1100591 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000592 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000593
594 rcu_read_lock();
595 degraded = 0;
596 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100597 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000598 if (rdev && test_bit(Faulty, &rdev->flags))
599 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000600 if (!rdev || test_bit(Faulty, &rdev->flags))
601 degraded++;
602 else if (test_bit(In_sync, &rdev->flags))
603 ;
604 else
605 /* not in-sync or faulty.
606 * If the reshape increases the number of devices,
607 * this is being recovered by the reshape, so
608 * this 'previous' section is not in_sync.
609 * If the number of devices is being reduced however,
610 * the device can only be part of the array if
611 * we are reverting a reshape, so this section will
612 * be in-sync.
613 */
614 if (conf->raid_disks >= conf->previous_raid_disks)
615 degraded++;
616 }
617 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100618 if (conf->raid_disks == conf->previous_raid_disks)
619 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000620 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100621 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000622 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100623 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000624 if (rdev && test_bit(Faulty, &rdev->flags))
625 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000626 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100627 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000628 else if (test_bit(In_sync, &rdev->flags))
629 ;
630 else
631 /* not in-sync or faulty.
632 * If reshape increases the number of devices, this
633 * section has already been recovered, else it
634 * almost certainly hasn't.
635 */
636 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100637 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000638 }
639 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100640 if (degraded2 > degraded)
641 return degraded2;
642 return degraded;
643}
644
645static int has_failed(struct r5conf *conf)
646{
647 int degraded;
648
649 if (conf->mddev->reshape_position == MaxSector)
650 return conf->mddev->degraded > conf->max_degraded;
651
652 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000653 if (degraded > conf->max_degraded)
654 return 1;
655 return 0;
656}
657
NeilBrownb5663ba2009-03-31 14:39:38 +1100658static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100659get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000660 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100663 int hash = stripe_hash_locks_hash(sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dan Williams45b42332007-07-09 11:56:43 -0700665 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Shaohua Li566c09c2013-11-14 15:16:17 +1100667 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 do {
NeilBrown72626682005-09-09 16:23:54 -0700670 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000671 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100672 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100673 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100675 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
Shaohua Li566c09c2013-11-14 15:16:17 +1100676 sh = get_free_stripe(conf, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (noblock && sh == NULL)
678 break;
679 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100680 set_bit(R5_INACTIVE_BLOCKED,
681 &conf->cache_state);
Shaohua Li566c09c2013-11-14 15:16:17 +1100682 wait_event_lock_irq(
683 conf->wait_for_stripe,
684 !list_empty(conf->inactive_list + hash) &&
685 (atomic_read(&conf->active_stripes)
686 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100687 || !test_bit(R5_INACTIVE_BLOCKED,
688 &conf->cache_state)),
Shaohua Li566c09c2013-11-14 15:16:17 +1100689 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100690 clear_bit(R5_INACTIVE_BLOCKED,
691 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100692 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100693 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100694 atomic_inc(&sh->count);
695 }
Shaohua Lie240c182014-04-09 11:27:42 +0800696 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100697 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800698 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!test_bit(STRIPE_HANDLE, &sh->state))
700 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100701 BUG_ON(list_empty(&sh->lru) &&
702 !test_bit(STRIPE_EXPANDING, &sh->state));
NeilBrown16a53ec2006-06-26 00:27:38 -0700703 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800704 if (sh->group) {
705 sh->group->stripes_cnt--;
706 sh->group = NULL;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
NeilBrown7da9d452014-01-22 11:45:03 +1100709 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100710 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 } while (sh == NULL);
713
Shaohua Li566c09c2013-11-14 15:16:17 +1100714 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return sh;
716}
717
shli@kernel.org7a87f432014-12-15 12:57:03 +1100718static bool is_full_stripe_write(struct stripe_head *sh)
719{
720 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
721 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
722}
723
shli@kernel.org59fc6302014-12-15 12:57:03 +1100724static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
725{
726 local_irq_disable();
727 if (sh1 > sh2) {
728 spin_lock(&sh2->stripe_lock);
729 spin_lock_nested(&sh1->stripe_lock, 1);
730 } else {
731 spin_lock(&sh1->stripe_lock);
732 spin_lock_nested(&sh2->stripe_lock, 1);
733 }
734}
735
736static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
737{
738 spin_unlock(&sh1->stripe_lock);
739 spin_unlock(&sh2->stripe_lock);
740 local_irq_enable();
741}
742
743/* Only freshly new full stripe normal write stripe can be added to a batch list */
744static bool stripe_can_batch(struct stripe_head *sh)
745{
746 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
747 is_full_stripe_write(sh);
748}
749
750/* we only do back search */
751static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
752{
753 struct stripe_head *head;
754 sector_t head_sector, tmp_sec;
755 int hash;
756 int dd_idx;
757
758 if (!stripe_can_batch(sh))
759 return;
760 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
761 tmp_sec = sh->sector;
762 if (!sector_div(tmp_sec, conf->chunk_sectors))
763 return;
764 head_sector = sh->sector - STRIPE_SECTORS;
765
766 hash = stripe_hash_locks_hash(head_sector);
767 spin_lock_irq(conf->hash_locks + hash);
768 head = __find_stripe(conf, head_sector, conf->generation);
769 if (head && !atomic_inc_not_zero(&head->count)) {
770 spin_lock(&conf->device_lock);
771 if (!atomic_read(&head->count)) {
772 if (!test_bit(STRIPE_HANDLE, &head->state))
773 atomic_inc(&conf->active_stripes);
774 BUG_ON(list_empty(&head->lru) &&
775 !test_bit(STRIPE_EXPANDING, &head->state));
776 list_del_init(&head->lru);
777 if (head->group) {
778 head->group->stripes_cnt--;
779 head->group = NULL;
780 }
781 }
782 atomic_inc(&head->count);
783 spin_unlock(&conf->device_lock);
784 }
785 spin_unlock_irq(conf->hash_locks + hash);
786
787 if (!head)
788 return;
789 if (!stripe_can_batch(head))
790 goto out;
791
792 lock_two_stripes(head, sh);
793 /* clear_batch_ready clear the flag */
794 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
795 goto unlock_out;
796
797 if (sh->batch_head)
798 goto unlock_out;
799
800 dd_idx = 0;
801 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
802 dd_idx++;
803 if (head->dev[dd_idx].towrite->bi_rw != sh->dev[dd_idx].towrite->bi_rw)
804 goto unlock_out;
805
806 if (head->batch_head) {
807 spin_lock(&head->batch_head->batch_lock);
808 /* This batch list is already running */
809 if (!stripe_can_batch(head)) {
810 spin_unlock(&head->batch_head->batch_lock);
811 goto unlock_out;
812 }
813
814 /*
815 * at this point, head's BATCH_READY could be cleared, but we
816 * can still add the stripe to batch list
817 */
818 list_add(&sh->batch_list, &head->batch_list);
819 spin_unlock(&head->batch_head->batch_lock);
820
821 sh->batch_head = head->batch_head;
822 } else {
823 head->batch_head = head;
824 sh->batch_head = head->batch_head;
825 spin_lock(&head->batch_lock);
826 list_add_tail(&sh->batch_list, &head->batch_list);
827 spin_unlock(&head->batch_lock);
828 }
829
830 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
831 if (atomic_dec_return(&conf->preread_active_stripes)
832 < IO_THRESHOLD)
833 md_wakeup_thread(conf->mddev->thread);
834
835 atomic_inc(&sh->count);
836unlock_out:
837 unlock_two_stripes(head, sh);
838out:
839 release_stripe(head);
840}
841
NeilBrown05616be2012-05-21 09:27:00 +1000842/* Determine if 'data_offset' or 'new_data_offset' should be used
843 * in this stripe_head.
844 */
845static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
846{
847 sector_t progress = conf->reshape_progress;
848 /* Need a memory barrier to make sure we see the value
849 * of conf->generation, or ->data_offset that was set before
850 * reshape_progress was updated.
851 */
852 smp_rmb();
853 if (progress == MaxSector)
854 return 0;
855 if (sh->generation == conf->generation - 1)
856 return 0;
857 /* We are in a reshape, and this is a new-generation stripe,
858 * so use new_data_offset.
859 */
860 return 1;
861}
862
NeilBrown6712ecf2007-09-27 12:47:43 +0200863static void
864raid5_end_read_request(struct bio *bi, int error);
865static void
866raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700867
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000868static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700869{
NeilBrownd1688a62011-10-11 16:49:52 +1100870 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700871 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100872 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -0700873
874 might_sleep();
875
876 for (i = disks; i--; ) {
877 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100878 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100879 struct bio *bi, *rbi;
880 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100881
882 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +0200883 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
884 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
885 rw = WRITE_FUA;
886 else
887 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100888 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100889 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200890 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700891 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100892 else if (test_and_clear_bit(R5_WantReplace,
893 &sh->dev[i].flags)) {
894 rw = WRITE;
895 replace_only = 1;
896 } else
Dan Williams91c00922007-01-02 13:52:30 -0700897 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000898 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
899 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700900
shli@kernel.org59fc6302014-12-15 12:57:03 +1100901again:
Dan Williams91c00922007-01-02 13:52:30 -0700902 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100903 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700904
Dan Williams91c00922007-01-02 13:52:30 -0700905 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100906 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100907 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
908 rdev = rcu_dereference(conf->disks[i].rdev);
909 if (!rdev) {
910 rdev = rrdev;
911 rrdev = NULL;
912 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100913 if (rw & WRITE) {
914 if (replace_only)
915 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100916 if (rdev == rrdev)
917 /* We raced and saw duplicates */
918 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100919 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +1100920 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100921 rdev = rrdev;
922 rrdev = NULL;
923 }
NeilBrown977df362011-12-23 10:17:53 +1100924
Dan Williams91c00922007-01-02 13:52:30 -0700925 if (rdev && test_bit(Faulty, &rdev->flags))
926 rdev = NULL;
927 if (rdev)
928 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100929 if (rrdev && test_bit(Faulty, &rrdev->flags))
930 rrdev = NULL;
931 if (rrdev)
932 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700933 rcu_read_unlock();
934
NeilBrown73e92e52011-07-28 11:39:22 +1000935 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100936 * need to check for writes. We never accept write errors
937 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000938 */
939 while ((rw & WRITE) && rdev &&
940 test_bit(WriteErrorSeen, &rdev->flags)) {
941 sector_t first_bad;
942 int bad_sectors;
943 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
944 &first_bad, &bad_sectors);
945 if (!bad)
946 break;
947
948 if (bad < 0) {
949 set_bit(BlockedBadBlocks, &rdev->flags);
950 if (!conf->mddev->external &&
951 conf->mddev->flags) {
952 /* It is very unlikely, but we might
953 * still need to write out the
954 * bad block log - better give it
955 * a chance*/
956 md_check_recovery(conf->mddev);
957 }
majianpeng18507532012-07-03 12:11:54 +1000958 /*
959 * Because md_wait_for_blocked_rdev
960 * will dec nr_pending, we must
961 * increment it first.
962 */
963 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000964 md_wait_for_blocked_rdev(rdev, conf->mddev);
965 } else {
966 /* Acknowledged bad block - skip the write */
967 rdev_dec_pending(rdev, conf->mddev);
968 rdev = NULL;
969 }
970 }
971
Dan Williams91c00922007-01-02 13:52:30 -0700972 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100973 if (s->syncing || s->expanding || s->expanded
974 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700975 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
976
Dan Williams2b7497f2008-06-28 08:31:52 +1000977 set_bit(STRIPE_IO_STARTED, &sh->state);
978
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700979 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700980 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700981 bi->bi_rw = rw;
982 bi->bi_end_io = (rw & WRITE)
983 ? raid5_end_write_request
984 : raid5_end_read_request;
985 bi->bi_private = sh;
986
Dan Williams91c00922007-01-02 13:52:30 -0700987 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700988 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700989 bi->bi_rw, i);
990 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100991 if (sh != head_sh)
992 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000993 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700994 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000995 + rdev->new_data_offset);
996 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700997 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000998 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100999 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
majianpenge59aa232013-11-14 15:16:19 +11001000 bi->bi_rw |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001001
Shaohua Lid592a992014-05-21 17:57:44 +08001002 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1003 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1004 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001005 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001006 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1007 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001008 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001009 /*
1010 * If this is discard request, set bi_vcnt 0. We don't
1011 * want to confuse SCSI because SCSI will replace payload
1012 */
1013 if (rw & REQ_DISCARD)
1014 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001015 if (rrdev)
1016 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001017
1018 if (conf->mddev->gendisk)
1019 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1020 bi, disk_devt(conf->mddev->gendisk),
1021 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -07001022 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +11001023 }
1024 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001025 if (s->syncing || s->expanding || s->expanded
1026 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001027 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1028
1029 set_bit(STRIPE_IO_STARTED, &sh->state);
1030
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001031 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001032 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001033 rbi->bi_rw = rw;
1034 BUG_ON(!(rw & WRITE));
1035 rbi->bi_end_io = raid5_end_write_request;
1036 rbi->bi_private = sh;
1037
NeilBrown977df362011-12-23 10:17:53 +11001038 pr_debug("%s: for %llu schedule op %ld on "
1039 "replacement disc %d\n",
1040 __func__, (unsigned long long)sh->sector,
1041 rbi->bi_rw, i);
1042 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001043 if (sh != head_sh)
1044 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001045 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001046 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001047 + rrdev->new_data_offset);
1048 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001049 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001050 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001051 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1052 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1053 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001054 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001055 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1056 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001057 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001058 /*
1059 * If this is discard request, set bi_vcnt 0. We don't
1060 * want to confuse SCSI because SCSI will replace payload
1061 */
1062 if (rw & REQ_DISCARD)
1063 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001064 if (conf->mddev->gendisk)
1065 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1066 rbi, disk_devt(conf->mddev->gendisk),
1067 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +11001068 generic_make_request(rbi);
1069 }
1070 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +10001071 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -07001072 set_bit(STRIPE_DEGRADED, &sh->state);
1073 pr_debug("skip op %ld on disc %d for sector %llu\n",
1074 bi->bi_rw, i, (unsigned long long)sh->sector);
1075 clear_bit(R5_LOCKED, &sh->dev[i].flags);
shli@kernel.org72ac7332014-12-15 12:57:03 +11001076 if (sh->batch_head)
1077 set_bit(STRIPE_BATCH_ERR,
1078 &sh->batch_head->state);
Dan Williams91c00922007-01-02 13:52:30 -07001079 set_bit(STRIPE_HANDLE, &sh->state);
1080 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001081
1082 if (!head_sh->batch_head)
1083 continue;
1084 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1085 batch_list);
1086 if (sh != head_sh)
1087 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001088 }
1089}
1090
1091static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001092async_copy_data(int frombio, struct bio *bio, struct page **page,
1093 sector_t sector, struct dma_async_tx_descriptor *tx,
1094 struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -07001095{
Kent Overstreet79886132013-11-23 17:19:00 -08001096 struct bio_vec bvl;
1097 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001098 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001099 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001100 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001101 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001102
Kent Overstreet4f024f32013-10-11 15:44:27 -07001103 if (bio->bi_iter.bi_sector >= sector)
1104 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001105 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001106 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001107
Dan Williams0403e382009-09-08 17:42:50 -07001108 if (frombio)
1109 flags |= ASYNC_TX_FENCE;
1110 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1111
Kent Overstreet79886132013-11-23 17:19:00 -08001112 bio_for_each_segment(bvl, bio, iter) {
1113 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001114 int clen;
1115 int b_offset = 0;
1116
1117 if (page_offset < 0) {
1118 b_offset = -page_offset;
1119 page_offset += b_offset;
1120 len -= b_offset;
1121 }
1122
1123 if (len > 0 && page_offset + len > STRIPE_SIZE)
1124 clen = STRIPE_SIZE - page_offset;
1125 else
1126 clen = len;
1127
1128 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001129 b_offset += bvl.bv_offset;
1130 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001131 if (frombio) {
1132 if (sh->raid_conf->skip_copy &&
1133 b_offset == 0 && page_offset == 0 &&
1134 clen == STRIPE_SIZE)
1135 *page = bio_page;
1136 else
1137 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001138 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001139 } else
1140 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001141 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001142 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001143 /* chain the operations */
1144 submit.depend_tx = tx;
1145
Dan Williams91c00922007-01-02 13:52:30 -07001146 if (clen < len) /* hit end of page */
1147 break;
1148 page_offset += len;
1149 }
1150
1151 return tx;
1152}
1153
1154static void ops_complete_biofill(void *stripe_head_ref)
1155{
1156 struct stripe_head *sh = stripe_head_ref;
1157 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -07001158 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001159
Harvey Harrisone46b2722008-04-28 02:15:50 -07001160 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001161 (unsigned long long)sh->sector);
1162
1163 /* clear completed biofills */
1164 for (i = sh->disks; i--; ) {
1165 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001166
1167 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001168 /* and check if we need to reply to a read request,
1169 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001170 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001171 */
1172 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001173 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001174
Dan Williams91c00922007-01-02 13:52:30 -07001175 BUG_ON(!dev->read);
1176 rbi = dev->read;
1177 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001178 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001179 dev->sector + STRIPE_SECTORS) {
1180 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10001181 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -07001182 rbi->bi_next = return_bi;
1183 return_bi = rbi;
1184 }
Dan Williams91c00922007-01-02 13:52:30 -07001185 rbi = rbi2;
1186 }
1187 }
1188 }
Dan Williams83de75c2008-06-28 08:31:58 +10001189 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001190
1191 return_io(return_bi);
1192
Dan Williamse4d84902007-09-24 10:06:13 -07001193 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001194 release_stripe(sh);
1195}
1196
1197static void ops_run_biofill(struct stripe_head *sh)
1198{
1199 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001200 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001201 int i;
1202
shli@kernel.org59fc6302014-12-15 12:57:03 +11001203 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001204 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001205 (unsigned long long)sh->sector);
1206
1207 for (i = sh->disks; i--; ) {
1208 struct r5dev *dev = &sh->dev[i];
1209 if (test_bit(R5_Wantfill, &dev->flags)) {
1210 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001211 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001212 dev->read = rbi = dev->toread;
1213 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001214 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001215 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001216 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001217 tx = async_copy_data(0, rbi, &dev->page,
1218 dev->sector, tx, sh);
Dan Williams91c00922007-01-02 13:52:30 -07001219 rbi = r5_next_bio(rbi, dev->sector);
1220 }
1221 }
1222 }
1223
1224 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001225 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1226 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001227}
1228
Dan Williams4e7d2c02009-08-29 19:13:11 -07001229static void mark_target_uptodate(struct stripe_head *sh, int target)
1230{
1231 struct r5dev *tgt;
1232
1233 if (target < 0)
1234 return;
1235
1236 tgt = &sh->dev[target];
1237 set_bit(R5_UPTODATE, &tgt->flags);
1238 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1239 clear_bit(R5_Wantcompute, &tgt->flags);
1240}
1241
Dan Williamsac6b53b2009-07-14 13:40:19 -07001242static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001243{
1244 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001245
Harvey Harrisone46b2722008-04-28 02:15:50 -07001246 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001247 (unsigned long long)sh->sector);
1248
Dan Williamsac6b53b2009-07-14 13:40:19 -07001249 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001250 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001251 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001252
Dan Williamsecc65c92008-06-28 08:31:57 +10001253 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1254 if (sh->check_state == check_state_compute_run)
1255 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001256 set_bit(STRIPE_HANDLE, &sh->state);
1257 release_stripe(sh);
1258}
1259
Dan Williamsd6f38f32009-07-14 11:50:52 -07001260/* return a pointer to the address conversion region of the scribble buffer */
1261static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001262 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001263{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001264 void *addr;
1265
1266 addr = flex_array_get(percpu->scribble, i);
1267 return addr + sizeof(struct page *) * (sh->disks + 2);
1268}
1269
1270/* return a pointer to the address conversion region of the scribble buffer */
1271static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1272{
1273 void *addr;
1274
1275 addr = flex_array_get(percpu->scribble, i);
1276 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001277}
1278
1279static struct dma_async_tx_descriptor *
1280ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1281{
Dan Williams91c00922007-01-02 13:52:30 -07001282 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001283 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001284 int target = sh->ops.target;
1285 struct r5dev *tgt = &sh->dev[target];
1286 struct page *xor_dest = tgt->page;
1287 int count = 0;
1288 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001289 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001290 int i;
1291
shli@kernel.org59fc6302014-12-15 12:57:03 +11001292 BUG_ON(sh->batch_head);
1293
Dan Williams91c00922007-01-02 13:52:30 -07001294 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001295 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001296 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1297
1298 for (i = disks; i--; )
1299 if (i != target)
1300 xor_srcs[count++] = sh->dev[i].page;
1301
1302 atomic_inc(&sh->count);
1303
Dan Williams0403e382009-09-08 17:42:50 -07001304 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001305 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001306 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001307 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001308 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001309 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001310
Dan Williams91c00922007-01-02 13:52:30 -07001311 return tx;
1312}
1313
Dan Williamsac6b53b2009-07-14 13:40:19 -07001314/* set_syndrome_sources - populate source buffers for gen_syndrome
1315 * @srcs - (struct page *) array of size sh->disks
1316 * @sh - stripe_head to parse
1317 *
1318 * Populates srcs in proper layout order for the stripe and returns the
1319 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1320 * destination buffer is recorded in srcs[count] and the Q destination
1321 * is recorded in srcs[count+1]].
1322 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001323static int set_syndrome_sources(struct page **srcs,
1324 struct stripe_head *sh,
1325 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001326{
1327 int disks = sh->disks;
1328 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1329 int d0_idx = raid6_d0(sh);
1330 int count;
1331 int i;
1332
1333 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001334 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001335
1336 count = 0;
1337 i = d0_idx;
1338 do {
1339 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001340 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001341
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001342 if (i == sh->qd_idx || i == sh->pd_idx ||
1343 (srctype == SYNDROME_SRC_ALL) ||
1344 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1345 test_bit(R5_Wantdrain, &dev->flags)) ||
1346 (srctype == SYNDROME_SRC_WRITTEN &&
1347 dev->written))
1348 srcs[slot] = sh->dev[i].page;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001349 i = raid6_next_disk(i, disks);
1350 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001351
NeilBrowne4424fe2009-10-16 16:27:34 +11001352 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001353}
1354
1355static struct dma_async_tx_descriptor *
1356ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1357{
1358 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001359 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001360 int target;
1361 int qd_idx = sh->qd_idx;
1362 struct dma_async_tx_descriptor *tx;
1363 struct async_submit_ctl submit;
1364 struct r5dev *tgt;
1365 struct page *dest;
1366 int i;
1367 int count;
1368
shli@kernel.org59fc6302014-12-15 12:57:03 +11001369 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370 if (sh->ops.target < 0)
1371 target = sh->ops.target2;
1372 else if (sh->ops.target2 < 0)
1373 target = sh->ops.target;
1374 else
1375 /* we should only have one valid target */
1376 BUG();
1377 BUG_ON(target < 0);
1378 pr_debug("%s: stripe %llu block: %d\n",
1379 __func__, (unsigned long long)sh->sector, target);
1380
1381 tgt = &sh->dev[target];
1382 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1383 dest = tgt->page;
1384
1385 atomic_inc(&sh->count);
1386
1387 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001388 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001389 blocks[count] = NULL; /* regenerating p is not necessary */
1390 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001391 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1392 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001393 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001394 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1395 } else {
1396 /* Compute any data- or p-drive using XOR */
1397 count = 0;
1398 for (i = disks; i-- ; ) {
1399 if (i == target || i == qd_idx)
1400 continue;
1401 blocks[count++] = sh->dev[i].page;
1402 }
1403
Dan Williams0403e382009-09-08 17:42:50 -07001404 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1405 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001406 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001407 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1408 }
1409
1410 return tx;
1411}
1412
1413static struct dma_async_tx_descriptor *
1414ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1415{
1416 int i, count, disks = sh->disks;
1417 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1418 int d0_idx = raid6_d0(sh);
1419 int faila = -1, failb = -1;
1420 int target = sh->ops.target;
1421 int target2 = sh->ops.target2;
1422 struct r5dev *tgt = &sh->dev[target];
1423 struct r5dev *tgt2 = &sh->dev[target2];
1424 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001425 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001426 struct async_submit_ctl submit;
1427
shli@kernel.org59fc6302014-12-15 12:57:03 +11001428 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001429 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1430 __func__, (unsigned long long)sh->sector, target, target2);
1431 BUG_ON(target < 0 || target2 < 0);
1432 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1433 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1434
Dan Williams6c910a72009-09-16 12:24:54 -07001435 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001436 * slot number conversion for 'faila' and 'failb'
1437 */
1438 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001439 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001440 count = 0;
1441 i = d0_idx;
1442 do {
1443 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1444
1445 blocks[slot] = sh->dev[i].page;
1446
1447 if (i == target)
1448 faila = slot;
1449 if (i == target2)
1450 failb = slot;
1451 i = raid6_next_disk(i, disks);
1452 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001453
1454 BUG_ON(faila == failb);
1455 if (failb < faila)
1456 swap(faila, failb);
1457 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1458 __func__, (unsigned long long)sh->sector, faila, failb);
1459
1460 atomic_inc(&sh->count);
1461
1462 if (failb == syndrome_disks+1) {
1463 /* Q disk is one of the missing disks */
1464 if (faila == syndrome_disks) {
1465 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001466 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1467 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001468 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001469 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001470 STRIPE_SIZE, &submit);
1471 } else {
1472 struct page *dest;
1473 int data_target;
1474 int qd_idx = sh->qd_idx;
1475
1476 /* Missing D+Q: recompute D from P, then recompute Q */
1477 if (target == qd_idx)
1478 data_target = target2;
1479 else
1480 data_target = target;
1481
1482 count = 0;
1483 for (i = disks; i-- ; ) {
1484 if (i == data_target || i == qd_idx)
1485 continue;
1486 blocks[count++] = sh->dev[i].page;
1487 }
1488 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001489 init_async_submit(&submit,
1490 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1491 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001492 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001493 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1494 &submit);
1495
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001496 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001497 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1498 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001499 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001500 return async_gen_syndrome(blocks, 0, count+2,
1501 STRIPE_SIZE, &submit);
1502 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001503 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001504 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1505 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001506 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001507 if (failb == syndrome_disks) {
1508 /* We're missing D+P. */
1509 return async_raid6_datap_recov(syndrome_disks+2,
1510 STRIPE_SIZE, faila,
1511 blocks, &submit);
1512 } else {
1513 /* We're missing D+D. */
1514 return async_raid6_2data_recov(syndrome_disks+2,
1515 STRIPE_SIZE, faila, failb,
1516 blocks, &submit);
1517 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001518 }
1519}
1520
Dan Williams91c00922007-01-02 13:52:30 -07001521static void ops_complete_prexor(void *stripe_head_ref)
1522{
1523 struct stripe_head *sh = stripe_head_ref;
1524
Harvey Harrisone46b2722008-04-28 02:15:50 -07001525 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001526 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001527}
1528
1529static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001530ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1531 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001532{
Dan Williams91c00922007-01-02 13:52:30 -07001533 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001534 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001535 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001536 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001537
1538 /* existing parity data subtracted */
1539 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1540
shli@kernel.org59fc6302014-12-15 12:57:03 +11001541 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001542 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001543 (unsigned long long)sh->sector);
1544
1545 for (i = disks; i--; ) {
1546 struct r5dev *dev = &sh->dev[i];
1547 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001548 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001549 xor_srcs[count++] = dev->page;
1550 }
1551
Dan Williams0403e382009-09-08 17:42:50 -07001552 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001553 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001554 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001555
1556 return tx;
1557}
1558
1559static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001560ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1561 struct dma_async_tx_descriptor *tx)
1562{
1563 struct page **blocks = to_addr_page(percpu, 0);
1564 int count;
1565 struct async_submit_ctl submit;
1566
1567 pr_debug("%s: stripe %llu\n", __func__,
1568 (unsigned long long)sh->sector);
1569
1570 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1571
1572 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1573 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1574 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1575
1576 return tx;
1577}
1578
1579static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001580ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001581{
1582 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001583 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001584 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001585
Harvey Harrisone46b2722008-04-28 02:15:50 -07001586 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001587 (unsigned long long)sh->sector);
1588
1589 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001590 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001591 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001592
shli@kernel.org59fc6302014-12-15 12:57:03 +11001593 sh = head_sh;
1594 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001595 struct bio *wbi;
1596
shli@kernel.org59fc6302014-12-15 12:57:03 +11001597again:
1598 dev = &sh->dev[i];
Shaohua Lib17459c2012-07-19 16:01:31 +10001599 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001600 chosen = dev->towrite;
1601 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001602 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001603 BUG_ON(dev->written);
1604 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001605 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001606 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001607
Kent Overstreet4f024f32013-10-11 15:44:27 -07001608 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001609 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001610 if (wbi->bi_rw & REQ_FUA)
1611 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001612 if (wbi->bi_rw & REQ_SYNC)
1613 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001614 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001615 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001616 else {
1617 tx = async_copy_data(1, wbi, &dev->page,
1618 dev->sector, tx, sh);
1619 if (dev->page != dev->orig_page) {
1620 set_bit(R5_SkipCopy, &dev->flags);
1621 clear_bit(R5_UPTODATE, &dev->flags);
1622 clear_bit(R5_OVERWRITE, &dev->flags);
1623 }
1624 }
Dan Williams91c00922007-01-02 13:52:30 -07001625 wbi = r5_next_bio(wbi, dev->sector);
1626 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001627
1628 if (head_sh->batch_head) {
1629 sh = list_first_entry(&sh->batch_list,
1630 struct stripe_head,
1631 batch_list);
1632 if (sh == head_sh)
1633 continue;
1634 goto again;
1635 }
Dan Williams91c00922007-01-02 13:52:30 -07001636 }
1637 }
1638
1639 return tx;
1640}
1641
Dan Williamsac6b53b2009-07-14 13:40:19 -07001642static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001643{
1644 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001645 int disks = sh->disks;
1646 int pd_idx = sh->pd_idx;
1647 int qd_idx = sh->qd_idx;
1648 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001649 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001650
Harvey Harrisone46b2722008-04-28 02:15:50 -07001651 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001652 (unsigned long long)sh->sector);
1653
Shaohua Libc0934f2012-05-22 13:55:05 +10001654 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001655 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001656 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001657 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001658 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001659
Dan Williams91c00922007-01-02 13:52:30 -07001660 for (i = disks; i--; ) {
1661 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001662
Tejun Heoe9c74692010-09-03 11:56:18 +02001663 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001664 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001665 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001666 if (fua)
1667 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001668 if (sync)
1669 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001670 }
Dan Williams91c00922007-01-02 13:52:30 -07001671 }
1672
Dan Williamsd8ee0722008-06-28 08:32:06 +10001673 if (sh->reconstruct_state == reconstruct_state_drain_run)
1674 sh->reconstruct_state = reconstruct_state_drain_result;
1675 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1676 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1677 else {
1678 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1679 sh->reconstruct_state = reconstruct_state_result;
1680 }
Dan Williams91c00922007-01-02 13:52:30 -07001681
1682 set_bit(STRIPE_HANDLE, &sh->state);
1683 release_stripe(sh);
1684}
1685
1686static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001687ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1688 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001689{
Dan Williams91c00922007-01-02 13:52:30 -07001690 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001691 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001692 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001693 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001694 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001695 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001696 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001697 int j = 0;
1698 struct stripe_head *head_sh = sh;
1699 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001700
Harvey Harrisone46b2722008-04-28 02:15:50 -07001701 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001702 (unsigned long long)sh->sector);
1703
Shaohua Li620125f2012-10-11 13:49:05 +11001704 for (i = 0; i < sh->disks; i++) {
1705 if (pd_idx == i)
1706 continue;
1707 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1708 break;
1709 }
1710 if (i >= sh->disks) {
1711 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001712 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1713 ops_complete_reconstruct(sh);
1714 return;
1715 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001716again:
1717 count = 0;
1718 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001719 /* check if prexor is active which means only process blocks
1720 * that are part of a read-modify-write (written)
1721 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001722 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001723 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001724 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1725 for (i = disks; i--; ) {
1726 struct r5dev *dev = &sh->dev[i];
shli@kernel.org59fc6302014-12-15 12:57:03 +11001727 if (head_sh->dev[i].written)
Dan Williams91c00922007-01-02 13:52:30 -07001728 xor_srcs[count++] = dev->page;
1729 }
1730 } else {
1731 xor_dest = sh->dev[pd_idx].page;
1732 for (i = disks; i--; ) {
1733 struct r5dev *dev = &sh->dev[i];
1734 if (i != pd_idx)
1735 xor_srcs[count++] = dev->page;
1736 }
1737 }
1738
Dan Williams91c00922007-01-02 13:52:30 -07001739 /* 1/ if we prexor'd then the dest is reused as a source
1740 * 2/ if we did not prexor then we are redoing the parity
1741 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1742 * for the synchronous xor case
1743 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001744 last_stripe = !head_sh->batch_head ||
1745 list_first_entry(&sh->batch_list,
1746 struct stripe_head, batch_list) == head_sh;
1747 if (last_stripe) {
1748 flags = ASYNC_TX_ACK |
1749 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001750
shli@kernel.org59fc6302014-12-15 12:57:03 +11001751 atomic_inc(&head_sh->count);
1752 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1753 to_addr_conv(sh, percpu, j));
1754 } else {
1755 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1756 init_async_submit(&submit, flags, tx, NULL, NULL,
1757 to_addr_conv(sh, percpu, j));
1758 }
Dan Williams91c00922007-01-02 13:52:30 -07001759
Dan Williamsa08abd82009-06-03 11:43:59 -07001760 if (unlikely(count == 1))
1761 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1762 else
1763 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001764 if (!last_stripe) {
1765 j++;
1766 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1767 batch_list);
1768 goto again;
1769 }
Dan Williams91c00922007-01-02 13:52:30 -07001770}
1771
Dan Williamsac6b53b2009-07-14 13:40:19 -07001772static void
1773ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1774 struct dma_async_tx_descriptor *tx)
1775{
1776 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001777 struct page **blocks;
1778 int count, i, j = 0;
1779 struct stripe_head *head_sh = sh;
1780 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001781 int synflags;
1782 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001783
1784 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1785
Shaohua Li620125f2012-10-11 13:49:05 +11001786 for (i = 0; i < sh->disks; i++) {
1787 if (sh->pd_idx == i || sh->qd_idx == i)
1788 continue;
1789 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1790 break;
1791 }
1792 if (i >= sh->disks) {
1793 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001794 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1795 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1796 ops_complete_reconstruct(sh);
1797 return;
1798 }
1799
shli@kernel.org59fc6302014-12-15 12:57:03 +11001800again:
1801 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001802
1803 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1804 synflags = SYNDROME_SRC_WRITTEN;
1805 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1806 } else {
1807 synflags = SYNDROME_SRC_ALL;
1808 txflags = ASYNC_TX_ACK;
1809 }
1810
1811 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001812 last_stripe = !head_sh->batch_head ||
1813 list_first_entry(&sh->batch_list,
1814 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001815
shli@kernel.org59fc6302014-12-15 12:57:03 +11001816 if (last_stripe) {
1817 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001818 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001819 head_sh, to_addr_conv(sh, percpu, j));
1820 } else
1821 init_async_submit(&submit, 0, tx, NULL, NULL,
1822 to_addr_conv(sh, percpu, j));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001823 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001824 if (!last_stripe) {
1825 j++;
1826 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1827 batch_list);
1828 goto again;
1829 }
Dan Williams91c00922007-01-02 13:52:30 -07001830}
1831
1832static void ops_complete_check(void *stripe_head_ref)
1833{
1834 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001835
Harvey Harrisone46b2722008-04-28 02:15:50 -07001836 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001837 (unsigned long long)sh->sector);
1838
Dan Williamsecc65c92008-06-28 08:31:57 +10001839 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001840 set_bit(STRIPE_HANDLE, &sh->state);
1841 release_stripe(sh);
1842}
1843
Dan Williamsac6b53b2009-07-14 13:40:19 -07001844static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001845{
Dan Williams91c00922007-01-02 13:52:30 -07001846 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001847 int pd_idx = sh->pd_idx;
1848 int qd_idx = sh->qd_idx;
1849 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001850 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001851 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001852 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001853 int count;
1854 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001855
Harvey Harrisone46b2722008-04-28 02:15:50 -07001856 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001857 (unsigned long long)sh->sector);
1858
shli@kernel.org59fc6302014-12-15 12:57:03 +11001859 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001860 count = 0;
1861 xor_dest = sh->dev[pd_idx].page;
1862 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001863 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001864 if (i == pd_idx || i == qd_idx)
1865 continue;
1866 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001867 }
1868
Dan Williamsd6f38f32009-07-14 11:50:52 -07001869 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001870 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001871 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001872 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001873
Dan Williams91c00922007-01-02 13:52:30 -07001874 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001875 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1876 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001877}
1878
Dan Williamsac6b53b2009-07-14 13:40:19 -07001879static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1880{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001881 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001882 struct async_submit_ctl submit;
1883 int count;
1884
1885 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1886 (unsigned long long)sh->sector, checkp);
1887
shli@kernel.org59fc6302014-12-15 12:57:03 +11001888 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001889 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001890 if (!checkp)
1891 srcs[count] = NULL;
1892
1893 atomic_inc(&sh->count);
1894 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001895 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001896 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1897 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1898}
1899
NeilBrown51acbce2013-02-28 09:08:34 +11001900static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001901{
1902 int overlap_clear = 0, i, disks = sh->disks;
1903 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001904 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001905 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001906 struct raid5_percpu *percpu;
1907 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001908
Dan Williamsd6f38f32009-07-14 11:50:52 -07001909 cpu = get_cpu();
1910 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001911 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001912 ops_run_biofill(sh);
1913 overlap_clear++;
1914 }
1915
Dan Williams7b3a8712008-06-28 08:32:09 +10001916 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001917 if (level < 6)
1918 tx = ops_run_compute5(sh, percpu);
1919 else {
1920 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1921 tx = ops_run_compute6_1(sh, percpu);
1922 else
1923 tx = ops_run_compute6_2(sh, percpu);
1924 }
1925 /* terminate the chain if reconstruct is not set to be run */
1926 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001927 async_tx_ack(tx);
1928 }
Dan Williams91c00922007-01-02 13:52:30 -07001929
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001930 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
1931 if (level < 6)
1932 tx = ops_run_prexor5(sh, percpu, tx);
1933 else
1934 tx = ops_run_prexor6(sh, percpu, tx);
1935 }
Dan Williams91c00922007-01-02 13:52:30 -07001936
Dan Williams600aa102008-06-28 08:32:05 +10001937 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001938 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001939 overlap_clear++;
1940 }
1941
Dan Williamsac6b53b2009-07-14 13:40:19 -07001942 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1943 if (level < 6)
1944 ops_run_reconstruct5(sh, percpu, tx);
1945 else
1946 ops_run_reconstruct6(sh, percpu, tx);
1947 }
Dan Williams91c00922007-01-02 13:52:30 -07001948
Dan Williamsac6b53b2009-07-14 13:40:19 -07001949 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1950 if (sh->check_state == check_state_run)
1951 ops_run_check_p(sh, percpu);
1952 else if (sh->check_state == check_state_run_q)
1953 ops_run_check_pq(sh, percpu, 0);
1954 else if (sh->check_state == check_state_run_pq)
1955 ops_run_check_pq(sh, percpu, 1);
1956 else
1957 BUG();
1958 }
Dan Williams91c00922007-01-02 13:52:30 -07001959
shli@kernel.org59fc6302014-12-15 12:57:03 +11001960 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07001961 for (i = disks; i--; ) {
1962 struct r5dev *dev = &sh->dev[i];
1963 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1964 wake_up(&sh->raid_conf->wait_for_overlap);
1965 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001966 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001967}
1968
NeilBrown486f0642015-02-25 12:10:35 +11001969static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct stripe_head *sh;
NeilBrowna9683a72015-02-25 12:02:51 +11001972 sh = kmem_cache_zalloc(conf->slab_cache, gfp);
NeilBrown3f294f42005-11-08 21:39:25 -08001973 if (!sh)
1974 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001975
NeilBrown3f294f42005-11-08 21:39:25 -08001976 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001977
Shaohua Lib17459c2012-07-19 16:01:31 +10001978 spin_lock_init(&sh->stripe_lock);
1979
NeilBrowna9683a72015-02-25 12:02:51 +11001980 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10001981 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001982 kmem_cache_free(conf->slab_cache, sh);
1983 return 0;
1984 }
NeilBrown486f0642015-02-25 12:10:35 +11001985 sh->hash_lock_index =
1986 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08001987 /* we just created an active stripe so... */
1988 atomic_set(&sh->count, 1);
1989 atomic_inc(&conf->active_stripes);
1990 INIT_LIST_HEAD(&sh->lru);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001991
1992 spin_lock_init(&sh->batch_lock);
1993 INIT_LIST_HEAD(&sh->batch_list);
1994 sh->batch_head = NULL;
NeilBrown3f294f42005-11-08 21:39:25 -08001995 release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11001996 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08001997 return 1;
1998}
1999
NeilBrownd1688a62011-10-11 16:49:52 +11002000static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002001{
Christoph Lametere18b8902006-12-06 20:33:20 -08002002 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002003 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
NeilBrownf4be6b42010-06-01 19:37:25 +10002005 if (conf->mddev->gendisk)
2006 sprintf(conf->cache_name[0],
2007 "raid%d-%s", conf->level, mdname(conf->mddev));
2008 else
2009 sprintf(conf->cache_name[0],
2010 "raid%d-%p", conf->level, conf->mddev);
2011 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2012
NeilBrownad01c9e2006-03-27 01:18:07 -08002013 conf->active_name = 0;
2014 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002016 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (!sc)
2018 return 1;
2019 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002020 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002021 while (num--)
2022 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return 0;
2026}
NeilBrown29269552006-03-27 01:18:10 -08002027
Dan Williamsd6f38f32009-07-14 11:50:52 -07002028/**
2029 * scribble_len - return the required size of the scribble region
2030 * @num - total number of disks in the array
2031 *
2032 * The size must be enough to contain:
2033 * 1/ a struct page pointer for each device in the array +2
2034 * 2/ room to convert each entry in (1) to its corresponding dma
2035 * (dma_map_page()) or page (page_address()) address.
2036 *
2037 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2038 * calculate over all devices (not just the data blocks), using zeros in place
2039 * of the P and Q blocks.
2040 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002041static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002042{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002043 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002044 size_t len;
2045
2046 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002047 ret = flex_array_alloc(len, cnt, flags);
2048 if (!ret)
2049 return NULL;
2050 /* always prealloc all elements, so no locking is required */
2051 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2052 flex_array_free(ret);
2053 return NULL;
2054 }
2055 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002056}
2057
NeilBrownd1688a62011-10-11 16:49:52 +11002058static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002059{
2060 /* Make all the stripes able to hold 'newsize' devices.
2061 * New slots in each stripe get 'page' set to a new page.
2062 *
2063 * This happens in stages:
2064 * 1/ create a new kmem_cache and allocate the required number of
2065 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002066 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002067 * to the new stripe_heads. This will have the side effect of
2068 * freezing the array as once all stripe_heads have been collected,
2069 * no IO will be possible. Old stripe heads are freed once their
2070 * pages have been transferred over, and the old kmem_cache is
2071 * freed when all stripes are done.
2072 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2073 * we simple return a failre status - no need to clean anything up.
2074 * 4/ allocate new pages for the new slots in the new stripe_heads.
2075 * If this fails, we don't bother trying the shrink the
2076 * stripe_heads down again, we just leave them as they are.
2077 * As each stripe_head is processed the new one is released into
2078 * active service.
2079 *
2080 * Once step2 is started, we cannot afford to wait for a write,
2081 * so we use GFP_NOIO allocations.
2082 */
2083 struct stripe_head *osh, *nsh;
2084 LIST_HEAD(newstripes);
2085 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002086 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002087 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002088 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002089 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002090 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002091
2092 if (newsize <= conf->pool_size)
2093 return 0; /* never bother to shrink */
2094
Dan Williamsb5470dc2008-06-27 21:44:04 -07002095 err = md_allow_write(conf->mddev);
2096 if (err)
2097 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002098
NeilBrownad01c9e2006-03-27 01:18:07 -08002099 /* Step 1 */
2100 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2101 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002102 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002103 if (!sc)
2104 return -ENOMEM;
2105
2106 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10002107 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002108 if (!nsh)
2109 break;
2110
NeilBrownad01c9e2006-03-27 01:18:07 -08002111 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10002112 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08002113
2114 list_add(&nsh->lru, &newstripes);
2115 }
2116 if (i) {
2117 /* didn't get enough, give up */
2118 while (!list_empty(&newstripes)) {
2119 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2120 list_del(&nsh->lru);
2121 kmem_cache_free(sc, nsh);
2122 }
2123 kmem_cache_destroy(sc);
2124 return -ENOMEM;
2125 }
2126 /* Step 2 - Must use GFP_NOIO now.
2127 * OK, we have enough stripes, start collecting inactive
2128 * stripes and copying them over
2129 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002130 hash = 0;
2131 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002132 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002133 lock_device_hash_lock(conf, hash);
2134 wait_event_cmd(conf->wait_for_stripe,
2135 !list_empty(conf->inactive_list + hash),
2136 unlock_device_hash_lock(conf, hash),
2137 lock_device_hash_lock(conf, hash));
2138 osh = get_free_stripe(conf, hash);
2139 unlock_device_hash_lock(conf, hash);
NeilBrownad01c9e2006-03-27 01:18:07 -08002140 atomic_set(&nsh->count, 1);
Shaohua Lid592a992014-05-21 17:57:44 +08002141 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002142 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002143 nsh->dev[i].orig_page = osh->dev[i].page;
2144 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002145 for( ; i<newsize; i++)
2146 nsh->dev[i].page = NULL;
Shaohua Li566c09c2013-11-14 15:16:17 +11002147 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002148 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002149 cnt++;
2150 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2151 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2152 hash++;
2153 cnt = 0;
2154 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002155 }
2156 kmem_cache_destroy(conf->slab_cache);
2157
2158 /* Step 3.
2159 * At this point, we are holding all the stripes so the array
2160 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002161 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002162 */
2163 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2164 if (ndisks) {
2165 for (i=0; i<conf->raid_disks; i++)
2166 ndisks[i] = conf->disks[i];
2167 kfree(conf->disks);
2168 conf->disks = ndisks;
2169 } else
2170 err = -ENOMEM;
2171
Dan Williamsd6f38f32009-07-14 11:50:52 -07002172 get_online_cpus();
Dan Williamsd6f38f32009-07-14 11:50:52 -07002173 for_each_present_cpu(cpu) {
2174 struct raid5_percpu *percpu;
shli@kernel.org46d5b782014-12-15 12:57:02 +11002175 struct flex_array *scribble;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002176
2177 percpu = per_cpu_ptr(conf->percpu, cpu);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002178 scribble = scribble_alloc(newsize, conf->chunk_sectors /
2179 STRIPE_SECTORS, GFP_NOIO);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002180
2181 if (scribble) {
shli@kernel.org46d5b782014-12-15 12:57:02 +11002182 flex_array_free(percpu->scribble);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002183 percpu->scribble = scribble;
2184 } else {
2185 err = -ENOMEM;
2186 break;
2187 }
2188 }
2189 put_online_cpus();
2190
NeilBrownad01c9e2006-03-27 01:18:07 -08002191 /* Step 4, return new stripes to service */
2192 while(!list_empty(&newstripes)) {
2193 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2194 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002195
NeilBrownad01c9e2006-03-27 01:18:07 -08002196 for (i=conf->raid_disks; i < newsize; i++)
2197 if (nsh->dev[i].page == NULL) {
2198 struct page *p = alloc_page(GFP_NOIO);
2199 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002200 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002201 if (!p)
2202 err = -ENOMEM;
2203 }
2204 release_stripe(nsh);
2205 }
2206 /* critical section pass, GFP_NOIO no longer needed */
2207
2208 conf->slab_cache = sc;
2209 conf->active_name = 1-conf->active_name;
2210 conf->pool_size = newsize;
2211 return err;
2212}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
NeilBrown486f0642015-02-25 12:10:35 +11002214static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 struct stripe_head *sh;
NeilBrown486f0642015-02-25 12:10:35 +11002217 int hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Shaohua Li566c09c2013-11-14 15:16:17 +11002219 spin_lock_irq(conf->hash_locks + hash);
2220 sh = get_free_stripe(conf, hash);
2221 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002222 if (!sh)
2223 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002224 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002225 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002226 kmem_cache_free(conf->slab_cache, sh);
2227 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002228 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002229 return 1;
2230}
2231
NeilBrownd1688a62011-10-11 16:49:52 +11002232static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002233{
NeilBrown486f0642015-02-25 12:10:35 +11002234 while (conf->max_nr_stripes &&
2235 drop_one_stripe(conf))
2236 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002237
NeilBrown29fc7e32006-02-03 03:03:41 -08002238 if (conf->slab_cache)
2239 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 conf->slab_cache = NULL;
2241}
2242
NeilBrown6712ecf2007-09-27 12:47:43 +02002243static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
NeilBrown99c0fb52009-03-31 14:39:38 +11002245 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002246 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002247 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07002249 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002250 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002251 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 for (i=0 ; i<disks; i++)
2254 if (bi == &sh->dev[i].req)
2255 break;
2256
Dan Williams45b42332007-07-09 11:56:43 -07002257 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
2258 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 uptodate);
2260 if (i == disks) {
2261 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002262 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
NeilBrown14a75d32011-12-23 10:17:52 +11002264 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002265 /* If replacement finished while this request was outstanding,
2266 * 'replacement' might be NULL already.
2267 * In that case it moved down to 'rdev'.
2268 * rdev is not removed until all requests are finished.
2269 */
NeilBrown14a75d32011-12-23 10:17:52 +11002270 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002271 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002272 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
NeilBrown05616be2012-05-21 09:27:00 +10002274 if (use_new_offset(conf, sh))
2275 s = sh->sector + rdev->new_data_offset;
2276 else
2277 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002280 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002281 /* Note that this cannot happen on a
2282 * replacement device. We just fail those on
2283 * any error
2284 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10002285 printk_ratelimited(
2286 KERN_INFO
2287 "md/raid:%s: read error corrected"
2288 " (%lu sectors at %llu on %s)\n",
2289 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002290 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002291 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002292 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002293 clear_bit(R5_ReadError, &sh->dev[i].flags);
2294 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002295 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2296 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2297
NeilBrown14a75d32011-12-23 10:17:52 +11002298 if (atomic_read(&rdev->read_errors))
2299 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002301 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002302 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002303 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002306 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002307 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2308 printk_ratelimited(
2309 KERN_WARNING
2310 "md/raid:%s: read error on replacement device "
2311 "(sector %llu on %s).\n",
2312 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002313 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002314 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002315 else if (conf->mddev->degraded >= conf->max_degraded) {
2316 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002317 printk_ratelimited(
2318 KERN_WARNING
2319 "md/raid:%s: read error not correctable "
2320 "(sector %llu on %s).\n",
2321 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002322 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002323 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002324 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002325 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002326 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002327 printk_ratelimited(
2328 KERN_WARNING
2329 "md/raid:%s: read error NOT corrected!! "
2330 "(sector %llu on %s).\n",
2331 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002332 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002333 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002334 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002335 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002336 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002337 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002338 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002339 else
2340 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002341 if (set_bad && test_bit(In_sync, &rdev->flags)
2342 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2343 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002344 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002345 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2346 set_bit(R5_ReadError, &sh->dev[i].flags);
2347 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2348 } else
2349 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002350 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002351 clear_bit(R5_ReadError, &sh->dev[i].flags);
2352 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002353 if (!(set_bad
2354 && test_bit(In_sync, &rdev->flags)
2355 && rdev_set_badblocks(
2356 rdev, sh->sector, STRIPE_SECTORS, 0)))
2357 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
NeilBrown14a75d32011-12-23 10:17:52 +11002360 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2362 set_bit(STRIPE_HANDLE, &sh->state);
2363 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
NeilBrownd710e132008-10-13 11:55:12 +11002366static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
NeilBrown99c0fb52009-03-31 14:39:38 +11002368 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002369 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002370 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002371 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10002373 sector_t first_bad;
2374 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002375 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
NeilBrown977df362011-12-23 10:17:53 +11002377 for (i = 0 ; i < disks; i++) {
2378 if (bi == &sh->dev[i].req) {
2379 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 break;
NeilBrown977df362011-12-23 10:17:53 +11002381 }
2382 if (bi == &sh->dev[i].rreq) {
2383 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002384 if (rdev)
2385 replacement = 1;
2386 else
2387 /* rdev was removed and 'replacement'
2388 * replaced it. rdev is not removed
2389 * until all requests are finished.
2390 */
2391 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002392 break;
2393 }
2394 }
Dan Williams45b42332007-07-09 11:56:43 -07002395 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2397 uptodate);
2398 if (i == disks) {
2399 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002400 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402
NeilBrown977df362011-12-23 10:17:53 +11002403 if (replacement) {
2404 if (!uptodate)
2405 md_error(conf->mddev, rdev);
2406 else if (is_badblock(rdev, sh->sector,
2407 STRIPE_SECTORS,
2408 &first_bad, &bad_sectors))
2409 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2410 } else {
2411 if (!uptodate) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002412 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002413 set_bit(WriteErrorSeen, &rdev->flags);
2414 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002415 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2416 set_bit(MD_RECOVERY_NEEDED,
2417 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002418 } else if (is_badblock(rdev, sh->sector,
2419 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002420 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002421 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002422 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2423 /* That was a successful write so make
2424 * sure it looks like we already did
2425 * a re-write.
2426 */
2427 set_bit(R5_ReWrite, &sh->dev[i].flags);
2428 }
NeilBrown977df362011-12-23 10:17:53 +11002429 }
2430 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
shli@kernel.org72ac7332014-12-15 12:57:03 +11002432 if (sh->batch_head && !uptodate)
2433 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2434
NeilBrown977df362011-12-23 10:17:53 +11002435 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2436 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07002438 release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002439
2440 if (sh->batch_head && sh != sh->batch_head)
2441 release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
NeilBrown784052e2009-03-31 15:19:07 +11002444static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Shaohua Lid592a992014-05-21 17:57:44 +08002445
NeilBrown784052e2009-03-31 15:19:07 +11002446static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
2448 struct r5dev *dev = &sh->dev[i];
2449
2450 bio_init(&dev->req);
2451 dev->req.bi_io_vec = &dev->vec;
Shaohua Lid592a992014-05-21 17:57:44 +08002452 dev->req.bi_max_vecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 dev->req.bi_private = sh;
2454
NeilBrown977df362011-12-23 10:17:53 +11002455 bio_init(&dev->rreq);
2456 dev->rreq.bi_io_vec = &dev->rvec;
Shaohua Lid592a992014-05-21 17:57:44 +08002457 dev->rreq.bi_max_vecs = 1;
NeilBrown977df362011-12-23 10:17:53 +11002458 dev->rreq.bi_private = sh;
NeilBrown977df362011-12-23 10:17:53 +11002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002461 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
NeilBrownfd01b882011-10-11 16:47:53 +11002464static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002467 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002468 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002469 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
NeilBrown908f4fb2011-12-23 10:17:50 +11002471 spin_lock_irqsave(&conf->device_lock, flags);
2472 clear_bit(In_sync, &rdev->flags);
2473 mddev->degraded = calc_degraded(conf);
2474 spin_unlock_irqrestore(&conf->device_lock, flags);
2475 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2476
NeilBrownde393cd2011-07-28 11:31:48 +10002477 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002478 set_bit(Faulty, &rdev->flags);
2479 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2480 printk(KERN_ALERT
2481 "md/raid:%s: Disk failure on %s, disabling device.\n"
2482 "md/raid:%s: Operation continuing on %d devices.\n",
2483 mdname(mddev),
2484 bdevname(rdev->bdev, b),
2485 mdname(mddev),
2486 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489/*
2490 * Input: a 'big' sector number,
2491 * Output: index of the data and parity disk, and the sector # in them.
2492 */
NeilBrownd1688a62011-10-11 16:49:52 +11002493static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002494 int previous, int *dd_idx,
2495 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002497 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002498 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002500 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002501 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002503 int algorithm = previous ? conf->prev_algo
2504 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002505 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2506 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002507 int raid_disks = previous ? conf->previous_raid_disks
2508 : conf->raid_disks;
2509 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /* First compute the information on this sector */
2512
2513 /*
2514 * Compute the chunk number and the sector offset inside the chunk
2515 */
2516 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2517 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /*
2520 * Compute the stripe number
2521 */
NeilBrown35f2a592010-04-20 14:13:34 +10002522 stripe = chunk_number;
2523 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002524 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * Select the parity disk based on the user selected algorithm.
2527 */
NeilBrown84789552011-07-27 11:00:36 +10002528 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002529 switch(conf->level) {
2530 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002531 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002532 break;
2533 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002534 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002536 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002537 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 (*dd_idx)++;
2539 break;
2540 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002541 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002542 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 (*dd_idx)++;
2544 break;
2545 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002546 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002547 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 break;
2549 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002550 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002551 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002553 case ALGORITHM_PARITY_0:
2554 pd_idx = 0;
2555 (*dd_idx)++;
2556 break;
2557 case ALGORITHM_PARITY_N:
2558 pd_idx = data_disks;
2559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002561 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002562 }
2563 break;
2564 case 6:
2565
NeilBrowne183eae2009-03-31 15:20:22 +11002566 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002567 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002568 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002569 qd_idx = pd_idx + 1;
2570 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002571 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002572 qd_idx = 0;
2573 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002574 (*dd_idx) += 2; /* D D P Q D */
2575 break;
2576 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002577 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002578 qd_idx = pd_idx + 1;
2579 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002580 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002581 qd_idx = 0;
2582 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002583 (*dd_idx) += 2; /* D D P Q D */
2584 break;
2585 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002586 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002587 qd_idx = (pd_idx + 1) % raid_disks;
2588 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002589 break;
2590 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002591 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002592 qd_idx = (pd_idx + 1) % raid_disks;
2593 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002594 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002595
2596 case ALGORITHM_PARITY_0:
2597 pd_idx = 0;
2598 qd_idx = 1;
2599 (*dd_idx) += 2;
2600 break;
2601 case ALGORITHM_PARITY_N:
2602 pd_idx = data_disks;
2603 qd_idx = data_disks + 1;
2604 break;
2605
2606 case ALGORITHM_ROTATING_ZERO_RESTART:
2607 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2608 * of blocks for computing Q is different.
2609 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002610 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002611 qd_idx = pd_idx + 1;
2612 if (pd_idx == raid_disks-1) {
2613 (*dd_idx)++; /* Q D D D P */
2614 qd_idx = 0;
2615 } else if (*dd_idx >= pd_idx)
2616 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002617 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002618 break;
2619
2620 case ALGORITHM_ROTATING_N_RESTART:
2621 /* Same a left_asymmetric, by first stripe is
2622 * D D D P Q rather than
2623 * Q D D D P
2624 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002625 stripe2 += 1;
2626 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002627 qd_idx = pd_idx + 1;
2628 if (pd_idx == raid_disks-1) {
2629 (*dd_idx)++; /* Q D D D P */
2630 qd_idx = 0;
2631 } else if (*dd_idx >= pd_idx)
2632 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002633 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002634 break;
2635
2636 case ALGORITHM_ROTATING_N_CONTINUE:
2637 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002638 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002639 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2640 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002641 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002642 break;
2643
2644 case ALGORITHM_LEFT_ASYMMETRIC_6:
2645 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002646 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002647 if (*dd_idx >= pd_idx)
2648 (*dd_idx)++;
2649 qd_idx = raid_disks - 1;
2650 break;
2651
2652 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002653 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002654 if (*dd_idx >= pd_idx)
2655 (*dd_idx)++;
2656 qd_idx = raid_disks - 1;
2657 break;
2658
2659 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002660 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002661 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2662 qd_idx = raid_disks - 1;
2663 break;
2664
2665 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002666 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002667 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2668 qd_idx = raid_disks - 1;
2669 break;
2670
2671 case ALGORITHM_PARITY_0_6:
2672 pd_idx = 0;
2673 (*dd_idx)++;
2674 qd_idx = raid_disks - 1;
2675 break;
2676
NeilBrown16a53ec2006-06-26 00:27:38 -07002677 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002678 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002679 }
2680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682
NeilBrown911d4ee2009-03-31 14:39:38 +11002683 if (sh) {
2684 sh->pd_idx = pd_idx;
2685 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002686 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 /*
2689 * Finally, compute the new sector number
2690 */
2691 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2692 return new_sector;
2693}
2694
NeilBrown784052e2009-03-31 15:19:07 +11002695static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
NeilBrownd1688a62011-10-11 16:49:52 +11002697 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002698 int raid_disks = sh->disks;
2699 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002701 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2702 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002703 int algorithm = previous ? conf->prev_algo
2704 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 sector_t stripe;
2706 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002707 sector_t chunk_number;
2708 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002710 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2713 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
NeilBrown16a53ec2006-06-26 00:27:38 -07002715 if (i == sh->pd_idx)
2716 return 0;
2717 switch(conf->level) {
2718 case 4: break;
2719 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002720 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 case ALGORITHM_LEFT_ASYMMETRIC:
2722 case ALGORITHM_RIGHT_ASYMMETRIC:
2723 if (i > sh->pd_idx)
2724 i--;
2725 break;
2726 case ALGORITHM_LEFT_SYMMETRIC:
2727 case ALGORITHM_RIGHT_SYMMETRIC:
2728 if (i < sh->pd_idx)
2729 i += raid_disks;
2730 i -= (sh->pd_idx + 1);
2731 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002732 case ALGORITHM_PARITY_0:
2733 i -= 1;
2734 break;
2735 case ALGORITHM_PARITY_N:
2736 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002738 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002739 }
2740 break;
2741 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002742 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002743 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002744 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002745 case ALGORITHM_LEFT_ASYMMETRIC:
2746 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002747 case ALGORITHM_ROTATING_ZERO_RESTART:
2748 case ALGORITHM_ROTATING_N_RESTART:
2749 if (sh->pd_idx == raid_disks-1)
2750 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002751 else if (i > sh->pd_idx)
2752 i -= 2; /* D D P Q D */
2753 break;
2754 case ALGORITHM_LEFT_SYMMETRIC:
2755 case ALGORITHM_RIGHT_SYMMETRIC:
2756 if (sh->pd_idx == raid_disks-1)
2757 i--; /* Q D D D P */
2758 else {
2759 /* D D P Q D */
2760 if (i < sh->pd_idx)
2761 i += raid_disks;
2762 i -= (sh->pd_idx + 2);
2763 }
2764 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002765 case ALGORITHM_PARITY_0:
2766 i -= 2;
2767 break;
2768 case ALGORITHM_PARITY_N:
2769 break;
2770 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002771 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002772 if (sh->pd_idx == 0)
2773 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002774 else {
2775 /* D D Q P D */
2776 if (i < sh->pd_idx)
2777 i += raid_disks;
2778 i -= (sh->pd_idx + 1);
2779 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002780 break;
2781 case ALGORITHM_LEFT_ASYMMETRIC_6:
2782 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2783 if (i > sh->pd_idx)
2784 i--;
2785 break;
2786 case ALGORITHM_LEFT_SYMMETRIC_6:
2787 case ALGORITHM_RIGHT_SYMMETRIC_6:
2788 if (i < sh->pd_idx)
2789 i += data_disks + 1;
2790 i -= (sh->pd_idx + 1);
2791 break;
2792 case ALGORITHM_PARITY_0_6:
2793 i -= 1;
2794 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002795 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002796 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002797 }
2798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800
2801 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002802 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
NeilBrown112bf892009-03-31 14:39:38 +11002804 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002805 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002806 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2807 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002808 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2809 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return 0;
2811 }
2812 return r_sector;
2813}
2814
Dan Williams600aa102008-06-28 08:32:05 +10002815static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002816schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002817 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002818{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002819 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002820 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002821 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002822
Dan Williamse33129d2007-01-02 13:52:30 -07002823 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002824
2825 for (i = disks; i--; ) {
2826 struct r5dev *dev = &sh->dev[i];
2827
2828 if (dev->towrite) {
2829 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002830 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002831 if (!expand)
2832 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002833 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002834 }
2835 }
NeilBrownce7d3632013-03-04 12:37:14 +11002836 /* if we are not expanding this is a proper write request, and
2837 * there will be bios with new data to be drained into the
2838 * stripe cache
2839 */
2840 if (!expand) {
2841 if (!s->locked)
2842 /* False alarm, nothing to do */
2843 return;
2844 sh->reconstruct_state = reconstruct_state_drain_run;
2845 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2846 } else
2847 sh->reconstruct_state = reconstruct_state_run;
2848
2849 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2850
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002851 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002852 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002853 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002854 } else {
2855 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2856 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002857 BUG_ON(level == 6 &&
2858 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
2859 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07002860
Dan Williamse33129d2007-01-02 13:52:30 -07002861 for (i = disks; i--; ) {
2862 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002863 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07002864 continue;
2865
Dan Williamse33129d2007-01-02 13:52:30 -07002866 if (dev->towrite &&
2867 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002868 test_bit(R5_Wantcompute, &dev->flags))) {
2869 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002870 set_bit(R5_LOCKED, &dev->flags);
2871 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002872 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002873 }
2874 }
NeilBrownce7d3632013-03-04 12:37:14 +11002875 if (!s->locked)
2876 /* False alarm - nothing to do */
2877 return;
2878 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2879 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2880 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2881 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002882 }
2883
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002884 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002885 * are in flight
2886 */
2887 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2888 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002889 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002890
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002891 if (level == 6) {
2892 int qd_idx = sh->qd_idx;
2893 struct r5dev *dev = &sh->dev[qd_idx];
2894
2895 set_bit(R5_LOCKED, &dev->flags);
2896 clear_bit(R5_UPTODATE, &dev->flags);
2897 s->locked++;
2898 }
2899
Dan Williams600aa102008-06-28 08:32:05 +10002900 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002901 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002902 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002903}
NeilBrown16a53ec2006-06-26 00:27:38 -07002904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905/*
2906 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002907 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 * The bi_next chain must be in order.
2909 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11002910static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
2911 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002914 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002915 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
NeilBrowncbe47ec2011-07-26 11:20:35 +10002917 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002918 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 (unsigned long long)sh->sector);
2920
Shaohua Lib17459c2012-07-19 16:01:31 +10002921 /*
2922 * If several bio share a stripe. The bio bi_phys_segments acts as a
2923 * reference count to avoid race. The reference count should already be
2924 * increased before this function is called (for example, in
2925 * make_request()), so other bio sharing this stripe will not free the
2926 * stripe. If a stripe is owned by one stripe, the stripe lock will
2927 * protect it.
2928 */
2929 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002930 /* Don't allow new IO added to stripes in batch list */
2931 if (sh->batch_head)
2932 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07002933 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002935 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002936 firstwrite = 1;
2937 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002939 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
2940 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 goto overlap;
2942 bip = & (*bip)->bi_next;
2943 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07002944 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 goto overlap;
2946
shli@kernel.orgda41ba62014-12-15 12:57:03 +11002947 if (!forwrite || previous)
2948 clear_bit(STRIPE_BATCH_READY, &sh->state);
2949
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002950 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (*bip)
2952 bi->bi_next = *bip;
2953 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002954 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (forwrite) {
2957 /* check if page is covered */
2958 sector_t sector = sh->dev[dd_idx].sector;
2959 for (bi=sh->dev[dd_idx].towrite;
2960 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07002961 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002963 if (bio_end_sector(bi) >= sector)
2964 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 }
2966 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11002967 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
2968 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002970
2971 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002972 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10002973 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002974 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002975
2976 if (conf->mddev->bitmap && firstwrite) {
2977 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2978 STRIPE_SECTORS, 0);
2979 sh->bm_seq = conf->seq_flush+1;
2980 set_bit(STRIPE_BIT_DELAY, &sh->state);
2981 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11002982
2983 if (stripe_can_batch(sh))
2984 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return 1;
2986
2987 overlap:
2988 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002989 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return 0;
2991}
2992
NeilBrownd1688a62011-10-11 16:49:52 +11002993static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002994
NeilBrownd1688a62011-10-11 16:49:52 +11002995static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002996 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002997{
NeilBrown784052e2009-03-31 15:19:07 +11002998 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002999 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003000 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003001 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003002 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003003
NeilBrown112bf892009-03-31 14:39:38 +11003004 raid5_compute_sector(conf,
3005 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003006 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003007 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003008 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003009}
3010
Dan Williamsa4456852007-07-09 11:56:43 -07003011static void
NeilBrownd1688a62011-10-11 16:49:52 +11003012handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003013 struct stripe_head_state *s, int disks,
3014 struct bio **return_bi)
3015{
3016 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003017 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003018 for (i = disks; i--; ) {
3019 struct bio *bi;
3020 int bitmap_end = 0;
3021
3022 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003023 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003024 rcu_read_lock();
3025 rdev = rcu_dereference(conf->disks[i].rdev);
3026 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003027 atomic_inc(&rdev->nr_pending);
3028 else
3029 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003030 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003031 if (rdev) {
3032 if (!rdev_set_badblocks(
3033 rdev,
3034 sh->sector,
3035 STRIPE_SECTORS, 0))
3036 md_error(conf->mddev, rdev);
3037 rdev_dec_pending(rdev, conf->mddev);
3038 }
Dan Williamsa4456852007-07-09 11:56:43 -07003039 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003040 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003041 /* fail all writes first */
3042 bi = sh->dev[i].towrite;
3043 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003044 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003045 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003046 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003047 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003048
3049 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3050 wake_up(&conf->wait_for_overlap);
3051
Kent Overstreet4f024f32013-10-11 15:44:27 -07003052 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003053 sh->dev[i].sector + STRIPE_SECTORS) {
3054 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
3055 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003056 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003057 md_write_end(conf->mddev);
3058 bi->bi_next = *return_bi;
3059 *return_bi = bi;
3060 }
3061 bi = nextbi;
3062 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003063 if (bitmap_end)
3064 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3065 STRIPE_SECTORS, 0, 0);
3066 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003067 /* and fail all 'written' */
3068 bi = sh->dev[i].written;
3069 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003070 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3071 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3072 sh->dev[i].page = sh->dev[i].orig_page;
3073 }
3074
Dan Williamsa4456852007-07-09 11:56:43 -07003075 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003076 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003077 sh->dev[i].sector + STRIPE_SECTORS) {
3078 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
3079 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003080 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003081 md_write_end(conf->mddev);
3082 bi->bi_next = *return_bi;
3083 *return_bi = bi;
3084 }
3085 bi = bi2;
3086 }
3087
Dan Williamsb5e98d62007-01-02 13:52:31 -07003088 /* fail any reads if this device is non-operational and
3089 * the data has not reached the cache yet.
3090 */
3091 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
3092 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3093 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003094 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003095 bi = sh->dev[i].toread;
3096 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003097 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003098 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3099 wake_up(&conf->wait_for_overlap);
Kent Overstreet4f024f32013-10-11 15:44:27 -07003100 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003101 sh->dev[i].sector + STRIPE_SECTORS) {
3102 struct bio *nextbi =
3103 r5_next_bio(bi, sh->dev[i].sector);
3104 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003105 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003106 bi->bi_next = *return_bi;
3107 *return_bi = bi;
3108 }
3109 bi = nextbi;
3110 }
3111 }
Dan Williamsa4456852007-07-09 11:56:43 -07003112 if (bitmap_end)
3113 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3114 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003115 /* If we were in the middle of a write the parity block might
3116 * still be locked - so just clear all R5_LOCKED flags
3117 */
3118 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003119 }
3120
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003121 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3122 if (atomic_dec_and_test(&conf->pending_full_writes))
3123 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003124}
3125
NeilBrown7f0da592011-07-28 11:39:22 +10003126static void
NeilBrownd1688a62011-10-11 16:49:52 +11003127handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003128 struct stripe_head_state *s)
3129{
3130 int abort = 0;
3131 int i;
3132
shli@kernel.org59fc6302014-12-15 12:57:03 +11003133 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003134 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003135 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3136 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003137 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003138 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003139 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003140 * Don't even need to abort as that is handled elsewhere
3141 * if needed, and not always wanted e.g. if there is a known
3142 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003143 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003144 * non-sync devices, or abort the recovery
3145 */
NeilBrown18b98372012-04-01 23:48:38 +10003146 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3147 /* During recovery devices cannot be removed, so
3148 * locking and refcounting of rdevs is not needed
3149 */
3150 for (i = 0; i < conf->raid_disks; i++) {
3151 struct md_rdev *rdev = conf->disks[i].rdev;
3152 if (rdev
3153 && !test_bit(Faulty, &rdev->flags)
3154 && !test_bit(In_sync, &rdev->flags)
3155 && !rdev_set_badblocks(rdev, sh->sector,
3156 STRIPE_SECTORS, 0))
3157 abort = 1;
3158 rdev = conf->disks[i].replacement;
3159 if (rdev
3160 && !test_bit(Faulty, &rdev->flags)
3161 && !test_bit(In_sync, &rdev->flags)
3162 && !rdev_set_badblocks(rdev, sh->sector,
3163 STRIPE_SECTORS, 0))
3164 abort = 1;
3165 }
3166 if (abort)
3167 conf->recovery_disabled =
3168 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003169 }
NeilBrown18b98372012-04-01 23:48:38 +10003170 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003171}
3172
NeilBrown9a3e1102011-12-23 10:17:53 +11003173static int want_replace(struct stripe_head *sh, int disk_idx)
3174{
3175 struct md_rdev *rdev;
3176 int rv = 0;
3177 /* Doing recovery so rcu locking not required */
3178 rdev = sh->raid_conf->disks[disk_idx].replacement;
3179 if (rdev
3180 && !test_bit(Faulty, &rdev->flags)
3181 && !test_bit(In_sync, &rdev->flags)
3182 && (rdev->recovery_offset <= sh->sector
3183 || rdev->mddev->recovery_cp <= sh->sector))
3184 rv = 1;
3185
3186 return rv;
3187}
3188
NeilBrown93b3dbc2011-07-27 11:00:36 +10003189/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10003190 * to be read or computed to satisfy a request.
3191 *
3192 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10003193 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07003194 */
NeilBrown2c58f062015-02-02 11:32:23 +11003195
3196static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3197 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003198{
3199 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003200 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3201 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003202 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003203
NeilBrowna79cfe12015-02-02 11:37:59 +11003204
3205 if (test_bit(R5_LOCKED, &dev->flags) ||
3206 test_bit(R5_UPTODATE, &dev->flags))
3207 /* No point reading this as we already have it or have
3208 * decided to get it.
3209 */
3210 return 0;
3211
3212 if (dev->toread ||
3213 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3214 /* We need this block to directly satisfy a request */
3215 return 1;
3216
3217 if (s->syncing || s->expanding ||
3218 (s->replacing && want_replace(sh, disk_idx)))
3219 /* When syncing, or expanding we read everything.
3220 * When replacing, we need the replaced block.
3221 */
3222 return 1;
3223
3224 if ((s->failed >= 1 && fdev[0]->toread) ||
3225 (s->failed >= 2 && fdev[1]->toread))
3226 /* If we want to read from a failed device, then
3227 * we need to actually read every other device.
3228 */
3229 return 1;
3230
NeilBrowna9d56952015-02-02 11:49:10 +11003231 /* Sometimes neither read-modify-write nor reconstruct-write
3232 * cycles can work. In those cases we read every block we
3233 * can. Then the parity-update is certain to have enough to
3234 * work with.
3235 * This can only be a problem when we need to write something,
3236 * and some device has failed. If either of those tests
3237 * fail we need look no further.
3238 */
3239 if (!s->failed || !s->to_write)
3240 return 0;
3241
3242 if (test_bit(R5_Insync, &dev->flags) &&
3243 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3244 /* Pre-reads at not permitted until after short delay
3245 * to gather multiple requests. However if this
3246 * device is no Insync, the block could only be be computed
3247 * and there is no need to delay that.
3248 */
3249 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003250
3251 for (i = 0; i < s->failed; i++) {
3252 if (fdev[i]->towrite &&
3253 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3254 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3255 /* If we have a partial write to a failed
3256 * device, then we will need to reconstruct
3257 * the content of that device, so all other
3258 * devices must be read.
3259 */
3260 return 1;
3261 }
3262
3263 /* If we are forced to do a reconstruct-write, either because
3264 * the current RAID6 implementation only supports that, or
3265 * or because parity cannot be trusted and we are currently
3266 * recovering it, there is extra need to be careful.
3267 * If one of the devices that we would need to read, because
3268 * it is not being overwritten (and maybe not written at all)
3269 * is missing/faulty, then we need to read everything we can.
3270 */
3271 if (sh->raid_conf->level != 6 &&
3272 sh->sector < sh->raid_conf->mddev->recovery_cp)
3273 /* reconstruct-write isn't being forced */
3274 return 0;
3275 for (i = 0; i < s->failed; i++) {
3276 if (!test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3277 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3278 return 1;
3279 }
3280
NeilBrown2c58f062015-02-02 11:32:23 +11003281 return 0;
3282}
3283
3284static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3285 int disk_idx, int disks)
3286{
3287 struct r5dev *dev = &sh->dev[disk_idx];
3288
3289 /* is the data in this block needed, and can we get it? */
3290 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003291 /* we would like to get this block, possibly by computing it,
3292 * otherwise read it if the backing disk is insync
3293 */
3294 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3295 BUG_ON(test_bit(R5_Wantread, &dev->flags));
3296 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003297 (s->failed && (disk_idx == s->failed_num[0] ||
3298 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003299 /* have disk failed, and we're requested to fetch it;
3300 * do compute it
3301 */
3302 pr_debug("Computing stripe %llu block %d\n",
3303 (unsigned long long)sh->sector, disk_idx);
3304 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3305 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3306 set_bit(R5_Wantcompute, &dev->flags);
3307 sh->ops.target = disk_idx;
3308 sh->ops.target2 = -1; /* no 2nd target */
3309 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003310 /* Careful: from this point on 'uptodate' is in the eye
3311 * of raid_run_ops which services 'compute' operations
3312 * before writes. R5_Wantcompute flags a block that will
3313 * be R5_UPTODATE by the time it is needed for a
3314 * subsequent operation.
3315 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003316 s->uptodate++;
3317 return 1;
3318 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3319 /* Computing 2-failure is *very* expensive; only
3320 * do it if failed >= 2
3321 */
3322 int other;
3323 for (other = disks; other--; ) {
3324 if (other == disk_idx)
3325 continue;
3326 if (!test_bit(R5_UPTODATE,
3327 &sh->dev[other].flags))
3328 break;
3329 }
3330 BUG_ON(other < 0);
3331 pr_debug("Computing stripe %llu blocks %d,%d\n",
3332 (unsigned long long)sh->sector,
3333 disk_idx, other);
3334 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3335 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3336 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3337 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3338 sh->ops.target = disk_idx;
3339 sh->ops.target2 = other;
3340 s->uptodate += 2;
3341 s->req_compute = 1;
3342 return 1;
3343 } else if (test_bit(R5_Insync, &dev->flags)) {
3344 set_bit(R5_LOCKED, &dev->flags);
3345 set_bit(R5_Wantread, &dev->flags);
3346 s->locked++;
3347 pr_debug("Reading block %d (sync=%d)\n",
3348 disk_idx, s->syncing);
3349 }
3350 }
3351
3352 return 0;
3353}
3354
3355/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003356 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003357 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003358static void handle_stripe_fill(struct stripe_head *sh,
3359 struct stripe_head_state *s,
3360 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003361{
3362 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003363
shli@kernel.org59fc6302014-12-15 12:57:03 +11003364 BUG_ON(sh->batch_head);
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003365 /* look for blocks to read/compute, skip this if a compute
3366 * is already in flight, or if the stripe contents are in the
3367 * midst of changing due to a write
3368 */
3369 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
3370 !sh->reconstruct_state)
3371 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003372 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003373 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003374 set_bit(STRIPE_HANDLE, &sh->state);
3375}
3376
Dan Williams1fe797e2008-06-28 09:16:30 +10003377/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003378 * any written block on an uptodate or failed drive can be returned.
3379 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3380 * never LOCKED, so we don't need to test 'failed' directly.
3381 */
NeilBrownd1688a62011-10-11 16:49:52 +11003382static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07003383 struct stripe_head *sh, int disks, struct bio **return_bi)
3384{
3385 int i;
3386 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003387 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003388 struct stripe_head *head_sh = sh;
3389 bool do_endio = false;
3390 int wakeup_nr = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003391
3392 for (i = disks; i--; )
3393 if (sh->dev[i].written) {
3394 dev = &sh->dev[i];
3395 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003396 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003397 test_bit(R5_Discard, &dev->flags) ||
3398 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003399 /* We can return any write requests */
3400 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003401 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003402 if (test_and_clear_bit(R5_Discard, &dev->flags))
3403 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003404 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3405 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003406 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003407 do_endio = true;
3408
3409returnbi:
3410 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003411 wbi = dev->written;
3412 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003413 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003414 dev->sector + STRIPE_SECTORS) {
3415 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003416 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003417 md_write_end(conf->mddev);
3418 wbi->bi_next = *return_bi;
3419 *return_bi = wbi;
3420 }
3421 wbi = wbi2;
3422 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003423 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3424 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003425 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003426 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003427 if (head_sh->batch_head) {
3428 sh = list_first_entry(&sh->batch_list,
3429 struct stripe_head,
3430 batch_list);
3431 if (sh != head_sh) {
3432 dev = &sh->dev[i];
3433 goto returnbi;
3434 }
3435 }
3436 sh = head_sh;
3437 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003438 } else if (test_bit(R5_Discard, &dev->flags))
3439 discard_pending = 1;
Shaohua Lid592a992014-05-21 17:57:44 +08003440 WARN_ON(test_bit(R5_SkipCopy, &dev->flags));
3441 WARN_ON(dev->page != dev->orig_page);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003442 }
3443 if (!discard_pending &&
3444 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3445 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3446 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3447 if (sh->qd_idx >= 0) {
3448 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3449 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3450 }
3451 /* now that discard is done we can proceed with any sync */
3452 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003453 /*
3454 * SCSI discard will change some bio fields and the stripe has
3455 * no updated data, so remove it from hash list and the stripe
3456 * will be reinitialized
3457 */
3458 spin_lock_irq(&conf->device_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003459unhash:
Shaohua Lid47648f2013-10-19 14:51:42 +08003460 remove_hash(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003461 if (head_sh->batch_head) {
3462 sh = list_first_entry(&sh->batch_list,
3463 struct stripe_head, batch_list);
3464 if (sh != head_sh)
3465 goto unhash;
3466 }
Shaohua Lid47648f2013-10-19 14:51:42 +08003467 spin_unlock_irq(&conf->device_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003468 sh = head_sh;
3469
NeilBrownf8dfcff2013-03-12 12:18:06 +11003470 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3471 set_bit(STRIPE_HANDLE, &sh->state);
3472
3473 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003474
3475 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3476 if (atomic_dec_and_test(&conf->pending_full_writes))
3477 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003478
3479 if (!head_sh->batch_head || !do_endio)
3480 return;
3481 for (i = 0; i < head_sh->disks; i++) {
3482 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
3483 wakeup_nr++;
3484 }
3485 while (!list_empty(&head_sh->batch_list)) {
3486 int i;
3487 sh = list_first_entry(&head_sh->batch_list,
3488 struct stripe_head, batch_list);
3489 list_del_init(&sh->batch_list);
3490
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11003491 set_mask_bits(&sh->state, ~STRIPE_EXPAND_SYNC_FLAG,
3492 head_sh->state & ~((1 << STRIPE_ACTIVE) |
3493 (1 << STRIPE_PREREAD_ACTIVE) |
3494 STRIPE_EXPAND_SYNC_FLAG));
shli@kernel.org59fc6302014-12-15 12:57:03 +11003495 sh->check_state = head_sh->check_state;
3496 sh->reconstruct_state = head_sh->reconstruct_state;
3497 for (i = 0; i < sh->disks; i++) {
3498 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3499 wakeup_nr++;
3500 sh->dev[i].flags = head_sh->dev[i].flags;
3501 }
3502
3503 spin_lock_irq(&sh->stripe_lock);
3504 sh->batch_head = NULL;
3505 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11003506 if (sh->state & STRIPE_EXPAND_SYNC_FLAG)
3507 set_bit(STRIPE_HANDLE, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003508 release_stripe(sh);
3509 }
3510
3511 spin_lock_irq(&head_sh->stripe_lock);
3512 head_sh->batch_head = NULL;
3513 spin_unlock_irq(&head_sh->stripe_lock);
3514 wake_up_nr(&conf->wait_for_overlap, wakeup_nr);
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11003515 if (head_sh->state & STRIPE_EXPAND_SYNC_FLAG)
3516 set_bit(STRIPE_HANDLE, &head_sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003517}
3518
NeilBrownd1688a62011-10-11 16:49:52 +11003519static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003520 struct stripe_head *sh,
3521 struct stripe_head_state *s,
3522 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003523{
3524 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003525 sector_t recovery_cp = conf->mddev->recovery_cp;
3526
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003527 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003528 * If yes, then the array is dirty (after unclean shutdown or
3529 * initial creation), so parity in some stripes might be inconsistent.
3530 * In this case, we need to always do reconstruct-write, to ensure
3531 * that in case of drive failure or read-error correction, we
3532 * generate correct data from the parity.
3533 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003534 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003535 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3536 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003537 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003538 * look like rcw is cheaper
3539 */
3540 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003541 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3542 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003543 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003544 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003545 /* would I have to read this buffer for read_modify_write */
3546 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003547 if ((dev->towrite || i == sh->pd_idx || i == sh->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003548 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003549 !(test_bit(R5_UPTODATE, &dev->flags) ||
3550 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003551 if (test_bit(R5_Insync, &dev->flags))
3552 rmw++;
3553 else
3554 rmw += 2*disks; /* cannot read it */
3555 }
3556 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003557 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3558 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003559 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003560 !(test_bit(R5_UPTODATE, &dev->flags) ||
3561 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003562 if (test_bit(R5_Insync, &dev->flags))
3563 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003564 else
3565 rcw += 2*disks;
3566 }
3567 }
Dan Williams45b42332007-07-09 11:56:43 -07003568 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003569 (unsigned long long)sh->sector, rmw, rcw);
3570 set_bit(STRIPE_HANDLE, &sh->state);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003571 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_ENABLE_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003572 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003573 if (conf->mddev->queue)
3574 blk_add_trace_msg(conf->mddev->queue,
3575 "raid5 rmw %llu %d",
3576 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003577 for (i = disks; i--; ) {
3578 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003579 if ((dev->towrite || i == sh->pd_idx || i == sh->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003580 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003581 !(test_bit(R5_UPTODATE, &dev->flags) ||
3582 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003583 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003584 if (test_bit(STRIPE_PREREAD_ACTIVE,
3585 &sh->state)) {
3586 pr_debug("Read_old block %d for r-m-w\n",
3587 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003588 set_bit(R5_LOCKED, &dev->flags);
3589 set_bit(R5_Wantread, &dev->flags);
3590 s->locked++;
3591 } else {
3592 set_bit(STRIPE_DELAYED, &sh->state);
3593 set_bit(STRIPE_HANDLE, &sh->state);
3594 }
3595 }
3596 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003597 }
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003598 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_ENABLE_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003599 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003600 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003601 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003602 for (i = disks; i--; ) {
3603 struct r5dev *dev = &sh->dev[i];
3604 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003605 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003606 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003607 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003608 test_bit(R5_Wantcompute, &dev->flags))) {
3609 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003610 if (test_bit(R5_Insync, &dev->flags) &&
3611 test_bit(STRIPE_PREREAD_ACTIVE,
3612 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003613 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003614 "%d for Reconstruct\n", i);
3615 set_bit(R5_LOCKED, &dev->flags);
3616 set_bit(R5_Wantread, &dev->flags);
3617 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003618 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003619 } else {
3620 set_bit(STRIPE_DELAYED, &sh->state);
3621 set_bit(STRIPE_HANDLE, &sh->state);
3622 }
3623 }
3624 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003625 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003626 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3627 (unsigned long long)sh->sector,
3628 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003629 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003630
3631 if (rcw > disks && rmw > disks &&
3632 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3633 set_bit(STRIPE_DELAYED, &sh->state);
3634
Dan Williamsa4456852007-07-09 11:56:43 -07003635 /* now if nothing is locked, and if we have enough data,
3636 * we can start a write request
3637 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003638 /* since handle_stripe can be called at any time we need to handle the
3639 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003640 * subsequent call wants to start a write request. raid_run_ops only
3641 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003642 * simultaneously. If this is not the case then new writes need to be
3643 * held off until the compute completes.
3644 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003645 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3646 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3647 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003648 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003649}
3650
NeilBrownd1688a62011-10-11 16:49:52 +11003651static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003652 struct stripe_head_state *s, int disks)
3653{
Dan Williamsecc65c92008-06-28 08:31:57 +10003654 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003655
shli@kernel.org59fc6302014-12-15 12:57:03 +11003656 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003657 set_bit(STRIPE_HANDLE, &sh->state);
3658
Dan Williamsecc65c92008-06-28 08:31:57 +10003659 switch (sh->check_state) {
3660 case check_state_idle:
3661 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003662 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003663 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003664 sh->check_state = check_state_run;
3665 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003666 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003667 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003668 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003669 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003670 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003671 /* fall through */
3672 case check_state_compute_result:
3673 sh->check_state = check_state_idle;
3674 if (!dev)
3675 dev = &sh->dev[sh->pd_idx];
3676
3677 /* check that a write has not made the stripe insync */
3678 if (test_bit(STRIPE_INSYNC, &sh->state))
3679 break;
3680
3681 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003682 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3683 BUG_ON(s->uptodate != disks);
3684
3685 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003686 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003687 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003688
Dan Williamsa4456852007-07-09 11:56:43 -07003689 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003690 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003691 break;
3692 case check_state_run:
3693 break; /* we will be called again upon completion */
3694 case check_state_check_result:
3695 sh->check_state = check_state_idle;
3696
3697 /* if a failure occurred during the check operation, leave
3698 * STRIPE_INSYNC not set and let the stripe be handled again
3699 */
3700 if (s->failed)
3701 break;
3702
3703 /* handle a successful check operation, if parity is correct
3704 * we are done. Otherwise update the mismatch count and repair
3705 * parity if !MD_RECOVERY_CHECK
3706 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003707 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003708 /* parity is correct (on disc,
3709 * not in buffer any more)
3710 */
3711 set_bit(STRIPE_INSYNC, &sh->state);
3712 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003713 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003714 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3715 /* don't try to repair!! */
3716 set_bit(STRIPE_INSYNC, &sh->state);
3717 else {
3718 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003719 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003720 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3721 set_bit(R5_Wantcompute,
3722 &sh->dev[sh->pd_idx].flags);
3723 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003724 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003725 s->uptodate++;
3726 }
3727 }
3728 break;
3729 case check_state_compute_run:
3730 break;
3731 default:
3732 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3733 __func__, sh->check_state,
3734 (unsigned long long) sh->sector);
3735 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003736 }
3737}
3738
NeilBrownd1688a62011-10-11 16:49:52 +11003739static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003740 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003741 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003742{
Dan Williamsa4456852007-07-09 11:56:43 -07003743 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003744 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003745 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003746
shli@kernel.org59fc6302014-12-15 12:57:03 +11003747 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003748 set_bit(STRIPE_HANDLE, &sh->state);
3749
3750 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003751
Dan Williamsa4456852007-07-09 11:56:43 -07003752 /* Want to check and possibly repair P and Q.
3753 * However there could be one 'failed' device, in which
3754 * case we can only check one of them, possibly using the
3755 * other to generate missing data
3756 */
3757
Dan Williamsd82dfee2009-07-14 13:40:57 -07003758 switch (sh->check_state) {
3759 case check_state_idle:
3760 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003761 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003762 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003763 * makes sense to check P (If anything else were failed,
3764 * we would have used P to recreate it).
3765 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003766 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003767 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003768 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003769 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003770 * anything, so it makes sense to check it
3771 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003772 if (sh->check_state == check_state_run)
3773 sh->check_state = check_state_run_pq;
3774 else
3775 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003776 }
Dan Williams36d1c642009-07-14 11:48:22 -07003777
Dan Williamsd82dfee2009-07-14 13:40:57 -07003778 /* discard potentially stale zero_sum_result */
3779 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003780
Dan Williamsd82dfee2009-07-14 13:40:57 -07003781 if (sh->check_state == check_state_run) {
3782 /* async_xor_zero_sum destroys the contents of P */
3783 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3784 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003785 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003786 if (sh->check_state >= check_state_run &&
3787 sh->check_state <= check_state_run_pq) {
3788 /* async_syndrome_zero_sum preserves P and Q, so
3789 * no need to mark them !uptodate here
3790 */
3791 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3792 break;
3793 }
Dan Williams36d1c642009-07-14 11:48:22 -07003794
Dan Williamsd82dfee2009-07-14 13:40:57 -07003795 /* we have 2-disk failure */
3796 BUG_ON(s->failed != 2);
3797 /* fall through */
3798 case check_state_compute_result:
3799 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003800
Dan Williamsd82dfee2009-07-14 13:40:57 -07003801 /* check that a write has not made the stripe insync */
3802 if (test_bit(STRIPE_INSYNC, &sh->state))
3803 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003804
3805 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003806 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003807 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003808 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003809 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003810 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003811 s->locked++;
3812 set_bit(R5_LOCKED, &dev->flags);
3813 set_bit(R5_Wantwrite, &dev->flags);
3814 }
3815 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003816 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003817 s->locked++;
3818 set_bit(R5_LOCKED, &dev->flags);
3819 set_bit(R5_Wantwrite, &dev->flags);
3820 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003821 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003822 dev = &sh->dev[pd_idx];
3823 s->locked++;
3824 set_bit(R5_LOCKED, &dev->flags);
3825 set_bit(R5_Wantwrite, &dev->flags);
3826 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003827 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003828 dev = &sh->dev[qd_idx];
3829 s->locked++;
3830 set_bit(R5_LOCKED, &dev->flags);
3831 set_bit(R5_Wantwrite, &dev->flags);
3832 }
3833 clear_bit(STRIPE_DEGRADED, &sh->state);
3834
3835 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003836 break;
3837 case check_state_run:
3838 case check_state_run_q:
3839 case check_state_run_pq:
3840 break; /* we will be called again upon completion */
3841 case check_state_check_result:
3842 sh->check_state = check_state_idle;
3843
3844 /* handle a successful check operation, if parity is correct
3845 * we are done. Otherwise update the mismatch count and repair
3846 * parity if !MD_RECOVERY_CHECK
3847 */
3848 if (sh->ops.zero_sum_result == 0) {
3849 /* both parities are correct */
3850 if (!s->failed)
3851 set_bit(STRIPE_INSYNC, &sh->state);
3852 else {
3853 /* in contrast to the raid5 case we can validate
3854 * parity, but still have a failure to write
3855 * back
3856 */
3857 sh->check_state = check_state_compute_result;
3858 /* Returning at this point means that we may go
3859 * off and bring p and/or q uptodate again so
3860 * we make sure to check zero_sum_result again
3861 * to verify if p or q need writeback
3862 */
3863 }
3864 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003865 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003866 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3867 /* don't try to repair!! */
3868 set_bit(STRIPE_INSYNC, &sh->state);
3869 else {
3870 int *target = &sh->ops.target;
3871
3872 sh->ops.target = -1;
3873 sh->ops.target2 = -1;
3874 sh->check_state = check_state_compute_run;
3875 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3876 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3877 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3878 set_bit(R5_Wantcompute,
3879 &sh->dev[pd_idx].flags);
3880 *target = pd_idx;
3881 target = &sh->ops.target2;
3882 s->uptodate++;
3883 }
3884 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3885 set_bit(R5_Wantcompute,
3886 &sh->dev[qd_idx].flags);
3887 *target = qd_idx;
3888 s->uptodate++;
3889 }
3890 }
3891 }
3892 break;
3893 case check_state_compute_run:
3894 break;
3895 default:
3896 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3897 __func__, sh->check_state,
3898 (unsigned long long) sh->sector);
3899 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003900 }
3901}
3902
NeilBrownd1688a62011-10-11 16:49:52 +11003903static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003904{
3905 int i;
3906
3907 /* We have read all the blocks in this stripe and now we need to
3908 * copy some of them into a target stripe for expand.
3909 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003910 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003911 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003912 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3913 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003914 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003915 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003916 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003917 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003918
NeilBrown784052e2009-03-31 15:19:07 +11003919 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003920 sector_t s = raid5_compute_sector(conf, bn, 0,
3921 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003922 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003923 if (sh2 == NULL)
3924 /* so far only the early blocks of this stripe
3925 * have been requested. When later blocks
3926 * get requested, we will try again
3927 */
3928 continue;
3929 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3930 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3931 /* must have already done this block */
3932 release_stripe(sh2);
3933 continue;
3934 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003935
3936 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003937 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003938 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003939 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003940 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003941
Dan Williamsa4456852007-07-09 11:56:43 -07003942 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3943 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3944 for (j = 0; j < conf->raid_disks; j++)
3945 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003946 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003947 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3948 break;
3949 if (j == conf->raid_disks) {
3950 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3951 set_bit(STRIPE_HANDLE, &sh2->state);
3952 }
3953 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003954
Dan Williamsa4456852007-07-09 11:56:43 -07003955 }
NeilBrowna2e08552007-09-11 15:23:36 -07003956 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003957 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003958}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960/*
3961 * handle_stripe - do things to a stripe.
3962 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003963 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3964 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003966 * return some read requests which now have data
3967 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 * schedule a read on some buffers
3969 * schedule a write of some buffers
3970 * return confirmation of parity correctness
3971 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 */
Dan Williamsa4456852007-07-09 11:56:43 -07003973
NeilBrownacfe7262011-07-27 11:00:36 +10003974static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003975{
NeilBrownd1688a62011-10-11 16:49:52 +11003976 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003977 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003978 struct r5dev *dev;
3979 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003980 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003981
NeilBrownacfe7262011-07-27 11:00:36 +10003982 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003983
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11003984 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
3985 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10003986 s->failed_num[0] = -1;
3987 s->failed_num[1] = -1;
3988
3989 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003990 rcu_read_lock();
3991 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003992 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003993 sector_t first_bad;
3994 int bad_sectors;
3995 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003996
NeilBrown16a53ec2006-06-26 00:27:38 -07003997 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003998
Dan Williams45b42332007-07-09 11:56:43 -07003999 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004000 i, dev->flags,
4001 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004002 /* maybe we can reply to a read
4003 *
4004 * new wantfill requests are only permitted while
4005 * ops_complete_biofill is guaranteed to be inactive
4006 */
4007 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4008 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4009 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004010
4011 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004012 if (test_bit(R5_LOCKED, &dev->flags))
4013 s->locked++;
4014 if (test_bit(R5_UPTODATE, &dev->flags))
4015 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004016 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004017 s->compute++;
4018 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004019 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004020
NeilBrownacfe7262011-07-27 11:00:36 +10004021 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004022 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004023 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004024 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004025 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004026 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004027 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004028 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004029 }
Dan Williamsa4456852007-07-09 11:56:43 -07004030 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004031 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004032 /* Prefer to use the replacement for reads, but only
4033 * if it is recovered enough and has no bad blocks.
4034 */
4035 rdev = rcu_dereference(conf->disks[i].replacement);
4036 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4037 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4038 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4039 &first_bad, &bad_sectors))
4040 set_bit(R5_ReadRepl, &dev->flags);
4041 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11004042 if (rdev)
4043 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004044 rdev = rcu_dereference(conf->disks[i].rdev);
4045 clear_bit(R5_ReadRepl, &dev->flags);
4046 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004047 if (rdev && test_bit(Faulty, &rdev->flags))
4048 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004049 if (rdev) {
4050 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4051 &first_bad, &bad_sectors);
4052 if (s->blocked_rdev == NULL
4053 && (test_bit(Blocked, &rdev->flags)
4054 || is_bad < 0)) {
4055 if (is_bad < 0)
4056 set_bit(BlockedBadBlocks,
4057 &rdev->flags);
4058 s->blocked_rdev = rdev;
4059 atomic_inc(&rdev->nr_pending);
4060 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004061 }
NeilBrown415e72d2010-06-17 17:25:21 +10004062 clear_bit(R5_Insync, &dev->flags);
4063 if (!rdev)
4064 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004065 else if (is_bad) {
4066 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004067 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4068 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004069 /* treat as in-sync, but with a read error
4070 * which we can now try to correct
4071 */
4072 set_bit(R5_Insync, &dev->flags);
4073 set_bit(R5_ReadError, &dev->flags);
4074 }
4075 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004076 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004077 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004078 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004079 set_bit(R5_Insync, &dev->flags);
4080 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4081 test_bit(R5_Expanded, &dev->flags))
4082 /* If we've reshaped into here, we assume it is Insync.
4083 * We will shortly update recovery_offset to make
4084 * it official.
4085 */
4086 set_bit(R5_Insync, &dev->flags);
4087
NeilBrown1cc03eb2014-01-06 13:19:42 +11004088 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004089 /* This flag does not apply to '.replacement'
4090 * only to .rdev, so make sure to check that*/
4091 struct md_rdev *rdev2 = rcu_dereference(
4092 conf->disks[i].rdev);
4093 if (rdev2 == rdev)
4094 clear_bit(R5_Insync, &dev->flags);
4095 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004096 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004097 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004098 } else
4099 clear_bit(R5_WriteError, &dev->flags);
4100 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004101 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004102 /* This flag does not apply to '.replacement'
4103 * only to .rdev, so make sure to check that*/
4104 struct md_rdev *rdev2 = rcu_dereference(
4105 conf->disks[i].rdev);
4106 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004107 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004108 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004109 } else
4110 clear_bit(R5_MadeGood, &dev->flags);
4111 }
NeilBrown977df362011-12-23 10:17:53 +11004112 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4113 struct md_rdev *rdev2 = rcu_dereference(
4114 conf->disks[i].replacement);
4115 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4116 s->handle_bad_blocks = 1;
4117 atomic_inc(&rdev2->nr_pending);
4118 } else
4119 clear_bit(R5_MadeGoodRepl, &dev->flags);
4120 }
NeilBrown415e72d2010-06-17 17:25:21 +10004121 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004122 /* The ReadError flag will just be confusing now */
4123 clear_bit(R5_ReadError, &dev->flags);
4124 clear_bit(R5_ReWrite, &dev->flags);
4125 }
NeilBrown415e72d2010-06-17 17:25:21 +10004126 if (test_bit(R5_ReadError, &dev->flags))
4127 clear_bit(R5_Insync, &dev->flags);
4128 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004129 if (s->failed < 2)
4130 s->failed_num[s->failed] = i;
4131 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004132 if (rdev && !test_bit(Faulty, &rdev->flags))
4133 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004134 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004135 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004136 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4137 /* If there is a failed device being replaced,
4138 * we must be recovering.
4139 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004140 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004141 * else we can only be replacing
4142 * sync and recovery both need to read all devices, and so
4143 * use the same flag.
4144 */
4145 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004146 sh->sector >= conf->mddev->recovery_cp ||
4147 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004148 s->syncing = 1;
4149 else
4150 s->replacing = 1;
4151 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004152 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004153}
NeilBrownf4168852007-02-28 20:11:53 -08004154
shli@kernel.org59fc6302014-12-15 12:57:03 +11004155static int clear_batch_ready(struct stripe_head *sh)
4156{
4157 struct stripe_head *tmp;
4158 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
4159 return 0;
4160 spin_lock(&sh->stripe_lock);
4161 if (!sh->batch_head) {
4162 spin_unlock(&sh->stripe_lock);
4163 return 0;
4164 }
4165
4166 /*
4167 * this stripe could be added to a batch list before we check
4168 * BATCH_READY, skips it
4169 */
4170 if (sh->batch_head != sh) {
4171 spin_unlock(&sh->stripe_lock);
4172 return 1;
4173 }
4174 spin_lock(&sh->batch_lock);
4175 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4176 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4177 spin_unlock(&sh->batch_lock);
4178 spin_unlock(&sh->stripe_lock);
4179
4180 /*
4181 * BATCH_READY is cleared, no new stripes can be added.
4182 * batch_list can be accessed without lock
4183 */
4184 return 0;
4185}
4186
shli@kernel.org72ac7332014-12-15 12:57:03 +11004187static void check_break_stripe_batch_list(struct stripe_head *sh)
4188{
4189 struct stripe_head *head_sh, *next;
4190 int i;
4191
4192 if (!test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
4193 return;
4194
4195 head_sh = sh;
4196 do {
4197 sh = list_first_entry(&sh->batch_list,
4198 struct stripe_head, batch_list);
4199 BUG_ON(sh == head_sh);
4200 } while (!test_bit(STRIPE_DEGRADED, &sh->state));
4201
4202 while (sh != head_sh) {
4203 next = list_first_entry(&sh->batch_list,
4204 struct stripe_head, batch_list);
4205 list_del_init(&sh->batch_list);
4206
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004207 set_mask_bits(&sh->state, ~STRIPE_EXPAND_SYNC_FLAG,
4208 head_sh->state & ~((1 << STRIPE_ACTIVE) |
4209 (1 << STRIPE_PREREAD_ACTIVE) |
4210 (1 << STRIPE_DEGRADED) |
4211 STRIPE_EXPAND_SYNC_FLAG));
shli@kernel.org72ac7332014-12-15 12:57:03 +11004212 sh->check_state = head_sh->check_state;
4213 sh->reconstruct_state = head_sh->reconstruct_state;
4214 for (i = 0; i < sh->disks; i++)
4215 sh->dev[i].flags = head_sh->dev[i].flags &
4216 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
4217
4218 spin_lock_irq(&sh->stripe_lock);
4219 sh->batch_head = NULL;
4220 spin_unlock_irq(&sh->stripe_lock);
4221
4222 set_bit(STRIPE_HANDLE, &sh->state);
4223 release_stripe(sh);
4224
4225 sh = next;
4226 }
4227}
4228
NeilBrowncc940152011-07-26 11:35:35 +10004229static void handle_stripe(struct stripe_head *sh)
4230{
4231 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004232 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004233 int i;
NeilBrown84789552011-07-27 11:00:36 +10004234 int prexor;
4235 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004236 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004237
4238 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004239 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004240 /* already being handled, ensure it gets handled
4241 * again when current action finishes */
4242 set_bit(STRIPE_HANDLE, &sh->state);
4243 return;
4244 }
4245
shli@kernel.org59fc6302014-12-15 12:57:03 +11004246 if (clear_batch_ready(sh) ) {
4247 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4248 return;
4249 }
4250
shli@kernel.org72ac7332014-12-15 12:57:03 +11004251 check_break_stripe_batch_list(sh);
4252
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004253 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004254 spin_lock(&sh->stripe_lock);
4255 /* Cannot process 'sync' concurrently with 'discard' */
4256 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4257 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4258 set_bit(STRIPE_SYNCING, &sh->state);
4259 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004260 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004261 }
4262 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004263 }
4264 clear_bit(STRIPE_DELAYED, &sh->state);
4265
4266 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4267 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4268 (unsigned long long)sh->sector, sh->state,
4269 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4270 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004271
NeilBrownacfe7262011-07-27 11:00:36 +10004272 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004273
NeilBrownbc2607f2011-07-28 11:39:22 +10004274 if (s.handle_bad_blocks) {
4275 set_bit(STRIPE_HANDLE, &sh->state);
4276 goto finish;
4277 }
4278
NeilBrown474af965fe2011-07-27 11:00:36 +10004279 if (unlikely(s.blocked_rdev)) {
4280 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004281 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004282 set_bit(STRIPE_HANDLE, &sh->state);
4283 goto finish;
4284 }
4285 /* There is nothing for the blocked_rdev to block */
4286 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4287 s.blocked_rdev = NULL;
4288 }
4289
4290 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4291 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4292 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4293 }
4294
4295 pr_debug("locked=%d uptodate=%d to_read=%d"
4296 " to_write=%d failed=%d failed_num=%d,%d\n",
4297 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4298 s.failed_num[0], s.failed_num[1]);
4299 /* check if the array has lost more than max_degraded devices and,
4300 * if so, some requests might need to be failed.
4301 */
NeilBrown9a3f5302011-11-08 16:22:01 +11004302 if (s.failed > conf->max_degraded) {
4303 sh->check_state = 0;
4304 sh->reconstruct_state = 0;
4305 if (s.to_read+s.to_write+s.written)
4306 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004307 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004308 handle_failed_sync(conf, sh, &s);
4309 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004310
NeilBrown84789552011-07-27 11:00:36 +10004311 /* Now we check to see if any write operations have recently
4312 * completed
4313 */
4314 prexor = 0;
4315 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4316 prexor = 1;
4317 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4318 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4319 sh->reconstruct_state = reconstruct_state_idle;
4320
4321 /* All the 'written' buffers and the parity block are ready to
4322 * be written back to disk
4323 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004324 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4325 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004326 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004327 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4328 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004329 for (i = disks; i--; ) {
4330 struct r5dev *dev = &sh->dev[i];
4331 if (test_bit(R5_LOCKED, &dev->flags) &&
4332 (i == sh->pd_idx || i == sh->qd_idx ||
4333 dev->written)) {
4334 pr_debug("Writing block %d\n", i);
4335 set_bit(R5_Wantwrite, &dev->flags);
4336 if (prexor)
4337 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004338 if (s.failed > 1)
4339 continue;
NeilBrown84789552011-07-27 11:00:36 +10004340 if (!test_bit(R5_Insync, &dev->flags) ||
4341 ((i == sh->pd_idx || i == sh->qd_idx) &&
4342 s.failed == 0))
4343 set_bit(STRIPE_INSYNC, &sh->state);
4344 }
4345 }
4346 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4347 s.dec_preread_active = 1;
4348 }
4349
NeilBrownef5b7c62012-11-22 09:13:36 +11004350 /*
4351 * might be able to return some write requests if the parity blocks
4352 * are safe, or on a failed drive
4353 */
4354 pdev = &sh->dev[sh->pd_idx];
4355 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4356 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4357 qdev = &sh->dev[sh->qd_idx];
4358 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4359 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4360 || conf->level < 6;
4361
4362 if (s.written &&
4363 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4364 && !test_bit(R5_LOCKED, &pdev->flags)
4365 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4366 test_bit(R5_Discard, &pdev->flags))))) &&
4367 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4368 && !test_bit(R5_LOCKED, &qdev->flags)
4369 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4370 test_bit(R5_Discard, &qdev->flags))))))
4371 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4372
4373 /* Now we might consider reading some blocks, either to check/generate
4374 * parity, or to satisfy requests
4375 * or to load a block that is being partially written.
4376 */
4377 if (s.to_read || s.non_overwrite
4378 || (conf->level == 6 && s.to_write && s.failed)
4379 || (s.syncing && (s.uptodate + s.compute < disks))
4380 || s.replacing
4381 || s.expanding)
4382 handle_stripe_fill(sh, &s, disks);
4383
NeilBrown84789552011-07-27 11:00:36 +10004384 /* Now to consider new write requests and what else, if anything
4385 * should be read. We do not handle new writes when:
4386 * 1/ A 'write' operation (copy+xor) is already in flight.
4387 * 2/ A 'check' operation is in flight, as it may clobber the parity
4388 * block.
4389 */
4390 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
4391 handle_stripe_dirtying(conf, sh, &s, disks);
4392
4393 /* maybe we need to check and possibly fix the parity for this stripe
4394 * Any reads will already have been scheduled, so we just see if enough
4395 * data is available. The parity check is held off while parity
4396 * dependent operations are in flight.
4397 */
4398 if (sh->check_state ||
4399 (s.syncing && s.locked == 0 &&
4400 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4401 !test_bit(STRIPE_INSYNC, &sh->state))) {
4402 if (conf->level == 6)
4403 handle_parity_checks6(conf, sh, &s, disks);
4404 else
4405 handle_parity_checks5(conf, sh, &s, disks);
4406 }
NeilBrownc5a31002011-07-27 11:00:36 +10004407
NeilBrownf94c0b62013-07-22 12:57:21 +10004408 if ((s.replacing || s.syncing) && s.locked == 0
4409 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4410 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004411 /* Write out to replacement devices where possible */
4412 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004413 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4414 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004415 set_bit(R5_WantReplace, &sh->dev[i].flags);
4416 set_bit(R5_LOCKED, &sh->dev[i].flags);
4417 s.locked++;
4418 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004419 if (s.replacing)
4420 set_bit(STRIPE_INSYNC, &sh->state);
4421 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004422 }
4423 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004424 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004425 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004426 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4427 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004428 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4429 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004430 }
4431
4432 /* If the failed drives are just a ReadError, then we might need
4433 * to progress the repair/check process
4434 */
4435 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4436 for (i = 0; i < s.failed; i++) {
4437 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4438 if (test_bit(R5_ReadError, &dev->flags)
4439 && !test_bit(R5_LOCKED, &dev->flags)
4440 && test_bit(R5_UPTODATE, &dev->flags)
4441 ) {
4442 if (!test_bit(R5_ReWrite, &dev->flags)) {
4443 set_bit(R5_Wantwrite, &dev->flags);
4444 set_bit(R5_ReWrite, &dev->flags);
4445 set_bit(R5_LOCKED, &dev->flags);
4446 s.locked++;
4447 } else {
4448 /* let's read it back */
4449 set_bit(R5_Wantread, &dev->flags);
4450 set_bit(R5_LOCKED, &dev->flags);
4451 s.locked++;
4452 }
4453 }
4454 }
4455
NeilBrown3687c062011-07-27 11:00:36 +10004456 /* Finish reconstruct operations initiated by the expansion process */
4457 if (sh->reconstruct_state == reconstruct_state_result) {
4458 struct stripe_head *sh_src
4459 = get_active_stripe(conf, sh->sector, 1, 1, 1);
4460 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4461 /* sh cannot be written until sh_src has been read.
4462 * so arrange for sh to be delayed a little
4463 */
4464 set_bit(STRIPE_DELAYED, &sh->state);
4465 set_bit(STRIPE_HANDLE, &sh->state);
4466 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4467 &sh_src->state))
4468 atomic_inc(&conf->preread_active_stripes);
4469 release_stripe(sh_src);
4470 goto finish;
4471 }
4472 if (sh_src)
4473 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004474
NeilBrown3687c062011-07-27 11:00:36 +10004475 sh->reconstruct_state = reconstruct_state_idle;
4476 clear_bit(STRIPE_EXPANDING, &sh->state);
4477 for (i = conf->raid_disks; i--; ) {
4478 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4479 set_bit(R5_LOCKED, &sh->dev[i].flags);
4480 s.locked++;
4481 }
4482 }
4483
4484 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4485 !sh->reconstruct_state) {
4486 /* Need to write out all blocks after computing parity */
4487 sh->disks = conf->raid_disks;
4488 stripe_set_idx(sh->sector, conf, 0, sh);
4489 schedule_reconstruction(sh, &s, 1, 1);
4490 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4491 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4492 atomic_dec(&conf->reshape_stripes);
4493 wake_up(&conf->wait_for_overlap);
4494 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4495 }
4496
4497 if (s.expanding && s.locked == 0 &&
4498 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4499 handle_stripe_expansion(conf, sh);
4500
4501finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004502 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004503 if (unlikely(s.blocked_rdev)) {
4504 if (conf->mddev->external)
4505 md_wait_for_blocked_rdev(s.blocked_rdev,
4506 conf->mddev);
4507 else
4508 /* Internal metadata will immediately
4509 * be written by raid5d, so we don't
4510 * need to wait here.
4511 */
4512 rdev_dec_pending(s.blocked_rdev,
4513 conf->mddev);
4514 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004515
NeilBrownbc2607f2011-07-28 11:39:22 +10004516 if (s.handle_bad_blocks)
4517 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004518 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004519 struct r5dev *dev = &sh->dev[i];
4520 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4521 /* We own a safe reference to the rdev */
4522 rdev = conf->disks[i].rdev;
4523 if (!rdev_set_badblocks(rdev, sh->sector,
4524 STRIPE_SECTORS, 0))
4525 md_error(conf->mddev, rdev);
4526 rdev_dec_pending(rdev, conf->mddev);
4527 }
NeilBrownb84db562011-07-28 11:39:23 +10004528 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4529 rdev = conf->disks[i].rdev;
4530 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004531 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004532 rdev_dec_pending(rdev, conf->mddev);
4533 }
NeilBrown977df362011-12-23 10:17:53 +11004534 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4535 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004536 if (!rdev)
4537 /* rdev have been moved down */
4538 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004539 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004540 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004541 rdev_dec_pending(rdev, conf->mddev);
4542 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004543 }
4544
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004545 if (s.ops_request)
4546 raid_run_ops(sh, s.ops_request);
4547
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004548 ops_run_io(sh, &s);
4549
NeilBrownc5709ef2011-07-26 11:35:20 +10004550 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004551 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004552 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004553 * have actually been submitted.
4554 */
4555 atomic_dec(&conf->preread_active_stripes);
4556 if (atomic_read(&conf->preread_active_stripes) <
4557 IO_THRESHOLD)
4558 md_wakeup_thread(conf->mddev->thread);
4559 }
4560
NeilBrownc5709ef2011-07-26 11:35:20 +10004561 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07004562
Dan Williams257a4b42011-11-08 16:22:06 +11004563 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004564}
4565
NeilBrownd1688a62011-10-11 16:49:52 +11004566static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
4568 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4569 while (!list_empty(&conf->delayed_list)) {
4570 struct list_head *l = conf->delayed_list.next;
4571 struct stripe_head *sh;
4572 sh = list_entry(l, struct stripe_head, lru);
4573 list_del_init(l);
4574 clear_bit(STRIPE_DELAYED, &sh->state);
4575 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4576 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004577 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004578 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 }
NeilBrown482c0832011-04-18 18:25:42 +10004580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581}
4582
Shaohua Li566c09c2013-11-14 15:16:17 +11004583static void activate_bit_delay(struct r5conf *conf,
4584 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004585{
4586 /* device_lock is held */
4587 struct list_head head;
4588 list_add(&head, &conf->bitmap_list);
4589 list_del_init(&conf->bitmap_list);
4590 while (!list_empty(&head)) {
4591 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004592 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004593 list_del_init(&sh->lru);
4594 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004595 hash = sh->hash_lock_index;
4596 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004597 }
4598}
4599
NeilBrown5c675f82014-12-15 12:56:56 +11004600static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004601{
NeilBrownd1688a62011-10-11 16:49:52 +11004602 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004603
4604 /* No difference between reads and writes. Just check
4605 * how busy the stripe_cache is
4606 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004607
NeilBrown54233992015-02-26 12:21:04 +11004608 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07004609 return 1;
4610 if (conf->quiesce)
4611 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004612 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004613 return 1;
4614
4615 return 0;
4616}
4617
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004618/* We want read requests to align with chunks where possible,
4619 * but write requests don't need to.
4620 */
NeilBrown64590f42014-12-15 12:56:57 +11004621static int raid5_mergeable_bvec(struct mddev *mddev,
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004622 struct bvec_merge_data *bvm,
4623 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004624{
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004625 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004626 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004627 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004628 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004629
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004630 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004631 return biovec->bv_len; /* always allow writes to be mergeable */
4632
Andre Noll664e7c42009-06-18 08:45:27 +10004633 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4634 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004635 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
4636 if (max < 0) max = 0;
4637 if (max <= biovec->bv_len && bio_sectors == 0)
4638 return biovec->bv_len;
4639 else
4640 return max;
4641}
4642
NeilBrownfd01b882011-10-11 16:47:53 +11004643static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004644{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004645 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10004646 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004647 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004648
Andre Noll664e7c42009-06-18 08:45:27 +10004649 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4650 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004651 return chunk_sectors >=
4652 ((sector & (chunk_sectors - 1)) + bio_sectors);
4653}
4654
4655/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004656 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4657 * later sampled by raid5d.
4658 */
NeilBrownd1688a62011-10-11 16:49:52 +11004659static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004660{
4661 unsigned long flags;
4662
4663 spin_lock_irqsave(&conf->device_lock, flags);
4664
4665 bi->bi_next = conf->retry_read_aligned_list;
4666 conf->retry_read_aligned_list = bi;
4667
4668 spin_unlock_irqrestore(&conf->device_lock, flags);
4669 md_wakeup_thread(conf->mddev->thread);
4670}
4671
NeilBrownd1688a62011-10-11 16:49:52 +11004672static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004673{
4674 struct bio *bi;
4675
4676 bi = conf->retry_read_aligned;
4677 if (bi) {
4678 conf->retry_read_aligned = NULL;
4679 return bi;
4680 }
4681 bi = conf->retry_read_aligned_list;
4682 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004683 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004684 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004685 /*
4686 * this sets the active strip count to 1 and the processed
4687 * strip count to zero (upper 8 bits)
4688 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004689 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004690 }
4691
4692 return bi;
4693}
4694
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004695/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004696 * The "raid5_align_endio" should check if the read succeeded and if it
4697 * did, call bio_endio on the original bio (having bio_put the new bio
4698 * first).
4699 * If the read failed..
4700 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004701static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004702{
4703 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004704 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004705 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004706 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004707 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004708
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004709 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004710
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004711 rdev = (void*)raid_bi->bi_next;
4712 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004713 mddev = rdev->mddev;
4714 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004715
4716 rdev_dec_pending(rdev, conf->mddev);
4717
4718 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004719 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4720 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004721 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004722 if (atomic_dec_and_test(&conf->active_aligned_reads))
4723 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004724 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004725 }
4726
Dan Williams45b42332007-07-09 11:56:43 -07004727 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004728
4729 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004730}
4731
Neil Brown387bb172007-02-08 14:20:29 -08004732static int bio_fits_rdev(struct bio *bi)
4733{
Jens Axboe165125e2007-07-24 09:28:11 +02004734 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004735
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004736 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004737 return 0;
4738 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004739 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004740 return 0;
4741
4742 if (q->merge_bvec_fn)
4743 /* it's too hard to apply the merge_bvec_fn at this stage,
4744 * just just give up
4745 */
4746 return 0;
4747
4748 return 1;
4749}
4750
NeilBrownfd01b882011-10-11 16:47:53 +11004751static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004752{
NeilBrownd1688a62011-10-11 16:49:52 +11004753 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11004754 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004755 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004756 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004757 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004758
4759 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004760 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004761 return 0;
4762 }
4763 /*
NeilBrowna167f662010-10-26 18:31:13 +11004764 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004765 */
NeilBrowna167f662010-10-26 18:31:13 +11004766 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004767 if (!align_bi)
4768 return 0;
4769 /*
4770 * set bi_end_io to a new function, and set bi_private to the
4771 * original bio.
4772 */
4773 align_bi->bi_end_io = raid5_align_endio;
4774 align_bi->bi_private = raid_bio;
4775 /*
4776 * compute position
4777 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004778 align_bi->bi_iter.bi_sector =
4779 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
4780 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004781
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004782 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004783 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004784 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4785 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4786 rdev->recovery_offset < end_sector) {
4787 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4788 if (rdev &&
4789 (test_bit(Faulty, &rdev->flags) ||
4790 !(test_bit(In_sync, &rdev->flags) ||
4791 rdev->recovery_offset >= end_sector)))
4792 rdev = NULL;
4793 }
4794 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004795 sector_t first_bad;
4796 int bad_sectors;
4797
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004798 atomic_inc(&rdev->nr_pending);
4799 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004800 raid_bio->bi_next = (void*)rdev;
4801 align_bi->bi_bdev = rdev->bdev;
NeilBrown3fd83712014-08-23 20:19:26 +10004802 __clear_bit(BIO_SEG_VALID, &align_bi->bi_flags);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004803
NeilBrown31c176e2011-07-28 11:39:22 +10004804 if (!bio_fits_rdev(align_bi) ||
Kent Overstreet4f024f32013-10-11 15:44:27 -07004805 is_badblock(rdev, align_bi->bi_iter.bi_sector,
4806 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004807 &first_bad, &bad_sectors)) {
4808 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004809 bio_put(align_bi);
4810 rdev_dec_pending(rdev, mddev);
4811 return 0;
4812 }
4813
majianpeng6c0544e2012-06-12 08:31:10 +08004814 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004815 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08004816
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004817 spin_lock_irq(&conf->device_lock);
4818 wait_event_lock_irq(conf->wait_for_stripe,
4819 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004820 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004821 atomic_inc(&conf->active_aligned_reads);
4822 spin_unlock_irq(&conf->device_lock);
4823
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004824 if (mddev->gendisk)
4825 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4826 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07004827 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004828 generic_make_request(align_bi);
4829 return 1;
4830 } else {
4831 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004832 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004833 return 0;
4834 }
4835}
4836
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004837/* __get_priority_stripe - get the next stripe to process
4838 *
4839 * Full stripe writes are allowed to pass preread active stripes up until
4840 * the bypass_threshold is exceeded. In general the bypass_count
4841 * increments when the handle_list is handled before the hold_list; however, it
4842 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4843 * stripe with in flight i/o. The bypass_count will be reset when the
4844 * head of the hold_list has changed, i.e. the head was promoted to the
4845 * handle_list.
4846 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004847static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004848{
Shaohua Li851c30c2013-08-28 14:30:16 +08004849 struct stripe_head *sh = NULL, *tmp;
4850 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004851 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004852
4853 if (conf->worker_cnt_per_group == 0) {
4854 handle_list = &conf->handle_list;
4855 } else if (group != ANY_GROUP) {
4856 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004857 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004858 } else {
4859 int i;
4860 for (i = 0; i < conf->group_cnt; i++) {
4861 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004862 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004863 if (!list_empty(handle_list))
4864 break;
4865 }
4866 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004867
4868 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4869 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004870 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004871 list_empty(&conf->hold_list) ? "empty" : "busy",
4872 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4873
Shaohua Li851c30c2013-08-28 14:30:16 +08004874 if (!list_empty(handle_list)) {
4875 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004876
4877 if (list_empty(&conf->hold_list))
4878 conf->bypass_count = 0;
4879 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4880 if (conf->hold_list.next == conf->last_hold)
4881 conf->bypass_count++;
4882 else {
4883 conf->last_hold = conf->hold_list.next;
4884 conf->bypass_count -= conf->bypass_threshold;
4885 if (conf->bypass_count < 0)
4886 conf->bypass_count = 0;
4887 }
4888 }
4889 } else if (!list_empty(&conf->hold_list) &&
4890 ((conf->bypass_threshold &&
4891 conf->bypass_count > conf->bypass_threshold) ||
4892 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004893
4894 list_for_each_entry(tmp, &conf->hold_list, lru) {
4895 if (conf->worker_cnt_per_group == 0 ||
4896 group == ANY_GROUP ||
4897 !cpu_online(tmp->cpu) ||
4898 cpu_to_group(tmp->cpu) == group) {
4899 sh = tmp;
4900 break;
4901 }
4902 }
4903
4904 if (sh) {
4905 conf->bypass_count -= conf->bypass_threshold;
4906 if (conf->bypass_count < 0)
4907 conf->bypass_count = 0;
4908 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004909 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004910 }
4911
4912 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004913 return NULL;
4914
Shaohua Libfc90cb2013-08-29 15:40:32 +08004915 if (wg) {
4916 wg->stripes_cnt--;
4917 sh->group = NULL;
4918 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004919 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08004920 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004921 return sh;
4922}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004923
Shaohua Li8811b592012-08-02 08:33:00 +10004924struct raid5_plug_cb {
4925 struct blk_plug_cb cb;
4926 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11004927 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10004928};
4929
4930static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4931{
4932 struct raid5_plug_cb *cb = container_of(
4933 blk_cb, struct raid5_plug_cb, cb);
4934 struct stripe_head *sh;
4935 struct mddev *mddev = cb->cb.data;
4936 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004937 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11004938 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10004939
4940 if (cb->list.next && !list_empty(&cb->list)) {
4941 spin_lock_irq(&conf->device_lock);
4942 while (!list_empty(&cb->list)) {
4943 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4944 list_del_init(&sh->lru);
4945 /*
4946 * avoid race release_stripe_plug() sees
4947 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4948 * is still in our list
4949 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01004950 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10004951 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004952 /*
4953 * STRIPE_ON_RELEASE_LIST could be set here. In that
4954 * case, the count is always > 1 here
4955 */
Shaohua Li566c09c2013-11-14 15:16:17 +11004956 hash = sh->hash_lock_index;
4957 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11004958 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004959 }
4960 spin_unlock_irq(&conf->device_lock);
4961 }
Shaohua Li566c09c2013-11-14 15:16:17 +11004962 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4963 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004964 if (mddev->queue)
4965 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004966 kfree(cb);
4967}
4968
4969static void release_stripe_plug(struct mddev *mddev,
4970 struct stripe_head *sh)
4971{
4972 struct blk_plug_cb *blk_cb = blk_check_plugged(
4973 raid5_unplug, mddev,
4974 sizeof(struct raid5_plug_cb));
4975 struct raid5_plug_cb *cb;
4976
4977 if (!blk_cb) {
4978 release_stripe(sh);
4979 return;
4980 }
4981
4982 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4983
Shaohua Li566c09c2013-11-14 15:16:17 +11004984 if (cb->list.next == NULL) {
4985 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10004986 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11004987 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
4988 INIT_LIST_HEAD(cb->temp_inactive_list + i);
4989 }
Shaohua Li8811b592012-08-02 08:33:00 +10004990
4991 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4992 list_add_tail(&sh->lru, &cb->list);
4993 else
4994 release_stripe(sh);
4995}
4996
Shaohua Li620125f2012-10-11 13:49:05 +11004997static void make_discard_request(struct mddev *mddev, struct bio *bi)
4998{
4999 struct r5conf *conf = mddev->private;
5000 sector_t logical_sector, last_sector;
5001 struct stripe_head *sh;
5002 int remaining;
5003 int stripe_sectors;
5004
5005 if (mddev->reshape_position != MaxSector)
5006 /* Skip discard while reshape is happening */
5007 return;
5008
Kent Overstreet4f024f32013-10-11 15:44:27 -07005009 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5010 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005011
5012 bi->bi_next = NULL;
5013 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5014
5015 stripe_sectors = conf->chunk_sectors *
5016 (conf->raid_disks - conf->max_degraded);
5017 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5018 stripe_sectors);
5019 sector_div(last_sector, stripe_sectors);
5020
5021 logical_sector *= conf->chunk_sectors;
5022 last_sector *= conf->chunk_sectors;
5023
5024 for (; logical_sector < last_sector;
5025 logical_sector += STRIPE_SECTORS) {
5026 DEFINE_WAIT(w);
5027 int d;
5028 again:
5029 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
5030 prepare_to_wait(&conf->wait_for_overlap, &w,
5031 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005032 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5033 if (test_bit(STRIPE_SYNCING, &sh->state)) {
5034 release_stripe(sh);
5035 schedule();
5036 goto again;
5037 }
5038 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005039 spin_lock_irq(&sh->stripe_lock);
5040 for (d = 0; d < conf->raid_disks; d++) {
5041 if (d == sh->pd_idx || d == sh->qd_idx)
5042 continue;
5043 if (sh->dev[d].towrite || sh->dev[d].toread) {
5044 set_bit(R5_Overlap, &sh->dev[d].flags);
5045 spin_unlock_irq(&sh->stripe_lock);
5046 release_stripe(sh);
5047 schedule();
5048 goto again;
5049 }
5050 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005051 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005052 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005053 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005054 for (d = 0; d < conf->raid_disks; d++) {
5055 if (d == sh->pd_idx || d == sh->qd_idx)
5056 continue;
5057 sh->dev[d].towrite = bi;
5058 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5059 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005060 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005061 }
5062 spin_unlock_irq(&sh->stripe_lock);
5063 if (conf->mddev->bitmap) {
5064 for (d = 0;
5065 d < conf->raid_disks - conf->max_degraded;
5066 d++)
5067 bitmap_startwrite(mddev->bitmap,
5068 sh->sector,
5069 STRIPE_SECTORS,
5070 0);
5071 sh->bm_seq = conf->seq_flush + 1;
5072 set_bit(STRIPE_BIT_DELAY, &sh->state);
5073 }
5074
5075 set_bit(STRIPE_HANDLE, &sh->state);
5076 clear_bit(STRIPE_DELAYED, &sh->state);
5077 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5078 atomic_inc(&conf->preread_active_stripes);
5079 release_stripe_plug(mddev, sh);
5080 }
5081
5082 remaining = raid5_dec_bi_active_stripes(bi);
5083 if (remaining == 0) {
5084 md_write_end(mddev);
5085 bio_endio(bi, 0);
5086 }
5087}
5088
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07005089static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090{
NeilBrownd1688a62011-10-11 16:49:52 +11005091 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005092 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 sector_t new_sector;
5094 sector_t logical_sector, last_sector;
5095 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005096 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005097 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005098 DEFINE_WAIT(w);
5099 bool do_prepare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Tejun Heoe9c74692010-09-03 11:56:18 +02005101 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
5102 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02005103 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005104 }
5105
NeilBrown3d310eb2005-06-21 17:17:26 -07005106 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005107
NeilBrown802ba062006-12-13 00:34:13 -08005108 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005109 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11005110 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02005111 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005112
Shaohua Li620125f2012-10-11 13:49:05 +11005113 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
5114 make_discard_request(mddev, bi);
5115 return;
5116 }
5117
Kent Overstreet4f024f32013-10-11 15:44:27 -07005118 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005119 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 bi->bi_next = NULL;
5121 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005122
Shaohua Li27c0f682014-04-09 11:25:47 +08005123 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005125 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005126 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005127
Shaohua Li27c0f682014-04-09 11:25:47 +08005128 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005129 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005130 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005131 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005132 if (do_prepare)
5133 prepare_to_wait(&conf->wait_for_overlap, &w,
5134 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005135 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005136 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08005137 * 64bit on a 32bit platform, and so it might be
5138 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005139 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08005140 * the lock is dropped, so once we get a reference
5141 * to the stripe that we think it is, we will have
5142 * to check again.
5143 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005144 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005145 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005146 ? logical_sector < conf->reshape_progress
5147 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005148 previous = 1;
5149 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005150 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005151 ? logical_sector < conf->reshape_safe
5152 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005153 spin_unlock_irq(&conf->device_lock);
5154 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005155 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005156 goto retry;
5157 }
5158 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005159 spin_unlock_irq(&conf->device_lock);
5160 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005161
NeilBrown112bf892009-03-31 14:39:38 +11005162 new_sector = raid5_compute_sector(conf, logical_sector,
5163 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005164 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10005165 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005166 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 (unsigned long long)logical_sector);
5168
NeilBrownb5663ba2009-03-31 14:39:38 +11005169 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10005170 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005172 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005173 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08005174 * stripe, so we must do the range check again.
5175 * Expansion could still move past after this
5176 * test, but as we are holding a reference to
5177 * 'sh', we know that if that happens,
5178 * STRIPE_EXPANDING will get set and the expansion
5179 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005180 */
5181 int must_retry = 0;
5182 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005183 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005184 ? logical_sector >= conf->reshape_progress
5185 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005186 /* mismatch, need to try again */
5187 must_retry = 1;
5188 spin_unlock_irq(&conf->device_lock);
5189 if (must_retry) {
5190 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005191 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005192 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005193 goto retry;
5194 }
5195 }
NeilBrownc46501b2013-08-27 15:52:13 +10005196 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5197 /* Might have got the wrong stripe_head
5198 * by accident
5199 */
5200 release_stripe(sh);
5201 goto retry;
5202 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005203
Namhyung Kimffd96e32011-07-18 17:38:51 +10005204 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005205 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005206 logical_sector < mddev->suspend_hi) {
5207 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005208 /* As the suspend_* range is controlled by
5209 * userspace, we want an interruptible
5210 * wait.
5211 */
5212 flush_signals(current);
5213 prepare_to_wait(&conf->wait_for_overlap,
5214 &w, TASK_INTERRUPTIBLE);
5215 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005216 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005217 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005218 do_prepare = true;
5219 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005220 goto retry;
5221 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005222
5223 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005224 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005225 /* Stripe is busy expanding or
5226 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 * and wait a while
5228 */
NeilBrown482c0832011-04-18 18:25:42 +10005229 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 release_stripe(sh);
5231 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005232 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 goto retry;
5234 }
NeilBrown6ed30032008-02-06 01:40:00 -08005235 set_bit(STRIPE_HANDLE, &sh->state);
5236 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005237 if ((!sh->batch_head || sh == sh->batch_head) &&
5238 (bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005239 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5240 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005241 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 } else {
5243 /* cannot get stripe for read-ahead, just give-up */
5244 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 break;
5246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005248 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005249
Shaohua Lie7836bd62012-07-19 16:01:31 +10005250 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005251 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
NeilBrown16a53ec2006-06-26 00:27:38 -07005253 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005255
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005256 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5257 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10005258 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260}
5261
NeilBrownfd01b882011-10-11 16:47:53 +11005262static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005263
NeilBrownfd01b882011-10-11 16:47:53 +11005264static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265{
NeilBrown52c03292006-06-26 00:27:43 -07005266 /* reshaping is quite different to recovery/resync so it is
5267 * handled quite separately ... here.
5268 *
5269 * On each call to sync_request, we gather one chunk worth of
5270 * destination stripes and flag them as expanding.
5271 * Then we find all the source stripes and request reads.
5272 * As the reads complete, handle_stripe will copy the data
5273 * into the destination stripe and release that stripe.
5274 */
NeilBrownd1688a62011-10-11 16:49:52 +11005275 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005277 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005278 int raid_disks = conf->previous_raid_disks;
5279 int data_disks = raid_disks - conf->max_degraded;
5280 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005281 int i;
5282 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005283 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005284 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005285 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005286 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07005287
NeilBrownfef9c612009-03-31 15:16:46 +11005288 if (sector_nr == 0) {
5289 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005290 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005291 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5292 sector_nr = raid5_size(mddev, 0, 0)
5293 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10005294 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005295 conf->reshape_progress > 0)
5296 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005297 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005298 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005299 mddev->curr_resync_completed = sector_nr;
5300 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005301 *skipped = 1;
5302 return sector_nr;
5303 }
NeilBrown52c03292006-06-26 00:27:43 -07005304 }
5305
NeilBrown7a661382009-03-31 15:21:40 +11005306 /* We need to process a full chunk at a time.
5307 * If old and new chunk sizes differ, we need to process the
5308 * largest of these
5309 */
Andre Noll664e7c42009-06-18 08:45:27 +10005310 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
5311 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005312 else
Andre Noll9d8f0362009-06-18 08:45:01 +10005313 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005314
NeilBrownb5254dd2012-05-21 09:27:01 +10005315 /* We update the metadata at least every 10 seconds, or when
5316 * the data about to be copied would over-write the source of
5317 * the data at the front of the range. i.e. one new_stripe
5318 * along from reshape_progress new_maps to after where
5319 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005320 */
NeilBrownfef9c612009-03-31 15:16:46 +11005321 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005322 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005323 readpos = conf->reshape_progress;
5324 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005325 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005326 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005327 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10005328 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11005329 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005330 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005331 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005332 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10005333 readpos -= min_t(sector_t, reshape_sectors, readpos);
5334 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005335 }
NeilBrown52c03292006-06-26 00:27:43 -07005336
NeilBrownb5254dd2012-05-21 09:27:01 +10005337 /* Having calculated the 'writepos' possibly use it
5338 * to set 'stripe_addr' which is where we will write to.
5339 */
5340 if (mddev->reshape_backwards) {
5341 BUG_ON(conf->reshape_progress == 0);
5342 stripe_addr = writepos;
5343 BUG_ON((mddev->dev_sectors &
5344 ~((sector_t)reshape_sectors - 1))
5345 - reshape_sectors - stripe_addr
5346 != sector_nr);
5347 } else {
5348 BUG_ON(writepos != sector_nr + reshape_sectors);
5349 stripe_addr = sector_nr;
5350 }
5351
NeilBrownc8f517c2009-03-31 15:28:40 +11005352 /* 'writepos' is the most advanced device address we might write.
5353 * 'readpos' is the least advanced device address we might read.
5354 * 'safepos' is the least address recorded in the metadata as having
5355 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005356 * If there is a min_offset_diff, these are adjusted either by
5357 * increasing the safepos/readpos if diff is negative, or
5358 * increasing writepos if diff is positive.
5359 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005360 * ensure safety in the face of a crash - that must be done by userspace
5361 * making a backup of the data. So in that case there is no particular
5362 * rush to update metadata.
5363 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5364 * update the metadata to advance 'safepos' to match 'readpos' so that
5365 * we can be safe in the event of a crash.
5366 * So we insist on updating metadata if safepos is behind writepos and
5367 * readpos is beyond writepos.
5368 * In any case, update the metadata every 10 seconds.
5369 * Maybe that number should be configurable, but I'm not sure it is
5370 * worth it.... maybe it could be a multiple of safemode_delay???
5371 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005372 if (conf->min_offset_diff < 0) {
5373 safepos += -conf->min_offset_diff;
5374 readpos += -conf->min_offset_diff;
5375 } else
5376 writepos += conf->min_offset_diff;
5377
NeilBrown2c810cd2012-05-21 09:27:00 +10005378 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005379 ? (safepos > writepos && readpos < writepos)
5380 : (safepos < writepos && readpos > writepos)) ||
5381 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005382 /* Cannot proceed until we've updated the superblock... */
5383 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005384 atomic_read(&conf->reshape_stripes)==0
5385 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5386 if (atomic_read(&conf->reshape_stripes) != 0)
5387 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005388 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005389 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005390 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07005391 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07005392 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07005393 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005394 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5395 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5396 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005397 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005398 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005399 spin_unlock_irq(&conf->device_lock);
5400 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005401 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005402 }
5403
NeilBrownab69ae12009-03-31 15:26:47 +11005404 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005405 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005406 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005407 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10005408 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005409 set_bit(STRIPE_EXPANDING, &sh->state);
5410 atomic_inc(&conf->reshape_stripes);
5411 /* If any of this stripe is beyond the end of the old
5412 * array, then we need to zero those blocks
5413 */
5414 for (j=sh->disks; j--;) {
5415 sector_t s;
5416 if (j == sh->pd_idx)
5417 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005418 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005419 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005420 continue;
NeilBrown784052e2009-03-31 15:19:07 +11005421 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005422 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005423 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005424 continue;
5425 }
5426 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5427 set_bit(R5_Expanded, &sh->dev[j].flags);
5428 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5429 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005430 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005431 set_bit(STRIPE_EXPAND_READY, &sh->state);
5432 set_bit(STRIPE_HANDLE, &sh->state);
5433 }
NeilBrownab69ae12009-03-31 15:26:47 +11005434 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005435 }
5436 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005437 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005438 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005439 else
NeilBrown7a661382009-03-31 15:21:40 +11005440 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005441 spin_unlock_irq(&conf->device_lock);
5442 /* Ok, those stripe are ready. We can start scheduling
5443 * reads on the source stripes.
5444 * The source stripes are determined by mapping the first and last
5445 * block on the destination stripes.
5446 */
NeilBrown52c03292006-06-26 00:27:43 -07005447 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005448 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005449 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005450 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005451 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005452 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005453 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005454 if (last_sector >= mddev->dev_sectors)
5455 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005456 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005457 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005458 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5459 set_bit(STRIPE_HANDLE, &sh->state);
5460 release_stripe(sh);
5461 first_sector += STRIPE_SECTORS;
5462 }
NeilBrownab69ae12009-03-31 15:26:47 +11005463 /* Now that the sources are clearly marked, we can release
5464 * the destination stripes
5465 */
5466 while (!list_empty(&stripes)) {
5467 sh = list_entry(stripes.next, struct stripe_head, lru);
5468 list_del_init(&sh->lru);
5469 release_stripe(sh);
5470 }
NeilBrownc6207272008-02-06 01:39:52 -08005471 /* If this takes us to the resync_max point where we have to pause,
5472 * then we need to write out the superblock.
5473 */
NeilBrown7a661382009-03-31 15:21:40 +11005474 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10005475 if ((sector_nr - mddev->curr_resync_completed) * 2
5476 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005477 /* Cannot proceed until we've updated the superblock... */
5478 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005479 atomic_read(&conf->reshape_stripes) == 0
5480 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5481 if (atomic_read(&conf->reshape_stripes) != 0)
5482 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005483 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005484 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005485 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08005486 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5487 md_wakeup_thread(mddev->thread);
5488 wait_event(mddev->sb_wait,
5489 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005490 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5491 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5492 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005493 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005494 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005495 spin_unlock_irq(&conf->device_lock);
5496 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005497 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005498 }
NeilBrownc91abf52013-11-19 12:02:01 +11005499ret:
NeilBrown7a661382009-03-31 15:21:40 +11005500 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07005501}
5502
NeilBrown09314792015-02-19 16:04:40 +11005503static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005504{
NeilBrownd1688a62011-10-11 16:49:52 +11005505 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005506 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005507 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005508 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005509 int still_degraded = 0;
5510 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
NeilBrown72626682005-09-09 16:23:54 -07005512 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005514
NeilBrown29269552006-03-27 01:18:10 -08005515 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5516 end_reshape(conf);
5517 return 0;
5518 }
NeilBrown72626682005-09-09 16:23:54 -07005519
5520 if (mddev->curr_resync < max_sector) /* aborted */
5521 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5522 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005523 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005524 conf->fullsync = 0;
5525 bitmap_close_sync(mddev->bitmap);
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 return 0;
5528 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005529
NeilBrown64bd6602009-08-03 10:59:58 +10005530 /* Allow raid5_quiesce to complete */
5531 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5532
NeilBrown52c03292006-06-26 00:27:43 -07005533 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5534 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005535
NeilBrownc6207272008-02-06 01:39:52 -08005536 /* No need to check resync_max as we never do more than one
5537 * stripe, and as resync_max will always be on a chunk boundary,
5538 * if the check in md_do_sync didn't fire, there is no chance
5539 * of overstepping resync_max here
5540 */
5541
NeilBrown16a53ec2006-06-26 00:27:38 -07005542 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 * to resync, then assert that we are finished, because there is
5544 * nothing we can do.
5545 */
NeilBrown3285edf2006-06-26 00:27:55 -07005546 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005547 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005548 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005549 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 return rv;
5551 }
majianpeng6f608042013-04-24 11:42:41 +10005552 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5553 !conf->fullsync &&
5554 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5555 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005556 /* we can skip this block, and probably more */
5557 sync_blocks /= STRIPE_SECTORS;
5558 *skipped = 1;
5559 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
NeilBrownb47490c2008-02-06 01:39:50 -08005562 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
5563
NeilBrowna8c906c2009-06-09 14:39:59 +10005564 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005566 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005568 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005570 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005572 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005573 * Note in case of > 1 drive failures it's possible we're rebuilding
5574 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005575 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005576 rcu_read_lock();
5577 for (i = 0; i < conf->raid_disks; i++) {
5578 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5579
5580 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005581 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005582 }
5583 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005584
5585 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5586
NeilBrown83206d62011-07-26 11:19:49 +10005587 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005588 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 release_stripe(sh);
5591
5592 return STRIPE_SECTORS;
5593}
5594
NeilBrownd1688a62011-10-11 16:49:52 +11005595static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005596{
5597 /* We may not be able to submit a whole bio at once as there
5598 * may not be enough stripe_heads available.
5599 * We cannot pre-allocate enough stripe_heads as we may need
5600 * more than exist in the cache (if we allow ever large chunks).
5601 * So we do one stripe head at a time and record in
5602 * ->bi_hw_segments how many have been done.
5603 *
5604 * We *know* that this entire raid_bio is in one chunk, so
5605 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5606 */
5607 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005608 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005609 sector_t sector, logical_sector, last_sector;
5610 int scnt = 0;
5611 int remaining;
5612 int handled = 0;
5613
Kent Overstreet4f024f32013-10-11 15:44:27 -07005614 logical_sector = raid_bio->bi_iter.bi_sector &
5615 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005616 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005617 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005618 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005619
5620 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005621 logical_sector += STRIPE_SECTORS,
5622 sector += STRIPE_SECTORS,
5623 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005624
Shaohua Lie7836bd62012-07-19 16:01:31 +10005625 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005626 /* already done this stripe */
5627 continue;
5628
hui jiao2844dc32014-06-05 11:34:24 +08005629 sh = get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005630
5631 if (!sh) {
5632 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005633 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005634 conf->retry_read_aligned = raid_bio;
5635 return handled;
5636 }
5637
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005638 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Neil Brown387bb172007-02-08 14:20:29 -08005639 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005640 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005641 conf->retry_read_aligned = raid_bio;
5642 return handled;
5643 }
5644
majianpeng3f9e7c12012-07-31 10:04:21 +10005645 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005646 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005647 release_stripe(sh);
5648 handled++;
5649 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005650 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005651 if (remaining == 0) {
5652 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5653 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10005654 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005655 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005656 if (atomic_dec_and_test(&conf->active_aligned_reads))
5657 wake_up(&conf->wait_for_stripe);
5658 return handled;
5659}
5660
Shaohua Libfc90cb2013-08-29 15:40:32 +08005661static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005662 struct r5worker *worker,
5663 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005664{
5665 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005666 int i, batch_size = 0, hash;
5667 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005668
5669 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005670 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005671 batch[batch_size++] = sh;
5672
Shaohua Li566c09c2013-11-14 15:16:17 +11005673 if (batch_size == 0) {
5674 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5675 if (!list_empty(temp_inactive_list + i))
5676 break;
5677 if (i == NR_STRIPE_HASH_LOCKS)
5678 return batch_size;
5679 release_inactive = true;
5680 }
Shaohua Li46a06402012-08-02 08:33:15 +10005681 spin_unlock_irq(&conf->device_lock);
5682
Shaohua Li566c09c2013-11-14 15:16:17 +11005683 release_inactive_stripe_list(conf, temp_inactive_list,
5684 NR_STRIPE_HASH_LOCKS);
5685
5686 if (release_inactive) {
5687 spin_lock_irq(&conf->device_lock);
5688 return 0;
5689 }
5690
Shaohua Li46a06402012-08-02 08:33:15 +10005691 for (i = 0; i < batch_size; i++)
5692 handle_stripe(batch[i]);
5693
5694 cond_resched();
5695
5696 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005697 for (i = 0; i < batch_size; i++) {
5698 hash = batch[i]->hash_lock_index;
5699 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5700 }
Shaohua Li46a06402012-08-02 08:33:15 +10005701 return batch_size;
5702}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005703
Shaohua Li851c30c2013-08-28 14:30:16 +08005704static void raid5_do_work(struct work_struct *work)
5705{
5706 struct r5worker *worker = container_of(work, struct r5worker, work);
5707 struct r5worker_group *group = worker->group;
5708 struct r5conf *conf = group->conf;
5709 int group_id = group - conf->worker_groups;
5710 int handled;
5711 struct blk_plug plug;
5712
5713 pr_debug("+++ raid5worker active\n");
5714
5715 blk_start_plug(&plug);
5716 handled = 0;
5717 spin_lock_irq(&conf->device_lock);
5718 while (1) {
5719 int batch_size, released;
5720
Shaohua Li566c09c2013-11-14 15:16:17 +11005721 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005722
Shaohua Li566c09c2013-11-14 15:16:17 +11005723 batch_size = handle_active_stripes(conf, group_id, worker,
5724 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005725 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005726 if (!batch_size && !released)
5727 break;
5728 handled += batch_size;
5729 }
5730 pr_debug("%d stripes handled\n", handled);
5731
5732 spin_unlock_irq(&conf->device_lock);
5733 blk_finish_plug(&plug);
5734
5735 pr_debug("--- raid5worker inactive\n");
5736}
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738/*
5739 * This is our raid5 kernel thread.
5740 *
5741 * We scan the hash table for stripes which can be handled now.
5742 * During the scan, completed stripes are saved for us by the interrupt
5743 * handler, so that they will not have to wait for our next wakeup.
5744 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005745static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746{
Shaohua Li4ed87312012-10-11 13:34:00 +11005747 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005748 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005750 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Dan Williams45b42332007-07-09 11:56:43 -07005752 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
5754 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005756 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 handled = 0;
5758 spin_lock_irq(&conf->device_lock);
5759 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005760 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005761 int batch_size, released;
5762
Shaohua Li566c09c2013-11-14 15:16:17 +11005763 released = release_stripe_list(conf, conf->temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
NeilBrown0021b7b2012-07-31 09:08:14 +02005765 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005766 !list_empty(&conf->bitmap_list)) {
5767 /* Now is a good time to flush some bitmap updates */
5768 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005769 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005770 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005771 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005772 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005773 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005774 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005775 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005776
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005777 while ((bio = remove_bio_from_retry(conf))) {
5778 int ok;
5779 spin_unlock_irq(&conf->device_lock);
5780 ok = retry_aligned_read(conf, bio);
5781 spin_lock_irq(&conf->device_lock);
5782 if (!ok)
5783 break;
5784 handled++;
5785 }
5786
Shaohua Li566c09c2013-11-14 15:16:17 +11005787 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5788 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005789 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005791 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
Shaohua Li46a06402012-08-02 08:33:15 +10005793 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5794 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005795 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005796 spin_lock_irq(&conf->device_lock);
5797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 }
Dan Williams45b42332007-07-09 11:56:43 -07005799 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 spin_unlock_irq(&conf->device_lock);
5802
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005803 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005804 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Dan Williams45b42332007-07-09 11:56:43 -07005806 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807}
5808
NeilBrown3f294f42005-11-08 21:39:25 -08005809static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005810raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005811{
NeilBrown7b1485b2014-12-15 12:56:59 +11005812 struct r5conf *conf;
5813 int ret = 0;
5814 spin_lock(&mddev->lock);
5815 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005816 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11005817 ret = sprintf(page, "%d\n", conf->max_nr_stripes);
5818 spin_unlock(&mddev->lock);
5819 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08005820}
5821
NeilBrownc41d4ac2010-06-01 19:37:24 +10005822int
NeilBrownfd01b882011-10-11 16:47:53 +11005823raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005824{
NeilBrownd1688a62011-10-11 16:49:52 +11005825 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005826 int err;
5827
5828 if (size <= 16 || size > 32768)
5829 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11005830
5831 while (size < conf->max_nr_stripes &&
5832 drop_one_stripe(conf))
5833 ;
5834
NeilBrownc41d4ac2010-06-01 19:37:24 +10005835 err = md_allow_write(mddev);
5836 if (err)
5837 return err;
NeilBrown486f0642015-02-25 12:10:35 +11005838
5839 while (size > conf->max_nr_stripes)
5840 if (!grow_one_stripe(conf, GFP_KERNEL))
5841 break;
5842
NeilBrownc41d4ac2010-06-01 19:37:24 +10005843 return 0;
5844}
5845EXPORT_SYMBOL(raid5_set_cache_size);
5846
NeilBrown3f294f42005-11-08 21:39:25 -08005847static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005848raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005849{
NeilBrown67918752014-12-15 12:57:01 +11005850 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07005851 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005852 int err;
5853
NeilBrown3f294f42005-11-08 21:39:25 -08005854 if (len >= PAGE_SIZE)
5855 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005856 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005857 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005858 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005859 if (err)
5860 return err;
NeilBrown67918752014-12-15 12:57:01 +11005861 conf = mddev->private;
5862 if (!conf)
5863 err = -ENODEV;
5864 else
5865 err = raid5_set_cache_size(mddev, new);
5866 mddev_unlock(mddev);
5867
5868 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08005869}
NeilBrown007583c2005-11-08 21:39:30 -08005870
NeilBrown96de1e62005-11-08 21:39:39 -08005871static struct md_sysfs_entry
5872raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5873 raid5_show_stripe_cache_size,
5874 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005875
5876static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11005877raid5_show_rmw_level(struct mddev *mddev, char *page)
5878{
5879 struct r5conf *conf = mddev->private;
5880 if (conf)
5881 return sprintf(page, "%d\n", conf->rmw_level);
5882 else
5883 return 0;
5884}
5885
5886static ssize_t
5887raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
5888{
5889 struct r5conf *conf = mddev->private;
5890 unsigned long new;
5891
5892 if (!conf)
5893 return -ENODEV;
5894
5895 if (len >= PAGE_SIZE)
5896 return -EINVAL;
5897
5898 if (kstrtoul(page, 10, &new))
5899 return -EINVAL;
5900
5901 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
5902 return -EINVAL;
5903
5904 if (new != PARITY_DISABLE_RMW &&
5905 new != PARITY_ENABLE_RMW &&
5906 new != PARITY_PREFER_RMW)
5907 return -EINVAL;
5908
5909 conf->rmw_level = new;
5910 return len;
5911}
5912
5913static struct md_sysfs_entry
5914raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
5915 raid5_show_rmw_level,
5916 raid5_store_rmw_level);
5917
5918
5919static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005920raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005921{
NeilBrown7b1485b2014-12-15 12:56:59 +11005922 struct r5conf *conf;
5923 int ret = 0;
5924 spin_lock(&mddev->lock);
5925 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005926 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11005927 ret = sprintf(page, "%d\n", conf->bypass_threshold);
5928 spin_unlock(&mddev->lock);
5929 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005930}
5931
5932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005933raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005934{
NeilBrown67918752014-12-15 12:57:01 +11005935 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07005936 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11005937 int err;
5938
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005939 if (len >= PAGE_SIZE)
5940 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005941 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005942 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005943
5944 err = mddev_lock(mddev);
5945 if (err)
5946 return err;
5947 conf = mddev->private;
5948 if (!conf)
5949 err = -ENODEV;
5950 else if (new > conf->max_nr_stripes)
5951 err = -EINVAL;
5952 else
5953 conf->bypass_threshold = new;
5954 mddev_unlock(mddev);
5955 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005956}
5957
5958static struct md_sysfs_entry
5959raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5960 S_IRUGO | S_IWUSR,
5961 raid5_show_preread_threshold,
5962 raid5_store_preread_threshold);
5963
5964static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08005965raid5_show_skip_copy(struct mddev *mddev, char *page)
5966{
NeilBrown7b1485b2014-12-15 12:56:59 +11005967 struct r5conf *conf;
5968 int ret = 0;
5969 spin_lock(&mddev->lock);
5970 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08005971 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11005972 ret = sprintf(page, "%d\n", conf->skip_copy);
5973 spin_unlock(&mddev->lock);
5974 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08005975}
5976
5977static ssize_t
5978raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
5979{
NeilBrown67918752014-12-15 12:57:01 +11005980 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08005981 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11005982 int err;
5983
Shaohua Lid592a992014-05-21 17:57:44 +08005984 if (len >= PAGE_SIZE)
5985 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08005986 if (kstrtoul(page, 10, &new))
5987 return -EINVAL;
5988 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08005989
NeilBrown67918752014-12-15 12:57:01 +11005990 err = mddev_lock(mddev);
5991 if (err)
5992 return err;
5993 conf = mddev->private;
5994 if (!conf)
5995 err = -ENODEV;
5996 else if (new != conf->skip_copy) {
5997 mddev_suspend(mddev);
5998 conf->skip_copy = new;
5999 if (new)
6000 mddev->queue->backing_dev_info.capabilities |=
6001 BDI_CAP_STABLE_WRITES;
6002 else
6003 mddev->queue->backing_dev_info.capabilities &=
6004 ~BDI_CAP_STABLE_WRITES;
6005 mddev_resume(mddev);
6006 }
6007 mddev_unlock(mddev);
6008 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006009}
6010
6011static struct md_sysfs_entry
6012raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6013 raid5_show_skip_copy,
6014 raid5_store_skip_copy);
6015
Shaohua Lid592a992014-05-21 17:57:44 +08006016static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006017stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006018{
NeilBrownd1688a62011-10-11 16:49:52 +11006019 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006020 if (conf)
6021 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6022 else
6023 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006024}
6025
NeilBrown96de1e62005-11-08 21:39:39 -08006026static struct md_sysfs_entry
6027raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006028
Shaohua Lib7214202013-08-27 17:50:42 +08006029static ssize_t
6030raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6031{
NeilBrown7b1485b2014-12-15 12:56:59 +11006032 struct r5conf *conf;
6033 int ret = 0;
6034 spin_lock(&mddev->lock);
6035 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006036 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006037 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6038 spin_unlock(&mddev->lock);
6039 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006040}
6041
majianpeng60aaf932013-11-14 15:16:20 +11006042static int alloc_thread_groups(struct r5conf *conf, int cnt,
6043 int *group_cnt,
6044 int *worker_cnt_per_group,
6045 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006046static ssize_t
6047raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6048{
NeilBrown67918752014-12-15 12:57:01 +11006049 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006050 unsigned long new;
6051 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006052 struct r5worker_group *new_groups, *old_groups;
6053 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006054
6055 if (len >= PAGE_SIZE)
6056 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006057 if (kstrtoul(page, 10, &new))
6058 return -EINVAL;
6059
NeilBrown67918752014-12-15 12:57:01 +11006060 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006061 if (err)
6062 return err;
NeilBrown67918752014-12-15 12:57:01 +11006063 conf = mddev->private;
6064 if (!conf)
6065 err = -ENODEV;
6066 else if (new != conf->worker_cnt_per_group) {
6067 mddev_suspend(mddev);
6068
6069 old_groups = conf->worker_groups;
6070 if (old_groups)
6071 flush_workqueue(raid5_wq);
6072
6073 err = alloc_thread_groups(conf, new,
6074 &group_cnt, &worker_cnt_per_group,
6075 &new_groups);
6076 if (!err) {
6077 spin_lock_irq(&conf->device_lock);
6078 conf->group_cnt = group_cnt;
6079 conf->worker_cnt_per_group = worker_cnt_per_group;
6080 conf->worker_groups = new_groups;
6081 spin_unlock_irq(&conf->device_lock);
6082
6083 if (old_groups)
6084 kfree(old_groups[0].workers);
6085 kfree(old_groups);
6086 }
6087 mddev_resume(mddev);
6088 }
6089 mddev_unlock(mddev);
6090
6091 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006092}
6093
6094static struct md_sysfs_entry
6095raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6096 raid5_show_group_thread_cnt,
6097 raid5_store_group_thread_cnt);
6098
NeilBrown007583c2005-11-08 21:39:30 -08006099static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006100 &raid5_stripecache_size.attr,
6101 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006102 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006103 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006104 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006105 &raid5_rmw_level.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006106 NULL,
6107};
NeilBrown007583c2005-11-08 21:39:30 -08006108static struct attribute_group raid5_attrs_group = {
6109 .name = NULL,
6110 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006111};
6112
majianpeng60aaf932013-11-14 15:16:20 +11006113static int alloc_thread_groups(struct r5conf *conf, int cnt,
6114 int *group_cnt,
6115 int *worker_cnt_per_group,
6116 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006117{
Shaohua Li566c09c2013-11-14 15:16:17 +11006118 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006119 ssize_t size;
6120 struct r5worker *workers;
6121
majianpeng60aaf932013-11-14 15:16:20 +11006122 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006123 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006124 *group_cnt = 0;
6125 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006126 return 0;
6127 }
majianpeng60aaf932013-11-14 15:16:20 +11006128 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006129 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006130 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6131 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6132 *group_cnt, GFP_NOIO);
6133 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006134 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006135 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006136 return -ENOMEM;
6137 }
6138
majianpeng60aaf932013-11-14 15:16:20 +11006139 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006140 struct r5worker_group *group;
6141
NeilBrown0c775d52013-11-25 11:12:43 +11006142 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006143 INIT_LIST_HEAD(&group->handle_list);
6144 group->conf = conf;
6145 group->workers = workers + i * cnt;
6146
6147 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006148 struct r5worker *worker = group->workers + j;
6149 worker->group = group;
6150 INIT_WORK(&worker->work, raid5_do_work);
6151
6152 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6153 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006154 }
6155 }
6156
6157 return 0;
6158}
6159
6160static void free_thread_groups(struct r5conf *conf)
6161{
6162 if (conf->worker_groups)
6163 kfree(conf->worker_groups[0].workers);
6164 kfree(conf->worker_groups);
6165 conf->worker_groups = NULL;
6166}
6167
Dan Williams80c3a6c2009-03-17 18:10:40 -07006168static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006169raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006170{
NeilBrownd1688a62011-10-11 16:49:52 +11006171 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006172
6173 if (!sectors)
6174 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006175 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006176 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006177 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006178
Andre Noll9d8f0362009-06-18 08:45:01 +10006179 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10006180 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006181 return sectors * (raid_disks - conf->max_degraded);
6182}
6183
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306184static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6185{
6186 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006187 if (percpu->scribble)
6188 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306189 percpu->spare_page = NULL;
6190 percpu->scribble = NULL;
6191}
6192
6193static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6194{
6195 if (conf->level == 6 && !percpu->spare_page)
6196 percpu->spare_page = alloc_page(GFP_KERNEL);
6197 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006198 percpu->scribble = scribble_alloc(max(conf->raid_disks,
6199 conf->previous_raid_disks), conf->chunk_sectors /
6200 STRIPE_SECTORS, GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306201
6202 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6203 free_scratch_buffer(conf, percpu);
6204 return -ENOMEM;
6205 }
6206
6207 return 0;
6208}
6209
NeilBrownd1688a62011-10-11 16:49:52 +11006210static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006211{
Dan Williams36d1c642009-07-14 11:48:22 -07006212 unsigned long cpu;
6213
6214 if (!conf->percpu)
6215 return;
6216
Dan Williams36d1c642009-07-14 11:48:22 -07006217#ifdef CONFIG_HOTPLUG_CPU
6218 unregister_cpu_notifier(&conf->cpu_notify);
6219#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306220
6221 get_online_cpus();
6222 for_each_possible_cpu(cpu)
6223 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07006224 put_online_cpus();
6225
6226 free_percpu(conf->percpu);
6227}
6228
NeilBrownd1688a62011-10-11 16:49:52 +11006229static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006230{
Shaohua Li851c30c2013-08-28 14:30:16 +08006231 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006232 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006233 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006234 kfree(conf->disks);
6235 kfree(conf->stripe_hashtbl);
6236 kfree(conf);
6237}
6238
Dan Williams36d1c642009-07-14 11:48:22 -07006239#ifdef CONFIG_HOTPLUG_CPU
6240static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
6241 void *hcpu)
6242{
NeilBrownd1688a62011-10-11 16:49:52 +11006243 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07006244 long cpu = (long)hcpu;
6245 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6246
6247 switch (action) {
6248 case CPU_UP_PREPARE:
6249 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306250 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07006251 pr_err("%s: failed memory allocation for cpu%ld\n",
6252 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07006253 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07006254 }
6255 break;
6256 case CPU_DEAD:
6257 case CPU_DEAD_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306258 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07006259 break;
6260 default:
6261 break;
6262 }
6263 return NOTIFY_OK;
6264}
6265#endif
6266
NeilBrownd1688a62011-10-11 16:49:52 +11006267static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006268{
6269 unsigned long cpu;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306270 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006271
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306272 conf->percpu = alloc_percpu(struct raid5_percpu);
6273 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006274 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006275
Dan Williams36d1c642009-07-14 11:48:22 -07006276#ifdef CONFIG_HOTPLUG_CPU
6277 conf->cpu_notify.notifier_call = raid456_cpu_notify;
6278 conf->cpu_notify.priority = 0;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306279 err = register_cpu_notifier(&conf->cpu_notify);
6280 if (err)
6281 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07006282#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306283
6284 get_online_cpus();
6285 for_each_present_cpu(cpu) {
6286 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6287 if (err) {
6288 pr_err("%s: failed memory allocation for cpu%ld\n",
6289 __func__, cpu);
6290 break;
6291 }
6292 }
Dan Williams36d1c642009-07-14 11:48:22 -07006293 put_online_cpus();
6294
6295 return err;
6296}
6297
NeilBrownd1688a62011-10-11 16:49:52 +11006298static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299{
NeilBrownd1688a62011-10-11 16:49:52 +11006300 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006301 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006302 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006304 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006305 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006306 int group_cnt, worker_cnt_per_group;
6307 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308
NeilBrown91adb562009-03-31 14:39:39 +11006309 if (mddev->new_level != 5
6310 && mddev->new_level != 4
6311 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10006312 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11006313 mdname(mddev), mddev->new_level);
6314 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 }
NeilBrown91adb562009-03-31 14:39:39 +11006316 if ((mddev->new_level == 5
6317 && !algorithm_valid_raid5(mddev->new_layout)) ||
6318 (mddev->new_level == 6
6319 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10006320 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11006321 mdname(mddev), mddev->new_layout);
6322 return ERR_PTR(-EIO);
6323 }
6324 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10006325 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11006326 mdname(mddev), mddev->raid_disks);
6327 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
Andre Noll664e7c42009-06-18 08:45:27 +10006330 if (!mddev->new_chunk_sectors ||
6331 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6332 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006333 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
6334 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006335 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006336 }
6337
NeilBrownd1688a62011-10-11 16:49:52 +11006338 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006339 if (conf == NULL)
6340 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08006341 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006342 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6343 &new_group)) {
6344 conf->group_cnt = group_cnt;
6345 conf->worker_cnt_per_group = worker_cnt_per_group;
6346 conf->worker_groups = new_group;
6347 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006348 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006349 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006350 seqcount_init(&conf->gen_lock);
Dan Williamsf5efd452009-10-16 15:55:38 +11006351 init_waitqueue_head(&conf->wait_for_stripe);
6352 init_waitqueue_head(&conf->wait_for_overlap);
6353 INIT_LIST_HEAD(&conf->handle_list);
6354 INIT_LIST_HEAD(&conf->hold_list);
6355 INIT_LIST_HEAD(&conf->delayed_list);
6356 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006357 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006358 atomic_set(&conf->active_stripes, 0);
6359 atomic_set(&conf->preread_active_stripes, 0);
6360 atomic_set(&conf->active_aligned_reads, 0);
6361 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006362 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006363
6364 conf->raid_disks = mddev->raid_disks;
6365 if (mddev->reshape_position == MaxSector)
6366 conf->previous_raid_disks = mddev->raid_disks;
6367 else
6368 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006369 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006370
NeilBrown5e5e3e72009-10-16 16:35:30 +11006371 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006372 GFP_KERNEL);
6373 if (!conf->disks)
6374 goto abort;
6375
6376 conf->mddev = mddev;
6377
6378 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6379 goto abort;
6380
Shaohua Li566c09c2013-11-14 15:16:17 +11006381 /* We init hash_locks[0] separately to that it can be used
6382 * as the reference lock in the spin_lock_nest_lock() call
6383 * in lock_all_device_hash_locks_irq in order to convince
6384 * lockdep that we know what we are doing.
6385 */
6386 spin_lock_init(conf->hash_locks);
6387 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6388 spin_lock_init(conf->hash_locks + i);
6389
6390 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6391 INIT_LIST_HEAD(conf->inactive_list + i);
6392
6393 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6394 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6395
Dan Williams36d1c642009-07-14 11:48:22 -07006396 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006397 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006398 if (raid5_alloc_percpu(conf) != 0)
6399 goto abort;
6400
NeilBrown0c55e022010-05-03 14:09:02 +10006401 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006402
NeilBrowndafb20f2012-03-19 12:46:39 +11006403 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006404 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006405 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11006406 || raid_disk < 0)
6407 continue;
6408 disk = conf->disks + raid_disk;
6409
NeilBrown17045f52011-12-23 10:17:53 +11006410 if (test_bit(Replacement, &rdev->flags)) {
6411 if (disk->replacement)
6412 goto abort;
6413 disk->replacement = rdev;
6414 } else {
6415 if (disk->rdev)
6416 goto abort;
6417 disk->rdev = rdev;
6418 }
NeilBrown91adb562009-03-31 14:39:39 +11006419
6420 if (test_bit(In_sync, &rdev->flags)) {
6421 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10006422 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
6423 " disk %d\n",
6424 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006425 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006426 /* Cannot rely on bitmap to complete recovery */
6427 conf->fullsync = 1;
6428 }
6429
NeilBrown91adb562009-03-31 14:39:39 +11006430 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006431 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006432 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006433 if (raid6_call.xor_syndrome)
6434 conf->rmw_level = PARITY_ENABLE_RMW;
6435 else
6436 conf->rmw_level = PARITY_DISABLE_RMW;
6437 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006438 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006439 conf->rmw_level = PARITY_ENABLE_RMW;
6440 }
NeilBrown91adb562009-03-31 14:39:39 +11006441 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006442 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006443 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10006444 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11006445 conf->prev_algo = mddev->layout;
6446 }
NeilBrown91adb562009-03-31 14:39:39 +11006447
NeilBrown486f0642015-02-25 12:10:35 +11006448 memory = NR_STRIPES * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11006449 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11006450 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
Shaohua Li566c09c2013-11-14 15:16:17 +11006451 if (grow_stripes(conf, NR_STRIPES)) {
NeilBrown91adb562009-03-31 14:39:39 +11006452 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006453 "md/raid:%s: couldn't allocate %dkB for buffers\n",
6454 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006455 goto abort;
6456 } else
NeilBrown0c55e022010-05-03 14:09:02 +10006457 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
6458 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006459
NeilBrown02326052012-07-03 15:56:52 +10006460 sprintf(pers_name, "raid%d", mddev->new_level);
6461 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11006462 if (!conf->thread) {
6463 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006464 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11006465 mdname(mddev));
6466 goto abort;
6467 }
6468
6469 return conf;
6470
6471 abort:
6472 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006473 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006474 return ERR_PTR(-EIO);
6475 } else
6476 return ERR_PTR(-ENOMEM);
6477}
6478
NeilBrownc148ffd2009-11-13 17:47:00 +11006479static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6480{
6481 switch (algo) {
6482 case ALGORITHM_PARITY_0:
6483 if (raid_disk < max_degraded)
6484 return 1;
6485 break;
6486 case ALGORITHM_PARITY_N:
6487 if (raid_disk >= raid_disks - max_degraded)
6488 return 1;
6489 break;
6490 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006491 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006492 raid_disk == raid_disks - 1)
6493 return 1;
6494 break;
6495 case ALGORITHM_LEFT_ASYMMETRIC_6:
6496 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6497 case ALGORITHM_LEFT_SYMMETRIC_6:
6498 case ALGORITHM_RIGHT_SYMMETRIC_6:
6499 if (raid_disk == raid_disks - 1)
6500 return 1;
6501 }
6502 return 0;
6503}
6504
NeilBrownfd01b882011-10-11 16:47:53 +11006505static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11006506{
NeilBrownd1688a62011-10-11 16:49:52 +11006507 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10006508 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11006509 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006510 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11006511 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11006512 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10006513 long long min_offset_diff = 0;
6514 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11006515
Andre Noll8c6ac862009-06-18 08:48:06 +10006516 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10006517 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10006518 " -- starting background reconstruction\n",
6519 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10006520
6521 rdev_for_each(rdev, mddev) {
6522 long long diff;
6523 if (rdev->raid_disk < 0)
6524 continue;
6525 diff = (rdev->new_data_offset - rdev->data_offset);
6526 if (first) {
6527 min_offset_diff = diff;
6528 first = 0;
6529 } else if (mddev->reshape_backwards &&
6530 diff < min_offset_diff)
6531 min_offset_diff = diff;
6532 else if (!mddev->reshape_backwards &&
6533 diff > min_offset_diff)
6534 min_offset_diff = diff;
6535 }
6536
NeilBrownf6705572006-03-27 01:18:11 -08006537 if (mddev->reshape_position != MaxSector) {
6538 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10006539 * Difficulties arise if the stripe we would write to
6540 * next is at or after the stripe we would read from next.
6541 * For a reshape that changes the number of devices, this
6542 * is only possible for a very short time, and mdadm makes
6543 * sure that time appears to have past before assembling
6544 * the array. So we fail if that time hasn't passed.
6545 * For a reshape that keeps the number of devices the same
6546 * mdadm must be monitoring the reshape can keeping the
6547 * critical areas read-only and backed up. It will start
6548 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08006549 */
6550 sector_t here_new, here_old;
6551 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11006552 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08006553
NeilBrown88ce4932009-03-31 15:24:23 +11006554 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10006555 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08006556 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08006557 mdname(mddev));
6558 return -EINVAL;
6559 }
NeilBrownf6705572006-03-27 01:18:11 -08006560 old_disks = mddev->raid_disks - mddev->delta_disks;
6561 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08006562 * further up in new geometry must map after here in old
6563 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08006564 */
6565 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10006566 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08006567 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10006568 printk(KERN_ERR "md/raid:%s: reshape_position not "
6569 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006570 return -EINVAL;
6571 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006572 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006573 /* here_new is the stripe we will write to */
6574 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10006575 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08006576 (old_disks-max_degraded));
6577 /* here_old is the first stripe that we might need to read
6578 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10006579 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10006580 if ((here_new * mddev->new_chunk_sectors !=
6581 here_old * mddev->chunk_sectors)) {
6582 printk(KERN_ERR "md/raid:%s: reshape position is"
6583 " confused - aborting\n", mdname(mddev));
6584 return -EINVAL;
6585 }
NeilBrown67ac6012009-08-13 10:06:24 +10006586 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10006587 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10006588 * and taking constant backups.
6589 * mdadm always starts a situation like this in
6590 * readonly mode so it can take control before
6591 * allowing any writes. So just check for that.
6592 */
NeilBrownb5254dd2012-05-21 09:27:01 +10006593 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
6594 abs(min_offset_diff) >= mddev->new_chunk_sectors)
6595 /* not really in-place - so OK */;
6596 else if (mddev->ro == 0) {
6597 printk(KERN_ERR "md/raid:%s: in-place reshape "
6598 "must be started in read-only mode "
6599 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10006600 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10006601 return -EINVAL;
6602 }
NeilBrown2c810cd2012-05-21 09:27:00 +10006603 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10006604 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10006605 here_old * mddev->chunk_sectors)
6606 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10006607 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08006608 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10006609 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
6610 "auto-recovery - aborting.\n",
6611 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006612 return -EINVAL;
6613 }
NeilBrown0c55e022010-05-03 14:09:02 +10006614 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
6615 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006616 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08006617 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006618 BUG_ON(mddev->level != mddev->new_level);
6619 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10006620 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11006621 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08006622 }
6623
NeilBrown245f46c2009-03-31 14:39:39 +11006624 if (mddev->private == NULL)
6625 conf = setup_conf(mddev);
6626 else
6627 conf = mddev->private;
6628
NeilBrown91adb562009-03-31 14:39:39 +11006629 if (IS_ERR(conf))
6630 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08006631
NeilBrownb5254dd2012-05-21 09:27:01 +10006632 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11006633 mddev->thread = conf->thread;
6634 conf->thread = NULL;
6635 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
NeilBrown17045f52011-12-23 10:17:53 +11006637 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
6638 i++) {
6639 rdev = conf->disks[i].rdev;
6640 if (!rdev && conf->disks[i].replacement) {
6641 /* The replacement is all we have yet */
6642 rdev = conf->disks[i].replacement;
6643 conf->disks[i].replacement = NULL;
6644 clear_bit(Replacement, &rdev->flags);
6645 conf->disks[i].rdev = rdev;
6646 }
6647 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11006648 continue;
NeilBrown17045f52011-12-23 10:17:53 +11006649 if (conf->disks[i].replacement &&
6650 conf->reshape_progress != MaxSector) {
6651 /* replacements and reshape simply do not mix. */
6652 printk(KERN_ERR "md: cannot handle concurrent "
6653 "replacement and reshape.\n");
6654 goto abort;
6655 }
NeilBrown2f115882010-06-17 17:41:03 +10006656 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11006657 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10006658 continue;
6659 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006660 /* This disc is not fully in-sync. However if it
6661 * just stored parity (beyond the recovery_offset),
6662 * when we don't need to be concerned about the
6663 * array being dirty.
6664 * When reshape goes 'backwards', we never have
6665 * partially completed devices, so we only need
6666 * to worry about reshape going forwards.
6667 */
6668 /* Hack because v0.91 doesn't store recovery_offset properly. */
6669 if (mddev->major_version == 0 &&
6670 mddev->minor_version > 90)
6671 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006672
NeilBrownc148ffd2009-11-13 17:47:00 +11006673 if (rdev->recovery_offset < reshape_offset) {
6674 /* We need to check old and new layout */
6675 if (!only_parity(rdev->raid_disk,
6676 conf->algorithm,
6677 conf->raid_disks,
6678 conf->max_degraded))
6679 continue;
6680 }
6681 if (!only_parity(rdev->raid_disk,
6682 conf->prev_algo,
6683 conf->previous_raid_disks,
6684 conf->max_degraded))
6685 continue;
6686 dirty_parity_disks++;
6687 }
NeilBrown91adb562009-03-31 14:39:39 +11006688
NeilBrown17045f52011-12-23 10:17:53 +11006689 /*
6690 * 0 for a fully functional array, 1 or 2 for a degraded array.
6691 */
NeilBrown908f4fb2011-12-23 10:17:50 +11006692 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693
NeilBrown674806d2010-06-16 17:17:53 +10006694 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006695 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006697 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 goto abort;
6699 }
6700
NeilBrown91adb562009-03-31 14:39:39 +11006701 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006702 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006703 mddev->resync_max_sectors = mddev->dev_sectors;
6704
NeilBrownc148ffd2009-11-13 17:47:00 +11006705 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006707 if (mddev->ok_start_degraded)
6708 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006709 "md/raid:%s: starting dirty degraded array"
6710 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006711 mdname(mddev));
6712 else {
6713 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006714 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006715 mdname(mddev));
6716 goto abort;
6717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 }
6719
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006721 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6722 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006723 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6724 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 else
NeilBrown0c55e022010-05-03 14:09:02 +10006726 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6727 " out of %d devices, algorithm %d\n",
6728 mdname(mddev), conf->level,
6729 mddev->raid_disks - mddev->degraded,
6730 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
6732 print_raid5_conf(conf);
6733
NeilBrownfef9c612009-03-31 15:16:46 +11006734 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006735 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006736 atomic_set(&conf->reshape_stripes, 0);
6737 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6738 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6739 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6740 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6741 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006742 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006743 }
6744
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006746 if (mddev->to_remove == &raid5_attrs_group)
6747 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006748 else if (mddev->kobj.sd &&
6749 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006750 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006751 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006752 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006753 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6754
6755 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006756 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006757 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006758 /* read-ahead size must cover two whole stripes, which
6759 * is 2 * (datadisks) * chunksize where 'n' is the
6760 * number of raid devices
6761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6763 int stripe = data_disks *
6764 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6765 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6766 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006767
NeilBrown9f7c2222010-07-26 12:04:13 +10006768 chunk_size = mddev->chunk_sectors << 9;
6769 blk_queue_io_min(mddev->queue, chunk_size);
6770 blk_queue_io_opt(mddev->queue, chunk_size *
6771 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07006772 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006773 /*
6774 * We can only discard a whole stripe. It doesn't make sense to
6775 * discard data disk but write parity disk
6776 */
6777 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11006778 /* Round up to power of 2, as discard handling
6779 * currently assumes that */
6780 while ((stripe-1) & stripe)
6781 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006782 mddev->queue->limits.discard_alignment = stripe;
6783 mddev->queue->limits.discard_granularity = stripe;
6784 /*
6785 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10006786 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11006787 */
6788 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10006789
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006790 blk_queue_max_write_same_sectors(mddev->queue, 0);
6791
NeilBrown05616be2012-05-21 09:27:00 +10006792 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006793 disk_stack_limits(mddev->gendisk, rdev->bdev,
6794 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10006795 disk_stack_limits(mddev->gendisk, rdev->bdev,
6796 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11006797 /*
6798 * discard_zeroes_data is required, otherwise data
6799 * could be lost. Consider a scenario: discard a stripe
6800 * (the stripe could be inconsistent if
6801 * discard_zeroes_data is 0); write one disk of the
6802 * stripe (the stripe could be inconsistent again
6803 * depending on which disks are used to calculate
6804 * parity); the disk is broken; The stripe data of this
6805 * disk is lost.
6806 */
6807 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6808 !bdev_get_queue(rdev->bdev)->
6809 limits.discard_zeroes_data)
6810 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10006811 /* Unfortunately, discard_zeroes_data is not currently
6812 * a guarantee - just a hint. So we only allow DISCARD
6813 * if the sysadmin has confirmed that only safe devices
6814 * are in use by setting a module parameter.
6815 */
6816 if (!devices_handle_discard_safely) {
6817 if (discard_supported) {
6818 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
6819 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
6820 }
6821 discard_supported = false;
6822 }
NeilBrown05616be2012-05-21 09:27:00 +10006823 }
Shaohua Li620125f2012-10-11 13:49:05 +11006824
6825 if (discard_supported &&
6826 mddev->queue->limits.max_discard_sectors >= stripe &&
6827 mddev->queue->limits.discard_granularity >= stripe)
6828 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6829 mddev->queue);
6830 else
6831 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6832 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 }
6834
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 return 0;
6836abort:
NeilBrown01f96c02011-09-21 15:30:20 +10006837 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11006838 print_raid5_conf(conf);
6839 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10006841 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 return -EIO;
6843}
6844
NeilBrownafa0f552014-12-15 12:56:58 +11006845static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846{
NeilBrownafa0f552014-12-15 12:56:58 +11006847 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Dan Williams95fc17a2009-07-31 12:39:15 +10006849 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10006850 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851}
6852
NeilBrownfd01b882011-10-11 16:47:53 +11006853static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854{
NeilBrownd1688a62011-10-11 16:49:52 +11006855 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 int i;
6857
Andre Noll9d8f0362009-06-18 08:45:01 +10006858 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
6859 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07006860 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 for (i = 0; i < conf->raid_disks; i++)
6862 seq_printf (seq, "%s",
6863 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08006864 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866}
6867
NeilBrownd1688a62011-10-11 16:49:52 +11006868static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869{
6870 int i;
6871 struct disk_info *tmp;
6872
NeilBrown0c55e022010-05-03 14:09:02 +10006873 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 if (!conf) {
6875 printk("(conf==NULL)\n");
6876 return;
6877 }
NeilBrown0c55e022010-05-03 14:09:02 +10006878 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
6879 conf->raid_disks,
6880 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
6882 for (i = 0; i < conf->raid_disks; i++) {
6883 char b[BDEVNAME_SIZE];
6884 tmp = conf->disks + i;
6885 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10006886 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
6887 i, !test_bit(Faulty, &tmp->rdev->flags),
6888 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 }
6890}
6891
NeilBrownfd01b882011-10-11 16:47:53 +11006892static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893{
6894 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11006895 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10006897 int count = 0;
6898 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
6900 for (i = 0; i < conf->raid_disks; i++) {
6901 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11006902 if (tmp->replacement
6903 && tmp->replacement->recovery_offset == MaxSector
6904 && !test_bit(Faulty, &tmp->replacement->flags)
6905 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6906 /* Replacement has just become active. */
6907 if (!tmp->rdev
6908 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6909 count++;
6910 if (tmp->rdev) {
6911 /* Replaced device not technically faulty,
6912 * but we need to be sure it gets removed
6913 * and never re-added.
6914 */
6915 set_bit(Faulty, &tmp->rdev->flags);
6916 sysfs_notify_dirent_safe(
6917 tmp->rdev->sysfs_state);
6918 }
6919 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6920 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10006921 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08006922 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07006923 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10006924 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11006925 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 }
6927 }
NeilBrown6b965622010-08-18 11:56:59 +10006928 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006929 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006930 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006932 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933}
6934
NeilBrownb8321b62011-12-23 10:17:51 +11006935static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936{
NeilBrownd1688a62011-10-11 16:49:52 +11006937 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006939 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006940 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 struct disk_info *p = conf->disks + number;
6942
6943 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006944 if (rdev == p->rdev)
6945 rdevp = &p->rdev;
6946 else if (rdev == p->replacement)
6947 rdevp = &p->replacement;
6948 else
6949 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006950
NeilBrown657e3e42011-12-23 10:17:52 +11006951 if (number >= conf->raid_disks &&
6952 conf->reshape_progress == MaxSector)
6953 clear_bit(In_sync, &rdev->flags);
6954
6955 if (test_bit(In_sync, &rdev->flags) ||
6956 atomic_read(&rdev->nr_pending)) {
6957 err = -EBUSY;
6958 goto abort;
6959 }
6960 /* Only remove non-faulty devices if recovery
6961 * isn't possible.
6962 */
6963 if (!test_bit(Faulty, &rdev->flags) &&
6964 mddev->recovery_disabled != conf->recovery_disabled &&
6965 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006966 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006967 number < conf->raid_disks) {
6968 err = -EBUSY;
6969 goto abort;
6970 }
6971 *rdevp = NULL;
6972 synchronize_rcu();
6973 if (atomic_read(&rdev->nr_pending)) {
6974 /* lost the race, try later */
6975 err = -EBUSY;
6976 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006977 } else if (p->replacement) {
6978 /* We must have just cleared 'rdev' */
6979 p->rdev = p->replacement;
6980 clear_bit(Replacement, &p->replacement->flags);
6981 smp_mb(); /* Make sure other CPUs may see both as identical
6982 * but will never see neither - if they are careful
6983 */
6984 p->replacement = NULL;
6985 clear_bit(WantReplacement, &rdev->flags);
6986 } else
6987 /* We might have just removed the Replacement as faulty-
6988 * clear the bit just in case
6989 */
6990 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991abort:
6992
6993 print_raid5_conf(conf);
6994 return err;
6995}
6996
NeilBrownfd01b882011-10-11 16:47:53 +11006997static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998{
NeilBrownd1688a62011-10-11 16:49:52 +11006999 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007000 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 int disk;
7002 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007003 int first = 0;
7004 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005
NeilBrown7f0da592011-07-28 11:39:22 +10007006 if (mddev->recovery_disabled == conf->recovery_disabled)
7007 return -EBUSY;
7008
NeilBrowndc10c642012-03-19 12:46:37 +11007009 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
Neil Brown6c2fce22008-06-28 08:31:31 +10007013 if (rdev->raid_disk >= 0)
7014 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
7016 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007017 * find the disk ... but prefer rdev->saved_raid_disk
7018 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007020 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007021 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007022 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007023 first = rdev->saved_raid_disk;
7024
7025 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007026 p = conf->disks + disk;
7027 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007028 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007030 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007031 if (rdev->saved_raid_disk != disk)
7032 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007033 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007036 }
7037 for (disk = first; disk <= last; disk++) {
7038 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007039 if (test_bit(WantReplacement, &p->rdev->flags) &&
7040 p->replacement == NULL) {
7041 clear_bit(In_sync, &rdev->flags);
7042 set_bit(Replacement, &rdev->flags);
7043 rdev->raid_disk = disk;
7044 err = 0;
7045 conf->fullsync = 1;
7046 rcu_assign_pointer(p->replacement, rdev);
7047 break;
7048 }
7049 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007050out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007052 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053}
7054
NeilBrownfd01b882011-10-11 16:47:53 +11007055static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056{
7057 /* no resync is happening, and there is enough space
7058 * on all devices, so we can resize.
7059 * We need to make sure resync covers any new space.
7060 * If the array is shrinking we should possibly wait until
7061 * any io in the removed space completes, but it hardly seems
7062 * worth it.
7063 */
NeilBrowna4a61252012-05-22 13:55:27 +10007064 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10007065 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007066 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7067 if (mddev->external_size &&
7068 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007069 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007070 if (mddev->bitmap) {
7071 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7072 if (ret)
7073 return ret;
7074 }
7075 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10007076 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10007077 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10007078 if (sectors > mddev->dev_sectors &&
7079 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007080 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7082 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007083 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007084 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 return 0;
7086}
7087
NeilBrownfd01b882011-10-11 16:47:53 +11007088static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007089{
7090 /* Can only proceed if there are plenty of stripe_heads.
7091 * We need a minimum of one full stripe,, and for sensible progress
7092 * it is best to have about 4 times that.
7093 * If we require 4 times, then the default 256 4K stripe_heads will
7094 * allow for chunk sizes up to 256K, which is probably OK.
7095 * If the chunk size is greater, user-space should request more
7096 * stripe_heads first.
7097 */
NeilBrownd1688a62011-10-11 16:49:52 +11007098 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007099 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
7100 > conf->max_nr_stripes ||
7101 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
7102 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10007103 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7104 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10007105 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7106 / STRIPE_SIZE)*4);
7107 return 0;
7108 }
7109 return 1;
7110}
7111
NeilBrownfd01b882011-10-11 16:47:53 +11007112static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007113{
NeilBrownd1688a62011-10-11 16:49:52 +11007114 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007115
NeilBrown88ce4932009-03-31 15:24:23 +11007116 if (mddev->delta_disks == 0 &&
7117 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007118 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007119 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007120 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007121 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007122 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007123 /* We might be able to shrink, but the devices must
7124 * be made bigger first.
7125 * For raid6, 4 is the minimum size.
7126 * Otherwise 2 is the minimum
7127 */
7128 int min = 2;
7129 if (mddev->level == 6)
7130 min = 4;
7131 if (mddev->raid_disks + mddev->delta_disks < min)
7132 return -EINVAL;
7133 }
NeilBrown29269552006-03-27 01:18:10 -08007134
NeilBrown01ee22b2009-06-18 08:47:20 +10007135 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007136 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007137
NeilBrowne56108d62012-10-11 14:24:13 +11007138 return resize_stripes(conf, (conf->previous_raid_disks
7139 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007140}
7141
NeilBrownfd01b882011-10-11 16:47:53 +11007142static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007143{
NeilBrownd1688a62011-10-11 16:49:52 +11007144 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007145 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007146 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007147 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007148
NeilBrownf4168852007-02-28 20:11:53 -08007149 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007150 return -EBUSY;
7151
NeilBrown01ee22b2009-06-18 08:47:20 +10007152 if (!check_stripe_cache(mddev))
7153 return -ENOSPC;
7154
NeilBrown30b67642012-05-22 13:55:28 +10007155 if (has_failed(conf))
7156 return -EINVAL;
7157
NeilBrownc6563a82012-05-21 09:27:00 +10007158 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007159 if (!test_bit(In_sync, &rdev->flags)
7160 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007161 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007162 }
NeilBrown63c70c42006-03-27 01:18:13 -08007163
NeilBrownf4168852007-02-28 20:11:53 -08007164 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007165 /* Not enough devices even to make a degraded array
7166 * of that size
7167 */
7168 return -EINVAL;
7169
NeilBrownec32a2b2009-03-31 15:17:38 +11007170 /* Refuse to reduce size of the array. Any reductions in
7171 * array size must be through explicit setting of array_size
7172 * attribute.
7173 */
7174 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7175 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10007176 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11007177 "before number of disks\n", mdname(mddev));
7178 return -EINVAL;
7179 }
7180
NeilBrownf6705572006-03-27 01:18:11 -08007181 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007182 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007183 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007184 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007185 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007186 conf->prev_chunk_sectors = conf->chunk_sectors;
7187 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007188 conf->prev_algo = conf->algorithm;
7189 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007190 conf->generation++;
7191 /* Code that selects data_offset needs to see the generation update
7192 * if reshape_progress has been set - so a memory barrier needed.
7193 */
7194 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007195 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007196 conf->reshape_progress = raid5_size(mddev, 0, 0);
7197 else
7198 conf->reshape_progress = 0;
7199 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007200 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007201 spin_unlock_irq(&conf->device_lock);
7202
NeilBrown4d77e3b2013-08-27 15:57:47 +10007203 /* Now make sure any requests that proceeded on the assumption
7204 * the reshape wasn't running - like Discard or Read - have
7205 * completed.
7206 */
7207 mddev_suspend(mddev);
7208 mddev_resume(mddev);
7209
NeilBrown29269552006-03-27 01:18:10 -08007210 /* Add some new drives, as many as will fit.
7211 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007212 * Don't add devices if we are reducing the number of
7213 * devices in the array. This is because it is not possible
7214 * to correctly record the "partially reconstructed" state of
7215 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007216 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007217 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007218 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007219 if (rdev->raid_disk < 0 &&
7220 !test_bit(Faulty, &rdev->flags)) {
7221 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007222 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007223 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007224 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007225 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007226 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007227
7228 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007229 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007230 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007231 } else if (rdev->raid_disk >= conf->previous_raid_disks
7232 && !test_bit(Faulty, &rdev->flags)) {
7233 /* This is a spare that was manually added */
7234 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007235 }
NeilBrown29269552006-03-27 01:18:10 -08007236
NeilBrown87a8dec2011-01-31 11:57:43 +11007237 /* When a reshape changes the number of devices,
7238 * ->degraded is measured against the larger of the
7239 * pre and post number of devices.
7240 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007241 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11007242 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007243 spin_unlock_irqrestore(&conf->device_lock, flags);
7244 }
NeilBrown63c70c42006-03-27 01:18:13 -08007245 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007246 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07007247 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08007248
NeilBrown29269552006-03-27 01:18:10 -08007249 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7250 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7251 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7252 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7253 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007254 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007255 if (!mddev->sync_thread) {
7256 mddev->recovery = 0;
7257 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007258 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007259 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007260 mddev->new_chunk_sectors =
7261 conf->chunk_sectors = conf->prev_chunk_sectors;
7262 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007263 rdev_for_each(rdev, mddev)
7264 rdev->new_data_offset = rdev->data_offset;
7265 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007266 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007267 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007268 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007269 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007270 spin_unlock_irq(&conf->device_lock);
7271 return -EAGAIN;
7272 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007273 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007274 md_wakeup_thread(mddev->sync_thread);
7275 md_new_event(mddev);
7276 return 0;
7277}
NeilBrown29269552006-03-27 01:18:10 -08007278
NeilBrownec32a2b2009-03-31 15:17:38 +11007279/* This is called from the reshape thread and should make any
7280 * changes needed in 'conf'
7281 */
NeilBrownd1688a62011-10-11 16:49:52 +11007282static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007283{
NeilBrown29269552006-03-27 01:18:10 -08007284
NeilBrownf6705572006-03-27 01:18:11 -08007285 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007286 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007287
NeilBrownf6705572006-03-27 01:18:11 -08007288 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007289 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007290 rdev_for_each(rdev, conf->mddev)
7291 rdev->data_offset = rdev->new_data_offset;
7292 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007293 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007294 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007295 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007296
7297 /* read-ahead size must cover two whole stripes, which is
7298 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7299 */
NeilBrown4a5add42010-06-01 19:37:28 +10007300 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007301 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007302 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007303 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07007304 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
7305 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
7306 }
NeilBrown29269552006-03-27 01:18:10 -08007307 }
NeilBrown29269552006-03-27 01:18:10 -08007308}
7309
NeilBrownec32a2b2009-03-31 15:17:38 +11007310/* This is called from the raid5d thread with mddev_lock held.
7311 * It makes config changes to the device.
7312 */
NeilBrownfd01b882011-10-11 16:47:53 +11007313static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007314{
NeilBrownd1688a62011-10-11 16:49:52 +11007315 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007316
7317 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7318
NeilBrownec32a2b2009-03-31 15:17:38 +11007319 if (mddev->delta_disks > 0) {
7320 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7321 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10007322 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11007323 } else {
7324 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007325 spin_lock_irq(&conf->device_lock);
7326 mddev->degraded = calc_degraded(conf);
7327 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007328 for (d = conf->raid_disks ;
7329 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007330 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007331 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007332 if (rdev)
7333 clear_bit(In_sync, &rdev->flags);
7334 rdev = conf->disks[d].replacement;
7335 if (rdev)
7336 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007337 }
NeilBrowncea9c222009-03-31 15:15:05 +11007338 }
NeilBrown88ce4932009-03-31 15:24:23 +11007339 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007340 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007341 mddev->reshape_position = MaxSector;
7342 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007343 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11007344 }
7345}
7346
NeilBrownfd01b882011-10-11 16:47:53 +11007347static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07007348{
NeilBrownd1688a62011-10-11 16:49:52 +11007349 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07007350
7351 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08007352 case 2: /* resume for a suspend */
7353 wake_up(&conf->wait_for_overlap);
7354 break;
7355
NeilBrown72626682005-09-09 16:23:54 -07007356 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007357 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007358 /* '2' tells resync/reshape to pause so that all
7359 * active stripes can drain
7360 */
7361 conf->quiesce = 2;
Shaohua Li566c09c2013-11-14 15:16:17 +11007362 wait_event_cmd(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08007363 atomic_read(&conf->active_stripes) == 0 &&
7364 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11007365 unlock_all_device_hash_locks_irq(conf),
7366 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10007367 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11007368 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007369 /* allow reshape to continue */
7370 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07007371 break;
7372
7373 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007374 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007375 conf->quiesce = 0;
7376 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08007377 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11007378 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007379 break;
7380 }
NeilBrown72626682005-09-09 16:23:54 -07007381}
NeilBrownb15c2e52006-01-06 00:20:16 -08007382
NeilBrownfd01b882011-10-11 16:47:53 +11007383static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11007384{
NeilBrowne373ab12011-10-11 16:48:59 +11007385 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07007386 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11007387
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007388 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11007389 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10007390 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7391 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007392 return ERR_PTR(-EINVAL);
7393 }
7394
NeilBrowne373ab12011-10-11 16:48:59 +11007395 sectors = raid0_conf->strip_zone[0].zone_end;
7396 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10007397 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007398 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11007399 mddev->new_layout = ALGORITHM_PARITY_N;
7400 mddev->new_chunk_sectors = mddev->chunk_sectors;
7401 mddev->raid_disks += 1;
7402 mddev->delta_disks = 1;
7403 /* make sure it will be not marked as dirty */
7404 mddev->recovery_cp = MaxSector;
7405
7406 return setup_conf(mddev);
7407}
7408
NeilBrownfd01b882011-10-11 16:47:53 +11007409static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007410{
7411 int chunksect;
7412
7413 if (mddev->raid_disks != 2 ||
7414 mddev->degraded > 1)
7415 return ERR_PTR(-EINVAL);
7416
7417 /* Should check if there are write-behind devices? */
7418
7419 chunksect = 64*2; /* 64K by default */
7420
7421 /* The array must be an exact multiple of chunksize */
7422 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7423 chunksect >>= 1;
7424
7425 if ((chunksect<<9) < STRIPE_SIZE)
7426 /* array size does not allow a suitable chunk size */
7427 return ERR_PTR(-EINVAL);
7428
7429 mddev->new_level = 5;
7430 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10007431 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11007432
7433 return setup_conf(mddev);
7434}
7435
NeilBrownfd01b882011-10-11 16:47:53 +11007436static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11007437{
7438 int new_layout;
7439
7440 switch (mddev->layout) {
7441 case ALGORITHM_LEFT_ASYMMETRIC_6:
7442 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7443 break;
7444 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7445 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7446 break;
7447 case ALGORITHM_LEFT_SYMMETRIC_6:
7448 new_layout = ALGORITHM_LEFT_SYMMETRIC;
7449 break;
7450 case ALGORITHM_RIGHT_SYMMETRIC_6:
7451 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
7452 break;
7453 case ALGORITHM_PARITY_0_6:
7454 new_layout = ALGORITHM_PARITY_0;
7455 break;
7456 case ALGORITHM_PARITY_N:
7457 new_layout = ALGORITHM_PARITY_N;
7458 break;
7459 default:
7460 return ERR_PTR(-EINVAL);
7461 }
7462 mddev->new_level = 5;
7463 mddev->new_layout = new_layout;
7464 mddev->delta_disks = -1;
7465 mddev->raid_disks -= 1;
7466 return setup_conf(mddev);
7467}
7468
NeilBrownfd01b882011-10-11 16:47:53 +11007469static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11007470{
NeilBrown88ce4932009-03-31 15:24:23 +11007471 /* For a 2-drive array, the layout and chunk size can be changed
7472 * immediately as not restriping is needed.
7473 * For larger arrays we record the new value - after validation
7474 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11007475 */
NeilBrownd1688a62011-10-11 16:49:52 +11007476 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10007477 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11007478
NeilBrown597a7112009-06-18 08:47:42 +10007479 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11007480 return -EINVAL;
7481 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007482 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11007483 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007484 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11007485 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007486 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11007487 /* not factor of array size */
7488 return -EINVAL;
7489 }
7490
7491 /* They look valid */
7492
NeilBrown88ce4932009-03-31 15:24:23 +11007493 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10007494 /* can make the change immediately */
7495 if (mddev->new_layout >= 0) {
7496 conf->algorithm = mddev->new_layout;
7497 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11007498 }
7499 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10007500 conf->chunk_sectors = new_chunk ;
7501 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11007502 }
7503 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7504 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11007505 }
NeilBrown50ac1682009-06-18 08:47:55 +10007506 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11007507}
7508
NeilBrownfd01b882011-10-11 16:47:53 +11007509static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11007510{
NeilBrown597a7112009-06-18 08:47:42 +10007511 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10007512
NeilBrown597a7112009-06-18 08:47:42 +10007513 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11007514 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007515 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007516 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11007517 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007518 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11007519 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007520 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11007521 /* not factor of array size */
7522 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007523 }
NeilBrown88ce4932009-03-31 15:24:23 +11007524
7525 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10007526 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11007527}
7528
NeilBrownfd01b882011-10-11 16:47:53 +11007529static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007530{
7531 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007532 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007533 * raid1 - if there are two drives. We need to know the chunk size
7534 * raid4 - trivial - just use a raid4 layout.
7535 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007536 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007537 if (mddev->level == 0)
7538 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11007539 if (mddev->level == 1)
7540 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11007541 if (mddev->level == 4) {
7542 mddev->new_layout = ALGORITHM_PARITY_N;
7543 mddev->new_level = 5;
7544 return setup_conf(mddev);
7545 }
NeilBrownfc9739c2009-03-31 14:57:20 +11007546 if (mddev->level == 6)
7547 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11007548
7549 return ERR_PTR(-EINVAL);
7550}
7551
NeilBrownfd01b882011-10-11 16:47:53 +11007552static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11007553{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007554 /* raid4 can take over:
7555 * raid0 - if there is only one strip zone
7556 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11007557 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007558 if (mddev->level == 0)
7559 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11007560 if (mddev->level == 5 &&
7561 mddev->layout == ALGORITHM_PARITY_N) {
7562 mddev->new_layout = 0;
7563 mddev->new_level = 4;
7564 return setup_conf(mddev);
7565 }
7566 return ERR_PTR(-EINVAL);
7567}
NeilBrownd562b0c2009-03-31 14:39:39 +11007568
NeilBrown84fc4b52011-10-11 16:49:58 +11007569static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11007570
NeilBrownfd01b882011-10-11 16:47:53 +11007571static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11007572{
7573 /* Currently can only take over a raid5. We map the
7574 * personality to an equivalent raid6 personality
7575 * with the Q block at the end.
7576 */
7577 int new_layout;
7578
7579 if (mddev->pers != &raid5_personality)
7580 return ERR_PTR(-EINVAL);
7581 if (mddev->degraded > 1)
7582 return ERR_PTR(-EINVAL);
7583 if (mddev->raid_disks > 253)
7584 return ERR_PTR(-EINVAL);
7585 if (mddev->raid_disks < 3)
7586 return ERR_PTR(-EINVAL);
7587
7588 switch (mddev->layout) {
7589 case ALGORITHM_LEFT_ASYMMETRIC:
7590 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
7591 break;
7592 case ALGORITHM_RIGHT_ASYMMETRIC:
7593 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
7594 break;
7595 case ALGORITHM_LEFT_SYMMETRIC:
7596 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
7597 break;
7598 case ALGORITHM_RIGHT_SYMMETRIC:
7599 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
7600 break;
7601 case ALGORITHM_PARITY_0:
7602 new_layout = ALGORITHM_PARITY_0_6;
7603 break;
7604 case ALGORITHM_PARITY_N:
7605 new_layout = ALGORITHM_PARITY_N;
7606 break;
7607 default:
7608 return ERR_PTR(-EINVAL);
7609 }
7610 mddev->new_level = 6;
7611 mddev->new_layout = new_layout;
7612 mddev->delta_disks = 1;
7613 mddev->raid_disks += 1;
7614 return setup_conf(mddev);
7615}
7616
NeilBrown84fc4b52011-10-11 16:49:58 +11007617static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07007618{
7619 .name = "raid6",
7620 .level = 6,
7621 .owner = THIS_MODULE,
7622 .make_request = make_request,
7623 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007624 .free = raid5_free,
NeilBrown16a53ec2006-06-26 00:27:38 -07007625 .status = status,
7626 .error_handler = error,
7627 .hot_add_disk = raid5_add_disk,
7628 .hot_remove_disk= raid5_remove_disk,
7629 .spare_active = raid5_spare_active,
7630 .sync_request = sync_request,
7631 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007632 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10007633 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08007634 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007635 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07007636 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11007637 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007638 .congested = raid5_congested,
NeilBrown64590f42014-12-15 12:56:57 +11007639 .mergeable_bvec = raid5_mergeable_bvec,
NeilBrown16a53ec2006-06-26 00:27:38 -07007640};
NeilBrown84fc4b52011-10-11 16:49:58 +11007641static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642{
7643 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08007644 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645 .owner = THIS_MODULE,
7646 .make_request = make_request,
7647 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007648 .free = raid5_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649 .status = status,
7650 .error_handler = error,
7651 .hot_add_disk = raid5_add_disk,
7652 .hot_remove_disk= raid5_remove_disk,
7653 .spare_active = raid5_spare_active,
7654 .sync_request = sync_request,
7655 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007656 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08007657 .check_reshape = raid5_check_reshape,
7658 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007659 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07007660 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11007661 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007662 .congested = raid5_congested,
NeilBrown64590f42014-12-15 12:56:57 +11007663 .mergeable_bvec = raid5_mergeable_bvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664};
7665
NeilBrown84fc4b52011-10-11 16:49:58 +11007666static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667{
NeilBrown2604b702006-01-06 00:20:36 -08007668 .name = "raid4",
7669 .level = 4,
7670 .owner = THIS_MODULE,
7671 .make_request = make_request,
7672 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007673 .free = raid5_free,
NeilBrown2604b702006-01-06 00:20:36 -08007674 .status = status,
7675 .error_handler = error,
7676 .hot_add_disk = raid5_add_disk,
7677 .hot_remove_disk= raid5_remove_disk,
7678 .spare_active = raid5_spare_active,
7679 .sync_request = sync_request,
7680 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007681 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08007682 .check_reshape = raid5_check_reshape,
7683 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007684 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08007685 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11007686 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007687 .congested = raid5_congested,
NeilBrown64590f42014-12-15 12:56:57 +11007688 .mergeable_bvec = raid5_mergeable_bvec,
NeilBrown2604b702006-01-06 00:20:36 -08007689};
7690
7691static int __init raid5_init(void)
7692{
Shaohua Li851c30c2013-08-28 14:30:16 +08007693 raid5_wq = alloc_workqueue("raid5wq",
7694 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7695 if (!raid5_wq)
7696 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07007697 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007698 register_md_personality(&raid5_personality);
7699 register_md_personality(&raid4_personality);
7700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701}
7702
NeilBrown2604b702006-01-06 00:20:36 -08007703static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704{
NeilBrown16a53ec2006-06-26 00:27:38 -07007705 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007706 unregister_md_personality(&raid5_personality);
7707 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08007708 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709}
7710
7711module_init(raid5_init);
7712module_exit(raid5_exit);
7713MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007714MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08007716MODULE_ALIAS("md-raid5");
7717MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08007718MODULE_ALIAS("md-level-5");
7719MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07007720MODULE_ALIAS("md-personality-8"); /* RAID6 */
7721MODULE_ALIAS("md-raid6");
7722MODULE_ALIAS("md-level-6");
7723
7724/* This used to be two separate modules, they were: */
7725MODULE_ALIAS("raid5");
7726MODULE_ALIAS("raid6");