]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/raid5.c
Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / drivers / md / raid5.c
CommitLineData
1da177e4
LT
1/*
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
16a53ecc 5 * Copyright (C) 2002, 2003 H. Peter Anvin
1da177e4 6 *
16a53ecc
N
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
1da177e4
LT
10 *
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
ae3c20cc
N
21/*
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.
7c13edc8
N
30 * 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
ae3c20cc
N
32 * 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
7c13edc8 35 * the number of the batch it will be in. This is seq_flush+1.
ae3c20cc
N
36 * 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 */
1da177e4 45
bff61975 46#include <linux/blkdev.h>
f6705578 47#include <linux/kthread.h>
f701d589 48#include <linux/raid/pq.h>
91c00924 49#include <linux/async_tx.h>
056075c7 50#include <linux/module.h>
07a3b417 51#include <linux/async.h>
bff61975 52#include <linux/seq_file.h>
36d1c647 53#include <linux/cpu.h>
5a0e3ad6 54#include <linux/slab.h>
8bda470e 55#include <linux/ratelimit.h>
851c30c9 56#include <linux/nodemask.h>
46d5b785 57#include <linux/flex_array.h>
a9add5d9
N
58#include <trace/events/block.h>
59
43b2e5d8 60#include "md.h"
bff61975 61#include "raid5.h"
54071b38 62#include "raid0.h"
ef740c37 63#include "bitmap.h"
72626685 64
394ed8e4
SL
65#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
66
851c30c9
SL
67#define cpu_to_group(cpu) cpu_to_node(cpu)
68#define ANY_GROUP NUMA_NO_NODE
69
8e0e99ba
N
70static bool devices_handle_discard_safely = false;
71module_param(devices_handle_discard_safely, bool, 0644);
72MODULE_PARM_DESC(devices_handle_discard_safely,
73 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
851c30c9 74static struct workqueue_struct *raid5_wq;
1da177e4 75
d1688a6d 76static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
db298e19
N
77{
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80}
1da177e4 81
566c09c5
SL
82static inline int stripe_hash_locks_hash(sector_t sect)
83{
84 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
85}
86
87static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
88{
89 spin_lock_irq(conf->hash_locks + hash);
90 spin_lock(&conf->device_lock);
91}
92
93static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
94{
95 spin_unlock(&conf->device_lock);
96 spin_unlock_irq(conf->hash_locks + hash);
97}
98
99static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
100{
101 int i;
102 local_irq_disable();
103 spin_lock(conf->hash_locks);
104 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
105 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
106 spin_lock(&conf->device_lock);
107}
108
109static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
110{
111 int i;
112 spin_unlock(&conf->device_lock);
113 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
114 spin_unlock(conf->hash_locks + i - 1);
115 local_irq_enable();
116}
117
d0dabf7e
N
118/* Find first data disk in a raid6 stripe */
119static inline int raid6_d0(struct stripe_head *sh)
120{
67cc2b81
N
121 if (sh->ddf_layout)
122 /* ddf always start from first device */
123 return 0;
124 /* md starts just after Q block */
d0dabf7e
N
125 if (sh->qd_idx == sh->disks - 1)
126 return 0;
127 else
128 return sh->qd_idx + 1;
129}
16a53ecc
N
130static inline int raid6_next_disk(int disk, int raid_disks)
131{
132 disk++;
133 return (disk < raid_disks) ? disk : 0;
134}
a4456856 135
d0dabf7e
N
136/* When walking through the disks in a raid5, starting at raid6_d0,
137 * We need to map each disk to a 'slot', where the data disks are slot
138 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
139 * is raid_disks-1. This help does that mapping.
140 */
67cc2b81
N
141static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
142 int *count, int syndrome_disks)
d0dabf7e 143{
6629542e 144 int slot = *count;
67cc2b81 145
e4424fee 146 if (sh->ddf_layout)
6629542e 147 (*count)++;
d0dabf7e 148 if (idx == sh->pd_idx)
67cc2b81 149 return syndrome_disks;
d0dabf7e 150 if (idx == sh->qd_idx)
67cc2b81 151 return syndrome_disks + 1;
e4424fee 152 if (!sh->ddf_layout)
6629542e 153 (*count)++;
d0dabf7e
N
154 return slot;
155}
156
34a6f80e 157static void return_io(struct bio_list *return_bi)
a4456856 158{
34a6f80e
N
159 struct bio *bi;
160 while ((bi = bio_list_pop(return_bi)) != NULL) {
4f024f37 161 bi->bi_iter.bi_size = 0;
0a82a8d1
LT
162 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
163 bi, 0);
4246a0b6 164 bio_endio(bi);
a4456856
DW
165 }
166}
167
d1688a6d 168static void print_raid5_conf (struct r5conf *conf);
1da177e4 169
600aa109
DW
170static int stripe_operations_active(struct stripe_head *sh)
171{
172 return sh->check_state || sh->reconstruct_state ||
173 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
174 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
175}
176
851c30c9
SL
177static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
178{
179 struct r5conf *conf = sh->raid_conf;
180 struct r5worker_group *group;
bfc90cb0 181 int thread_cnt;
851c30c9
SL
182 int i, cpu = sh->cpu;
183
184 if (!cpu_online(cpu)) {
185 cpu = cpumask_any(cpu_online_mask);
186 sh->cpu = cpu;
187 }
188
189 if (list_empty(&sh->lru)) {
190 struct r5worker_group *group;
191 group = conf->worker_groups + cpu_to_group(cpu);
192 list_add_tail(&sh->lru, &group->handle_list);
bfc90cb0
SL
193 group->stripes_cnt++;
194 sh->group = group;
851c30c9
SL
195 }
196
197 if (conf->worker_cnt_per_group == 0) {
198 md_wakeup_thread(conf->mddev->thread);
199 return;
200 }
201
202 group = conf->worker_groups + cpu_to_group(sh->cpu);
203
bfc90cb0
SL
204 group->workers[0].working = true;
205 /* at least one worker should run to avoid race */
206 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
207
208 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
209 /* wakeup more workers */
210 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
211 if (group->workers[i].working == false) {
212 group->workers[i].working = true;
213 queue_work_on(sh->cpu, raid5_wq,
214 &group->workers[i].work);
215 thread_cnt--;
216 }
217 }
851c30c9
SL
218}
219
566c09c5
SL
220static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
221 struct list_head *temp_inactive_list)
1da177e4 222{
1e6d690b
SL
223 int i;
224 int injournal = 0; /* number of date pages with R5_InJournal */
225
4eb788df
SL
226 BUG_ON(!list_empty(&sh->lru));
227 BUG_ON(atomic_read(&conf->active_stripes)==0);
1e6d690b
SL
228
229 if (r5c_is_writeback(conf->log))
230 for (i = sh->disks; i--; )
231 if (test_bit(R5_InJournal, &sh->dev[i].flags))
232 injournal++;
a39f7afd
SL
233 /*
234 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
235 * data in journal, so they are not released to cached lists
236 */
237 if (conf->quiesce && r5c_is_writeback(conf->log) &&
238 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) {
239 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
240 r5c_make_stripe_write_out(sh);
241 set_bit(STRIPE_HANDLE, &sh->state);
242 }
1e6d690b 243
4eb788df
SL
244 if (test_bit(STRIPE_HANDLE, &sh->state)) {
245 if (test_bit(STRIPE_DELAYED, &sh->state) &&
ad3ab8b6 246 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4eb788df 247 list_add_tail(&sh->lru, &conf->delayed_list);
ad3ab8b6 248 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
4eb788df
SL
249 sh->bm_seq - conf->seq_write > 0)
250 list_add_tail(&sh->lru, &conf->bitmap_list);
251 else {
252 clear_bit(STRIPE_DELAYED, &sh->state);
253 clear_bit(STRIPE_BIT_DELAY, &sh->state);
851c30c9
SL
254 if (conf->worker_cnt_per_group == 0) {
255 list_add_tail(&sh->lru, &conf->handle_list);
256 } else {
257 raid5_wakeup_stripe_thread(sh);
258 return;
259 }
4eb788df
SL
260 }
261 md_wakeup_thread(conf->mddev->thread);
262 } else {
263 BUG_ON(stripe_operations_active(sh));
264 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
265 if (atomic_dec_return(&conf->preread_active_stripes)
266 < IO_THRESHOLD)
267 md_wakeup_thread(conf->mddev->thread);
268 atomic_dec(&conf->active_stripes);
1e6d690b
SL
269 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
270 if (!r5c_is_writeback(conf->log))
271 list_add_tail(&sh->lru, temp_inactive_list);
272 else {
273 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
274 if (injournal == 0)
275 list_add_tail(&sh->lru, temp_inactive_list);
276 else if (injournal == conf->raid_disks - conf->max_degraded) {
277 /* full stripe */
278 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
279 atomic_inc(&conf->r5c_cached_full_stripes);
280 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
281 atomic_dec(&conf->r5c_cached_partial_stripes);
282 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
a39f7afd 283 r5c_check_cached_full_stripe(conf);
03b047f4
SL
284 } else
285 /*
286 * STRIPE_R5C_PARTIAL_STRIPE is set in
287 * r5c_try_caching_write(). No need to
288 * set it again.
289 */
1e6d690b 290 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
1e6d690b
SL
291 }
292 }
1da177e4
LT
293 }
294}
d0dabf7e 295
566c09c5
SL
296static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
297 struct list_head *temp_inactive_list)
4eb788df
SL
298{
299 if (atomic_dec_and_test(&sh->count))
566c09c5
SL
300 do_release_stripe(conf, sh, temp_inactive_list);
301}
302
303/*
304 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
305 *
306 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
307 * given time. Adding stripes only takes device lock, while deleting stripes
308 * only takes hash lock.
309 */
310static void release_inactive_stripe_list(struct r5conf *conf,
311 struct list_head *temp_inactive_list,
312 int hash)
313{
314 int size;
6ab2a4b8 315 bool do_wakeup = false;
566c09c5
SL
316 unsigned long flags;
317
318 if (hash == NR_STRIPE_HASH_LOCKS) {
319 size = NR_STRIPE_HASH_LOCKS;
320 hash = NR_STRIPE_HASH_LOCKS - 1;
321 } else
322 size = 1;
323 while (size) {
324 struct list_head *list = &temp_inactive_list[size - 1];
325
326 /*
6d036f7d 327 * We don't hold any lock here yet, raid5_get_active_stripe() might
566c09c5
SL
328 * remove stripes from the list
329 */
330 if (!list_empty_careful(list)) {
331 spin_lock_irqsave(conf->hash_locks + hash, flags);
4bda556a
SL
332 if (list_empty(conf->inactive_list + hash) &&
333 !list_empty(list))
334 atomic_dec(&conf->empty_inactive_list_nr);
566c09c5 335 list_splice_tail_init(list, conf->inactive_list + hash);
6ab2a4b8 336 do_wakeup = true;
566c09c5
SL
337 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
338 }
339 size--;
340 hash--;
341 }
342
343 if (do_wakeup) {
6ab2a4b8 344 wake_up(&conf->wait_for_stripe);
b1b46486
YL
345 if (atomic_read(&conf->active_stripes) == 0)
346 wake_up(&conf->wait_for_quiescent);
566c09c5
SL
347 if (conf->retry_read_aligned)
348 md_wakeup_thread(conf->mddev->thread);
349 }
4eb788df
SL
350}
351
773ca82f 352/* should hold conf->device_lock already */
566c09c5
SL
353static int release_stripe_list(struct r5conf *conf,
354 struct list_head *temp_inactive_list)
773ca82f 355{
eae8263f 356 struct stripe_head *sh, *t;
773ca82f
SL
357 int count = 0;
358 struct llist_node *head;
359
360 head = llist_del_all(&conf->released_stripes);
d265d9dc 361 head = llist_reverse_order(head);
eae8263f 362 llist_for_each_entry_safe(sh, t, head, release_list) {
566c09c5
SL
363 int hash;
364
773ca82f
SL
365 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
366 smp_mb();
367 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
368 /*
369 * Don't worry the bit is set here, because if the bit is set
370 * again, the count is always > 1. This is true for
371 * STRIPE_ON_UNPLUG_LIST bit too.
372 */
566c09c5
SL
373 hash = sh->hash_lock_index;
374 __release_stripe(conf, sh, &temp_inactive_list[hash]);
773ca82f
SL
375 count++;
376 }
377
378 return count;
379}
380
6d036f7d 381void raid5_release_stripe(struct stripe_head *sh)
1da177e4 382{
d1688a6d 383 struct r5conf *conf = sh->raid_conf;
1da177e4 384 unsigned long flags;
566c09c5
SL
385 struct list_head list;
386 int hash;
773ca82f 387 bool wakeup;
16a53ecc 388
cf170f3f
ES
389 /* Avoid release_list until the last reference.
390 */
391 if (atomic_add_unless(&sh->count, -1, 1))
392 return;
393
ad4068de 394 if (unlikely(!conf->mddev->thread) ||
395 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
773ca82f
SL
396 goto slow_path;
397 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
398 if (wakeup)
399 md_wakeup_thread(conf->mddev->thread);
400 return;
401slow_path:
4eb788df 402 local_irq_save(flags);
773ca82f 403 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
4eb788df 404 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
566c09c5
SL
405 INIT_LIST_HEAD(&list);
406 hash = sh->hash_lock_index;
407 do_release_stripe(conf, sh, &list);
4eb788df 408 spin_unlock(&conf->device_lock);
566c09c5 409 release_inactive_stripe_list(conf, &list, hash);
4eb788df
SL
410 }
411 local_irq_restore(flags);
1da177e4
LT
412}
413
fccddba0 414static inline void remove_hash(struct stripe_head *sh)
1da177e4 415{
45b4233c
DW
416 pr_debug("remove_hash(), stripe %llu\n",
417 (unsigned long long)sh->sector);
1da177e4 418
fccddba0 419 hlist_del_init(&sh->hash);
1da177e4
LT
420}
421
d1688a6d 422static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
1da177e4 423{
fccddba0 424 struct hlist_head *hp = stripe_hash(conf, sh->sector);
1da177e4 425
45b4233c
DW
426 pr_debug("insert_hash(), stripe %llu\n",
427 (unsigned long long)sh->sector);
1da177e4 428
fccddba0 429 hlist_add_head(&sh->hash, hp);
1da177e4
LT
430}
431
1da177e4 432/* find an idle stripe, make sure it is unhashed, and return it. */
566c09c5 433static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
1da177e4
LT
434{
435 struct stripe_head *sh = NULL;
436 struct list_head *first;
437
566c09c5 438 if (list_empty(conf->inactive_list + hash))
1da177e4 439 goto out;
566c09c5 440 first = (conf->inactive_list + hash)->next;
1da177e4
LT
441 sh = list_entry(first, struct stripe_head, lru);
442 list_del_init(first);
443 remove_hash(sh);
444 atomic_inc(&conf->active_stripes);
566c09c5 445 BUG_ON(hash != sh->hash_lock_index);
4bda556a
SL
446 if (list_empty(conf->inactive_list + hash))
447 atomic_inc(&conf->empty_inactive_list_nr);
1da177e4
LT
448out:
449 return sh;
450}
451
e4e11e38 452static void shrink_buffers(struct stripe_head *sh)
1da177e4
LT
453{
454 struct page *p;
455 int i;
e4e11e38 456 int num = sh->raid_conf->pool_size;
1da177e4 457
e4e11e38 458 for (i = 0; i < num ; i++) {
d592a996 459 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
1da177e4
LT
460 p = sh->dev[i].page;
461 if (!p)
462 continue;
463 sh->dev[i].page = NULL;
2d1f3b5d 464 put_page(p);
1da177e4
LT
465 }
466}
467
a9683a79 468static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
1da177e4
LT
469{
470 int i;
e4e11e38 471 int num = sh->raid_conf->pool_size;
1da177e4 472
e4e11e38 473 for (i = 0; i < num; i++) {
1da177e4
LT
474 struct page *page;
475
a9683a79 476 if (!(page = alloc_page(gfp))) {
1da177e4
LT
477 return 1;
478 }
479 sh->dev[i].page = page;
d592a996 480 sh->dev[i].orig_page = page;
1da177e4
LT
481 }
482 return 0;
483}
484
784052ec 485static void raid5_build_block(struct stripe_head *sh, int i, int previous);
d1688a6d 486static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
911d4ee8 487 struct stripe_head *sh);
1da177e4 488
b5663ba4 489static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
1da177e4 490{
d1688a6d 491 struct r5conf *conf = sh->raid_conf;
566c09c5 492 int i, seq;
1da177e4 493
78bafebd
ES
494 BUG_ON(atomic_read(&sh->count) != 0);
495 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
600aa109 496 BUG_ON(stripe_operations_active(sh));
59fc630b 497 BUG_ON(sh->batch_head);
d84e0f10 498
45b4233c 499 pr_debug("init_stripe called, stripe %llu\n",
b8e6a15a 500 (unsigned long long)sector);
566c09c5
SL
501retry:
502 seq = read_seqcount_begin(&conf->gen_lock);
86b42c71 503 sh->generation = conf->generation - previous;
b5663ba4 504 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
1da177e4 505 sh->sector = sector;
911d4ee8 506 stripe_set_idx(sector, conf, previous, sh);
1da177e4
LT
507 sh->state = 0;
508
7ecaa1e6 509 for (i = sh->disks; i--; ) {
1da177e4
LT
510 struct r5dev *dev = &sh->dev[i];
511
d84e0f10 512 if (dev->toread || dev->read || dev->towrite || dev->written ||
1da177e4 513 test_bit(R5_LOCKED, &dev->flags)) {
cc6167b4 514 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
1da177e4 515 (unsigned long long)sh->sector, i, dev->toread,
d84e0f10 516 dev->read, dev->towrite, dev->written,
1da177e4 517 test_bit(R5_LOCKED, &dev->flags));
8cfa7b0f 518 WARN_ON(1);
1da177e4
LT
519 }
520 dev->flags = 0;
784052ec 521 raid5_build_block(sh, i, previous);
1da177e4 522 }
566c09c5
SL
523 if (read_seqcount_retry(&conf->gen_lock, seq))
524 goto retry;
7a87f434 525 sh->overwrite_disks = 0;
1da177e4 526 insert_hash(conf, sh);
851c30c9 527 sh->cpu = smp_processor_id();
da41ba65 528 set_bit(STRIPE_BATCH_READY, &sh->state);
1da177e4
LT
529}
530
d1688a6d 531static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
86b42c71 532 short generation)
1da177e4
LT
533{
534 struct stripe_head *sh;
535
45b4233c 536 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
b67bfe0d 537 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
86b42c71 538 if (sh->sector == sector && sh->generation == generation)
1da177e4 539 return sh;
45b4233c 540 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
1da177e4
LT
541 return NULL;
542}
543
674806d6
N
544/*
545 * Need to check if array has failed when deciding whether to:
546 * - start an array
547 * - remove non-faulty devices
548 * - add a spare
549 * - allow a reshape
550 * This determination is simple when no reshape is happening.
551 * However if there is a reshape, we need to carefully check
552 * both the before and after sections.
553 * This is because some failed devices may only affect one
554 * of the two sections, and some non-in_sync devices may
555 * be insync in the section most affected by failed devices.
556 */
2e38a37f 557int raid5_calc_degraded(struct r5conf *conf)
674806d6 558{
908f4fbd 559 int degraded, degraded2;
674806d6 560 int i;
674806d6
N
561
562 rcu_read_lock();
563 degraded = 0;
564 for (i = 0; i < conf->previous_raid_disks; i++) {
3cb03002 565 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
e5c86471
N
566 if (rdev && test_bit(Faulty, &rdev->flags))
567 rdev = rcu_dereference(conf->disks[i].replacement);
674806d6
N
568 if (!rdev || test_bit(Faulty, &rdev->flags))
569 degraded++;
570 else if (test_bit(In_sync, &rdev->flags))
571 ;
572 else
573 /* not in-sync or faulty.
574 * If the reshape increases the number of devices,
575 * this is being recovered by the reshape, so
576 * this 'previous' section is not in_sync.
577 * If the number of devices is being reduced however,
578 * the device can only be part of the array if
579 * we are reverting a reshape, so this section will
580 * be in-sync.
581 */
582 if (conf->raid_disks >= conf->previous_raid_disks)
583 degraded++;
584 }
585 rcu_read_unlock();
908f4fbd
N
586 if (conf->raid_disks == conf->previous_raid_disks)
587 return degraded;
674806d6 588 rcu_read_lock();
908f4fbd 589 degraded2 = 0;
674806d6 590 for (i = 0; i < conf->raid_disks; i++) {
3cb03002 591 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
e5c86471
N
592 if (rdev && test_bit(Faulty, &rdev->flags))
593 rdev = rcu_dereference(conf->disks[i].replacement);
674806d6 594 if (!rdev || test_bit(Faulty, &rdev->flags))
908f4fbd 595 degraded2++;
674806d6
N
596 else if (test_bit(In_sync, &rdev->flags))
597 ;
598 else
599 /* not in-sync or faulty.
600 * If reshape increases the number of devices, this
601 * section has already been recovered, else it
602 * almost certainly hasn't.
603 */
604 if (conf->raid_disks <= conf->previous_raid_disks)
908f4fbd 605 degraded2++;
674806d6
N
606 }
607 rcu_read_unlock();
908f4fbd
N
608 if (degraded2 > degraded)
609 return degraded2;
610 return degraded;
611}
612
613static int has_failed(struct r5conf *conf)
614{
615 int degraded;
616
617 if (conf->mddev->reshape_position == MaxSector)
618 return conf->mddev->degraded > conf->max_degraded;
619
2e38a37f 620 degraded = raid5_calc_degraded(conf);
674806d6
N
621 if (degraded > conf->max_degraded)
622 return 1;
623 return 0;
624}
625
6d036f7d
SL
626struct stripe_head *
627raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
628 int previous, int noblock, int noquiesce)
1da177e4
LT
629{
630 struct stripe_head *sh;
566c09c5 631 int hash = stripe_hash_locks_hash(sector);
ff00d3b4 632 int inc_empty_inactive_list_flag;
1da177e4 633
45b4233c 634 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
1da177e4 635
566c09c5 636 spin_lock_irq(conf->hash_locks + hash);
1da177e4
LT
637
638 do {
b1b46486 639 wait_event_lock_irq(conf->wait_for_quiescent,
a8c906ca 640 conf->quiesce == 0 || noquiesce,
566c09c5 641 *(conf->hash_locks + hash));
86b42c71 642 sh = __find_stripe(conf, sector, conf->generation - previous);
1da177e4 643 if (!sh) {
edbe83ab 644 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
566c09c5 645 sh = get_free_stripe(conf, hash);
713bc5c2
SL
646 if (!sh && !test_bit(R5_DID_ALLOC,
647 &conf->cache_state))
edbe83ab
N
648 set_bit(R5_ALLOC_MORE,
649 &conf->cache_state);
650 }
1da177e4
LT
651 if (noblock && sh == NULL)
652 break;
a39f7afd
SL
653
654 r5c_check_stripe_cache_usage(conf);
1da177e4 655 if (!sh) {
5423399a
N
656 set_bit(R5_INACTIVE_BLOCKED,
657 &conf->cache_state);
a39f7afd 658 r5l_wake_reclaim(conf->log, 0);
6ab2a4b8
SL
659 wait_event_lock_irq(
660 conf->wait_for_stripe,
566c09c5
SL
661 !list_empty(conf->inactive_list + hash) &&
662 (atomic_read(&conf->active_stripes)
663 < (conf->max_nr_stripes * 3 / 4)
5423399a
N
664 || !test_bit(R5_INACTIVE_BLOCKED,
665 &conf->cache_state)),
6ab2a4b8 666 *(conf->hash_locks + hash));
5423399a
N
667 clear_bit(R5_INACTIVE_BLOCKED,
668 &conf->cache_state);
7da9d450 669 } else {
b5663ba4 670 init_stripe(sh, sector, previous);
7da9d450
N
671 atomic_inc(&sh->count);
672 }
e240c183 673 } else if (!atomic_inc_not_zero(&sh->count)) {
6d183de4 674 spin_lock(&conf->device_lock);
e240c183 675 if (!atomic_read(&sh->count)) {
1da177e4
LT
676 if (!test_bit(STRIPE_HANDLE, &sh->state))
677 atomic_inc(&conf->active_stripes);
5af9bef7
N
678 BUG_ON(list_empty(&sh->lru) &&
679 !test_bit(STRIPE_EXPANDING, &sh->state));
ff00d3b4
ZL
680 inc_empty_inactive_list_flag = 0;
681 if (!list_empty(conf->inactive_list + hash))
682 inc_empty_inactive_list_flag = 1;
16a53ecc 683 list_del_init(&sh->lru);
ff00d3b4
ZL
684 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
685 atomic_inc(&conf->empty_inactive_list_nr);
bfc90cb0
SL
686 if (sh->group) {
687 sh->group->stripes_cnt--;
688 sh->group = NULL;
689 }
1da177e4 690 }
7da9d450 691 atomic_inc(&sh->count);
6d183de4 692 spin_unlock(&conf->device_lock);
1da177e4
LT
693 }
694 } while (sh == NULL);
695
566c09c5 696 spin_unlock_irq(conf->hash_locks + hash);
1da177e4
LT
697 return sh;
698}
699
7a87f434 700static bool is_full_stripe_write(struct stripe_head *sh)
701{
702 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
703 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
704}
705
59fc630b 706static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
707{
708 local_irq_disable();
709 if (sh1 > sh2) {
710 spin_lock(&sh2->stripe_lock);
711 spin_lock_nested(&sh1->stripe_lock, 1);
712 } else {
713 spin_lock(&sh1->stripe_lock);
714 spin_lock_nested(&sh2->stripe_lock, 1);
715 }
716}
717
718static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
719{
720 spin_unlock(&sh1->stripe_lock);
721 spin_unlock(&sh2->stripe_lock);
722 local_irq_enable();
723}
724
725/* Only freshly new full stripe normal write stripe can be added to a batch list */
726static bool stripe_can_batch(struct stripe_head *sh)
727{
9c3e333d
SL
728 struct r5conf *conf = sh->raid_conf;
729
730 if (conf->log)
731 return false;
59fc630b 732 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
d0852df5 733 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
59fc630b 734 is_full_stripe_write(sh);
735}
736
737/* we only do back search */
738static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
739{
740 struct stripe_head *head;
741 sector_t head_sector, tmp_sec;
742 int hash;
743 int dd_idx;
ff00d3b4 744 int inc_empty_inactive_list_flag;
59fc630b 745
59fc630b 746 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
747 tmp_sec = sh->sector;
748 if (!sector_div(tmp_sec, conf->chunk_sectors))
749 return;
750 head_sector = sh->sector - STRIPE_SECTORS;
751
752 hash = stripe_hash_locks_hash(head_sector);
753 spin_lock_irq(conf->hash_locks + hash);
754 head = __find_stripe(conf, head_sector, conf->generation);
755 if (head && !atomic_inc_not_zero(&head->count)) {
756 spin_lock(&conf->device_lock);
757 if (!atomic_read(&head->count)) {
758 if (!test_bit(STRIPE_HANDLE, &head->state))
759 atomic_inc(&conf->active_stripes);
760 BUG_ON(list_empty(&head->lru) &&
761 !test_bit(STRIPE_EXPANDING, &head->state));
ff00d3b4
ZL
762 inc_empty_inactive_list_flag = 0;
763 if (!list_empty(conf->inactive_list + hash))
764 inc_empty_inactive_list_flag = 1;
59fc630b 765 list_del_init(&head->lru);
ff00d3b4
ZL
766 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
767 atomic_inc(&conf->empty_inactive_list_nr);
59fc630b 768 if (head->group) {
769 head->group->stripes_cnt--;
770 head->group = NULL;
771 }
772 }
773 atomic_inc(&head->count);
774 spin_unlock(&conf->device_lock);
775 }
776 spin_unlock_irq(conf->hash_locks + hash);
777
778 if (!head)
779 return;
780 if (!stripe_can_batch(head))
781 goto out;
782
783 lock_two_stripes(head, sh);
784 /* clear_batch_ready clear the flag */
785 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
786 goto unlock_out;
787
788 if (sh->batch_head)
789 goto unlock_out;
790
791 dd_idx = 0;
792 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
793 dd_idx++;
1eff9d32 794 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
796a5cf0 795 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
59fc630b 796 goto unlock_out;
797
798 if (head->batch_head) {
799 spin_lock(&head->batch_head->batch_lock);
800 /* This batch list is already running */
801 if (!stripe_can_batch(head)) {
802 spin_unlock(&head->batch_head->batch_lock);
803 goto unlock_out;
804 }
805
806 /*
807 * at this point, head's BATCH_READY could be cleared, but we
808 * can still add the stripe to batch list
809 */
810 list_add(&sh->batch_list, &head->batch_list);
811 spin_unlock(&head->batch_head->batch_lock);
812
813 sh->batch_head = head->batch_head;
814 } else {
815 head->batch_head = head;
816 sh->batch_head = head->batch_head;
817 spin_lock(&head->batch_lock);
818 list_add_tail(&sh->batch_list, &head->batch_list);
819 spin_unlock(&head->batch_lock);
820 }
821
822 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
823 if (atomic_dec_return(&conf->preread_active_stripes)
824 < IO_THRESHOLD)
825 md_wakeup_thread(conf->mddev->thread);
826
2b6b2457
N
827 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
828 int seq = sh->bm_seq;
829 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
830 sh->batch_head->bm_seq > seq)
831 seq = sh->batch_head->bm_seq;
832 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
833 sh->batch_head->bm_seq = seq;
834 }
835
59fc630b 836 atomic_inc(&sh->count);
837unlock_out:
838 unlock_two_stripes(head, sh);
839out:
6d036f7d 840 raid5_release_stripe(head);
59fc630b 841}
842
05616be5
N
843/* Determine if 'data_offset' or 'new_data_offset' should be used
844 * in this stripe_head.
845 */
846static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
847{
848 sector_t progress = conf->reshape_progress;
849 /* Need a memory barrier to make sure we see the value
850 * of conf->generation, or ->data_offset that was set before
851 * reshape_progress was updated.
852 */
853 smp_rmb();
854 if (progress == MaxSector)
855 return 0;
856 if (sh->generation == conf->generation - 1)
857 return 0;
858 /* We are in a reshape, and this is a new-generation stripe,
859 * so use new_data_offset.
860 */
861 return 1;
862}
863
765d704d
SL
864static void flush_deferred_bios(struct r5conf *conf)
865{
866 struct bio_list tmp;
867 struct bio *bio;
868
869 if (!conf->batch_bio_dispatch || !conf->group_cnt)
870 return;
871
872 bio_list_init(&tmp);
873 spin_lock(&conf->pending_bios_lock);
874 bio_list_merge(&tmp, &conf->pending_bios);
875 bio_list_init(&conf->pending_bios);
876 spin_unlock(&conf->pending_bios_lock);
877
878 while ((bio = bio_list_pop(&tmp)))
879 generic_make_request(bio);
880}
881
882static void defer_bio_issue(struct r5conf *conf, struct bio *bio)
883{
884 /*
885 * change group_cnt will drain all bios, so this is safe
886 *
887 * A read generally means a read-modify-write, which usually means a
888 * randwrite, so we don't delay it
889 */
890 if (!conf->batch_bio_dispatch || !conf->group_cnt ||
891 bio_op(bio) == REQ_OP_READ) {
892 generic_make_request(bio);
893 return;
894 }
895 spin_lock(&conf->pending_bios_lock);
896 bio_list_add(&conf->pending_bios, bio);
897 spin_unlock(&conf->pending_bios_lock);
898 md_wakeup_thread(conf->mddev->thread);
899}
900
6712ecf8 901static void
4246a0b6 902raid5_end_read_request(struct bio *bi);
6712ecf8 903static void
4246a0b6 904raid5_end_write_request(struct bio *bi);
91c00924 905
c4e5ac0a 906static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
91c00924 907{
d1688a6d 908 struct r5conf *conf = sh->raid_conf;
91c00924 909 int i, disks = sh->disks;
59fc630b 910 struct stripe_head *head_sh = sh;
91c00924
DW
911
912 might_sleep();
913
1e6d690b
SL
914 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
915 /* writing out phase */
d7bd398e
SL
916 if (s->waiting_extra_page)
917 return;
1e6d690b
SL
918 if (r5l_write_stripe(conf->log, sh) == 0)
919 return;
920 } else { /* caching phase */
921 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) {
922 r5c_cache_data(conf->log, sh, s);
923 return;
924 }
925 }
926
91c00924 927 for (i = disks; i--; ) {
796a5cf0 928 int op, op_flags = 0;
9a3e1101 929 int replace_only = 0;
977df362
N
930 struct bio *bi, *rbi;
931 struct md_rdev *rdev, *rrdev = NULL;
59fc630b 932
933 sh = head_sh;
e9c7469b 934 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
796a5cf0 935 op = REQ_OP_WRITE;
e9c7469b 936 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
70fd7614 937 op_flags = REQ_FUA;
9e444768 938 if (test_bit(R5_Discard, &sh->dev[i].flags))
796a5cf0 939 op = REQ_OP_DISCARD;
e9c7469b 940 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
796a5cf0 941 op = REQ_OP_READ;
9a3e1101
N
942 else if (test_and_clear_bit(R5_WantReplace,
943 &sh->dev[i].flags)) {
796a5cf0 944 op = REQ_OP_WRITE;
9a3e1101
N
945 replace_only = 1;
946 } else
91c00924 947 continue;
bc0934f0 948 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
796a5cf0 949 op_flags |= REQ_SYNC;
91c00924 950
59fc630b 951again:
91c00924 952 bi = &sh->dev[i].req;
977df362 953 rbi = &sh->dev[i].rreq; /* For writing to replacement */
91c00924 954
91c00924 955 rcu_read_lock();
9a3e1101 956 rrdev = rcu_dereference(conf->disks[i].replacement);
dd054fce
N
957 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
958 rdev = rcu_dereference(conf->disks[i].rdev);
959 if (!rdev) {
960 rdev = rrdev;
961 rrdev = NULL;
962 }
796a5cf0 963 if (op_is_write(op)) {
9a3e1101
N
964 if (replace_only)
965 rdev = NULL;
dd054fce
N
966 if (rdev == rrdev)
967 /* We raced and saw duplicates */
968 rrdev = NULL;
9a3e1101 969 } else {
59fc630b 970 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
9a3e1101
N
971 rdev = rrdev;
972 rrdev = NULL;
973 }
977df362 974
91c00924
DW
975 if (rdev && test_bit(Faulty, &rdev->flags))
976 rdev = NULL;
977 if (rdev)
978 atomic_inc(&rdev->nr_pending);
977df362
N
979 if (rrdev && test_bit(Faulty, &rrdev->flags))
980 rrdev = NULL;
981 if (rrdev)
982 atomic_inc(&rrdev->nr_pending);
91c00924
DW
983 rcu_read_unlock();
984
73e92e51 985 /* We have already checked bad blocks for reads. Now
977df362
N
986 * need to check for writes. We never accept write errors
987 * on the replacement, so we don't to check rrdev.
73e92e51 988 */
796a5cf0 989 while (op_is_write(op) && rdev &&
73e92e51
N
990 test_bit(WriteErrorSeen, &rdev->flags)) {
991 sector_t first_bad;
992 int bad_sectors;
993 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
994 &first_bad, &bad_sectors);
995 if (!bad)
996 break;
997
998 if (bad < 0) {
999 set_bit(BlockedBadBlocks, &rdev->flags);
1000 if (!conf->mddev->external &&
2953079c 1001 conf->mddev->sb_flags) {
73e92e51
N
1002 /* It is very unlikely, but we might
1003 * still need to write out the
1004 * bad block log - better give it
1005 * a chance*/
1006 md_check_recovery(conf->mddev);
1007 }
1850753d 1008 /*
1009 * Because md_wait_for_blocked_rdev
1010 * will dec nr_pending, we must
1011 * increment it first.
1012 */
1013 atomic_inc(&rdev->nr_pending);
73e92e51
N
1014 md_wait_for_blocked_rdev(rdev, conf->mddev);
1015 } else {
1016 /* Acknowledged bad block - skip the write */
1017 rdev_dec_pending(rdev, conf->mddev);
1018 rdev = NULL;
1019 }
1020 }
1021
91c00924 1022 if (rdev) {
9a3e1101
N
1023 if (s->syncing || s->expanding || s->expanded
1024 || s->replacing)
91c00924
DW
1025 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1026
2b7497f0
DW
1027 set_bit(STRIPE_IO_STARTED, &sh->state);
1028
91c00924 1029 bi->bi_bdev = rdev->bdev;
796a5cf0
MC
1030 bio_set_op_attrs(bi, op, op_flags);
1031 bi->bi_end_io = op_is_write(op)
2f6db2a7
KO
1032 ? raid5_end_write_request
1033 : raid5_end_read_request;
1034 bi->bi_private = sh;
1035
6296b960 1036 pr_debug("%s: for %llu schedule op %d on disc %d\n",
e46b272b 1037 __func__, (unsigned long long)sh->sector,
1eff9d32 1038 bi->bi_opf, i);
91c00924 1039 atomic_inc(&sh->count);
59fc630b 1040 if (sh != head_sh)
1041 atomic_inc(&head_sh->count);
05616be5 1042 if (use_new_offset(conf, sh))
4f024f37 1043 bi->bi_iter.bi_sector = (sh->sector
05616be5
N
1044 + rdev->new_data_offset);
1045 else
4f024f37 1046 bi->bi_iter.bi_sector = (sh->sector
05616be5 1047 + rdev->data_offset);
59fc630b 1048 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
1eff9d32 1049 bi->bi_opf |= REQ_NOMERGE;
3f9e7c14 1050
d592a996
SL
1051 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1052 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
86aa1397
SL
1053
1054 if (!op_is_write(op) &&
1055 test_bit(R5_InJournal, &sh->dev[i].flags))
1056 /*
1057 * issuing read for a page in journal, this
1058 * must be preparing for prexor in rmw; read
1059 * the data into orig_page
1060 */
1061 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1062 else
1063 sh->dev[i].vec.bv_page = sh->dev[i].page;
4997b72e 1064 bi->bi_vcnt = 1;
91c00924
DW
1065 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1066 bi->bi_io_vec[0].bv_offset = 0;
4f024f37 1067 bi->bi_iter.bi_size = STRIPE_SIZE;
37c61ff3
SL
1068 /*
1069 * If this is discard request, set bi_vcnt 0. We don't
1070 * want to confuse SCSI because SCSI will replace payload
1071 */
796a5cf0 1072 if (op == REQ_OP_DISCARD)
37c61ff3 1073 bi->bi_vcnt = 0;
977df362
N
1074 if (rrdev)
1075 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
e3620a3a
JB
1076
1077 if (conf->mddev->gendisk)
1078 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1079 bi, disk_devt(conf->mddev->gendisk),
1080 sh->dev[i].sector);
765d704d 1081 defer_bio_issue(conf, bi);
977df362
N
1082 }
1083 if (rrdev) {
9a3e1101
N
1084 if (s->syncing || s->expanding || s->expanded
1085 || s->replacing)
977df362
N
1086 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1087
1088 set_bit(STRIPE_IO_STARTED, &sh->state);
1089
1090 rbi->bi_bdev = rrdev->bdev;
796a5cf0
MC
1091 bio_set_op_attrs(rbi, op, op_flags);
1092 BUG_ON(!op_is_write(op));
2f6db2a7
KO
1093 rbi->bi_end_io = raid5_end_write_request;
1094 rbi->bi_private = sh;
1095
6296b960 1096 pr_debug("%s: for %llu schedule op %d on "
977df362
N
1097 "replacement disc %d\n",
1098 __func__, (unsigned long long)sh->sector,
1eff9d32 1099 rbi->bi_opf, i);
977df362 1100 atomic_inc(&sh->count);
59fc630b 1101 if (sh != head_sh)
1102 atomic_inc(&head_sh->count);
05616be5 1103 if (use_new_offset(conf, sh))
4f024f37 1104 rbi->bi_iter.bi_sector = (sh->sector
05616be5
N
1105 + rrdev->new_data_offset);
1106 else
4f024f37 1107 rbi->bi_iter.bi_sector = (sh->sector
05616be5 1108 + rrdev->data_offset);
d592a996
SL
1109 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1110 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1111 sh->dev[i].rvec.bv_page = sh->dev[i].page;
4997b72e 1112 rbi->bi_vcnt = 1;
977df362
N
1113 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1114 rbi->bi_io_vec[0].bv_offset = 0;
4f024f37 1115 rbi->bi_iter.bi_size = STRIPE_SIZE;
37c61ff3
SL
1116 /*
1117 * If this is discard request, set bi_vcnt 0. We don't
1118 * want to confuse SCSI because SCSI will replace payload
1119 */
796a5cf0 1120 if (op == REQ_OP_DISCARD)
37c61ff3 1121 rbi->bi_vcnt = 0;
e3620a3a
JB
1122 if (conf->mddev->gendisk)
1123 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1124 rbi, disk_devt(conf->mddev->gendisk),
1125 sh->dev[i].sector);
765d704d 1126 defer_bio_issue(conf, rbi);
977df362
N
1127 }
1128 if (!rdev && !rrdev) {
796a5cf0 1129 if (op_is_write(op))
91c00924 1130 set_bit(STRIPE_DEGRADED, &sh->state);
6296b960 1131 pr_debug("skip op %d on disc %d for sector %llu\n",
1eff9d32 1132 bi->bi_opf, i, (unsigned long long)sh->sector);
91c00924
DW
1133 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1134 set_bit(STRIPE_HANDLE, &sh->state);
1135 }
59fc630b 1136
1137 if (!head_sh->batch_head)
1138 continue;
1139 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1140 batch_list);
1141 if (sh != head_sh)
1142 goto again;
91c00924
DW
1143 }
1144}
1145
1146static struct dma_async_tx_descriptor *
d592a996
SL
1147async_copy_data(int frombio, struct bio *bio, struct page **page,
1148 sector_t sector, struct dma_async_tx_descriptor *tx,
1e6d690b 1149 struct stripe_head *sh, int no_skipcopy)
91c00924 1150{
7988613b
KO
1151 struct bio_vec bvl;
1152 struct bvec_iter iter;
91c00924 1153 struct page *bio_page;
91c00924 1154 int page_offset;
a08abd8c 1155 struct async_submit_ctl submit;
0403e382 1156 enum async_tx_flags flags = 0;
91c00924 1157
4f024f37
KO
1158 if (bio->bi_iter.bi_sector >= sector)
1159 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
91c00924 1160 else
4f024f37 1161 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
a08abd8c 1162
0403e382
DW
1163 if (frombio)
1164 flags |= ASYNC_TX_FENCE;
1165 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1166
7988613b
KO
1167 bio_for_each_segment(bvl, bio, iter) {
1168 int len = bvl.bv_len;
91c00924
DW
1169 int clen;
1170 int b_offset = 0;
1171
1172 if (page_offset < 0) {
1173 b_offset = -page_offset;
1174 page_offset += b_offset;
1175 len -= b_offset;
1176 }
1177
1178 if (len > 0 && page_offset + len > STRIPE_SIZE)
1179 clen = STRIPE_SIZE - page_offset;
1180 else
1181 clen = len;
1182
1183 if (clen > 0) {
7988613b
KO
1184 b_offset += bvl.bv_offset;
1185 bio_page = bvl.bv_page;
d592a996
SL
1186 if (frombio) {
1187 if (sh->raid_conf->skip_copy &&
1188 b_offset == 0 && page_offset == 0 &&
1e6d690b
SL
1189 clen == STRIPE_SIZE &&
1190 !no_skipcopy)
d592a996
SL
1191 *page = bio_page;
1192 else
1193 tx = async_memcpy(*page, bio_page, page_offset,
a08abd8c 1194 b_offset, clen, &submit);
d592a996
SL
1195 } else
1196 tx = async_memcpy(bio_page, *page, b_offset,
a08abd8c 1197 page_offset, clen, &submit);
91c00924 1198 }
a08abd8c
DW
1199 /* chain the operations */
1200 submit.depend_tx = tx;
1201
91c00924
DW
1202 if (clen < len) /* hit end of page */
1203 break;
1204 page_offset += len;
1205 }
1206
1207 return tx;
1208}
1209
1210static void ops_complete_biofill(void *stripe_head_ref)
1211{
1212 struct stripe_head *sh = stripe_head_ref;
34a6f80e 1213 struct bio_list return_bi = BIO_EMPTY_LIST;
e4d84909 1214 int i;
91c00924 1215
e46b272b 1216 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1217 (unsigned long long)sh->sector);
1218
1219 /* clear completed biofills */
1220 for (i = sh->disks; i--; ) {
1221 struct r5dev *dev = &sh->dev[i];
91c00924
DW
1222
1223 /* acknowledge completion of a biofill operation */
e4d84909
DW
1224 /* and check if we need to reply to a read request,
1225 * new R5_Wantfill requests are held off until
83de75cc 1226 * !STRIPE_BIOFILL_RUN
e4d84909
DW
1227 */
1228 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
91c00924 1229 struct bio *rbi, *rbi2;
91c00924 1230
91c00924
DW
1231 BUG_ON(!dev->read);
1232 rbi = dev->read;
1233 dev->read = NULL;
4f024f37 1234 while (rbi && rbi->bi_iter.bi_sector <
91c00924
DW
1235 dev->sector + STRIPE_SECTORS) {
1236 rbi2 = r5_next_bio(rbi, dev->sector);
34a6f80e
N
1237 if (!raid5_dec_bi_active_stripes(rbi))
1238 bio_list_add(&return_bi, rbi);
91c00924
DW
1239 rbi = rbi2;
1240 }
1241 }
1242 }
83de75cc 1243 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
91c00924 1244
34a6f80e 1245 return_io(&return_bi);
91c00924 1246
e4d84909 1247 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 1248 raid5_release_stripe(sh);
91c00924
DW
1249}
1250
1251static void ops_run_biofill(struct stripe_head *sh)
1252{
1253 struct dma_async_tx_descriptor *tx = NULL;
a08abd8c 1254 struct async_submit_ctl submit;
91c00924
DW
1255 int i;
1256
59fc630b 1257 BUG_ON(sh->batch_head);
e46b272b 1258 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1259 (unsigned long long)sh->sector);
1260
1261 for (i = sh->disks; i--; ) {
1262 struct r5dev *dev = &sh->dev[i];
1263 if (test_bit(R5_Wantfill, &dev->flags)) {
1264 struct bio *rbi;
b17459c0 1265 spin_lock_irq(&sh->stripe_lock);
91c00924
DW
1266 dev->read = rbi = dev->toread;
1267 dev->toread = NULL;
b17459c0 1268 spin_unlock_irq(&sh->stripe_lock);
4f024f37 1269 while (rbi && rbi->bi_iter.bi_sector <
91c00924 1270 dev->sector + STRIPE_SECTORS) {
d592a996 1271 tx = async_copy_data(0, rbi, &dev->page,
1e6d690b 1272 dev->sector, tx, sh, 0);
91c00924
DW
1273 rbi = r5_next_bio(rbi, dev->sector);
1274 }
1275 }
1276 }
1277
1278 atomic_inc(&sh->count);
a08abd8c
DW
1279 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1280 async_trigger_callback(&submit);
91c00924
DW
1281}
1282
4e7d2c0a 1283static void mark_target_uptodate(struct stripe_head *sh, int target)
91c00924 1284{
4e7d2c0a 1285 struct r5dev *tgt;
91c00924 1286
4e7d2c0a
DW
1287 if (target < 0)
1288 return;
91c00924 1289
4e7d2c0a 1290 tgt = &sh->dev[target];
91c00924
DW
1291 set_bit(R5_UPTODATE, &tgt->flags);
1292 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1293 clear_bit(R5_Wantcompute, &tgt->flags);
4e7d2c0a
DW
1294}
1295
ac6b53b6 1296static void ops_complete_compute(void *stripe_head_ref)
91c00924
DW
1297{
1298 struct stripe_head *sh = stripe_head_ref;
91c00924 1299
e46b272b 1300 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1301 (unsigned long long)sh->sector);
1302
ac6b53b6 1303 /* mark the computed target(s) as uptodate */
4e7d2c0a 1304 mark_target_uptodate(sh, sh->ops.target);
ac6b53b6 1305 mark_target_uptodate(sh, sh->ops.target2);
4e7d2c0a 1306
ecc65c9b
DW
1307 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1308 if (sh->check_state == check_state_compute_run)
1309 sh->check_state = check_state_compute_result;
91c00924 1310 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 1311 raid5_release_stripe(sh);
91c00924
DW
1312}
1313
d6f38f31
DW
1314/* return a pointer to the address conversion region of the scribble buffer */
1315static addr_conv_t *to_addr_conv(struct stripe_head *sh,
46d5b785 1316 struct raid5_percpu *percpu, int i)
d6f38f31 1317{
46d5b785 1318 void *addr;
1319
1320 addr = flex_array_get(percpu->scribble, i);
1321 return addr + sizeof(struct page *) * (sh->disks + 2);
1322}
1323
1324/* return a pointer to the address conversion region of the scribble buffer */
1325static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1326{
1327 void *addr;
1328
1329 addr = flex_array_get(percpu->scribble, i);
1330 return addr;
d6f38f31
DW
1331}
1332
1333static struct dma_async_tx_descriptor *
1334ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
91c00924 1335{
91c00924 1336 int disks = sh->disks;
46d5b785 1337 struct page **xor_srcs = to_addr_page(percpu, 0);
91c00924
DW
1338 int target = sh->ops.target;
1339 struct r5dev *tgt = &sh->dev[target];
1340 struct page *xor_dest = tgt->page;
1341 int count = 0;
1342 struct dma_async_tx_descriptor *tx;
a08abd8c 1343 struct async_submit_ctl submit;
91c00924
DW
1344 int i;
1345
59fc630b 1346 BUG_ON(sh->batch_head);
1347
91c00924 1348 pr_debug("%s: stripe %llu block: %d\n",
e46b272b 1349 __func__, (unsigned long long)sh->sector, target);
91c00924
DW
1350 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1351
1352 for (i = disks; i--; )
1353 if (i != target)
1354 xor_srcs[count++] = sh->dev[i].page;
1355
1356 atomic_inc(&sh->count);
1357
0403e382 1358 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
46d5b785 1359 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
91c00924 1360 if (unlikely(count == 1))
a08abd8c 1361 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
91c00924 1362 else
a08abd8c 1363 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
91c00924 1364
91c00924
DW
1365 return tx;
1366}
1367
ac6b53b6
DW
1368/* set_syndrome_sources - populate source buffers for gen_syndrome
1369 * @srcs - (struct page *) array of size sh->disks
1370 * @sh - stripe_head to parse
1371 *
1372 * Populates srcs in proper layout order for the stripe and returns the
1373 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1374 * destination buffer is recorded in srcs[count] and the Q destination
1375 * is recorded in srcs[count+1]].
1376 */
584acdd4
MS
1377static int set_syndrome_sources(struct page **srcs,
1378 struct stripe_head *sh,
1379 int srctype)
ac6b53b6
DW
1380{
1381 int disks = sh->disks;
1382 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1383 int d0_idx = raid6_d0(sh);
1384 int count;
1385 int i;
1386
1387 for (i = 0; i < disks; i++)
5dd33c9a 1388 srcs[i] = NULL;
ac6b53b6
DW
1389
1390 count = 0;
1391 i = d0_idx;
1392 do {
1393 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
584acdd4 1394 struct r5dev *dev = &sh->dev[i];
ac6b53b6 1395
584acdd4
MS
1396 if (i == sh->qd_idx || i == sh->pd_idx ||
1397 (srctype == SYNDROME_SRC_ALL) ||
1398 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1e6d690b
SL
1399 (test_bit(R5_Wantdrain, &dev->flags) ||
1400 test_bit(R5_InJournal, &dev->flags))) ||
584acdd4 1401 (srctype == SYNDROME_SRC_WRITTEN &&
1e6d690b
SL
1402 dev->written)) {
1403 if (test_bit(R5_InJournal, &dev->flags))
1404 srcs[slot] = sh->dev[i].orig_page;
1405 else
1406 srcs[slot] = sh->dev[i].page;
1407 }
ac6b53b6
DW
1408 i = raid6_next_disk(i, disks);
1409 } while (i != d0_idx);
ac6b53b6 1410
e4424fee 1411 return syndrome_disks;
ac6b53b6
DW
1412}
1413
1414static struct dma_async_tx_descriptor *
1415ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1416{
1417 int disks = sh->disks;
46d5b785 1418 struct page **blocks = to_addr_page(percpu, 0);
ac6b53b6
DW
1419 int target;
1420 int qd_idx = sh->qd_idx;
1421 struct dma_async_tx_descriptor *tx;
1422 struct async_submit_ctl submit;
1423 struct r5dev *tgt;
1424 struct page *dest;
1425 int i;
1426 int count;
1427
59fc630b 1428 BUG_ON(sh->batch_head);
ac6b53b6
DW
1429 if (sh->ops.target < 0)
1430 target = sh->ops.target2;
1431 else if (sh->ops.target2 < 0)
1432 target = sh->ops.target;
91c00924 1433 else
ac6b53b6
DW
1434 /* we should only have one valid target */
1435 BUG();
1436 BUG_ON(target < 0);
1437 pr_debug("%s: stripe %llu block: %d\n",
1438 __func__, (unsigned long long)sh->sector, target);
1439
1440 tgt = &sh->dev[target];
1441 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1442 dest = tgt->page;
1443
1444 atomic_inc(&sh->count);
1445
1446 if (target == qd_idx) {
584acdd4 1447 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
ac6b53b6
DW
1448 blocks[count] = NULL; /* regenerating p is not necessary */
1449 BUG_ON(blocks[count+1] != dest); /* q should already be set */
0403e382
DW
1450 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1451 ops_complete_compute, sh,
46d5b785 1452 to_addr_conv(sh, percpu, 0));
ac6b53b6
DW
1453 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1454 } else {
1455 /* Compute any data- or p-drive using XOR */
1456 count = 0;
1457 for (i = disks; i-- ; ) {
1458 if (i == target || i == qd_idx)
1459 continue;
1460 blocks[count++] = sh->dev[i].page;
1461 }
1462
0403e382
DW
1463 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1464 NULL, ops_complete_compute, sh,
46d5b785 1465 to_addr_conv(sh, percpu, 0));
ac6b53b6
DW
1466 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1467 }
91c00924 1468
91c00924
DW
1469 return tx;
1470}
1471
ac6b53b6
DW
1472static struct dma_async_tx_descriptor *
1473ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1474{
1475 int i, count, disks = sh->disks;
1476 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1477 int d0_idx = raid6_d0(sh);
1478 int faila = -1, failb = -1;
1479 int target = sh->ops.target;
1480 int target2 = sh->ops.target2;
1481 struct r5dev *tgt = &sh->dev[target];
1482 struct r5dev *tgt2 = &sh->dev[target2];
1483 struct dma_async_tx_descriptor *tx;
46d5b785 1484 struct page **blocks = to_addr_page(percpu, 0);
ac6b53b6
DW
1485 struct async_submit_ctl submit;
1486
59fc630b 1487 BUG_ON(sh->batch_head);
ac6b53b6
DW
1488 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1489 __func__, (unsigned long long)sh->sector, target, target2);
1490 BUG_ON(target < 0 || target2 < 0);
1491 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1492 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1493
6c910a78 1494 /* we need to open-code set_syndrome_sources to handle the
ac6b53b6
DW
1495 * slot number conversion for 'faila' and 'failb'
1496 */
1497 for (i = 0; i < disks ; i++)
5dd33c9a 1498 blocks[i] = NULL;
ac6b53b6
DW
1499 count = 0;
1500 i = d0_idx;
1501 do {
1502 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1503
1504 blocks[slot] = sh->dev[i].page;
1505
1506 if (i == target)
1507 faila = slot;
1508 if (i == target2)
1509 failb = slot;
1510 i = raid6_next_disk(i, disks);
1511 } while (i != d0_idx);
ac6b53b6
DW
1512
1513 BUG_ON(faila == failb);
1514 if (failb < faila)
1515 swap(faila, failb);
1516 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1517 __func__, (unsigned long long)sh->sector, faila, failb);
1518
1519 atomic_inc(&sh->count);
1520
1521 if (failb == syndrome_disks+1) {
1522 /* Q disk is one of the missing disks */
1523 if (faila == syndrome_disks) {
1524 /* Missing P+Q, just recompute */
0403e382
DW
1525 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1526 ops_complete_compute, sh,
46d5b785 1527 to_addr_conv(sh, percpu, 0));
e4424fee 1528 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
ac6b53b6
DW
1529 STRIPE_SIZE, &submit);
1530 } else {
1531 struct page *dest;
1532 int data_target;
1533 int qd_idx = sh->qd_idx;
1534
1535 /* Missing D+Q: recompute D from P, then recompute Q */
1536 if (target == qd_idx)
1537 data_target = target2;
1538 else
1539 data_target = target;
1540
1541 count = 0;
1542 for (i = disks; i-- ; ) {
1543 if (i == data_target || i == qd_idx)
1544 continue;
1545 blocks[count++] = sh->dev[i].page;
1546 }
1547 dest = sh->dev[data_target].page;
0403e382
DW
1548 init_async_submit(&submit,
1549 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1550 NULL, NULL, NULL,
46d5b785 1551 to_addr_conv(sh, percpu, 0));
ac6b53b6
DW
1552 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1553 &submit);
1554
584acdd4 1555 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
0403e382
DW
1556 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1557 ops_complete_compute, sh,
46d5b785 1558 to_addr_conv(sh, percpu, 0));
ac6b53b6
DW
1559 return async_gen_syndrome(blocks, 0, count+2,
1560 STRIPE_SIZE, &submit);
1561 }
ac6b53b6 1562 } else {
6c910a78
DW
1563 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1564 ops_complete_compute, sh,
46d5b785 1565 to_addr_conv(sh, percpu, 0));
6c910a78
DW
1566 if (failb == syndrome_disks) {
1567 /* We're missing D+P. */
1568 return async_raid6_datap_recov(syndrome_disks+2,
1569 STRIPE_SIZE, faila,
1570 blocks, &submit);
1571 } else {
1572 /* We're missing D+D. */
1573 return async_raid6_2data_recov(syndrome_disks+2,
1574 STRIPE_SIZE, faila, failb,
1575 blocks, &submit);
1576 }
ac6b53b6
DW
1577 }
1578}
1579
91c00924
DW
1580static void ops_complete_prexor(void *stripe_head_ref)
1581{
1582 struct stripe_head *sh = stripe_head_ref;
1583
e46b272b 1584 pr_debug("%s: stripe %llu\n", __func__,
91c00924 1585 (unsigned long long)sh->sector);
1e6d690b
SL
1586
1587 if (r5c_is_writeback(sh->raid_conf->log))
1588 /*
1589 * raid5-cache write back uses orig_page during prexor.
1590 * After prexor, it is time to free orig_page
1591 */
1592 r5c_release_extra_page(sh);
91c00924
DW
1593}
1594
1595static struct dma_async_tx_descriptor *
584acdd4
MS
1596ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1597 struct dma_async_tx_descriptor *tx)
91c00924 1598{
91c00924 1599 int disks = sh->disks;
46d5b785 1600 struct page **xor_srcs = to_addr_page(percpu, 0);
91c00924 1601 int count = 0, pd_idx = sh->pd_idx, i;
a08abd8c 1602 struct async_submit_ctl submit;
91c00924
DW
1603
1604 /* existing parity data subtracted */
1605 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1606
59fc630b 1607 BUG_ON(sh->batch_head);
e46b272b 1608 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1609 (unsigned long long)sh->sector);
1610
1611 for (i = disks; i--; ) {
1612 struct r5dev *dev = &sh->dev[i];
1613 /* Only process blocks that are known to be uptodate */
1e6d690b
SL
1614 if (test_bit(R5_InJournal, &dev->flags))
1615 xor_srcs[count++] = dev->orig_page;
1616 else if (test_bit(R5_Wantdrain, &dev->flags))
91c00924
DW
1617 xor_srcs[count++] = dev->page;
1618 }
1619
0403e382 1620 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
46d5b785 1621 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
a08abd8c 1622 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
91c00924
DW
1623
1624 return tx;
1625}
1626
584acdd4
MS
1627static struct dma_async_tx_descriptor *
1628ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1629 struct dma_async_tx_descriptor *tx)
1630{
1631 struct page **blocks = to_addr_page(percpu, 0);
1632 int count;
1633 struct async_submit_ctl submit;
1634
1635 pr_debug("%s: stripe %llu\n", __func__,
1636 (unsigned long long)sh->sector);
1637
1638 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1639
1640 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1641 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1642 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1643
1644 return tx;
1645}
1646
91c00924 1647static struct dma_async_tx_descriptor *
d8ee0728 1648ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
91c00924 1649{
1e6d690b 1650 struct r5conf *conf = sh->raid_conf;
91c00924 1651 int disks = sh->disks;
d8ee0728 1652 int i;
59fc630b 1653 struct stripe_head *head_sh = sh;
91c00924 1654
e46b272b 1655 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1656 (unsigned long long)sh->sector);
1657
1658 for (i = disks; i--; ) {
59fc630b 1659 struct r5dev *dev;
91c00924 1660 struct bio *chosen;
91c00924 1661
59fc630b 1662 sh = head_sh;
1663 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
91c00924
DW
1664 struct bio *wbi;
1665
59fc630b 1666again:
1667 dev = &sh->dev[i];
1e6d690b
SL
1668 /*
1669 * clear R5_InJournal, so when rewriting a page in
1670 * journal, it is not skipped by r5l_log_stripe()
1671 */
1672 clear_bit(R5_InJournal, &dev->flags);
b17459c0 1673 spin_lock_irq(&sh->stripe_lock);
91c00924
DW
1674 chosen = dev->towrite;
1675 dev->towrite = NULL;
7a87f434 1676 sh->overwrite_disks = 0;
91c00924
DW
1677 BUG_ON(dev->written);
1678 wbi = dev->written = chosen;
b17459c0 1679 spin_unlock_irq(&sh->stripe_lock);
d592a996 1680 WARN_ON(dev->page != dev->orig_page);
91c00924 1681
4f024f37 1682 while (wbi && wbi->bi_iter.bi_sector <
91c00924 1683 dev->sector + STRIPE_SECTORS) {
1eff9d32 1684 if (wbi->bi_opf & REQ_FUA)
e9c7469b 1685 set_bit(R5_WantFUA, &dev->flags);
1eff9d32 1686 if (wbi->bi_opf & REQ_SYNC)
bc0934f0 1687 set_bit(R5_SyncIO, &dev->flags);
796a5cf0 1688 if (bio_op(wbi) == REQ_OP_DISCARD)
620125f2 1689 set_bit(R5_Discard, &dev->flags);
d592a996
SL
1690 else {
1691 tx = async_copy_data(1, wbi, &dev->page,
1e6d690b
SL
1692 dev->sector, tx, sh,
1693 r5c_is_writeback(conf->log));
1694 if (dev->page != dev->orig_page &&
1695 !r5c_is_writeback(conf->log)) {
d592a996
SL
1696 set_bit(R5_SkipCopy, &dev->flags);
1697 clear_bit(R5_UPTODATE, &dev->flags);
1698 clear_bit(R5_OVERWRITE, &dev->flags);
1699 }
1700 }
91c00924
DW
1701 wbi = r5_next_bio(wbi, dev->sector);
1702 }
59fc630b 1703
1704 if (head_sh->batch_head) {
1705 sh = list_first_entry(&sh->batch_list,
1706 struct stripe_head,
1707 batch_list);
1708 if (sh == head_sh)
1709 continue;
1710 goto again;
1711 }
91c00924
DW
1712 }
1713 }
1714
1715 return tx;
1716}
1717
ac6b53b6 1718static void ops_complete_reconstruct(void *stripe_head_ref)
91c00924
DW
1719{
1720 struct stripe_head *sh = stripe_head_ref;
ac6b53b6
DW
1721 int disks = sh->disks;
1722 int pd_idx = sh->pd_idx;
1723 int qd_idx = sh->qd_idx;
1724 int i;
9e444768 1725 bool fua = false, sync = false, discard = false;
91c00924 1726
e46b272b 1727 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1728 (unsigned long long)sh->sector);
1729
bc0934f0 1730 for (i = disks; i--; ) {
e9c7469b 1731 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
bc0934f0 1732 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
9e444768 1733 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
bc0934f0 1734 }
e9c7469b 1735
91c00924
DW
1736 for (i = disks; i--; ) {
1737 struct r5dev *dev = &sh->dev[i];
ac6b53b6 1738
e9c7469b 1739 if (dev->written || i == pd_idx || i == qd_idx) {
d592a996 1740 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
9e444768 1741 set_bit(R5_UPTODATE, &dev->flags);
e9c7469b
TH
1742 if (fua)
1743 set_bit(R5_WantFUA, &dev->flags);
bc0934f0
SL
1744 if (sync)
1745 set_bit(R5_SyncIO, &dev->flags);
e9c7469b 1746 }
91c00924
DW
1747 }
1748
d8ee0728
DW
1749 if (sh->reconstruct_state == reconstruct_state_drain_run)
1750 sh->reconstruct_state = reconstruct_state_drain_result;
1751 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1752 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1753 else {
1754 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1755 sh->reconstruct_state = reconstruct_state_result;
1756 }
91c00924
DW
1757
1758 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 1759 raid5_release_stripe(sh);
91c00924
DW
1760}
1761
1762static void
ac6b53b6
DW
1763ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1764 struct dma_async_tx_descriptor *tx)
91c00924 1765{
91c00924 1766 int disks = sh->disks;
59fc630b 1767 struct page **xor_srcs;
a08abd8c 1768 struct async_submit_ctl submit;
59fc630b 1769 int count, pd_idx = sh->pd_idx, i;
91c00924 1770 struct page *xor_dest;
d8ee0728 1771 int prexor = 0;
91c00924 1772 unsigned long flags;
59fc630b 1773 int j = 0;
1774 struct stripe_head *head_sh = sh;
1775 int last_stripe;
91c00924 1776
e46b272b 1777 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1778 (unsigned long long)sh->sector);
1779
620125f2
SL
1780 for (i = 0; i < sh->disks; i++) {
1781 if (pd_idx == i)
1782 continue;
1783 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1784 break;
1785 }
1786 if (i >= sh->disks) {
1787 atomic_inc(&sh->count);
620125f2
SL
1788 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1789 ops_complete_reconstruct(sh);
1790 return;
1791 }
59fc630b 1792again:
1793 count = 0;
1794 xor_srcs = to_addr_page(percpu, j);
91c00924
DW
1795 /* check if prexor is active which means only process blocks
1796 * that are part of a read-modify-write (written)
1797 */
59fc630b 1798 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
d8ee0728 1799 prexor = 1;
91c00924
DW
1800 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1801 for (i = disks; i--; ) {
1802 struct r5dev *dev = &sh->dev[i];
1e6d690b
SL
1803 if (head_sh->dev[i].written ||
1804 test_bit(R5_InJournal, &head_sh->dev[i].flags))
91c00924
DW
1805 xor_srcs[count++] = dev->page;
1806 }
1807 } else {
1808 xor_dest = sh->dev[pd_idx].page;
1809 for (i = disks; i--; ) {
1810 struct r5dev *dev = &sh->dev[i];
1811 if (i != pd_idx)
1812 xor_srcs[count++] = dev->page;
1813 }
1814 }
1815
91c00924
DW
1816 /* 1/ if we prexor'd then the dest is reused as a source
1817 * 2/ if we did not prexor then we are redoing the parity
1818 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1819 * for the synchronous xor case
1820 */
59fc630b 1821 last_stripe = !head_sh->batch_head ||
1822 list_first_entry(&sh->batch_list,
1823 struct stripe_head, batch_list) == head_sh;
1824 if (last_stripe) {
1825 flags = ASYNC_TX_ACK |
1826 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1827
1828 atomic_inc(&head_sh->count);
1829 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1830 to_addr_conv(sh, percpu, j));
1831 } else {
1832 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1833 init_async_submit(&submit, flags, tx, NULL, NULL,
1834 to_addr_conv(sh, percpu, j));
1835 }
91c00924 1836
a08abd8c
DW
1837 if (unlikely(count == 1))
1838 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1839 else
1840 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
59fc630b 1841 if (!last_stripe) {
1842 j++;
1843 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1844 batch_list);
1845 goto again;
1846 }
91c00924
DW
1847}
1848
ac6b53b6
DW
1849static void
1850ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1851 struct dma_async_tx_descriptor *tx)
1852{
1853 struct async_submit_ctl submit;
59fc630b 1854 struct page **blocks;
1855 int count, i, j = 0;
1856 struct stripe_head *head_sh = sh;
1857 int last_stripe;
584acdd4
MS
1858 int synflags;
1859 unsigned long txflags;
ac6b53b6
DW
1860
1861 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1862
620125f2
SL
1863 for (i = 0; i < sh->disks; i++) {
1864 if (sh->pd_idx == i || sh->qd_idx == i)
1865 continue;
1866 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1867 break;
1868 }
1869 if (i >= sh->disks) {
1870 atomic_inc(&sh->count);
620125f2
SL
1871 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1872 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1873 ops_complete_reconstruct(sh);
1874 return;
1875 }
1876
59fc630b 1877again:
1878 blocks = to_addr_page(percpu, j);
584acdd4
MS
1879
1880 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1881 synflags = SYNDROME_SRC_WRITTEN;
1882 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1883 } else {
1884 synflags = SYNDROME_SRC_ALL;
1885 txflags = ASYNC_TX_ACK;
1886 }
1887
1888 count = set_syndrome_sources(blocks, sh, synflags);
59fc630b 1889 last_stripe = !head_sh->batch_head ||
1890 list_first_entry(&sh->batch_list,
1891 struct stripe_head, batch_list) == head_sh;
1892
1893 if (last_stripe) {
1894 atomic_inc(&head_sh->count);
584acdd4 1895 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
59fc630b 1896 head_sh, to_addr_conv(sh, percpu, j));
1897 } else
1898 init_async_submit(&submit, 0, tx, NULL, NULL,
1899 to_addr_conv(sh, percpu, j));
48769695 1900 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
59fc630b 1901 if (!last_stripe) {
1902 j++;
1903 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1904 batch_list);
1905 goto again;
1906 }
91c00924
DW
1907}
1908
1909static void ops_complete_check(void *stripe_head_ref)
1910{
1911 struct stripe_head *sh = stripe_head_ref;
91c00924 1912
e46b272b 1913 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1914 (unsigned long long)sh->sector);
1915
ecc65c9b 1916 sh->check_state = check_state_check_result;
91c00924 1917 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 1918 raid5_release_stripe(sh);
91c00924
DW
1919}
1920
ac6b53b6 1921static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
91c00924 1922{
91c00924 1923 int disks = sh->disks;
ac6b53b6
DW
1924 int pd_idx = sh->pd_idx;
1925 int qd_idx = sh->qd_idx;
1926 struct page *xor_dest;
46d5b785 1927 struct page **xor_srcs = to_addr_page(percpu, 0);
91c00924 1928 struct dma_async_tx_descriptor *tx;
a08abd8c 1929 struct async_submit_ctl submit;
ac6b53b6
DW
1930 int count;
1931 int i;
91c00924 1932
e46b272b 1933 pr_debug("%s: stripe %llu\n", __func__,
91c00924
DW
1934 (unsigned long long)sh->sector);
1935
59fc630b 1936 BUG_ON(sh->batch_head);
ac6b53b6
DW
1937 count = 0;
1938 xor_dest = sh->dev[pd_idx].page;
1939 xor_srcs[count++] = xor_dest;
91c00924 1940 for (i = disks; i--; ) {
ac6b53b6
DW
1941 if (i == pd_idx || i == qd_idx)
1942 continue;
1943 xor_srcs[count++] = sh->dev[i].page;
91c00924
DW
1944 }
1945
d6f38f31 1946 init_async_submit(&submit, 0, NULL, NULL, NULL,
46d5b785 1947 to_addr_conv(sh, percpu, 0));
099f53cb 1948 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
a08abd8c 1949 &sh->ops.zero_sum_result, &submit);
91c00924 1950
91c00924 1951 atomic_inc(&sh->count);
a08abd8c
DW
1952 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1953 tx = async_trigger_callback(&submit);
91c00924
DW
1954}
1955
ac6b53b6
DW
1956static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1957{
46d5b785 1958 struct page **srcs = to_addr_page(percpu, 0);
ac6b53b6
DW
1959 struct async_submit_ctl submit;
1960 int count;
1961
1962 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1963 (unsigned long long)sh->sector, checkp);
1964
59fc630b 1965 BUG_ON(sh->batch_head);
584acdd4 1966 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
ac6b53b6
DW
1967 if (!checkp)
1968 srcs[count] = NULL;
91c00924 1969
91c00924 1970 atomic_inc(&sh->count);
ac6b53b6 1971 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
46d5b785 1972 sh, to_addr_conv(sh, percpu, 0));
ac6b53b6
DW
1973 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1974 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
91c00924
DW
1975}
1976
51acbcec 1977static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
91c00924
DW
1978{
1979 int overlap_clear = 0, i, disks = sh->disks;
1980 struct dma_async_tx_descriptor *tx = NULL;
d1688a6d 1981 struct r5conf *conf = sh->raid_conf;
ac6b53b6 1982 int level = conf->level;
d6f38f31
DW
1983 struct raid5_percpu *percpu;
1984 unsigned long cpu;
91c00924 1985
d6f38f31
DW
1986 cpu = get_cpu();
1987 percpu = per_cpu_ptr(conf->percpu, cpu);
83de75cc 1988 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
91c00924
DW
1989 ops_run_biofill(sh);
1990 overlap_clear++;
1991 }
1992
7b3a871e 1993 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
ac6b53b6
DW
1994 if (level < 6)
1995 tx = ops_run_compute5(sh, percpu);
1996 else {
1997 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1998 tx = ops_run_compute6_1(sh, percpu);
1999 else
2000 tx = ops_run_compute6_2(sh, percpu);
2001 }
2002 /* terminate the chain if reconstruct is not set to be run */
2003 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
7b3a871e
DW
2004 async_tx_ack(tx);
2005 }
91c00924 2006
584acdd4
MS
2007 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2008 if (level < 6)
2009 tx = ops_run_prexor5(sh, percpu, tx);
2010 else
2011 tx = ops_run_prexor6(sh, percpu, tx);
2012 }
91c00924 2013
600aa109 2014 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
d8ee0728 2015 tx = ops_run_biodrain(sh, tx);
91c00924
DW
2016 overlap_clear++;
2017 }
2018
ac6b53b6
DW
2019 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2020 if (level < 6)
2021 ops_run_reconstruct5(sh, percpu, tx);
2022 else
2023 ops_run_reconstruct6(sh, percpu, tx);
2024 }
91c00924 2025
ac6b53b6
DW
2026 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2027 if (sh->check_state == check_state_run)
2028 ops_run_check_p(sh, percpu);
2029 else if (sh->check_state == check_state_run_q)
2030 ops_run_check_pq(sh, percpu, 0);
2031 else if (sh->check_state == check_state_run_pq)
2032 ops_run_check_pq(sh, percpu, 1);
2033 else
2034 BUG();
2035 }
91c00924 2036
59fc630b 2037 if (overlap_clear && !sh->batch_head)
91c00924
DW
2038 for (i = disks; i--; ) {
2039 struct r5dev *dev = &sh->dev[i];
2040 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2041 wake_up(&sh->raid_conf->wait_for_overlap);
2042 }
d6f38f31 2043 put_cpu();
91c00924
DW
2044}
2045
5f9d1fde
SL
2046static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2047 int disks)
f18c1a35
N
2048{
2049 struct stripe_head *sh;
5f9d1fde 2050 int i;
f18c1a35
N
2051
2052 sh = kmem_cache_zalloc(sc, gfp);
2053 if (sh) {
2054 spin_lock_init(&sh->stripe_lock);
2055 spin_lock_init(&sh->batch_lock);
2056 INIT_LIST_HEAD(&sh->batch_list);
2057 INIT_LIST_HEAD(&sh->lru);
a39f7afd 2058 INIT_LIST_HEAD(&sh->r5c);
d7bd398e 2059 INIT_LIST_HEAD(&sh->log_list);
f18c1a35 2060 atomic_set(&sh->count, 1);
a39f7afd 2061 sh->log_start = MaxSector;
5f9d1fde
SL
2062 for (i = 0; i < disks; i++) {
2063 struct r5dev *dev = &sh->dev[i];
2064
3a83f467
ML
2065 bio_init(&dev->req, &dev->vec, 1);
2066 bio_init(&dev->rreq, &dev->rvec, 1);
5f9d1fde 2067 }
f18c1a35
N
2068 }
2069 return sh;
2070}
486f0644 2071static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
1da177e4
LT
2072{
2073 struct stripe_head *sh;
f18c1a35 2074
5f9d1fde 2075 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
3f294f4f
N
2076 if (!sh)
2077 return 0;
6ce32846 2078
3f294f4f 2079 sh->raid_conf = conf;
3f294f4f 2080
a9683a79 2081 if (grow_buffers(sh, gfp)) {
e4e11e38 2082 shrink_buffers(sh);
3f294f4f
N
2083 kmem_cache_free(conf->slab_cache, sh);
2084 return 0;
2085 }
486f0644
N
2086 sh->hash_lock_index =
2087 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
3f294f4f 2088 /* we just created an active stripe so... */
3f294f4f 2089 atomic_inc(&conf->active_stripes);
59fc630b 2090
6d036f7d 2091 raid5_release_stripe(sh);
486f0644 2092 conf->max_nr_stripes++;
3f294f4f
N
2093 return 1;
2094}
2095
d1688a6d 2096static int grow_stripes(struct r5conf *conf, int num)
3f294f4f 2097{
e18b890b 2098 struct kmem_cache *sc;
5e5e3e78 2099 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1da177e4 2100
f4be6b43
N
2101 if (conf->mddev->gendisk)
2102 sprintf(conf->cache_name[0],
2103 "raid%d-%s", conf->level, mdname(conf->mddev));
2104 else
2105 sprintf(conf->cache_name[0],
2106 "raid%d-%p", conf->level, conf->mddev);
2107 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2108
ad01c9e3
N
2109 conf->active_name = 0;
2110 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1da177e4 2111 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
20c2df83 2112 0, 0, NULL);
1da177e4
LT
2113 if (!sc)
2114 return 1;
2115 conf->slab_cache = sc;
ad01c9e3 2116 conf->pool_size = devs;
486f0644
N
2117 while (num--)
2118 if (!grow_one_stripe(conf, GFP_KERNEL))
1da177e4 2119 return 1;
486f0644 2120
1da177e4
LT
2121 return 0;
2122}
29269553 2123
d6f38f31
DW
2124/**
2125 * scribble_len - return the required size of the scribble region
2126 * @num - total number of disks in the array
2127 *
2128 * The size must be enough to contain:
2129 * 1/ a struct page pointer for each device in the array +2
2130 * 2/ room to convert each entry in (1) to its corresponding dma
2131 * (dma_map_page()) or page (page_address()) address.
2132 *
2133 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2134 * calculate over all devices (not just the data blocks), using zeros in place
2135 * of the P and Q blocks.
2136 */
46d5b785 2137static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
d6f38f31 2138{
46d5b785 2139 struct flex_array *ret;
d6f38f31
DW
2140 size_t len;
2141
2142 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
46d5b785 2143 ret = flex_array_alloc(len, cnt, flags);
2144 if (!ret)
2145 return NULL;
2146 /* always prealloc all elements, so no locking is required */
2147 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2148 flex_array_free(ret);
2149 return NULL;
2150 }
2151 return ret;
d6f38f31
DW
2152}
2153
738a2738
N
2154static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2155{
2156 unsigned long cpu;
2157 int err = 0;
2158
27a353c0
SL
2159 /*
2160 * Never shrink. And mddev_suspend() could deadlock if this is called
2161 * from raid5d. In that case, scribble_disks and scribble_sectors
2162 * should equal to new_disks and new_sectors
2163 */
2164 if (conf->scribble_disks >= new_disks &&
2165 conf->scribble_sectors >= new_sectors)
2166 return 0;
738a2738
N
2167 mddev_suspend(conf->mddev);
2168 get_online_cpus();
2169 for_each_present_cpu(cpu) {
2170 struct raid5_percpu *percpu;
2171 struct flex_array *scribble;
2172
2173 percpu = per_cpu_ptr(conf->percpu, cpu);
2174 scribble = scribble_alloc(new_disks,
2175 new_sectors / STRIPE_SECTORS,
2176 GFP_NOIO);
2177
2178 if (scribble) {
2179 flex_array_free(percpu->scribble);
2180 percpu->scribble = scribble;
2181 } else {
2182 err = -ENOMEM;
2183 break;
2184 }
2185 }
2186 put_online_cpus();
2187 mddev_resume(conf->mddev);
27a353c0
SL
2188 if (!err) {
2189 conf->scribble_disks = new_disks;
2190 conf->scribble_sectors = new_sectors;
2191 }
738a2738
N
2192 return err;
2193}
2194
d1688a6d 2195static int resize_stripes(struct r5conf *conf, int newsize)
ad01c9e3
N
2196{
2197 /* Make all the stripes able to hold 'newsize' devices.
2198 * New slots in each stripe get 'page' set to a new page.
2199 *
2200 * This happens in stages:
2201 * 1/ create a new kmem_cache and allocate the required number of
2202 * stripe_heads.
83f0d77a 2203 * 2/ gather all the old stripe_heads and transfer the pages across
ad01c9e3
N
2204 * to the new stripe_heads. This will have the side effect of
2205 * freezing the array as once all stripe_heads have been collected,
2206 * no IO will be possible. Old stripe heads are freed once their
2207 * pages have been transferred over, and the old kmem_cache is
2208 * freed when all stripes are done.
2209 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2210 * we simple return a failre status - no need to clean anything up.
2211 * 4/ allocate new pages for the new slots in the new stripe_heads.
2212 * If this fails, we don't bother trying the shrink the
2213 * stripe_heads down again, we just leave them as they are.
2214 * As each stripe_head is processed the new one is released into
2215 * active service.
2216 *
2217 * Once step2 is started, we cannot afford to wait for a write,
2218 * so we use GFP_NOIO allocations.
2219 */
2220 struct stripe_head *osh, *nsh;
2221 LIST_HEAD(newstripes);
2222 struct disk_info *ndisks;
b5470dc5 2223 int err;
e18b890b 2224 struct kmem_cache *sc;
ad01c9e3 2225 int i;
566c09c5 2226 int hash, cnt;
ad01c9e3
N
2227
2228 if (newsize <= conf->pool_size)
2229 return 0; /* never bother to shrink */
2230
b5470dc5
DW
2231 err = md_allow_write(conf->mddev);
2232 if (err)
2233 return err;
2a2275d6 2234
ad01c9e3
N
2235 /* Step 1 */
2236 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2237 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
20c2df83 2238 0, 0, NULL);
ad01c9e3
N
2239 if (!sc)
2240 return -ENOMEM;
2241
2d5b569b
N
2242 /* Need to ensure auto-resizing doesn't interfere */
2243 mutex_lock(&conf->cache_size_mutex);
2244
ad01c9e3 2245 for (i = conf->max_nr_stripes; i; i--) {
5f9d1fde 2246 nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
ad01c9e3
N
2247 if (!nsh)
2248 break;
2249
ad01c9e3 2250 nsh->raid_conf = conf;
ad01c9e3
N
2251 list_add(&nsh->lru, &newstripes);
2252 }
2253 if (i) {
2254 /* didn't get enough, give up */
2255 while (!list_empty(&newstripes)) {
2256 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2257 list_del(&nsh->lru);
2258 kmem_cache_free(sc, nsh);
2259 }
2260 kmem_cache_destroy(sc);
2d5b569b 2261 mutex_unlock(&conf->cache_size_mutex);
ad01c9e3
N
2262 return -ENOMEM;
2263 }
2264 /* Step 2 - Must use GFP_NOIO now.
2265 * OK, we have enough stripes, start collecting inactive
2266 * stripes and copying them over
2267 */
566c09c5
SL
2268 hash = 0;
2269 cnt = 0;
ad01c9e3 2270 list_for_each_entry(nsh, &newstripes, lru) {
566c09c5 2271 lock_device_hash_lock(conf, hash);
6ab2a4b8 2272 wait_event_cmd(conf->wait_for_stripe,
566c09c5
SL
2273 !list_empty(conf->inactive_list + hash),
2274 unlock_device_hash_lock(conf, hash),
2275 lock_device_hash_lock(conf, hash));
2276 osh = get_free_stripe(conf, hash);
2277 unlock_device_hash_lock(conf, hash);
f18c1a35 2278
d592a996 2279 for(i=0; i<conf->pool_size; i++) {
ad01c9e3 2280 nsh->dev[i].page = osh->dev[i].page;
d592a996
SL
2281 nsh->dev[i].orig_page = osh->dev[i].page;
2282 }
566c09c5 2283 nsh->hash_lock_index = hash;
ad01c9e3 2284 kmem_cache_free(conf->slab_cache, osh);
566c09c5
SL
2285 cnt++;
2286 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2287 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2288 hash++;
2289 cnt = 0;
2290 }
ad01c9e3
N
2291 }
2292 kmem_cache_destroy(conf->slab_cache);
2293
2294 /* Step 3.
2295 * At this point, we are holding all the stripes so the array
2296 * is completely stalled, so now is a good time to resize
d6f38f31 2297 * conf->disks and the scribble region
ad01c9e3
N
2298 */
2299 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2300 if (ndisks) {
d7bd398e 2301 for (i = 0; i < conf->pool_size; i++)
ad01c9e3 2302 ndisks[i] = conf->disks[i];
d7bd398e
SL
2303
2304 for (i = conf->pool_size; i < newsize; i++) {
2305 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2306 if (!ndisks[i].extra_page)
2307 err = -ENOMEM;
2308 }
2309
2310 if (err) {
2311 for (i = conf->pool_size; i < newsize; i++)
2312 if (ndisks[i].extra_page)
2313 put_page(ndisks[i].extra_page);
2314 kfree(ndisks);
2315 } else {
2316 kfree(conf->disks);
2317 conf->disks = ndisks;
2318 }
ad01c9e3
N
2319 } else
2320 err = -ENOMEM;
2321
2d5b569b 2322 mutex_unlock(&conf->cache_size_mutex);
ad01c9e3
N
2323 /* Step 4, return new stripes to service */
2324 while(!list_empty(&newstripes)) {
2325 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2326 list_del_init(&nsh->lru);
d6f38f31 2327
ad01c9e3
N
2328 for (i=conf->raid_disks; i < newsize; i++)
2329 if (nsh->dev[i].page == NULL) {
2330 struct page *p = alloc_page(GFP_NOIO);
2331 nsh->dev[i].page = p;
d592a996 2332 nsh->dev[i].orig_page = p;
ad01c9e3
N
2333 if (!p)
2334 err = -ENOMEM;
2335 }
6d036f7d 2336 raid5_release_stripe(nsh);
ad01c9e3
N
2337 }
2338 /* critical section pass, GFP_NOIO no longer needed */
2339
2340 conf->slab_cache = sc;
2341 conf->active_name = 1-conf->active_name;
6e9eac2d
N
2342 if (!err)
2343 conf->pool_size = newsize;
ad01c9e3
N
2344 return err;
2345}
1da177e4 2346
486f0644 2347static int drop_one_stripe(struct r5conf *conf)
1da177e4
LT
2348{
2349 struct stripe_head *sh;
49895bcc 2350 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
1da177e4 2351
566c09c5
SL
2352 spin_lock_irq(conf->hash_locks + hash);
2353 sh = get_free_stripe(conf, hash);
2354 spin_unlock_irq(conf->hash_locks + hash);
3f294f4f
N
2355 if (!sh)
2356 return 0;
78bafebd 2357 BUG_ON(atomic_read(&sh->count));
e4e11e38 2358 shrink_buffers(sh);
3f294f4f
N
2359 kmem_cache_free(conf->slab_cache, sh);
2360 atomic_dec(&conf->active_stripes);
486f0644 2361 conf->max_nr_stripes--;
3f294f4f
N
2362 return 1;
2363}
2364
d1688a6d 2365static void shrink_stripes(struct r5conf *conf)
3f294f4f 2366{
486f0644
N
2367 while (conf->max_nr_stripes &&
2368 drop_one_stripe(conf))
2369 ;
3f294f4f 2370
644df1a8 2371 kmem_cache_destroy(conf->slab_cache);
1da177e4
LT
2372 conf->slab_cache = NULL;
2373}
2374
4246a0b6 2375static void raid5_end_read_request(struct bio * bi)
1da177e4 2376{
99c0fb5f 2377 struct stripe_head *sh = bi->bi_private;
d1688a6d 2378 struct r5conf *conf = sh->raid_conf;
7ecaa1e6 2379 int disks = sh->disks, i;
d6950432 2380 char b[BDEVNAME_SIZE];
dd054fce 2381 struct md_rdev *rdev = NULL;
05616be5 2382 sector_t s;
1da177e4
LT
2383
2384 for (i=0 ; i<disks; i++)
2385 if (bi == &sh->dev[i].req)
2386 break;
2387
4246a0b6 2388 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
45b4233c 2389 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
4246a0b6 2390 bi->bi_error);
1da177e4 2391 if (i == disks) {
5f9d1fde 2392 bio_reset(bi);
1da177e4 2393 BUG();
6712ecf8 2394 return;
1da177e4 2395 }
14a75d3e 2396 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
dd054fce
N
2397 /* If replacement finished while this request was outstanding,
2398 * 'replacement' might be NULL already.
2399 * In that case it moved down to 'rdev'.
2400 * rdev is not removed until all requests are finished.
2401 */
14a75d3e 2402 rdev = conf->disks[i].replacement;
dd054fce 2403 if (!rdev)
14a75d3e 2404 rdev = conf->disks[i].rdev;
1da177e4 2405
05616be5
N
2406 if (use_new_offset(conf, sh))
2407 s = sh->sector + rdev->new_data_offset;
2408 else
2409 s = sh->sector + rdev->data_offset;
4246a0b6 2410 if (!bi->bi_error) {
1da177e4 2411 set_bit(R5_UPTODATE, &sh->dev[i].flags);
4e5314b5 2412 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
14a75d3e
N
2413 /* Note that this cannot happen on a
2414 * replacement device. We just fail those on
2415 * any error
2416 */
cc6167b4
N
2417 pr_info_ratelimited(
2418 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
8bda470e 2419 mdname(conf->mddev), STRIPE_SECTORS,
05616be5 2420 (unsigned long long)s,
8bda470e 2421 bdevname(rdev->bdev, b));
ddd5115f 2422 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
4e5314b5
N
2423 clear_bit(R5_ReadError, &sh->dev[i].flags);
2424 clear_bit(R5_ReWrite, &sh->dev[i].flags);
3f9e7c14 2425 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2426 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2427
86aa1397
SL
2428 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2429 /*
2430 * end read for a page in journal, this
2431 * must be preparing for prexor in rmw
2432 */
2433 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2434
14a75d3e
N
2435 if (atomic_read(&rdev->read_errors))
2436 atomic_set(&rdev->read_errors, 0);
1da177e4 2437 } else {
14a75d3e 2438 const char *bdn = bdevname(rdev->bdev, b);
ba22dcbf 2439 int retry = 0;
2e8ac303 2440 int set_bad = 0;
d6950432 2441
1da177e4 2442 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
d6950432 2443 atomic_inc(&rdev->read_errors);
14a75d3e 2444 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
cc6167b4
N
2445 pr_warn_ratelimited(
2446 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
14a75d3e 2447 mdname(conf->mddev),
05616be5 2448 (unsigned long long)s,
14a75d3e 2449 bdn);
2e8ac303 2450 else if (conf->mddev->degraded >= conf->max_degraded) {
2451 set_bad = 1;
cc6167b4
N
2452 pr_warn_ratelimited(
2453 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
8bda470e 2454 mdname(conf->mddev),
05616be5 2455 (unsigned long long)s,
8bda470e 2456 bdn);
2e8ac303 2457 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
4e5314b5 2458 /* Oh, no!!! */
2e8ac303 2459 set_bad = 1;
cc6167b4
N
2460 pr_warn_ratelimited(
2461 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
8bda470e 2462 mdname(conf->mddev),
05616be5 2463 (unsigned long long)s,
8bda470e 2464 bdn);
2e8ac303 2465 } else if (atomic_read(&rdev->read_errors)
ba22dcbf 2466 > conf->max_nr_stripes)
cc6167b4 2467 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
d6950432 2468 mdname(conf->mddev), bdn);
ba22dcbf
N
2469 else
2470 retry = 1;
edfa1f65
BY
2471 if (set_bad && test_bit(In_sync, &rdev->flags)
2472 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2473 retry = 1;
ba22dcbf 2474 if (retry)
3f9e7c14 2475 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2476 set_bit(R5_ReadError, &sh->dev[i].flags);
2477 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2478 } else
2479 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
ba22dcbf 2480 else {
4e5314b5
N
2481 clear_bit(R5_ReadError, &sh->dev[i].flags);
2482 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2e8ac303 2483 if (!(set_bad
2484 && test_bit(In_sync, &rdev->flags)
2485 && rdev_set_badblocks(
2486 rdev, sh->sector, STRIPE_SECTORS, 0)))
2487 md_error(conf->mddev, rdev);
ba22dcbf 2488 }
1da177e4 2489 }
14a75d3e 2490 rdev_dec_pending(rdev, conf->mddev);
c9445555 2491 bio_reset(bi);
1da177e4
LT
2492 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2493 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 2494 raid5_release_stripe(sh);
1da177e4
LT
2495}
2496
4246a0b6 2497static void raid5_end_write_request(struct bio *bi)
1da177e4 2498{
99c0fb5f 2499 struct stripe_head *sh = bi->bi_private;
d1688a6d 2500 struct r5conf *conf = sh->raid_conf;
7ecaa1e6 2501 int disks = sh->disks, i;
977df362 2502 struct md_rdev *uninitialized_var(rdev);
b84db560
N
2503 sector_t first_bad;
2504 int bad_sectors;
977df362 2505 int replacement = 0;
1da177e4 2506
977df362
N
2507 for (i = 0 ; i < disks; i++) {
2508 if (bi == &sh->dev[i].req) {
2509 rdev = conf->disks[i].rdev;
1da177e4 2510 break;
977df362
N
2511 }
2512 if (bi == &sh->dev[i].rreq) {
2513 rdev = conf->disks[i].replacement;
dd054fce
N
2514 if (rdev)
2515 replacement = 1;
2516 else
2517 /* rdev was removed and 'replacement'
2518 * replaced it. rdev is not removed
2519 * until all requests are finished.
2520 */
2521 rdev = conf->disks[i].rdev;
977df362
N
2522 break;
2523 }
2524 }
4246a0b6 2525 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
1da177e4 2526 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
4246a0b6 2527 bi->bi_error);
1da177e4 2528 if (i == disks) {
5f9d1fde 2529 bio_reset(bi);
1da177e4 2530 BUG();
6712ecf8 2531 return;
1da177e4
LT
2532 }
2533
977df362 2534 if (replacement) {
4246a0b6 2535 if (bi->bi_error)
977df362
N
2536 md_error(conf->mddev, rdev);
2537 else if (is_badblock(rdev, sh->sector,
2538 STRIPE_SECTORS,
2539 &first_bad, &bad_sectors))
2540 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2541 } else {
4246a0b6 2542 if (bi->bi_error) {
9f97e4b1 2543 set_bit(STRIPE_DEGRADED, &sh->state);
977df362
N
2544 set_bit(WriteErrorSeen, &rdev->flags);
2545 set_bit(R5_WriteError, &sh->dev[i].flags);
3a6de292
N
2546 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2547 set_bit(MD_RECOVERY_NEEDED,
2548 &rdev->mddev->recovery);
977df362
N
2549 } else if (is_badblock(rdev, sh->sector,
2550 STRIPE_SECTORS,
c0b32972 2551 &first_bad, &bad_sectors)) {
977df362 2552 set_bit(R5_MadeGood, &sh->dev[i].flags);
c0b32972
N
2553 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2554 /* That was a successful write so make
2555 * sure it looks like we already did
2556 * a re-write.
2557 */
2558 set_bit(R5_ReWrite, &sh->dev[i].flags);
2559 }
977df362
N
2560 }
2561 rdev_dec_pending(rdev, conf->mddev);
1da177e4 2562
4246a0b6 2563 if (sh->batch_head && bi->bi_error && !replacement)
72ac7330 2564 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2565
c9445555 2566 bio_reset(bi);
977df362
N
2567 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2568 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1da177e4 2569 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 2570 raid5_release_stripe(sh);
59fc630b 2571
2572 if (sh->batch_head && sh != sh->batch_head)
6d036f7d 2573 raid5_release_stripe(sh->batch_head);
1da177e4
LT
2574}
2575
784052ec 2576static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1da177e4
LT
2577{
2578 struct r5dev *dev = &sh->dev[i];
2579
1da177e4 2580 dev->flags = 0;
6d036f7d 2581 dev->sector = raid5_compute_blocknr(sh, i, previous);
1da177e4
LT
2582}
2583
849674e4 2584static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
1da177e4
LT
2585{
2586 char b[BDEVNAME_SIZE];
d1688a6d 2587 struct r5conf *conf = mddev->private;
908f4fbd 2588 unsigned long flags;
0c55e022 2589 pr_debug("raid456: error called\n");
1da177e4 2590
908f4fbd
N
2591 spin_lock_irqsave(&conf->device_lock, flags);
2592 clear_bit(In_sync, &rdev->flags);
2e38a37f 2593 mddev->degraded = raid5_calc_degraded(conf);
908f4fbd
N
2594 spin_unlock_irqrestore(&conf->device_lock, flags);
2595 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2596
de393cde 2597 set_bit(Blocked, &rdev->flags);
6f8d0c77 2598 set_bit(Faulty, &rdev->flags);
2953079c
SL
2599 set_mask_bits(&mddev->sb_flags, 0,
2600 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
cc6167b4
N
2601 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2602 "md/raid:%s: Operation continuing on %d devices.\n",
2603 mdname(mddev),
2604 bdevname(rdev->bdev, b),
2605 mdname(mddev),
2606 conf->raid_disks - mddev->degraded);
2e38a37f 2607 r5c_update_on_rdev_error(mddev);
16a53ecc 2608}
1da177e4
LT
2609
2610/*
2611 * Input: a 'big' sector number,
2612 * Output: index of the data and parity disk, and the sector # in them.
2613 */
6d036f7d
SL
2614sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2615 int previous, int *dd_idx,
2616 struct stripe_head *sh)
1da177e4 2617{
6e3b96ed 2618 sector_t stripe, stripe2;
35f2a591 2619 sector_t chunk_number;
1da177e4 2620 unsigned int chunk_offset;
911d4ee8 2621 int pd_idx, qd_idx;
67cc2b81 2622 int ddf_layout = 0;
1da177e4 2623 sector_t new_sector;
e183eaed
N
2624 int algorithm = previous ? conf->prev_algo
2625 : conf->algorithm;
09c9e5fa
AN
2626 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2627 : conf->chunk_sectors;
112bf897
N
2628 int raid_disks = previous ? conf->previous_raid_disks
2629 : conf->raid_disks;
2630 int data_disks = raid_disks - conf->max_degraded;
1da177e4
LT
2631
2632 /* First compute the information on this sector */
2633
2634 /*
2635 * Compute the chunk number and the sector offset inside the chunk
2636 */
2637 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2638 chunk_number = r_sector;
1da177e4
LT
2639
2640 /*
2641 * Compute the stripe number
2642 */
35f2a591
N
2643 stripe = chunk_number;
2644 *dd_idx = sector_div(stripe, data_disks);
6e3b96ed 2645 stripe2 = stripe;
1da177e4
LT
2646 /*
2647 * Select the parity disk based on the user selected algorithm.
2648 */
84789554 2649 pd_idx = qd_idx = -1;
16a53ecc
N
2650 switch(conf->level) {
2651 case 4:
911d4ee8 2652 pd_idx = data_disks;
16a53ecc
N
2653 break;
2654 case 5:
e183eaed 2655 switch (algorithm) {
1da177e4 2656 case ALGORITHM_LEFT_ASYMMETRIC:
6e3b96ed 2657 pd_idx = data_disks - sector_div(stripe2, raid_disks);
911d4ee8 2658 if (*dd_idx >= pd_idx)
1da177e4
LT
2659 (*dd_idx)++;
2660 break;
2661 case ALGORITHM_RIGHT_ASYMMETRIC:
6e3b96ed 2662 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8 2663 if (*dd_idx >= pd_idx)
1da177e4
LT
2664 (*dd_idx)++;
2665 break;
2666 case ALGORITHM_LEFT_SYMMETRIC:
6e3b96ed 2667 pd_idx = data_disks - sector_div(stripe2, raid_disks);
911d4ee8 2668 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1da177e4
LT
2669 break;
2670 case ALGORITHM_RIGHT_SYMMETRIC:
6e3b96ed 2671 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8 2672 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1da177e4 2673 break;
99c0fb5f
N
2674 case ALGORITHM_PARITY_0:
2675 pd_idx = 0;
2676 (*dd_idx)++;
2677 break;
2678 case ALGORITHM_PARITY_N:
2679 pd_idx = data_disks;
2680 break;
1da177e4 2681 default:
99c0fb5f 2682 BUG();
16a53ecc
N
2683 }
2684 break;
2685 case 6:
2686
e183eaed 2687 switch (algorithm) {
16a53ecc 2688 case ALGORITHM_LEFT_ASYMMETRIC:
6e3b96ed 2689 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
911d4ee8
N
2690 qd_idx = pd_idx + 1;
2691 if (pd_idx == raid_disks-1) {
99c0fb5f 2692 (*dd_idx)++; /* Q D D D P */
911d4ee8
N
2693 qd_idx = 0;
2694 } else if (*dd_idx >= pd_idx)
16a53ecc
N
2695 (*dd_idx) += 2; /* D D P Q D */
2696 break;
2697 case ALGORITHM_RIGHT_ASYMMETRIC:
6e3b96ed 2698 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8
N
2699 qd_idx = pd_idx + 1;
2700 if (pd_idx == raid_disks-1) {
99c0fb5f 2701 (*dd_idx)++; /* Q D D D P */
911d4ee8
N
2702 qd_idx = 0;
2703 } else if (*dd_idx >= pd_idx)
16a53ecc
N
2704 (*dd_idx) += 2; /* D D P Q D */
2705 break;
2706 case ALGORITHM_LEFT_SYMMETRIC:
6e3b96ed 2707 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
911d4ee8
N
2708 qd_idx = (pd_idx + 1) % raid_disks;
2709 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
16a53ecc
N
2710 break;
2711 case ALGORITHM_RIGHT_SYMMETRIC:
6e3b96ed 2712 pd_idx = sector_div(stripe2, raid_disks);
911d4ee8
N
2713 qd_idx = (pd_idx + 1) % raid_disks;
2714 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
16a53ecc 2715 break;
99c0fb5f
N
2716
2717 case ALGORITHM_PARITY_0:
2718 pd_idx = 0;
2719 qd_idx = 1;
2720 (*dd_idx) += 2;
2721 break;
2722 case ALGORITHM_PARITY_N:
2723 pd_idx = data_disks;
2724 qd_idx = data_disks + 1;
2725 break;
2726
2727 case ALGORITHM_ROTATING_ZERO_RESTART:
2728 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2729 * of blocks for computing Q is different.
2730 */
6e3b96ed 2731 pd_idx = sector_div(stripe2, raid_disks);
99c0fb5f
N
2732 qd_idx = pd_idx + 1;
2733 if (pd_idx == raid_disks-1) {
2734 (*dd_idx)++; /* Q D D D P */
2735 qd_idx = 0;
2736 } else if (*dd_idx >= pd_idx)
2737 (*dd_idx) += 2; /* D D P Q D */
67cc2b81 2738 ddf_layout = 1;
99c0fb5f
N
2739 break;
2740
2741 case ALGORITHM_ROTATING_N_RESTART:
2742 /* Same a left_asymmetric, by first stripe is
2743 * D D D P Q rather than
2744 * Q D D D P
2745 */
6e3b96ed
N
2746 stripe2 += 1;
2747 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
99c0fb5f
N
2748 qd_idx = pd_idx + 1;
2749 if (pd_idx == raid_disks-1) {
2750 (*dd_idx)++; /* Q D D D P */
2751 qd_idx = 0;
2752 } else if (*dd_idx >= pd_idx)
2753 (*dd_idx) += 2; /* D D P Q D */
67cc2b81 2754 ddf_layout = 1;
99c0fb5f
N
2755 break;
2756
2757 case ALGORITHM_ROTATING_N_CONTINUE:
2758 /* Same as left_symmetric but Q is before P */
6e3b96ed 2759 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
99c0fb5f
N
2760 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2761 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
67cc2b81 2762 ddf_layout = 1;
99c0fb5f
N
2763 break;
2764
2765 case ALGORITHM_LEFT_ASYMMETRIC_6:
2766 /* RAID5 left_asymmetric, with Q on last device */
6e3b96ed 2767 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2768 if (*dd_idx >= pd_idx)
2769 (*dd_idx)++;
2770 qd_idx = raid_disks - 1;
2771 break;
2772
2773 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6e3b96ed 2774 pd_idx = sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2775 if (*dd_idx >= pd_idx)
2776 (*dd_idx)++;
2777 qd_idx = raid_disks - 1;
2778 break;
2779
2780 case ALGORITHM_LEFT_SYMMETRIC_6:
6e3b96ed 2781 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2782 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2783 qd_idx = raid_disks - 1;
2784 break;
2785
2786 case ALGORITHM_RIGHT_SYMMETRIC_6:
6e3b96ed 2787 pd_idx = sector_div(stripe2, raid_disks-1);
99c0fb5f
N
2788 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2789 qd_idx = raid_disks - 1;
2790 break;
2791
2792 case ALGORITHM_PARITY_0_6:
2793 pd_idx = 0;
2794 (*dd_idx)++;
2795 qd_idx = raid_disks - 1;
2796 break;
2797
16a53ecc 2798 default:
99c0fb5f 2799 BUG();
16a53ecc
N
2800 }
2801 break;
1da177e4
LT
2802 }
2803
911d4ee8
N
2804 if (sh) {
2805 sh->pd_idx = pd_idx;
2806 sh->qd_idx = qd_idx;
67cc2b81 2807 sh->ddf_layout = ddf_layout;
911d4ee8 2808 }
1da177e4
LT
2809 /*
2810 * Finally, compute the new sector number
2811 */
2812 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2813 return new_sector;
2814}
2815
6d036f7d 2816sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
1da177e4 2817{
d1688a6d 2818 struct r5conf *conf = sh->raid_conf;
b875e531
N
2819 int raid_disks = sh->disks;
2820 int data_disks = raid_disks - conf->max_degraded;
1da177e4 2821 sector_t new_sector = sh->sector, check;
09c9e5fa
AN
2822 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2823 : conf->chunk_sectors;
e183eaed
N
2824 int algorithm = previous ? conf->prev_algo
2825 : conf->algorithm;
1da177e4
LT
2826 sector_t stripe;
2827 int chunk_offset;
35f2a591
N
2828 sector_t chunk_number;
2829 int dummy1, dd_idx = i;
1da177e4 2830 sector_t r_sector;
911d4ee8 2831 struct stripe_head sh2;
1da177e4
LT
2832
2833 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2834 stripe = new_sector;
1da177e4 2835
16a53ecc
N
2836 if (i == sh->pd_idx)
2837 return 0;
2838 switch(conf->level) {
2839 case 4: break;
2840 case 5:
e183eaed 2841 switch (algorithm) {
1da177e4
LT
2842 case ALGORITHM_LEFT_ASYMMETRIC:
2843 case ALGORITHM_RIGHT_ASYMMETRIC:
2844 if (i > sh->pd_idx)
2845 i--;
2846 break;
2847 case ALGORITHM_LEFT_SYMMETRIC:
2848 case ALGORITHM_RIGHT_SYMMETRIC:
2849 if (i < sh->pd_idx)
2850 i += raid_disks;
2851 i -= (sh->pd_idx + 1);
2852 break;
99c0fb5f
N
2853 case ALGORITHM_PARITY_0:
2854 i -= 1;
2855 break;
2856 case ALGORITHM_PARITY_N:
2857 break;
1da177e4 2858 default:
99c0fb5f 2859 BUG();
16a53ecc
N
2860 }
2861 break;
2862 case 6:
d0dabf7e 2863 if (i == sh->qd_idx)
16a53ecc 2864 return 0; /* It is the Q disk */
e183eaed 2865 switch (algorithm) {
16a53ecc
N
2866 case ALGORITHM_LEFT_ASYMMETRIC:
2867 case ALGORITHM_RIGHT_ASYMMETRIC:
99c0fb5f
N
2868 case ALGORITHM_ROTATING_ZERO_RESTART:
2869 case ALGORITHM_ROTATING_N_RESTART:
2870 if (sh->pd_idx == raid_disks-1)
2871 i--; /* Q D D D P */
16a53ecc
N
2872 else if (i > sh->pd_idx)
2873 i -= 2; /* D D P Q D */
2874 break;
2875 case ALGORITHM_LEFT_SYMMETRIC:
2876 case ALGORITHM_RIGHT_SYMMETRIC:
2877 if (sh->pd_idx == raid_disks-1)
2878 i--; /* Q D D D P */
2879 else {
2880 /* D D P Q D */
2881 if (i < sh->pd_idx)
2882 i += raid_disks;
2883 i -= (sh->pd_idx + 2);
2884 }
2885 break;
99c0fb5f
N
2886 case ALGORITHM_PARITY_0:
2887 i -= 2;
2888 break;
2889 case ALGORITHM_PARITY_N:
2890 break;
2891 case ALGORITHM_ROTATING_N_CONTINUE:
e4424fee 2892 /* Like left_symmetric, but P is before Q */
99c0fb5f
N
2893 if (sh->pd_idx == 0)
2894 i--; /* P D D D Q */
e4424fee
N
2895 else {
2896 /* D D Q P D */
2897 if (i < sh->pd_idx)
2898 i += raid_disks;
2899 i -= (sh->pd_idx + 1);
2900 }
99c0fb5f
N
2901 break;
2902 case ALGORITHM_LEFT_ASYMMETRIC_6:
2903 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2904 if (i > sh->pd_idx)
2905 i--;
2906 break;
2907 case ALGORITHM_LEFT_SYMMETRIC_6:
2908 case ALGORITHM_RIGHT_SYMMETRIC_6:
2909 if (i < sh->pd_idx)
2910 i += data_disks + 1;
2911 i -= (sh->pd_idx + 1);
2912 break;
2913 case ALGORITHM_PARITY_0_6:
2914 i -= 1;
2915 break;
16a53ecc 2916 default:
99c0fb5f 2917 BUG();
16a53ecc
N
2918 }
2919 break;
1da177e4
LT
2920 }
2921
2922 chunk_number = stripe * data_disks + i;
35f2a591 2923 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
1da177e4 2924
112bf897 2925 check = raid5_compute_sector(conf, r_sector,
784052ec 2926 previous, &dummy1, &sh2);
911d4ee8
N
2927 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2928 || sh2.qd_idx != sh->qd_idx) {
cc6167b4
N
2929 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2930 mdname(conf->mddev));
1da177e4
LT
2931 return 0;
2932 }
2933 return r_sector;
2934}
2935
07e83364
SL
2936/*
2937 * There are cases where we want handle_stripe_dirtying() and
2938 * schedule_reconstruction() to delay towrite to some dev of a stripe.
2939 *
2940 * This function checks whether we want to delay the towrite. Specifically,
2941 * we delay the towrite when:
2942 *
2943 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
2944 * stripe has data in journal (for other devices).
2945 *
2946 * In this case, when reading data for the non-overwrite dev, it is
2947 * necessary to handle complex rmw of write back cache (prexor with
2948 * orig_page, and xor with page). To keep read path simple, we would
2949 * like to flush data in journal to RAID disks first, so complex rmw
2950 * is handled in the write patch (handle_stripe_dirtying).
2951 *
39b99586
SL
2952 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
2953 *
2954 * It is important to be able to flush all stripes in raid5-cache.
2955 * Therefore, we need reserve some space on the journal device for
2956 * these flushes. If flush operation includes pending writes to the
2957 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
2958 * for the flush out. If we exclude these pending writes from flush
2959 * operation, we only need (conf->max_degraded + 1) pages per stripe.
2960 * Therefore, excluding pending writes in these cases enables more
2961 * efficient use of the journal device.
2962 *
2963 * Note: To make sure the stripe makes progress, we only delay
2964 * towrite for stripes with data already in journal (injournal > 0).
2965 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
2966 * no_space_stripes list.
2967 *
07e83364 2968 */
39b99586
SL
2969static inline bool delay_towrite(struct r5conf *conf,
2970 struct r5dev *dev,
2971 struct stripe_head_state *s)
07e83364 2972{
39b99586
SL
2973 /* case 1 above */
2974 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2975 !test_bit(R5_Insync, &dev->flags) && s->injournal)
2976 return true;
2977 /* case 2 above */
2978 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2979 s->injournal > 0)
2980 return true;
2981 return false;
07e83364
SL
2982}
2983
600aa109 2984static void
c0f7bddb 2985schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
600aa109 2986 int rcw, int expand)
e33129d8 2987{
584acdd4 2988 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
d1688a6d 2989 struct r5conf *conf = sh->raid_conf;
c0f7bddb 2990 int level = conf->level;
e33129d8
DW
2991
2992 if (rcw) {
1e6d690b
SL
2993 /*
2994 * In some cases, handle_stripe_dirtying initially decided to
2995 * run rmw and allocates extra page for prexor. However, rcw is
2996 * cheaper later on. We need to free the extra page now,
2997 * because we won't be able to do that in ops_complete_prexor().
2998 */
2999 r5c_release_extra_page(sh);
e33129d8
DW
3000
3001 for (i = disks; i--; ) {
3002 struct r5dev *dev = &sh->dev[i];
3003
39b99586 3004 if (dev->towrite && !delay_towrite(conf, dev, s)) {
e33129d8 3005 set_bit(R5_LOCKED, &dev->flags);
d8ee0728 3006 set_bit(R5_Wantdrain, &dev->flags);
e33129d8
DW
3007 if (!expand)
3008 clear_bit(R5_UPTODATE, &dev->flags);
600aa109 3009 s->locked++;
1e6d690b
SL
3010 } else if (test_bit(R5_InJournal, &dev->flags)) {
3011 set_bit(R5_LOCKED, &dev->flags);
3012 s->locked++;
e33129d8
DW
3013 }
3014 }
ce7d363a
N
3015 /* if we are not expanding this is a proper write request, and
3016 * there will be bios with new data to be drained into the
3017 * stripe cache
3018 */
3019 if (!expand) {
3020 if (!s->locked)
3021 /* False alarm, nothing to do */
3022 return;
3023 sh->reconstruct_state = reconstruct_state_drain_run;
3024 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3025 } else
3026 sh->reconstruct_state = reconstruct_state_run;
3027
3028 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3029
c0f7bddb 3030 if (s->locked + conf->max_degraded == disks)
8b3e6cdc 3031 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
c0f7bddb 3032 atomic_inc(&conf->pending_full_writes);
e33129d8
DW
3033 } else {
3034 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3035 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
584acdd4
MS
3036 BUG_ON(level == 6 &&
3037 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3038 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
e33129d8 3039
e33129d8
DW
3040 for (i = disks; i--; ) {
3041 struct r5dev *dev = &sh->dev[i];
584acdd4 3042 if (i == pd_idx || i == qd_idx)
e33129d8
DW
3043 continue;
3044
e33129d8
DW
3045 if (dev->towrite &&
3046 (test_bit(R5_UPTODATE, &dev->flags) ||
d8ee0728
DW
3047 test_bit(R5_Wantcompute, &dev->flags))) {
3048 set_bit(R5_Wantdrain, &dev->flags);
e33129d8
DW
3049 set_bit(R5_LOCKED, &dev->flags);
3050 clear_bit(R5_UPTODATE, &dev->flags);
600aa109 3051 s->locked++;
1e6d690b
SL
3052 } else if (test_bit(R5_InJournal, &dev->flags)) {
3053 set_bit(R5_LOCKED, &dev->flags);
3054 s->locked++;
e33129d8
DW
3055 }
3056 }
ce7d363a
N
3057 if (!s->locked)
3058 /* False alarm - nothing to do */
3059 return;
3060 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3061 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3062 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3063 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
e33129d8
DW
3064 }
3065
c0f7bddb 3066 /* keep the parity disk(s) locked while asynchronous operations
e33129d8
DW
3067 * are in flight
3068 */
3069 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3070 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
600aa109 3071 s->locked++;
e33129d8 3072
c0f7bddb
YT
3073 if (level == 6) {
3074 int qd_idx = sh->qd_idx;
3075 struct r5dev *dev = &sh->dev[qd_idx];
3076
3077 set_bit(R5_LOCKED, &dev->flags);
3078 clear_bit(R5_UPTODATE, &dev->flags);
3079 s->locked++;
3080 }
3081
600aa109 3082 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
e46b272b 3083 __func__, (unsigned long long)sh->sector,
600aa109 3084 s->locked, s->ops_request);
e33129d8 3085}
16a53ecc 3086
1da177e4
LT
3087/*
3088 * Each stripe/dev can have one or more bion attached.
16a53ecc 3089 * toread/towrite point to the first in a chain.
1da177e4
LT
3090 * The bi_next chain must be in order.
3091 */
da41ba65 3092static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3093 int forwrite, int previous)
1da177e4
LT
3094{
3095 struct bio **bip;
d1688a6d 3096 struct r5conf *conf = sh->raid_conf;
72626685 3097 int firstwrite=0;
1da177e4 3098
cbe47ec5 3099 pr_debug("adding bi b#%llu to stripe s#%llu\n",
4f024f37 3100 (unsigned long long)bi->bi_iter.bi_sector,
1da177e4
LT
3101 (unsigned long long)sh->sector);
3102
b17459c0
SL
3103 /*
3104 * If several bio share a stripe. The bio bi_phys_segments acts as a
3105 * reference count to avoid race. The reference count should already be
3106 * increased before this function is called (for example, in
849674e4 3107 * raid5_make_request()), so other bio sharing this stripe will not free the
b17459c0
SL
3108 * stripe. If a stripe is owned by one stripe, the stripe lock will
3109 * protect it.
3110 */
3111 spin_lock_irq(&sh->stripe_lock);
59fc630b 3112 /* Don't allow new IO added to stripes in batch list */
3113 if (sh->batch_head)
3114 goto overlap;
72626685 3115 if (forwrite) {
1da177e4 3116 bip = &sh->dev[dd_idx].towrite;
7eaf7e8e 3117 if (*bip == NULL)
72626685
N
3118 firstwrite = 1;
3119 } else
1da177e4 3120 bip = &sh->dev[dd_idx].toread;
4f024f37
KO
3121 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3122 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
1da177e4
LT
3123 goto overlap;
3124 bip = & (*bip)->bi_next;
3125 }
4f024f37 3126 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
1da177e4
LT
3127 goto overlap;
3128
da41ba65 3129 if (!forwrite || previous)
3130 clear_bit(STRIPE_BATCH_READY, &sh->state);
3131
78bafebd 3132 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1da177e4
LT
3133 if (*bip)
3134 bi->bi_next = *bip;
3135 *bip = bi;
e7836bd6 3136 raid5_inc_bi_active_stripes(bi);
72626685 3137
1da177e4
LT
3138 if (forwrite) {
3139 /* check if page is covered */
3140 sector_t sector = sh->dev[dd_idx].sector;
3141 for (bi=sh->dev[dd_idx].towrite;
3142 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
4f024f37 3143 bi && bi->bi_iter.bi_sector <= sector;
1da177e4 3144 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
f73a1c7d
KO
3145 if (bio_end_sector(bi) >= sector)
3146 sector = bio_end_sector(bi);
1da177e4
LT
3147 }
3148 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
7a87f434 3149 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3150 sh->overwrite_disks++;
1da177e4 3151 }
cbe47ec5
N
3152
3153 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
4f024f37 3154 (unsigned long long)(*bip)->bi_iter.bi_sector,
cbe47ec5
N
3155 (unsigned long long)sh->sector, dd_idx);
3156
3157 if (conf->mddev->bitmap && firstwrite) {
d0852df5
N
3158 /* Cannot hold spinlock over bitmap_startwrite,
3159 * but must ensure this isn't added to a batch until
3160 * we have added to the bitmap and set bm_seq.
3161 * So set STRIPE_BITMAP_PENDING to prevent
3162 * batching.
3163 * If multiple add_stripe_bio() calls race here they
3164 * much all set STRIPE_BITMAP_PENDING. So only the first one
3165 * to complete "bitmap_startwrite" gets to set
3166 * STRIPE_BIT_DELAY. This is important as once a stripe
3167 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3168 * any more.
3169 */
3170 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3171 spin_unlock_irq(&sh->stripe_lock);
cbe47ec5
N
3172 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3173 STRIPE_SECTORS, 0);
d0852df5
N
3174 spin_lock_irq(&sh->stripe_lock);
3175 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3176 if (!sh->batch_head) {
3177 sh->bm_seq = conf->seq_flush+1;
3178 set_bit(STRIPE_BIT_DELAY, &sh->state);
3179 }
cbe47ec5 3180 }
d0852df5 3181 spin_unlock_irq(&sh->stripe_lock);
59fc630b 3182
3183 if (stripe_can_batch(sh))
3184 stripe_add_to_batch_list(conf, sh);
1da177e4
LT
3185 return 1;
3186
3187 overlap:
3188 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
b17459c0 3189 spin_unlock_irq(&sh->stripe_lock);
1da177e4
LT
3190 return 0;
3191}
3192
d1688a6d 3193static void end_reshape(struct r5conf *conf);
29269553 3194
d1688a6d 3195static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
911d4ee8 3196 struct stripe_head *sh)
ccfcc3c1 3197{
784052ec 3198 int sectors_per_chunk =
09c9e5fa 3199 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
911d4ee8 3200 int dd_idx;
2d2063ce 3201 int chunk_offset = sector_div(stripe, sectors_per_chunk);
112bf897 3202 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2d2063ce 3203
112bf897
N
3204 raid5_compute_sector(conf,
3205 stripe * (disks - conf->max_degraded)
b875e531 3206 *sectors_per_chunk + chunk_offset,
112bf897 3207 previous,
911d4ee8 3208 &dd_idx, sh);
ccfcc3c1
N
3209}
3210
a4456856 3211static void
d1688a6d 3212handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
a4456856 3213 struct stripe_head_state *s, int disks,
34a6f80e 3214 struct bio_list *return_bi)
a4456856
DW
3215{
3216 int i;
59fc630b 3217 BUG_ON(sh->batch_head);
a4456856
DW
3218 for (i = disks; i--; ) {
3219 struct bio *bi;
3220 int bitmap_end = 0;
3221
3222 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
3cb03002 3223 struct md_rdev *rdev;
a4456856
DW
3224 rcu_read_lock();
3225 rdev = rcu_dereference(conf->disks[i].rdev);
f5b67ae8
N
3226 if (rdev && test_bit(In_sync, &rdev->flags) &&
3227 !test_bit(Faulty, &rdev->flags))
7f0da59b
N
3228 atomic_inc(&rdev->nr_pending);
3229 else
3230 rdev = NULL;
a4456856 3231 rcu_read_unlock();
7f0da59b
N
3232 if (rdev) {
3233 if (!rdev_set_badblocks(
3234 rdev,
3235 sh->sector,
3236 STRIPE_SECTORS, 0))
3237 md_error(conf->mddev, rdev);
3238 rdev_dec_pending(rdev, conf->mddev);
3239 }
a4456856 3240 }
b17459c0 3241 spin_lock_irq(&sh->stripe_lock);
a4456856
DW
3242 /* fail all writes first */
3243 bi = sh->dev[i].towrite;
3244 sh->dev[i].towrite = NULL;
7a87f434 3245 sh->overwrite_disks = 0;
b17459c0 3246 spin_unlock_irq(&sh->stripe_lock);
1ed850f3 3247 if (bi)
a4456856 3248 bitmap_end = 1;
a4456856 3249
0576b1c6
SL
3250 r5l_stripe_write_finished(sh);
3251
a4456856
DW
3252 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3253 wake_up(&conf->wait_for_overlap);
3254
4f024f37 3255 while (bi && bi->bi_iter.bi_sector <
a4456856
DW
3256 sh->dev[i].sector + STRIPE_SECTORS) {
3257 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
4246a0b6
CH
3258
3259 bi->bi_error = -EIO;
e7836bd6 3260 if (!raid5_dec_bi_active_stripes(bi)) {
a4456856 3261 md_write_end(conf->mddev);
34a6f80e 3262 bio_list_add(return_bi, bi);
a4456856
DW
3263 }
3264 bi = nextbi;
3265 }
7eaf7e8e
SL
3266 if (bitmap_end)
3267 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3268 STRIPE_SECTORS, 0, 0);
3269 bitmap_end = 0;
a4456856
DW
3270 /* and fail all 'written' */
3271 bi = sh->dev[i].written;
3272 sh->dev[i].written = NULL;
d592a996
SL
3273 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3274 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3275 sh->dev[i].page = sh->dev[i].orig_page;
3276 }
3277
a4456856 3278 if (bi) bitmap_end = 1;
4f024f37 3279 while (bi && bi->bi_iter.bi_sector <
a4456856
DW
3280 sh->dev[i].sector + STRIPE_SECTORS) {
3281 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
4246a0b6
CH
3282
3283 bi->bi_error = -EIO;
e7836bd6 3284 if (!raid5_dec_bi_active_stripes(bi)) {
a4456856 3285 md_write_end(conf->mddev);
34a6f80e 3286 bio_list_add(return_bi, bi);
a4456856
DW
3287 }
3288 bi = bi2;
3289 }
3290
b5e98d65
DW
3291 /* fail any reads if this device is non-operational and
3292 * the data has not reached the cache yet.
3293 */
3294 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
6e74a9cf 3295 s->failed > conf->max_degraded &&
b5e98d65
DW
3296 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3297 test_bit(R5_ReadError, &sh->dev[i].flags))) {
143c4d05 3298 spin_lock_irq(&sh->stripe_lock);
a4456856
DW
3299 bi = sh->dev[i].toread;
3300 sh->dev[i].toread = NULL;
143c4d05 3301 spin_unlock_irq(&sh->stripe_lock);
a4456856
DW
3302 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3303 wake_up(&conf->wait_for_overlap);
ebda780b
SL
3304 if (bi)
3305 s->to_read--;
4f024f37 3306 while (bi && bi->bi_iter.bi_sector <
a4456856
DW
3307 sh->dev[i].sector + STRIPE_SECTORS) {
3308 struct bio *nextbi =
3309 r5_next_bio(bi, sh->dev[i].sector);
4246a0b6
CH
3310
3311 bi->bi_error = -EIO;
34a6f80e
N
3312 if (!raid5_dec_bi_active_stripes(bi))
3313 bio_list_add(return_bi, bi);
a4456856
DW
3314 bi = nextbi;
3315 }
3316 }
a4456856
DW
3317 if (bitmap_end)
3318 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3319 STRIPE_SECTORS, 0, 0);
8cfa7b0f
N
3320 /* If we were in the middle of a write the parity block might
3321 * still be locked - so just clear all R5_LOCKED flags
3322 */
3323 clear_bit(R5_LOCKED, &sh->dev[i].flags);
a4456856 3324 }
ebda780b
SL
3325 s->to_write = 0;
3326 s->written = 0;
a4456856 3327
8b3e6cdc
DW
3328 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3329 if (atomic_dec_and_test(&conf->pending_full_writes))
3330 md_wakeup_thread(conf->mddev->thread);
a4456856
DW
3331}
3332
7f0da59b 3333static void
d1688a6d 3334handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
7f0da59b
N
3335 struct stripe_head_state *s)
3336{
3337 int abort = 0;
3338 int i;
3339
59fc630b 3340 BUG_ON(sh->batch_head);
7f0da59b 3341 clear_bit(STRIPE_SYNCING, &sh->state);
f8dfcffd
N
3342 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3343 wake_up(&conf->wait_for_overlap);
7f0da59b 3344 s->syncing = 0;
9a3e1101 3345 s->replacing = 0;
7f0da59b 3346 /* There is nothing more to do for sync/check/repair.
18b9837e
N
3347 * Don't even need to abort as that is handled elsewhere
3348 * if needed, and not always wanted e.g. if there is a known
3349 * bad block here.
9a3e1101 3350 * For recover/replace we need to record a bad block on all
7f0da59b
N
3351 * non-sync devices, or abort the recovery
3352 */
18b9837e
N
3353 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3354 /* During recovery devices cannot be removed, so
3355 * locking and refcounting of rdevs is not needed
3356 */
e50d3992 3357 rcu_read_lock();
18b9837e 3358 for (i = 0; i < conf->raid_disks; i++) {
e50d3992 3359 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
18b9837e
N
3360 if (rdev
3361 && !test_bit(Faulty, &rdev->flags)
3362 && !test_bit(In_sync, &rdev->flags)
3363 && !rdev_set_badblocks(rdev, sh->sector,
3364 STRIPE_SECTORS, 0))
3365 abort = 1;
e50d3992 3366 rdev = rcu_dereference(conf->disks[i].replacement);
18b9837e
N
3367 if (rdev
3368 && !test_bit(Faulty, &rdev->flags)
3369 && !test_bit(In_sync, &rdev->flags)
3370 && !rdev_set_badblocks(rdev, sh->sector,
3371 STRIPE_SECTORS, 0))
3372 abort = 1;
3373 }
e50d3992 3374 rcu_read_unlock();
18b9837e
N
3375 if (abort)
3376 conf->recovery_disabled =
3377 conf->mddev->recovery_disabled;
7f0da59b 3378 }
18b9837e 3379 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
7f0da59b
N
3380}
3381
9a3e1101
N
3382static int want_replace(struct stripe_head *sh, int disk_idx)
3383{
3384 struct md_rdev *rdev;
3385 int rv = 0;
3f232d6a
N
3386
3387 rcu_read_lock();
3388 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
9a3e1101
N
3389 if (rdev
3390 && !test_bit(Faulty, &rdev->flags)
3391 && !test_bit(In_sync, &rdev->flags)
3392 && (rdev->recovery_offset <= sh->sector
3393 || rdev->mddev->recovery_cp <= sh->sector))
3394 rv = 1;
3f232d6a 3395 rcu_read_unlock();
9a3e1101
N
3396 return rv;
3397}
3398
2c58f06e
N
3399static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3400 int disk_idx, int disks)
a4456856 3401{
5599becc 3402 struct r5dev *dev = &sh->dev[disk_idx];
f2b3b44d
N
3403 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3404 &sh->dev[s->failed_num[1]] };
ea664c82 3405 int i;
5599becc 3406
a79cfe12
N
3407
3408 if (test_bit(R5_LOCKED, &dev->flags) ||
3409 test_bit(R5_UPTODATE, &dev->flags))
3410 /* No point reading this as we already have it or have
3411 * decided to get it.
3412 */
3413 return 0;
3414
3415 if (dev->toread ||
3416 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3417 /* We need this block to directly satisfy a request */
3418 return 1;
3419
3420 if (s->syncing || s->expanding ||
3421 (s->replacing && want_replace(sh, disk_idx)))
3422 /* When syncing, or expanding we read everything.
3423 * When replacing, we need the replaced block.
3424 */
3425 return 1;
3426
3427 if ((s->failed >= 1 && fdev[0]->toread) ||
3428 (s->failed >= 2 && fdev[1]->toread))
3429 /* If we want to read from a failed device, then
3430 * we need to actually read every other device.
3431 */
3432 return 1;
3433
a9d56950
N
3434 /* Sometimes neither read-modify-write nor reconstruct-write
3435 * cycles can work. In those cases we read every block we
3436 * can. Then the parity-update is certain to have enough to
3437 * work with.
3438 * This can only be a problem when we need to write something,
3439 * and some device has failed. If either of those tests
3440 * fail we need look no further.
3441 */
3442 if (!s->failed || !s->to_write)
3443 return 0;
3444
3445 if (test_bit(R5_Insync, &dev->flags) &&
3446 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3447 /* Pre-reads at not permitted until after short delay
3448 * to gather multiple requests. However if this
3449 * device is no Insync, the block could only be be computed
3450 * and there is no need to delay that.
3451 */
3452 return 0;
ea664c82 3453
36707bb2 3454 for (i = 0; i < s->failed && i < 2; i++) {
ea664c82
N
3455 if (fdev[i]->towrite &&
3456 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3457 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3458 /* If we have a partial write to a failed
3459 * device, then we will need to reconstruct
3460 * the content of that device, so all other
3461 * devices must be read.
3462 */
3463 return 1;
3464 }
3465
3466 /* If we are forced to do a reconstruct-write, either because
3467 * the current RAID6 implementation only supports that, or
3468 * or because parity cannot be trusted and we are currently
3469 * recovering it, there is extra need to be careful.
3470 * If one of the devices that we would need to read, because
3471 * it is not being overwritten (and maybe not written at all)
3472 * is missing/faulty, then we need to read everything we can.
3473 */
3474 if (sh->raid_conf->level != 6 &&
3475 sh->sector < sh->raid_conf->mddev->recovery_cp)
3476 /* reconstruct-write isn't being forced */
3477 return 0;
36707bb2 3478 for (i = 0; i < s->failed && i < 2; i++) {
10d82c5f
N
3479 if (s->failed_num[i] != sh->pd_idx &&
3480 s->failed_num[i] != sh->qd_idx &&
3481 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
ea664c82
N
3482 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3483 return 1;
3484 }
3485
2c58f06e
N
3486 return 0;
3487}
3488
ba02684d
SL
3489/* fetch_block - checks the given member device to see if its data needs
3490 * to be read or computed to satisfy a request.
3491 *
3492 * Returns 1 when no more member devices need to be checked, otherwise returns
3493 * 0 to tell the loop in handle_stripe_fill to continue
3494 */
2c58f06e
N
3495static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3496 int disk_idx, int disks)
3497{
3498 struct r5dev *dev = &sh->dev[disk_idx];
3499
3500 /* is the data in this block needed, and can we get it? */
3501 if (need_this_block(sh, s, disk_idx, disks)) {
5599becc
YT
3502 /* we would like to get this block, possibly by computing it,
3503 * otherwise read it if the backing disk is insync
3504 */
3505 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3506 BUG_ON(test_bit(R5_Wantread, &dev->flags));
b0c783b3 3507 BUG_ON(sh->batch_head);
5599becc 3508 if ((s->uptodate == disks - 1) &&
f2b3b44d
N
3509 (s->failed && (disk_idx == s->failed_num[0] ||
3510 disk_idx == s->failed_num[1]))) {
5599becc
YT
3511 /* have disk failed, and we're requested to fetch it;
3512 * do compute it
a4456856 3513 */
5599becc
YT
3514 pr_debug("Computing stripe %llu block %d\n",
3515 (unsigned long long)sh->sector, disk_idx);
3516 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3517 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3518 set_bit(R5_Wantcompute, &dev->flags);
3519 sh->ops.target = disk_idx;
3520 sh->ops.target2 = -1; /* no 2nd target */
3521 s->req_compute = 1;
93b3dbce
N
3522 /* Careful: from this point on 'uptodate' is in the eye
3523 * of raid_run_ops which services 'compute' operations
3524 * before writes. R5_Wantcompute flags a block that will
3525 * be R5_UPTODATE by the time it is needed for a
3526 * subsequent operation.
3527 */
5599becc
YT
3528 s->uptodate++;
3529 return 1;
3530 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3531 /* Computing 2-failure is *very* expensive; only
3532 * do it if failed >= 2
3533 */
3534 int other;
3535 for (other = disks; other--; ) {
3536 if (other == disk_idx)
3537 continue;
3538 if (!test_bit(R5_UPTODATE,
3539 &sh->dev[other].flags))
3540 break;
a4456856 3541 }
5599becc
YT
3542 BUG_ON(other < 0);
3543 pr_debug("Computing stripe %llu blocks %d,%d\n",
3544 (unsigned long long)sh->sector,
3545 disk_idx, other);
3546 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3547 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3548 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3549 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3550 sh->ops.target = disk_idx;
3551 sh->ops.target2 = other;
3552 s->uptodate += 2;
3553 s->req_compute = 1;
3554 return 1;
3555 } else if (test_bit(R5_Insync, &dev->flags)) {
3556 set_bit(R5_LOCKED, &dev->flags);
3557 set_bit(R5_Wantread, &dev->flags);
3558 s->locked++;
3559 pr_debug("Reading block %d (sync=%d)\n",
3560 disk_idx, s->syncing);
a4456856
DW
3561 }
3562 }
5599becc
YT
3563
3564 return 0;
3565}
3566
3567/**
93b3dbce 3568 * handle_stripe_fill - read or compute data to satisfy pending requests.
5599becc 3569 */
93b3dbce
N
3570static void handle_stripe_fill(struct stripe_head *sh,
3571 struct stripe_head_state *s,
3572 int disks)
5599becc
YT
3573{
3574 int i;
3575
3576 /* look for blocks to read/compute, skip this if a compute
3577 * is already in flight, or if the stripe contents are in the
3578 * midst of changing due to a write
3579 */
3580 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
07e83364
SL
3581 !sh->reconstruct_state) {
3582
3583 /*
3584 * For degraded stripe with data in journal, do not handle
3585 * read requests yet, instead, flush the stripe to raid
3586 * disks first, this avoids handling complex rmw of write
3587 * back cache (prexor with orig_page, and then xor with
3588 * page) in the read path
3589 */
3590 if (s->injournal && s->failed) {
3591 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3592 r5c_make_stripe_write_out(sh);
3593 goto out;
3594 }
3595
5599becc 3596 for (i = disks; i--; )
93b3dbce 3597 if (fetch_block(sh, s, i, disks))
5599becc 3598 break;
07e83364
SL
3599 }
3600out:
a4456856
DW
3601 set_bit(STRIPE_HANDLE, &sh->state);
3602}
3603
787b76fa
N
3604static void break_stripe_batch_list(struct stripe_head *head_sh,
3605 unsigned long handle_flags);
1fe797e6 3606/* handle_stripe_clean_event
a4456856
DW
3607 * any written block on an uptodate or failed drive can be returned.
3608 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3609 * never LOCKED, so we don't need to test 'failed' directly.
3610 */
d1688a6d 3611static void handle_stripe_clean_event(struct r5conf *conf,
34a6f80e 3612 struct stripe_head *sh, int disks, struct bio_list *return_bi)
a4456856
DW
3613{
3614 int i;
3615 struct r5dev *dev;
f8dfcffd 3616 int discard_pending = 0;
59fc630b 3617 struct stripe_head *head_sh = sh;
3618 bool do_endio = false;
a4456856
DW
3619
3620 for (i = disks; i--; )
3621 if (sh->dev[i].written) {
3622 dev = &sh->dev[i];
3623 if (!test_bit(R5_LOCKED, &dev->flags) &&
9e444768 3624 (test_bit(R5_UPTODATE, &dev->flags) ||
d592a996
SL
3625 test_bit(R5_Discard, &dev->flags) ||
3626 test_bit(R5_SkipCopy, &dev->flags))) {
a4456856
DW
3627 /* We can return any write requests */
3628 struct bio *wbi, *wbi2;
45b4233c 3629 pr_debug("Return write for disc %d\n", i);
ca64cae9
N
3630 if (test_and_clear_bit(R5_Discard, &dev->flags))
3631 clear_bit(R5_UPTODATE, &dev->flags);
d592a996
SL
3632 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3633 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
d592a996 3634 }
59fc630b 3635 do_endio = true;
3636
3637returnbi:
3638 dev->page = dev->orig_page;
a4456856
DW
3639 wbi = dev->written;
3640 dev->written = NULL;
4f024f37 3641 while (wbi && wbi->bi_iter.bi_sector <
a4456856
DW
3642 dev->sector + STRIPE_SECTORS) {
3643 wbi2 = r5_next_bio(wbi, dev->sector);
e7836bd6 3644 if (!raid5_dec_bi_active_stripes(wbi)) {
a4456856 3645 md_write_end(conf->mddev);
34a6f80e 3646 bio_list_add(return_bi, wbi);
a4456856
DW
3647 }
3648 wbi = wbi2;
3649 }
7eaf7e8e
SL
3650 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3651 STRIPE_SECTORS,
a4456856 3652 !test_bit(STRIPE_DEGRADED, &sh->state),
7eaf7e8e 3653 0);
59fc630b 3654 if (head_sh->batch_head) {
3655 sh = list_first_entry(&sh->batch_list,
3656 struct stripe_head,
3657 batch_list);
3658 if (sh != head_sh) {
3659 dev = &sh->dev[i];
3660 goto returnbi;
3661 }
3662 }
3663 sh = head_sh;
3664 dev = &sh->dev[i];
f8dfcffd
N
3665 } else if (test_bit(R5_Discard, &dev->flags))
3666 discard_pending = 1;
3667 }
f6bed0ef 3668
0576b1c6
SL
3669 r5l_stripe_write_finished(sh);
3670
f8dfcffd
N
3671 if (!discard_pending &&
3672 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
b8a9d66d 3673 int hash;
f8dfcffd
N
3674 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3675 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3676 if (sh->qd_idx >= 0) {
3677 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3678 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3679 }
3680 /* now that discard is done we can proceed with any sync */
3681 clear_bit(STRIPE_DISCARD, &sh->state);
d47648fc
SL
3682 /*
3683 * SCSI discard will change some bio fields and the stripe has
3684 * no updated data, so remove it from hash list and the stripe
3685 * will be reinitialized
3686 */
59fc630b 3687unhash:
b8a9d66d
RG
3688 hash = sh->hash_lock_index;
3689 spin_lock_irq(conf->hash_locks + hash);
d47648fc 3690 remove_hash(sh);
b8a9d66d 3691 spin_unlock_irq(conf->hash_locks + hash);
59fc630b 3692 if (head_sh->batch_head) {
3693 sh = list_first_entry(&sh->batch_list,
3694 struct stripe_head, batch_list);
3695 if (sh != head_sh)
3696 goto unhash;
3697 }
59fc630b 3698 sh = head_sh;
3699
f8dfcffd
N
3700 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3701 set_bit(STRIPE_HANDLE, &sh->state);
3702
3703 }
8b3e6cdc
DW
3704
3705 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3706 if (atomic_dec_and_test(&conf->pending_full_writes))
3707 md_wakeup_thread(conf->mddev->thread);
59fc630b 3708
787b76fa
N
3709 if (head_sh->batch_head && do_endio)
3710 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
a4456856
DW
3711}
3712
86aa1397
SL
3713/*
3714 * For RMW in write back cache, we need extra page in prexor to store the
3715 * old data. This page is stored in dev->orig_page.
3716 *
3717 * This function checks whether we have data for prexor. The exact logic
3718 * is:
3719 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3720 */
3721static inline bool uptodate_for_rmw(struct r5dev *dev)
3722{
3723 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3724 (!test_bit(R5_InJournal, &dev->flags) ||
3725 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3726}
3727
d7bd398e
SL
3728static int handle_stripe_dirtying(struct r5conf *conf,
3729 struct stripe_head *sh,
3730 struct stripe_head_state *s,
3731 int disks)
a4456856
DW
3732{
3733 int rmw = 0, rcw = 0, i;
a7854487
AL
3734 sector_t recovery_cp = conf->mddev->recovery_cp;
3735
584acdd4 3736 /* Check whether resync is now happening or should start.
a7854487
AL
3737 * If yes, then the array is dirty (after unclean shutdown or
3738 * initial creation), so parity in some stripes might be inconsistent.
3739 * In this case, we need to always do reconstruct-write, to ensure
3740 * that in case of drive failure or read-error correction, we
3741 * generate correct data from the parity.
3742 */
584acdd4 3743 if (conf->rmw_level == PARITY_DISABLE_RMW ||
26ac1073
N
3744 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3745 s->failed == 0)) {
a7854487 3746 /* Calculate the real rcw later - for now make it
c8ac1803
N
3747 * look like rcw is cheaper
3748 */
3749 rcw = 1; rmw = 2;
584acdd4
MS
3750 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3751 conf->rmw_level, (unsigned long long)recovery_cp,
a7854487 3752 (unsigned long long)sh->sector);
c8ac1803 3753 } else for (i = disks; i--; ) {
a4456856
DW
3754 /* would I have to read this buffer for read_modify_write */
3755 struct r5dev *dev = &sh->dev[i];
39b99586 3756 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
07e83364 3757 i == sh->pd_idx || i == sh->qd_idx ||
1e6d690b 3758 test_bit(R5_InJournal, &dev->flags)) &&
a4456856 3759 !test_bit(R5_LOCKED, &dev->flags) &&
86aa1397 3760 !(uptodate_for_rmw(dev) ||
f38e1219 3761 test_bit(R5_Wantcompute, &dev->flags))) {
a4456856
DW
3762 if (test_bit(R5_Insync, &dev->flags))
3763 rmw++;
3764 else
3765 rmw += 2*disks; /* cannot read it */
3766 }
3767 /* Would I have to read this buffer for reconstruct_write */
584acdd4
MS
3768 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3769 i != sh->pd_idx && i != sh->qd_idx &&
a4456856 3770 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219 3771 !(test_bit(R5_UPTODATE, &dev->flags) ||
1e6d690b 3772 test_bit(R5_Wantcompute, &dev->flags))) {
67f45548
N
3773 if (test_bit(R5_Insync, &dev->flags))
3774 rcw++;
a4456856
DW
3775 else
3776 rcw += 2*disks;
3777 }
3778 }
1e6d690b 3779
39b99586
SL
3780 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3781 (unsigned long long)sh->sector, sh->state, rmw, rcw);
a4456856 3782 set_bit(STRIPE_HANDLE, &sh->state);
41257580 3783 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
a4456856 3784 /* prefer read-modify-write, but need to get some data */
e3620a3a
JB
3785 if (conf->mddev->queue)
3786 blk_add_trace_msg(conf->mddev->queue,
3787 "raid5 rmw %llu %d",
3788 (unsigned long long)sh->sector, rmw);
a4456856
DW
3789 for (i = disks; i--; ) {
3790 struct r5dev *dev = &sh->dev[i];
1e6d690b
SL
3791 if (test_bit(R5_InJournal, &dev->flags) &&
3792 dev->page == dev->orig_page &&
3793 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3794 /* alloc page for prexor */
d7bd398e
SL
3795 struct page *p = alloc_page(GFP_NOIO);
3796
3797 if (p) {
3798 dev->orig_page = p;
3799 continue;
3800 }
3801
3802 /*
3803 * alloc_page() failed, try use
3804 * disk_info->extra_page
3805 */
3806 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3807 &conf->cache_state)) {
3808 r5c_use_extra_page(sh);
3809 break;
3810 }
1e6d690b 3811
d7bd398e
SL
3812 /* extra_page in use, add to delayed_list */
3813 set_bit(STRIPE_DELAYED, &sh->state);
3814 s->waiting_extra_page = 1;
3815 return -EAGAIN;
1e6d690b 3816 }
d7bd398e 3817 }
1e6d690b 3818
d7bd398e
SL
3819 for (i = disks; i--; ) {
3820 struct r5dev *dev = &sh->dev[i];
39b99586 3821 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
1e6d690b
SL
3822 i == sh->pd_idx || i == sh->qd_idx ||
3823 test_bit(R5_InJournal, &dev->flags)) &&
a4456856 3824 !test_bit(R5_LOCKED, &dev->flags) &&
86aa1397 3825 !(uptodate_for_rmw(dev) ||
1e6d690b 3826 test_bit(R5_Wantcompute, &dev->flags)) &&
a4456856 3827 test_bit(R5_Insync, &dev->flags)) {
67f45548
N
3828 if (test_bit(STRIPE_PREREAD_ACTIVE,
3829 &sh->state)) {
3830 pr_debug("Read_old block %d for r-m-w\n",
3831 i);
a4456856
DW
3832 set_bit(R5_LOCKED, &dev->flags);
3833 set_bit(R5_Wantread, &dev->flags);
3834 s->locked++;
3835 } else {
3836 set_bit(STRIPE_DELAYED, &sh->state);
3837 set_bit(STRIPE_HANDLE, &sh->state);
3838 }
3839 }
3840 }
a9add5d9 3841 }
41257580 3842 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
a4456856 3843 /* want reconstruct write, but need to get some data */
a9add5d9 3844 int qread =0;
c8ac1803 3845 rcw = 0;
a4456856
DW
3846 for (i = disks; i--; ) {
3847 struct r5dev *dev = &sh->dev[i];
3848 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
c8ac1803 3849 i != sh->pd_idx && i != sh->qd_idx &&
a4456856 3850 !test_bit(R5_LOCKED, &dev->flags) &&
f38e1219 3851 !(test_bit(R5_UPTODATE, &dev->flags) ||
c8ac1803
N
3852 test_bit(R5_Wantcompute, &dev->flags))) {
3853 rcw++;
67f45548
N
3854 if (test_bit(R5_Insync, &dev->flags) &&
3855 test_bit(STRIPE_PREREAD_ACTIVE,
3856 &sh->state)) {
45b4233c 3857 pr_debug("Read_old block "
a4456856
DW
3858 "%d for Reconstruct\n", i);
3859 set_bit(R5_LOCKED, &dev->flags);
3860 set_bit(R5_Wantread, &dev->flags);
3861 s->locked++;
a9add5d9 3862 qread++;
a4456856
DW
3863 } else {
3864 set_bit(STRIPE_DELAYED, &sh->state);
3865 set_bit(STRIPE_HANDLE, &sh->state);
3866 }
3867 }
3868 }
e3620a3a 3869 if (rcw && conf->mddev->queue)
a9add5d9
N
3870 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3871 (unsigned long long)sh->sector,
3872 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
c8ac1803 3873 }
b1b02fe9
N
3874
3875 if (rcw > disks && rmw > disks &&
3876 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3877 set_bit(STRIPE_DELAYED, &sh->state);
3878
a4456856
DW
3879 /* now if nothing is locked, and if we have enough data,
3880 * we can start a write request
3881 */
f38e1219
DW
3882 /* since handle_stripe can be called at any time we need to handle the
3883 * case where a compute block operation has been submitted and then a
ac6b53b6
DW
3884 * subsequent call wants to start a write request. raid_run_ops only
3885 * handles the case where compute block and reconstruct are requested
f38e1219
DW
3886 * simultaneously. If this is not the case then new writes need to be
3887 * held off until the compute completes.
3888 */
976ea8d4
DW
3889 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3890 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
1e6d690b 3891 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
c0f7bddb 3892 schedule_reconstruction(sh, s, rcw == 0, 0);
d7bd398e 3893 return 0;
a4456856
DW
3894}
3895
d1688a6d 3896static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
a4456856
DW
3897 struct stripe_head_state *s, int disks)
3898{
ecc65c9b 3899 struct r5dev *dev = NULL;
bd2ab670 3900
59fc630b 3901 BUG_ON(sh->batch_head);
a4456856 3902 set_bit(STRIPE_HANDLE, &sh->state);
e89f8962 3903
ecc65c9b
DW
3904 switch (sh->check_state) {
3905 case check_state_idle:
3906 /* start a new check operation if there are no failures */
bd2ab670 3907 if (s->failed == 0) {
bd2ab670 3908 BUG_ON(s->uptodate != disks);
ecc65c9b
DW
3909 sh->check_state = check_state_run;
3910 set_bit(STRIPE_OP_CHECK, &s->ops_request);
bd2ab670 3911 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
bd2ab670 3912 s->uptodate--;
ecc65c9b 3913 break;
bd2ab670 3914 }
f2b3b44d 3915 dev = &sh->dev[s->failed_num[0]];
ecc65c9b
DW
3916 /* fall through */
3917 case check_state_compute_result:
3918 sh->check_state = check_state_idle;
3919 if (!dev)
3920 dev = &sh->dev[sh->pd_idx];
3921
3922 /* check that a write has not made the stripe insync */
3923 if (test_bit(STRIPE_INSYNC, &sh->state))
3924 break;
c8894419 3925
a4456856 3926 /* either failed parity check, or recovery is happening */
a4456856
DW
3927 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3928 BUG_ON(s->uptodate != disks);
3929
3930 set_bit(R5_LOCKED, &dev->flags);
ecc65c9b 3931 s->locked++;
a4456856 3932 set_bit(R5_Wantwrite, &dev->flags);
830ea016 3933
a4456856 3934 clear_bit(STRIPE_DEGRADED, &sh->state);
a4456856 3935 set_bit(STRIPE_INSYNC, &sh->state);
ecc65c9b
DW
3936 break;
3937 case check_state_run:
3938 break; /* we will be called again upon completion */
3939 case check_state_check_result:
3940 sh->check_state = check_state_idle;
3941
3942 /* if a failure occurred during the check operation, leave
3943 * STRIPE_INSYNC not set and let the stripe be handled again
3944 */
3945 if (s->failed)
3946 break;
3947
3948 /* handle a successful check operation, if parity is correct
3949 * we are done. Otherwise update the mismatch count and repair
3950 * parity if !MD_RECOVERY_CHECK
3951 */
ad283ea4 3952 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
ecc65c9b
DW
3953 /* parity is correct (on disc,
3954 * not in buffer any more)
3955 */
3956 set_bit(STRIPE_INSYNC, &sh->state);
3957 else {
7f7583d4 3958 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
ecc65c9b
DW
3959 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3960 /* don't try to repair!! */
3961 set_bit(STRIPE_INSYNC, &sh->state);
3962 else {
3963 sh->check_state = check_state_compute_run;
976ea8d4 3964 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
ecc65c9b
DW
3965 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3966 set_bit(R5_Wantcompute,
3967 &sh->dev[sh->pd_idx].flags);
3968 sh->ops.target = sh->pd_idx;
ac6b53b6 3969 sh->ops.target2 = -1;
ecc65c9b
DW
3970 s->uptodate++;
3971 }
3972 }
3973 break;
3974 case check_state_compute_run:
3975 break;
3976 default:
cc6167b4 3977 pr_err("%s: unknown check_state: %d sector: %llu\n",
ecc65c9b
DW
3978 __func__, sh->check_state,
3979 (unsigned long long) sh->sector);
3980 BUG();
a4456856
DW
3981 }
3982}
3983
d1688a6d 3984static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
36d1c647 3985 struct stripe_head_state *s,
f2b3b44d 3986 int disks)
a4456856 3987{
a4456856 3988 int pd_idx = sh->pd_idx;
34e04e87 3989 int qd_idx = sh->qd_idx;
d82dfee0 3990 struct r5dev *dev;
a4456856 3991
59fc630b 3992 BUG_ON(sh->batch_head);
a4456856
DW
3993 set_bit(STRIPE_HANDLE, &sh->state);
3994
3995 BUG_ON(s->failed > 2);
d82dfee0 3996
a4456856
DW
3997 /* Want to check and possibly repair P and Q.
3998 * However there could be one 'failed' device, in which
3999 * case we can only check one of them, possibly using the
4000 * other to generate missing data
4001 */
4002
d82dfee0
DW
4003 switch (sh->check_state) {
4004 case check_state_idle:
4005 /* start a new check operation if there are < 2 failures */
f2b3b44d 4006 if (s->failed == s->q_failed) {
d82dfee0 4007 /* The only possible failed device holds Q, so it
a4456856
DW
4008 * makes sense to check P (If anything else were failed,
4009 * we would have used P to recreate it).
4010 */
d82dfee0 4011 sh->check_state = check_state_run;
a4456856 4012 }
f2b3b44d 4013 if (!s->q_failed && s->failed < 2) {
d82dfee0 4014 /* Q is not failed, and we didn't use it to generate
a4456856
DW
4015 * anything, so it makes sense to check it
4016 */
d82dfee0
DW
4017 if (sh->check_state == check_state_run)
4018 sh->check_state = check_state_run_pq;
4019 else
4020 sh->check_state = check_state_run_q;
a4456856 4021 }
a4456856 4022
d82dfee0
DW
4023 /* discard potentially stale zero_sum_result */
4024 sh->ops.zero_sum_result = 0;
a4456856 4025
d82dfee0
DW
4026 if (sh->check_state == check_state_run) {
4027 /* async_xor_zero_sum destroys the contents of P */
4028 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4029 s->uptodate--;
a4456856 4030 }
d82dfee0
DW
4031 if (sh->check_state >= check_state_run &&
4032 sh->check_state <= check_state_run_pq) {
4033 /* async_syndrome_zero_sum preserves P and Q, so
4034 * no need to mark them !uptodate here
4035 */
4036 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4037 break;
a4456856
DW
4038 }
4039
d82dfee0
DW
4040 /* we have 2-disk failure */
4041 BUG_ON(s->failed != 2);
4042 /* fall through */
4043 case check_state_compute_result:
4044 sh->check_state = check_state_idle;
a4456856 4045
d82dfee0
DW
4046 /* check that a write has not made the stripe insync */
4047 if (test_bit(STRIPE_INSYNC, &sh->state))
4048 break;
a4456856
DW
4049
4050 /* now write out any block on a failed drive,
d82dfee0 4051 * or P or Q if they were recomputed
a4456856 4052 */
d82dfee0 4053 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
a4456856 4054 if (s->failed == 2) {
f2b3b44d 4055 dev = &sh->dev[s->failed_num[1]];
a4456856
DW
4056 s->locked++;
4057 set_bit(R5_LOCKED, &dev->flags);
4058 set_bit(R5_Wantwrite, &dev->flags);
4059 }
4060 if (s->failed >= 1) {
f2b3b44d 4061 dev = &sh->dev[s->failed_num[0]];
a4456856
DW
4062 s->locked++;
4063 set_bit(R5_LOCKED, &dev->flags);
4064 set_bit(R5_Wantwrite, &dev->flags);
4065 }
d82dfee0 4066 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
a4456856
DW
4067 dev = &sh->dev[pd_idx];
4068 s->locked++;
4069 set_bit(R5_LOCKED, &dev->flags);
4070 set_bit(R5_Wantwrite, &dev->flags);
4071 }
d82dfee0 4072 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
a4456856
DW
4073 dev = &sh->dev[qd_idx];
4074 s->locked++;
4075 set_bit(R5_LOCKED, &dev->flags);
4076 set_bit(R5_Wantwrite, &dev->flags);
4077 }
4078 clear_bit(STRIPE_DEGRADED, &sh->state);
4079
4080 set_bit(STRIPE_INSYNC, &sh->state);
d82dfee0
DW
4081 break;
4082 case check_state_run:
4083 case check_state_run_q:
4084 case check_state_run_pq:
4085 break; /* we will be called again upon completion */
4086 case check_state_check_result:
4087 sh->check_state = check_state_idle;
4088
4089 /* handle a successful check operation, if parity is correct
4090 * we are done. Otherwise update the mismatch count and repair
4091 * parity if !MD_RECOVERY_CHECK
4092 */
4093 if (sh->ops.zero_sum_result == 0) {
4094 /* both parities are correct */
4095 if (!s->failed)
4096 set_bit(STRIPE_INSYNC, &sh->state);
4097 else {
4098 /* in contrast to the raid5 case we can validate
4099 * parity, but still have a failure to write
4100 * back
4101 */
4102 sh->check_state = check_state_compute_result;
4103 /* Returning at this point means that we may go
4104 * off and bring p and/or q uptodate again so
4105 * we make sure to check zero_sum_result again
4106 * to verify if p or q need writeback
4107 */
4108 }
4109 } else {
7f7583d4 4110 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
d82dfee0
DW
4111 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4112 /* don't try to repair!! */
4113 set_bit(STRIPE_INSYNC, &sh->state);
4114 else {
4115 int *target = &sh->ops.target;
4116
4117 sh->ops.target = -1;
4118 sh->ops.target2 = -1;
4119 sh->check_state = check_state_compute_run;
4120 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4121 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4122 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4123 set_bit(R5_Wantcompute,
4124 &sh->dev[pd_idx].flags);
4125 *target = pd_idx;
4126 target = &sh->ops.target2;
4127 s->uptodate++;
4128 }
4129 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4130 set_bit(R5_Wantcompute,
4131 &sh->dev[qd_idx].flags);
4132 *target = qd_idx;
4133 s->uptodate++;
4134 }
4135 }
4136 }
4137 break;
4138 case check_state_compute_run:
4139 break;
4140 default:
cc6167b4
N
4141 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4142 __func__, sh->check_state,
4143 (unsigned long long) sh->sector);
d82dfee0 4144 BUG();
a4456856
DW
4145 }
4146}
4147
d1688a6d 4148static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
a4456856
DW
4149{
4150 int i;
4151
4152 /* We have read all the blocks in this stripe and now we need to
4153 * copy some of them into a target stripe for expand.
4154 */
f0a50d37 4155 struct dma_async_tx_descriptor *tx = NULL;
59fc630b 4156 BUG_ON(sh->batch_head);
a4456856
DW
4157 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4158 for (i = 0; i < sh->disks; i++)
34e04e87 4159 if (i != sh->pd_idx && i != sh->qd_idx) {
911d4ee8 4160 int dd_idx, j;
a4456856 4161 struct stripe_head *sh2;
a08abd8c 4162 struct async_submit_ctl submit;
a4456856 4163
6d036f7d 4164 sector_t bn = raid5_compute_blocknr(sh, i, 1);
911d4ee8
N
4165 sector_t s = raid5_compute_sector(conf, bn, 0,
4166 &dd_idx, NULL);
6d036f7d 4167 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
a4456856
DW
4168 if (sh2 == NULL)
4169 /* so far only the early blocks of this stripe
4170 * have been requested. When later blocks
4171 * get requested, we will try again
4172 */
4173 continue;
4174 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4175 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4176 /* must have already done this block */
6d036f7d 4177 raid5_release_stripe(sh2);
a4456856
DW
4178 continue;
4179 }
f0a50d37
DW
4180
4181 /* place all the copies on one channel */
a08abd8c 4182 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
f0a50d37 4183 tx = async_memcpy(sh2->dev[dd_idx].page,
88ba2aa5 4184 sh->dev[i].page, 0, 0, STRIPE_SIZE,
a08abd8c 4185 &submit);
f0a50d37 4186
a4456856
DW
4187 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4188 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4189 for (j = 0; j < conf->raid_disks; j++)
4190 if (j != sh2->pd_idx &&
86c374ba 4191 j != sh2->qd_idx &&
a4456856
DW
4192 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4193 break;
4194 if (j == conf->raid_disks) {
4195 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4196 set_bit(STRIPE_HANDLE, &sh2->state);
4197 }
6d036f7d 4198 raid5_release_stripe(sh2);
f0a50d37 4199
a4456856 4200 }
a2e08551 4201 /* done submitting copies, wait for them to complete */
749586b7 4202 async_tx_quiesce(&tx);
a4456856 4203}
1da177e4
LT
4204
4205/*
4206 * handle_stripe - do things to a stripe.
4207 *
9a3e1101
N
4208 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4209 * state of various bits to see what needs to be done.
1da177e4 4210 * Possible results:
9a3e1101
N
4211 * return some read requests which now have data
4212 * return some write requests which are safely on storage
1da177e4
LT
4213 * schedule a read on some buffers
4214 * schedule a write of some buffers
4215 * return confirmation of parity correctness
4216 *
1da177e4 4217 */
a4456856 4218
acfe726b 4219static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
1da177e4 4220{
d1688a6d 4221 struct r5conf *conf = sh->raid_conf;
f416885e 4222 int disks = sh->disks;
474af965
N
4223 struct r5dev *dev;
4224 int i;
9a3e1101 4225 int do_recovery = 0;
1da177e4 4226
acfe726b
N
4227 memset(s, 0, sizeof(*s));
4228
dabc4ec6 4229 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4230 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
acfe726b
N
4231 s->failed_num[0] = -1;
4232 s->failed_num[1] = -1;
6e74a9cf 4233 s->log_failed = r5l_log_disk_error(conf);
1da177e4 4234
acfe726b 4235 /* Now to look around and see what can be done */
1da177e4 4236 rcu_read_lock();
16a53ecc 4237 for (i=disks; i--; ) {
3cb03002 4238 struct md_rdev *rdev;
31c176ec
N
4239 sector_t first_bad;
4240 int bad_sectors;
4241 int is_bad = 0;
acfe726b 4242
16a53ecc 4243 dev = &sh->dev[i];
1da177e4 4244
45b4233c 4245 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
9a3e1101
N
4246 i, dev->flags,
4247 dev->toread, dev->towrite, dev->written);
6c0069c0
YT
4248 /* maybe we can reply to a read
4249 *
4250 * new wantfill requests are only permitted while
4251 * ops_complete_biofill is guaranteed to be inactive
4252 */
4253 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4254 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4255 set_bit(R5_Wantfill, &dev->flags);
1da177e4 4256
16a53ecc 4257 /* now count some things */
cc94015a
N
4258 if (test_bit(R5_LOCKED, &dev->flags))
4259 s->locked++;
4260 if (test_bit(R5_UPTODATE, &dev->flags))
4261 s->uptodate++;
2d6e4ecc 4262 if (test_bit(R5_Wantcompute, &dev->flags)) {
cc94015a
N
4263 s->compute++;
4264 BUG_ON(s->compute > 2);
2d6e4ecc 4265 }
1da177e4 4266
acfe726b 4267 if (test_bit(R5_Wantfill, &dev->flags))
cc94015a 4268 s->to_fill++;
acfe726b 4269 else if (dev->toread)
cc94015a 4270 s->to_read++;
16a53ecc 4271 if (dev->towrite) {
cc94015a 4272 s->to_write++;
16a53ecc 4273 if (!test_bit(R5_OVERWRITE, &dev->flags))
cc94015a 4274 s->non_overwrite++;
16a53ecc 4275 }
a4456856 4276 if (dev->written)
cc94015a 4277 s->written++;
14a75d3e
N
4278 /* Prefer to use the replacement for reads, but only
4279 * if it is recovered enough and has no bad blocks.
4280 */
4281 rdev = rcu_dereference(conf->disks[i].replacement);
4282 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4283 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4284 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4285 &first_bad, &bad_sectors))
4286 set_bit(R5_ReadRepl, &dev->flags);
4287 else {
e6030cb0 4288 if (rdev && !test_bit(Faulty, &rdev->flags))
9a3e1101 4289 set_bit(R5_NeedReplace, &dev->flags);
e6030cb0
N
4290 else
4291 clear_bit(R5_NeedReplace, &dev->flags);
14a75d3e
N
4292 rdev = rcu_dereference(conf->disks[i].rdev);
4293 clear_bit(R5_ReadRepl, &dev->flags);
4294 }
9283d8c5
N
4295 if (rdev && test_bit(Faulty, &rdev->flags))
4296 rdev = NULL;
31c176ec
N
4297 if (rdev) {
4298 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4299 &first_bad, &bad_sectors);
4300 if (s->blocked_rdev == NULL
4301 && (test_bit(Blocked, &rdev->flags)
4302 || is_bad < 0)) {
4303 if (is_bad < 0)
4304 set_bit(BlockedBadBlocks,
4305 &rdev->flags);
4306 s->blocked_rdev = rdev;
4307 atomic_inc(&rdev->nr_pending);
4308 }
6bfe0b49 4309 }
415e72d0
N
4310 clear_bit(R5_Insync, &dev->flags);
4311 if (!rdev)
4312 /* Not in-sync */;
31c176ec
N
4313 else if (is_bad) {
4314 /* also not in-sync */
18b9837e
N
4315 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4316 test_bit(R5_UPTODATE, &dev->flags)) {
31c176ec
N
4317 /* treat as in-sync, but with a read error
4318 * which we can now try to correct
4319 */
4320 set_bit(R5_Insync, &dev->flags);
4321 set_bit(R5_ReadError, &dev->flags);
4322 }
4323 } else if (test_bit(In_sync, &rdev->flags))
415e72d0 4324 set_bit(R5_Insync, &dev->flags);
30d7a483 4325 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
415e72d0 4326 /* in sync if before recovery_offset */
30d7a483
N
4327 set_bit(R5_Insync, &dev->flags);
4328 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4329 test_bit(R5_Expanded, &dev->flags))
4330 /* If we've reshaped into here, we assume it is Insync.
4331 * We will shortly update recovery_offset to make
4332 * it official.
4333 */
4334 set_bit(R5_Insync, &dev->flags);
4335
1cc03eb9 4336 if (test_bit(R5_WriteError, &dev->flags)) {
14a75d3e
N
4337 /* This flag does not apply to '.replacement'
4338 * only to .rdev, so make sure to check that*/
4339 struct md_rdev *rdev2 = rcu_dereference(
4340 conf->disks[i].rdev);
4341 if (rdev2 == rdev)
4342 clear_bit(R5_Insync, &dev->flags);
4343 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
bc2607f3 4344 s->handle_bad_blocks = 1;
14a75d3e 4345 atomic_inc(&rdev2->nr_pending);
bc2607f3
N
4346 } else
4347 clear_bit(R5_WriteError, &dev->flags);
4348 }
1cc03eb9 4349 if (test_bit(R5_MadeGood, &dev->flags)) {
14a75d3e
N
4350 /* This flag does not apply to '.replacement'
4351 * only to .rdev, so make sure to check that*/
4352 struct md_rdev *rdev2 = rcu_dereference(
4353 conf->disks[i].rdev);
4354 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
b84db560 4355 s->handle_bad_blocks = 1;
14a75d3e 4356 atomic_inc(&rdev2->nr_pending);
b84db560
N
4357 } else
4358 clear_bit(R5_MadeGood, &dev->flags);
4359 }
977df362
N
4360 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4361 struct md_rdev *rdev2 = rcu_dereference(
4362 conf->disks[i].replacement);
4363 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4364 s->handle_bad_blocks = 1;
4365 atomic_inc(&rdev2->nr_pending);
4366 } else
4367 clear_bit(R5_MadeGoodRepl, &dev->flags);
4368 }
415e72d0 4369 if (!test_bit(R5_Insync, &dev->flags)) {
16a53ecc
N
4370 /* The ReadError flag will just be confusing now */
4371 clear_bit(R5_ReadError, &dev->flags);
4372 clear_bit(R5_ReWrite, &dev->flags);
1da177e4 4373 }
415e72d0
N
4374 if (test_bit(R5_ReadError, &dev->flags))
4375 clear_bit(R5_Insync, &dev->flags);
4376 if (!test_bit(R5_Insync, &dev->flags)) {
cc94015a
N
4377 if (s->failed < 2)
4378 s->failed_num[s->failed] = i;
4379 s->failed++;
9a3e1101
N
4380 if (rdev && !test_bit(Faulty, &rdev->flags))
4381 do_recovery = 1;
415e72d0 4382 }
2ded3703
SL
4383
4384 if (test_bit(R5_InJournal, &dev->flags))
4385 s->injournal++;
1e6d690b
SL
4386 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4387 s->just_cached++;
1da177e4 4388 }
9a3e1101
N
4389 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4390 /* If there is a failed device being replaced,
4391 * we must be recovering.
4392 * else if we are after recovery_cp, we must be syncing
c6d2e084 4393 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
9a3e1101
N
4394 * else we can only be replacing
4395 * sync and recovery both need to read all devices, and so
4396 * use the same flag.
4397 */
4398 if (do_recovery ||
c6d2e084 4399 sh->sector >= conf->mddev->recovery_cp ||
4400 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
9a3e1101
N
4401 s->syncing = 1;
4402 else
4403 s->replacing = 1;
4404 }
1da177e4 4405 rcu_read_unlock();
cc94015a
N
4406}
4407
59fc630b 4408static int clear_batch_ready(struct stripe_head *sh)
4409{
b15a9dbd
N
4410 /* Return '1' if this is a member of batch, or
4411 * '0' if it is a lone stripe or a head which can now be
4412 * handled.
4413 */
59fc630b 4414 struct stripe_head *tmp;
4415 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
b15a9dbd 4416 return (sh->batch_head && sh->batch_head != sh);
59fc630b 4417 spin_lock(&sh->stripe_lock);
4418 if (!sh->batch_head) {
4419 spin_unlock(&sh->stripe_lock);
4420 return 0;
4421 }
4422
4423 /*
4424 * this stripe could be added to a batch list before we check
4425 * BATCH_READY, skips it
4426 */
4427 if (sh->batch_head != sh) {
4428 spin_unlock(&sh->stripe_lock);
4429 return 1;
4430 }
4431 spin_lock(&sh->batch_lock);
4432 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4433 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4434 spin_unlock(&sh->batch_lock);
4435 spin_unlock(&sh->stripe_lock);
4436
4437 /*
4438 * BATCH_READY is cleared, no new stripes can be added.
4439 * batch_list can be accessed without lock
4440 */
4441 return 0;
4442}
4443
3960ce79
N
4444static void break_stripe_batch_list(struct stripe_head *head_sh,
4445 unsigned long handle_flags)
72ac7330 4446{
4e3d62ff 4447 struct stripe_head *sh, *next;
72ac7330 4448 int i;
fb642b92 4449 int do_wakeup = 0;
72ac7330 4450
bb27051f
N
4451 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4452
72ac7330 4453 list_del_init(&sh->batch_list);
4454
fb3229d5 4455 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
1b956f7a
N
4456 (1 << STRIPE_SYNCING) |
4457 (1 << STRIPE_REPLACED) |
1b956f7a
N
4458 (1 << STRIPE_DELAYED) |
4459 (1 << STRIPE_BIT_DELAY) |
4460 (1 << STRIPE_FULL_WRITE) |
4461 (1 << STRIPE_BIOFILL_RUN) |
4462 (1 << STRIPE_COMPUTE_RUN) |
4463 (1 << STRIPE_OPS_REQ_PENDING) |
4464 (1 << STRIPE_DISCARD) |
4465 (1 << STRIPE_BATCH_READY) |
4466 (1 << STRIPE_BATCH_ERR) |
fb3229d5
SL
4467 (1 << STRIPE_BITMAP_PENDING)),
4468 "stripe state: %lx\n", sh->state);
4469 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4470 (1 << STRIPE_REPLACED)),
4471 "head stripe state: %lx\n", head_sh->state);
1b956f7a
N
4472
4473 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
550da24f 4474 (1 << STRIPE_PREREAD_ACTIVE) |
1b956f7a
N
4475 (1 << STRIPE_DEGRADED)),
4476 head_sh->state & (1 << STRIPE_INSYNC));
4477
72ac7330 4478 sh->check_state = head_sh->check_state;
4479 sh->reconstruct_state = head_sh->reconstruct_state;
fb642b92
N
4480 for (i = 0; i < sh->disks; i++) {
4481 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4482 do_wakeup = 1;
72ac7330 4483 sh->dev[i].flags = head_sh->dev[i].flags &
4484 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
fb642b92 4485 }
72ac7330 4486 spin_lock_irq(&sh->stripe_lock);
4487 sh->batch_head = NULL;
4488 spin_unlock_irq(&sh->stripe_lock);
3960ce79
N
4489 if (handle_flags == 0 ||
4490 sh->state & handle_flags)
4491 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 4492 raid5_release_stripe(sh);
72ac7330 4493 }
fb642b92
N
4494 spin_lock_irq(&head_sh->stripe_lock);
4495 head_sh->batch_head = NULL;
4496 spin_unlock_irq(&head_sh->stripe_lock);
4497 for (i = 0; i < head_sh->disks; i++)
4498 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4499 do_wakeup = 1;
3960ce79
N
4500 if (head_sh->state & handle_flags)
4501 set_bit(STRIPE_HANDLE, &head_sh->state);
fb642b92
N
4502
4503 if (do_wakeup)
4504 wake_up(&head_sh->raid_conf->wait_for_overlap);
72ac7330 4505}
4506
cc94015a
N
4507static void handle_stripe(struct stripe_head *sh)
4508{
4509 struct stripe_head_state s;
d1688a6d 4510 struct r5conf *conf = sh->raid_conf;
3687c061 4511 int i;
84789554
N
4512 int prexor;
4513 int disks = sh->disks;
474af965 4514 struct r5dev *pdev, *qdev;
cc94015a
N
4515
4516 clear_bit(STRIPE_HANDLE, &sh->state);
257a4b42 4517 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
cc94015a
N
4518 /* already being handled, ensure it gets handled
4519 * again when current action finishes */
4520 set_bit(STRIPE_HANDLE, &sh->state);
4521 return;
4522 }
4523
59fc630b 4524 if (clear_batch_ready(sh) ) {
4525 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4526 return;
4527 }
4528
4e3d62ff 4529 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
3960ce79 4530 break_stripe_batch_list(sh, 0);
72ac7330 4531
dabc4ec6 4532 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
f8dfcffd
N
4533 spin_lock(&sh->stripe_lock);
4534 /* Cannot process 'sync' concurrently with 'discard' */
4535 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4536 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4537 set_bit(STRIPE_SYNCING, &sh->state);
4538 clear_bit(STRIPE_INSYNC, &sh->state);
f94c0b66 4539 clear_bit(STRIPE_REPLACED, &sh->state);
f8dfcffd
N
4540 }
4541 spin_unlock(&sh->stripe_lock);
cc94015a
N
4542 }
4543 clear_bit(STRIPE_DELAYED, &sh->state);
4544
4545 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4546 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4547 (unsigned long long)sh->sector, sh->state,
4548 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4549 sh->check_state, sh->reconstruct_state);
3687c061 4550
acfe726b 4551 analyse_stripe(sh, &s);
c5a31000 4552
b70abcb2
SL
4553 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4554 goto finish;
4555
bc2607f3
N
4556 if (s.handle_bad_blocks) {
4557 set_bit(STRIPE_HANDLE, &sh->state);
4558 goto finish;
4559 }
4560
474af965
N
4561 if (unlikely(s.blocked_rdev)) {
4562 if (s.syncing || s.expanding || s.expanded ||
9a3e1101 4563 s.replacing || s.to_write || s.written) {
474af965
N
4564 set_bit(STRIPE_HANDLE, &sh->state);
4565 goto finish;
4566 }
4567 /* There is nothing for the blocked_rdev to block */
4568 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4569 s.blocked_rdev = NULL;
4570 }
4571
4572 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4573 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4574 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4575 }
4576
4577 pr_debug("locked=%d uptodate=%d to_read=%d"
4578 " to_write=%d failed=%d failed_num=%d,%d\n",
4579 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4580 s.failed_num[0], s.failed_num[1]);
4581 /* check if the array has lost more than max_degraded devices and,
4582 * if so, some requests might need to be failed.
4583 */
6e74a9cf 4584 if (s.failed > conf->max_degraded || s.log_failed) {
9a3f530f
N
4585 sh->check_state = 0;
4586 sh->reconstruct_state = 0;
626f2092 4587 break_stripe_batch_list(sh, 0);
9a3f530f
N
4588 if (s.to_read+s.to_write+s.written)
4589 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
9a3e1101 4590 if (s.syncing + s.replacing)
9a3f530f
N
4591 handle_failed_sync(conf, sh, &s);
4592 }
474af965 4593
84789554
N
4594 /* Now we check to see if any write operations have recently
4595 * completed
4596 */
4597 prexor = 0;
4598 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4599 prexor = 1;
4600 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4601 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4602 sh->reconstruct_state = reconstruct_state_idle;
4603
4604 /* All the 'written' buffers and the parity block are ready to
4605 * be written back to disk
4606 */
9e444768
SL
4607 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4608 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
84789554 4609 BUG_ON(sh->qd_idx >= 0 &&
9e444768
SL
4610 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4611 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
84789554
N
4612 for (i = disks; i--; ) {
4613 struct r5dev *dev = &sh->dev[i];
4614 if (test_bit(R5_LOCKED, &dev->flags) &&
4615 (i == sh->pd_idx || i == sh->qd_idx ||
1e6d690b
SL
4616 dev->written || test_bit(R5_InJournal,
4617 &dev->flags))) {
84789554
N
4618 pr_debug("Writing block %d\n", i);
4619 set_bit(R5_Wantwrite, &dev->flags);
4620 if (prexor)
4621 continue;
9c4bdf69
N
4622 if (s.failed > 1)
4623 continue;
84789554
N
4624 if (!test_bit(R5_Insync, &dev->flags) ||
4625 ((i == sh->pd_idx || i == sh->qd_idx) &&
4626 s.failed == 0))
4627 set_bit(STRIPE_INSYNC, &sh->state);
4628 }
4629 }
4630 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4631 s.dec_preread_active = 1;
4632 }
4633
ef5b7c69
N
4634 /*
4635 * might be able to return some write requests if the parity blocks
4636 * are safe, or on a failed drive
4637 */
4638 pdev = &sh->dev[sh->pd_idx];
4639 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4640 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4641 qdev = &sh->dev[sh->qd_idx];
4642 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4643 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4644 || conf->level < 6;
4645
4646 if (s.written &&
4647 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4648 && !test_bit(R5_LOCKED, &pdev->flags)
4649 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4650 test_bit(R5_Discard, &pdev->flags))))) &&
4651 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4652 && !test_bit(R5_LOCKED, &qdev->flags)
4653 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4654 test_bit(R5_Discard, &qdev->flags))))))
4655 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4656
1e6d690b
SL
4657 if (s.just_cached)
4658 r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi);
4659 r5l_stripe_write_finished(sh);
4660
ef5b7c69
N
4661 /* Now we might consider reading some blocks, either to check/generate
4662 * parity, or to satisfy requests
4663 * or to load a block that is being partially written.
4664 */
4665 if (s.to_read || s.non_overwrite
4666 || (conf->level == 6 && s.to_write && s.failed)
4667 || (s.syncing && (s.uptodate + s.compute < disks))
4668 || s.replacing
4669 || s.expanding)
4670 handle_stripe_fill(sh, &s, disks);
4671
2ded3703
SL
4672 /*
4673 * When the stripe finishes full journal write cycle (write to journal
4674 * and raid disk), this is the clean up procedure so it is ready for
4675 * next operation.
4676 */
4677 r5c_finish_stripe_write_out(conf, sh, &s);
4678
4679 /*
4680 * Now to consider new write requests, cache write back and what else,
4681 * if anything should be read. We do not handle new writes when:
84789554
N
4682 * 1/ A 'write' operation (copy+xor) is already in flight.
4683 * 2/ A 'check' operation is in flight, as it may clobber the parity
4684 * block.
2ded3703 4685 * 3/ A r5c cache log write is in flight.
84789554 4686 */
2ded3703
SL
4687
4688 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4689 if (!r5c_is_writeback(conf->log)) {
4690 if (s.to_write)
4691 handle_stripe_dirtying(conf, sh, &s, disks);
4692 } else { /* write back cache */
4693 int ret = 0;
4694
4695 /* First, try handle writes in caching phase */
4696 if (s.to_write)
4697 ret = r5c_try_caching_write(conf, sh, &s,
4698 disks);
4699 /*
4700 * If caching phase failed: ret == -EAGAIN
4701 * OR
4702 * stripe under reclaim: !caching && injournal
4703 *
4704 * fall back to handle_stripe_dirtying()
4705 */
4706 if (ret == -EAGAIN ||
4707 /* stripe under reclaim: !caching && injournal */
4708 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
d7bd398e
SL
4709 s.injournal > 0)) {
4710 ret = handle_stripe_dirtying(conf, sh, &s,
4711 disks);
4712 if (ret == -EAGAIN)
4713 goto finish;
4714 }
2ded3703
SL
4715 }
4716 }
84789554
N
4717
4718 /* maybe we need to check and possibly fix the parity for this stripe
4719 * Any reads will already have been scheduled, so we just see if enough
4720 * data is available. The parity check is held off while parity
4721 * dependent operations are in flight.
4722 */
4723 if (sh->check_state ||
4724 (s.syncing && s.locked == 0 &&
4725 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4726 !test_bit(STRIPE_INSYNC, &sh->state))) {
4727 if (conf->level == 6)
4728 handle_parity_checks6(conf, sh, &s, disks);
4729 else
4730 handle_parity_checks5(conf, sh, &s, disks);
4731 }
c5a31000 4732
f94c0b66
N
4733 if ((s.replacing || s.syncing) && s.locked == 0
4734 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4735 && !test_bit(STRIPE_REPLACED, &sh->state)) {
9a3e1101
N
4736 /* Write out to replacement devices where possible */
4737 for (i = 0; i < conf->raid_disks; i++)
f94c0b66
N
4738 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4739 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
9a3e1101
N
4740 set_bit(R5_WantReplace, &sh->dev[i].flags);
4741 set_bit(R5_LOCKED, &sh->dev[i].flags);
4742 s.locked++;
4743 }
f94c0b66
N
4744 if (s.replacing)
4745 set_bit(STRIPE_INSYNC, &sh->state);
4746 set_bit(STRIPE_REPLACED, &sh->state);
9a3e1101
N
4747 }
4748 if ((s.syncing || s.replacing) && s.locked == 0 &&
f94c0b66 4749 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
9a3e1101 4750 test_bit(STRIPE_INSYNC, &sh->state)) {
c5a31000
N
4751 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4752 clear_bit(STRIPE_SYNCING, &sh->state);
f8dfcffd
N
4753 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4754 wake_up(&conf->wait_for_overlap);
c5a31000
N
4755 }
4756
4757 /* If the failed drives are just a ReadError, then we might need
4758 * to progress the repair/check process
4759 */
4760 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4761 for (i = 0; i < s.failed; i++) {
4762 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4763 if (test_bit(R5_ReadError, &dev->flags)
4764 && !test_bit(R5_LOCKED, &dev->flags)
4765 && test_bit(R5_UPTODATE, &dev->flags)
4766 ) {
4767 if (!test_bit(R5_ReWrite, &dev->flags)) {
4768 set_bit(R5_Wantwrite, &dev->flags);
4769 set_bit(R5_ReWrite, &dev->flags);
4770 set_bit(R5_LOCKED, &dev->flags);
4771 s.locked++;
4772 } else {
4773 /* let's read it back */
4774 set_bit(R5_Wantread, &dev->flags);
4775 set_bit(R5_LOCKED, &dev->flags);
4776 s.locked++;
4777 }
4778 }
4779 }
4780
3687c061
N
4781 /* Finish reconstruct operations initiated by the expansion process */
4782 if (sh->reconstruct_state == reconstruct_state_result) {
4783 struct stripe_head *sh_src
6d036f7d 4784 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
3687c061
N
4785 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4786 /* sh cannot be written until sh_src has been read.
4787 * so arrange for sh to be delayed a little
4788 */
4789 set_bit(STRIPE_DELAYED, &sh->state);
4790 set_bit(STRIPE_HANDLE, &sh->state);
4791 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4792 &sh_src->state))
4793 atomic_inc(&conf->preread_active_stripes);
6d036f7d 4794 raid5_release_stripe(sh_src);
3687c061
N
4795 goto finish;
4796 }
4797 if (sh_src)
6d036f7d 4798 raid5_release_stripe(sh_src);
3687c061
N
4799
4800 sh->reconstruct_state = reconstruct_state_idle;
4801 clear_bit(STRIPE_EXPANDING, &sh->state);
4802 for (i = conf->raid_disks; i--; ) {
4803 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4804 set_bit(R5_LOCKED, &sh->dev[i].flags);
4805 s.locked++;
4806 }
4807 }
f416885e 4808
3687c061
N
4809 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4810 !sh->reconstruct_state) {
4811 /* Need to write out all blocks after computing parity */
4812 sh->disks = conf->raid_disks;
4813 stripe_set_idx(sh->sector, conf, 0, sh);
4814 schedule_reconstruction(sh, &s, 1, 1);
4815 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4816 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4817 atomic_dec(&conf->reshape_stripes);
4818 wake_up(&conf->wait_for_overlap);
4819 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4820 }
4821
4822 if (s.expanding && s.locked == 0 &&
4823 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4824 handle_stripe_expansion(conf, sh);
16a53ecc 4825
3687c061 4826finish:
6bfe0b49 4827 /* wait for this device to become unblocked */
5f066c63
N
4828 if (unlikely(s.blocked_rdev)) {
4829 if (conf->mddev->external)
4830 md_wait_for_blocked_rdev(s.blocked_rdev,
4831 conf->mddev);
4832 else
4833 /* Internal metadata will immediately
4834 * be written by raid5d, so we don't
4835 * need to wait here.
4836 */
4837 rdev_dec_pending(s.blocked_rdev,
4838 conf->mddev);
4839 }
6bfe0b49 4840
bc2607f3
N
4841 if (s.handle_bad_blocks)
4842 for (i = disks; i--; ) {
3cb03002 4843 struct md_rdev *rdev;
bc2607f3
N
4844 struct r5dev *dev = &sh->dev[i];
4845 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4846 /* We own a safe reference to the rdev */
4847 rdev = conf->disks[i].rdev;
4848 if (!rdev_set_badblocks(rdev, sh->sector,
4849 STRIPE_SECTORS, 0))
4850 md_error(conf->mddev, rdev);
4851 rdev_dec_pending(rdev, conf->mddev);
4852 }
b84db560
N
4853 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4854 rdev = conf->disks[i].rdev;
4855 rdev_clear_badblocks(rdev, sh->sector,
c6563a8c 4856 STRIPE_SECTORS, 0);
b84db560
N
4857 rdev_dec_pending(rdev, conf->mddev);
4858 }
977df362
N
4859 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4860 rdev = conf->disks[i].replacement;
dd054fce
N
4861 if (!rdev)
4862 /* rdev have been moved down */
4863 rdev = conf->disks[i].rdev;
977df362 4864 rdev_clear_badblocks(rdev, sh->sector,
c6563a8c 4865 STRIPE_SECTORS, 0);
977df362
N
4866 rdev_dec_pending(rdev, conf->mddev);
4867 }
bc2607f3
N
4868 }
4869
6c0069c0
YT
4870 if (s.ops_request)
4871 raid_run_ops(sh, s.ops_request);
4872
f0e43bcd 4873 ops_run_io(sh, &s);
16a53ecc 4874
c5709ef6 4875 if (s.dec_preread_active) {
729a1866 4876 /* We delay this until after ops_run_io so that if make_request
e9c7469b 4877 * is waiting on a flush, it won't continue until the writes
729a1866
N
4878 * have actually been submitted.
4879 */
4880 atomic_dec(&conf->preread_active_stripes);
4881 if (atomic_read(&conf->preread_active_stripes) <
4882 IO_THRESHOLD)
4883 md_wakeup_thread(conf->mddev->thread);
4884 }
4885
c3cce6cd 4886 if (!bio_list_empty(&s.return_bi)) {
2953079c 4887 if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
c3cce6cd
N
4888 spin_lock_irq(&conf->device_lock);
4889 bio_list_merge(&conf->return_bi, &s.return_bi);
4890 spin_unlock_irq(&conf->device_lock);
4891 md_wakeup_thread(conf->mddev->thread);
4892 } else
4893 return_io(&s.return_bi);
4894 }
16a53ecc 4895
257a4b42 4896 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
16a53ecc
N
4897}
4898
d1688a6d 4899static void raid5_activate_delayed(struct r5conf *conf)
16a53ecc
N
4900{
4901 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4902 while (!list_empty(&conf->delayed_list)) {
4903 struct list_head *l = conf->delayed_list.next;
4904 struct stripe_head *sh;
4905 sh = list_entry(l, struct stripe_head, lru);
4906 list_del_init(l);
4907 clear_bit(STRIPE_DELAYED, &sh->state);
4908 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4909 atomic_inc(&conf->preread_active_stripes);
8b3e6cdc 4910 list_add_tail(&sh->lru, &conf->hold_list);
851c30c9 4911 raid5_wakeup_stripe_thread(sh);
16a53ecc 4912 }
482c0834 4913 }
16a53ecc
N
4914}
4915
566c09c5
SL
4916static void activate_bit_delay(struct r5conf *conf,
4917 struct list_head *temp_inactive_list)
16a53ecc
N
4918{
4919 /* device_lock is held */
4920 struct list_head head;
4921 list_add(&head, &conf->bitmap_list);
4922 list_del_init(&conf->bitmap_list);
4923 while (!list_empty(&head)) {
4924 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
566c09c5 4925 int hash;
16a53ecc
N
4926 list_del_init(&sh->lru);
4927 atomic_inc(&sh->count);
566c09c5
SL
4928 hash = sh->hash_lock_index;
4929 __release_stripe(conf, sh, &temp_inactive_list[hash]);
16a53ecc
N
4930 }
4931}
4932
5c675f83 4933static int raid5_congested(struct mddev *mddev, int bits)
f022b2fd 4934{
d1688a6d 4935 struct r5conf *conf = mddev->private;
f022b2fd
N
4936
4937 /* No difference between reads and writes. Just check
4938 * how busy the stripe_cache is
4939 */
3fa841d7 4940
5423399a 4941 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
f022b2fd 4942 return 1;
a39f7afd
SL
4943
4944 /* Also checks whether there is pressure on r5cache log space */
4945 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
4946 return 1;
f022b2fd
N
4947 if (conf->quiesce)
4948 return 1;
4bda556a 4949 if (atomic_read(&conf->empty_inactive_list_nr))
f022b2fd
N
4950 return 1;
4951
4952 return 0;
4953}
4954
fd01b88c 4955static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
f679623f 4956{
3cb5edf4 4957 struct r5conf *conf = mddev->private;
4f024f37 4958 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
3cb5edf4 4959 unsigned int chunk_sectors;
aa8b57aa 4960 unsigned int bio_sectors = bio_sectors(bio);
f679623f 4961
3cb5edf4 4962 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
f679623f
RBJ
4963 return chunk_sectors >=
4964 ((sector & (chunk_sectors - 1)) + bio_sectors);
4965}
4966
46031f9a
RBJ
4967/*
4968 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4969 * later sampled by raid5d.
4970 */
d1688a6d 4971static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
46031f9a
RBJ
4972{
4973 unsigned long flags;
4974
4975 spin_lock_irqsave(&conf->device_lock, flags);
4976
4977 bi->bi_next = conf->retry_read_aligned_list;
4978 conf->retry_read_aligned_list = bi;
4979
4980 spin_unlock_irqrestore(&conf->device_lock, flags);
4981 md_wakeup_thread(conf->mddev->thread);
4982}
4983
d1688a6d 4984static struct bio *remove_bio_from_retry(struct r5conf *conf)
46031f9a
RBJ
4985{
4986 struct bio *bi;
4987
4988 bi = conf->retry_read_aligned;
4989 if (bi) {
4990 conf->retry_read_aligned = NULL;
4991 return bi;
4992 }
4993 bi = conf->retry_read_aligned_list;
4994 if(bi) {
387bb173 4995 conf->retry_read_aligned_list = bi->bi_next;
46031f9a 4996 bi->bi_next = NULL;
960e739d
JA
4997 /*
4998 * this sets the active strip count to 1 and the processed
4999 * strip count to zero (upper 8 bits)
5000 */
e7836bd6 5001 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
46031f9a
RBJ
5002 }
5003
5004 return bi;
5005}
5006
f679623f
RBJ
5007/*
5008 * The "raid5_align_endio" should check if the read succeeded and if it
5009 * did, call bio_endio on the original bio (having bio_put the new bio
5010 * first).
5011 * If the read failed..
5012 */
4246a0b6 5013static void raid5_align_endio(struct bio *bi)
f679623f
RBJ
5014{
5015 struct bio* raid_bi = bi->bi_private;
fd01b88c 5016 struct mddev *mddev;
d1688a6d 5017 struct r5conf *conf;
3cb03002 5018 struct md_rdev *rdev;
9b81c842 5019 int error = bi->bi_error;
46031f9a 5020
f679623f 5021 bio_put(bi);
46031f9a 5022
46031f9a
RBJ
5023 rdev = (void*)raid_bi->bi_next;
5024 raid_bi->bi_next = NULL;
2b7f2228
N
5025 mddev = rdev->mddev;
5026 conf = mddev->private;
46031f9a
RBJ
5027
5028 rdev_dec_pending(rdev, conf->mddev);
5029
9b81c842 5030 if (!error) {
0a82a8d1
LT
5031 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
5032 raid_bi, 0);
4246a0b6 5033 bio_endio(raid_bi);
46031f9a 5034 if (atomic_dec_and_test(&conf->active_aligned_reads))
b1b46486 5035 wake_up(&conf->wait_for_quiescent);
6712ecf8 5036 return;
46031f9a
RBJ
5037 }
5038
45b4233c 5039 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
46031f9a
RBJ
5040
5041 add_bio_to_retry(raid_bi, conf);
f679623f
RBJ
5042}
5043
7ef6b12a 5044static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
f679623f 5045{
d1688a6d 5046 struct r5conf *conf = mddev->private;
8553fe7e 5047 int dd_idx;
f679623f 5048 struct bio* align_bi;
3cb03002 5049 struct md_rdev *rdev;
671488cc 5050 sector_t end_sector;
f679623f
RBJ
5051
5052 if (!in_chunk_boundary(mddev, raid_bio)) {
7ef6b12a 5053 pr_debug("%s: non aligned\n", __func__);
f679623f
RBJ
5054 return 0;
5055 }
5056 /*
d7a10308 5057 * use bio_clone_fast to make a copy of the bio
f679623f 5058 */
d7a10308 5059 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set);
f679623f
RBJ
5060 if (!align_bi)
5061 return 0;
5062 /*
5063 * set bi_end_io to a new function, and set bi_private to the
5064 * original bio.
5065 */
5066 align_bi->bi_end_io = raid5_align_endio;
5067 align_bi->bi_private = raid_bio;
5068 /*
5069 * compute position
5070 */
4f024f37
KO
5071 align_bi->bi_iter.bi_sector =
5072 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5073 0, &dd_idx, NULL);
f679623f 5074
f73a1c7d 5075 end_sector = bio_end_sector(align_bi);
f679623f 5076 rcu_read_lock();
671488cc
N
5077 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5078 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5079 rdev->recovery_offset < end_sector) {
5080 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5081 if (rdev &&
5082 (test_bit(Faulty, &rdev->flags) ||
5083 !(test_bit(In_sync, &rdev->flags) ||
5084 rdev->recovery_offset >= end_sector)))
5085 rdev = NULL;
5086 }
03b047f4
SL
5087
5088 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5089 rcu_read_unlock();
5090 bio_put(align_bi);
5091 return 0;
5092 }
5093
671488cc 5094 if (rdev) {
31c176ec
N
5095 sector_t first_bad;
5096 int bad_sectors;
5097
f679623f
RBJ
5098 atomic_inc(&rdev->nr_pending);
5099 rcu_read_unlock();
46031f9a
RBJ
5100 raid_bio->bi_next = (void*)rdev;
5101 align_bi->bi_bdev = rdev->bdev;
b7c44ed9 5102 bio_clear_flag(align_bi, BIO_SEG_VALID);
46031f9a 5103
7140aafc 5104 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
4f024f37 5105 bio_sectors(align_bi),
31c176ec 5106 &first_bad, &bad_sectors)) {
387bb173
NB
5107 bio_put(align_bi);
5108 rdev_dec_pending(rdev, mddev);
5109 return 0;
5110 }
5111
6c0544e2 5112 /* No reshape active, so we can trust rdev->data_offset */
4f024f37 5113 align_bi->bi_iter.bi_sector += rdev->data_offset;
6c0544e2 5114
46031f9a 5115 spin_lock_irq(&conf->device_lock);
b1b46486 5116 wait_event_lock_irq(conf->wait_for_quiescent,
46031f9a 5117 conf->quiesce == 0,
eed8c02e 5118 conf->device_lock);
46031f9a
RBJ
5119 atomic_inc(&conf->active_aligned_reads);
5120 spin_unlock_irq(&conf->device_lock);
5121
e3620a3a
JB
5122 if (mddev->gendisk)
5123 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
5124 align_bi, disk_devt(mddev->gendisk),
4f024f37 5125 raid_bio->bi_iter.bi_sector);
f679623f
RBJ
5126 generic_make_request(align_bi);
5127 return 1;
5128 } else {
5129 rcu_read_unlock();
46031f9a 5130 bio_put(align_bi);
f679623f
RBJ
5131 return 0;
5132 }
5133}
5134
7ef6b12a
ML
5135static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5136{
5137 struct bio *split;
5138
5139 do {
5140 sector_t sector = raid_bio->bi_iter.bi_sector;
5141 unsigned chunk_sects = mddev->chunk_sectors;
5142 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5143
5144 if (sectors < bio_sectors(raid_bio)) {
5145 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
5146 bio_chain(split, raid_bio);
5147 } else
5148 split = raid_bio;
5149
5150 if (!raid5_read_one_chunk(mddev, split)) {
5151 if (split != raid_bio)
5152 generic_make_request(raid_bio);
5153 return split;
5154 }
5155 } while (split != raid_bio);
5156
5157 return NULL;
5158}
5159
8b3e6cdc
DW
5160/* __get_priority_stripe - get the next stripe to process
5161 *
5162 * Full stripe writes are allowed to pass preread active stripes up until
5163 * the bypass_threshold is exceeded. In general the bypass_count
5164 * increments when the handle_list is handled before the hold_list; however, it
5165 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5166 * stripe with in flight i/o. The bypass_count will be reset when the
5167 * head of the hold_list has changed, i.e. the head was promoted to the
5168 * handle_list.
5169 */
851c30c9 5170static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
8b3e6cdc 5171{
851c30c9
SL
5172 struct stripe_head *sh = NULL, *tmp;
5173 struct list_head *handle_list = NULL;
bfc90cb0 5174 struct r5worker_group *wg = NULL;
851c30c9
SL
5175
5176 if (conf->worker_cnt_per_group == 0) {
5177 handle_list = &conf->handle_list;
5178 } else if (group != ANY_GROUP) {
5179 handle_list = &conf->worker_groups[group].handle_list;
bfc90cb0 5180 wg = &conf->worker_groups[group];
851c30c9
SL
5181 } else {
5182 int i;
5183 for (i = 0; i < conf->group_cnt; i++) {
5184 handle_list = &conf->worker_groups[i].handle_list;
bfc90cb0 5185 wg = &conf->worker_groups[i];
851c30c9
SL
5186 if (!list_empty(handle_list))
5187 break;
5188 }
5189 }
8b3e6cdc
DW
5190
5191 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5192 __func__,
851c30c9 5193 list_empty(handle_list) ? "empty" : "busy",
8b3e6cdc
DW
5194 list_empty(&conf->hold_list) ? "empty" : "busy",
5195 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5196
851c30c9
SL
5197 if (!list_empty(handle_list)) {
5198 sh = list_entry(handle_list->next, typeof(*sh), lru);
8b3e6cdc
DW
5199
5200 if (list_empty(&conf->hold_list))
5201 conf->bypass_count = 0;
5202 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5203 if (conf->hold_list.next == conf->last_hold)
5204 conf->bypass_count++;
5205 else {
5206 conf->last_hold = conf->hold_list.next;
5207 conf->bypass_count -= conf->bypass_threshold;
5208 if (conf->bypass_count < 0)
5209 conf->bypass_count = 0;
5210 }
5211 }
5212 } else if (!list_empty(&conf->hold_list) &&
5213 ((conf->bypass_threshold &&
5214 conf->bypass_count > conf->bypass_threshold) ||
5215 atomic_read(&conf->pending_full_writes) == 0)) {
851c30c9
SL
5216
5217 list_for_each_entry(tmp, &conf->hold_list, lru) {
5218 if (conf->worker_cnt_per_group == 0 ||
5219 group == ANY_GROUP ||
5220 !cpu_online(tmp->cpu) ||
5221 cpu_to_group(tmp->cpu) == group) {
5222 sh = tmp;
5223 break;
5224 }
5225 }
5226
5227 if (sh) {
5228 conf->bypass_count -= conf->bypass_threshold;
5229 if (conf->bypass_count < 0)
5230 conf->bypass_count = 0;
5231 }
bfc90cb0 5232 wg = NULL;
851c30c9
SL
5233 }
5234
5235 if (!sh)
8b3e6cdc
DW
5236 return NULL;
5237
bfc90cb0
SL
5238 if (wg) {
5239 wg->stripes_cnt--;
5240 sh->group = NULL;
5241 }
8b3e6cdc 5242 list_del_init(&sh->lru);
c7a6d35e 5243 BUG_ON(atomic_inc_return(&sh->count) != 1);
8b3e6cdc
DW
5244 return sh;
5245}
f679623f 5246
8811b596
SL
5247struct raid5_plug_cb {
5248 struct blk_plug_cb cb;
5249 struct list_head list;
566c09c5 5250 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
8811b596
SL
5251};
5252
5253static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5254{
5255 struct raid5_plug_cb *cb = container_of(
5256 blk_cb, struct raid5_plug_cb, cb);
5257 struct stripe_head *sh;
5258 struct mddev *mddev = cb->cb.data;
5259 struct r5conf *conf = mddev->private;
a9add5d9 5260 int cnt = 0;
566c09c5 5261 int hash;
8811b596
SL
5262
5263 if (cb->list.next && !list_empty(&cb->list)) {
5264 spin_lock_irq(&conf->device_lock);
5265 while (!list_empty(&cb->list)) {
5266 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5267 list_del_init(&sh->lru);
5268 /*
5269 * avoid race release_stripe_plug() sees
5270 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5271 * is still in our list
5272 */
4e857c58 5273 smp_mb__before_atomic();
8811b596 5274 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
773ca82f
SL
5275 /*
5276 * STRIPE_ON_RELEASE_LIST could be set here. In that
5277 * case, the count is always > 1 here
5278 */
566c09c5
SL
5279 hash = sh->hash_lock_index;
5280 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
a9add5d9 5281 cnt++;
8811b596
SL
5282 }
5283 spin_unlock_irq(&conf->device_lock);
5284 }
566c09c5
SL
5285 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5286 NR_STRIPE_HASH_LOCKS);
e3620a3a
JB
5287 if (mddev->queue)
5288 trace_block_unplug(mddev->queue, cnt, !from_schedule);
8811b596
SL
5289 kfree(cb);
5290}
5291
5292static void release_stripe_plug(struct mddev *mddev,
5293 struct stripe_head *sh)
5294{
5295 struct blk_plug_cb *blk_cb = blk_check_plugged(
5296 raid5_unplug, mddev,
5297 sizeof(struct raid5_plug_cb));
5298 struct raid5_plug_cb *cb;
5299
5300 if (!blk_cb) {
6d036f7d 5301 raid5_release_stripe(sh);
8811b596
SL
5302 return;
5303 }
5304
5305 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5306
566c09c5
SL
5307 if (cb->list.next == NULL) {
5308 int i;
8811b596 5309 INIT_LIST_HEAD(&cb->list);
566c09c5
SL
5310 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5311 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5312 }
8811b596
SL
5313
5314 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5315 list_add_tail(&sh->lru, &cb->list);
5316 else
6d036f7d 5317 raid5_release_stripe(sh);
8811b596
SL
5318}
5319
620125f2
SL
5320static void make_discard_request(struct mddev *mddev, struct bio *bi)
5321{
5322 struct r5conf *conf = mddev->private;
5323 sector_t logical_sector, last_sector;
5324 struct stripe_head *sh;
5325 int remaining;
5326 int stripe_sectors;
5327
5328 if (mddev->reshape_position != MaxSector)
5329 /* Skip discard while reshape is happening */
5330 return;
5331
4f024f37
KO
5332 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5333 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
620125f2
SL
5334
5335 bi->bi_next = NULL;
5336 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5337
5338 stripe_sectors = conf->chunk_sectors *
5339 (conf->raid_disks - conf->max_degraded);
5340 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5341 stripe_sectors);
5342 sector_div(last_sector, stripe_sectors);
5343
5344 logical_sector *= conf->chunk_sectors;
5345 last_sector *= conf->chunk_sectors;
5346
5347 for (; logical_sector < last_sector;
5348 logical_sector += STRIPE_SECTORS) {
5349 DEFINE_WAIT(w);
5350 int d;
5351 again:
6d036f7d 5352 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
620125f2
SL
5353 prepare_to_wait(&conf->wait_for_overlap, &w,
5354 TASK_UNINTERRUPTIBLE);
f8dfcffd
N
5355 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5356 if (test_bit(STRIPE_SYNCING, &sh->state)) {
6d036f7d 5357 raid5_release_stripe(sh);
f8dfcffd
N
5358 schedule();
5359 goto again;
5360 }
5361 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
620125f2
SL
5362 spin_lock_irq(&sh->stripe_lock);
5363 for (d = 0; d < conf->raid_disks; d++) {
5364 if (d == sh->pd_idx || d == sh->qd_idx)
5365 continue;
5366 if (sh->dev[d].towrite || sh->dev[d].toread) {
5367 set_bit(R5_Overlap, &sh->dev[d].flags);
5368 spin_unlock_irq(&sh->stripe_lock);
6d036f7d 5369 raid5_release_stripe(sh);
620125f2
SL
5370 schedule();
5371 goto again;
5372 }
5373 }
f8dfcffd 5374 set_bit(STRIPE_DISCARD, &sh->state);
620125f2 5375 finish_wait(&conf->wait_for_overlap, &w);
7a87f434 5376 sh->overwrite_disks = 0;
620125f2
SL
5377 for (d = 0; d < conf->raid_disks; d++) {
5378 if (d == sh->pd_idx || d == sh->qd_idx)
5379 continue;
5380 sh->dev[d].towrite = bi;
5381 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5382 raid5_inc_bi_active_stripes(bi);
7a87f434 5383 sh->overwrite_disks++;
620125f2
SL
5384 }
5385 spin_unlock_irq(&sh->stripe_lock);
5386 if (conf->mddev->bitmap) {
5387 for (d = 0;
5388 d < conf->raid_disks - conf->max_degraded;
5389 d++)
5390 bitmap_startwrite(mddev->bitmap,
5391 sh->sector,
5392 STRIPE_SECTORS,
5393 0);
5394 sh->bm_seq = conf->seq_flush + 1;
5395 set_bit(STRIPE_BIT_DELAY, &sh->state);
5396 }
5397
5398 set_bit(STRIPE_HANDLE, &sh->state);
5399 clear_bit(STRIPE_DELAYED, &sh->state);
5400 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5401 atomic_inc(&conf->preread_active_stripes);
5402 release_stripe_plug(mddev, sh);
5403 }
5404
5405 remaining = raid5_dec_bi_active_stripes(bi);
5406 if (remaining == 0) {
5407 md_write_end(mddev);
4246a0b6 5408 bio_endio(bi);
620125f2
SL
5409 }
5410}
5411
849674e4 5412static void raid5_make_request(struct mddev *mddev, struct bio * bi)
1da177e4 5413{
d1688a6d 5414 struct r5conf *conf = mddev->private;
911d4ee8 5415 int dd_idx;
1da177e4
LT
5416 sector_t new_sector;
5417 sector_t logical_sector, last_sector;
5418 struct stripe_head *sh;
a362357b 5419 const int rw = bio_data_dir(bi);
49077326 5420 int remaining;
27c0f68f
SL
5421 DEFINE_WAIT(w);
5422 bool do_prepare;
3bddb7f8 5423 bool do_flush = false;
1da177e4 5424
1eff9d32 5425 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
828cbe98
SL
5426 int ret = r5l_handle_flush_request(conf->log, bi);
5427
5428 if (ret == 0)
5429 return;
5430 if (ret == -ENODEV) {
5431 md_flush_request(mddev, bi);
5432 return;
5433 }
5434 /* ret == -EAGAIN, fallback */
3bddb7f8
SL
5435 /*
5436 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5437 * we need to flush journal device
5438 */
5439 do_flush = bi->bi_opf & REQ_PREFLUSH;
e5dcdd80
N
5440 }
5441
3d310eb7 5442 md_write_start(mddev, bi);
06d91a5f 5443
9ffc8f7c
EM
5444 /*
5445 * If array is degraded, better not do chunk aligned read because
5446 * later we might have to read it again in order to reconstruct
5447 * data on failed drives.
5448 */
5449 if (rw == READ && mddev->degraded == 0 &&
7ef6b12a
ML
5450 mddev->reshape_position == MaxSector) {
5451 bi = chunk_aligned_read(mddev, bi);
5452 if (!bi)
5453 return;
5454 }
52488615 5455
796a5cf0 5456 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
620125f2
SL
5457 make_discard_request(mddev, bi);
5458 return;
5459 }
5460
4f024f37 5461 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
f73a1c7d 5462 last_sector = bio_end_sector(bi);
1da177e4
LT
5463 bi->bi_next = NULL;
5464 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
06d91a5f 5465
27c0f68f 5466 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1da177e4 5467 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
b5663ba4 5468 int previous;
c46501b2 5469 int seq;
b578d55f 5470
27c0f68f 5471 do_prepare = false;
7ecaa1e6 5472 retry:
c46501b2 5473 seq = read_seqcount_begin(&conf->gen_lock);
b5663ba4 5474 previous = 0;
27c0f68f
SL
5475 if (do_prepare)
5476 prepare_to_wait(&conf->wait_for_overlap, &w,
5477 TASK_UNINTERRUPTIBLE);
b0f9ec04 5478 if (unlikely(conf->reshape_progress != MaxSector)) {
fef9c61f 5479 /* spinlock is needed as reshape_progress may be
df8e7f76
N
5480 * 64bit on a 32bit platform, and so it might be
5481 * possible to see a half-updated value
aeb878b0 5482 * Of course reshape_progress could change after
df8e7f76
N
5483 * the lock is dropped, so once we get a reference
5484 * to the stripe that we think it is, we will have
5485 * to check again.
5486 */
7ecaa1e6 5487 spin_lock_irq(&conf->device_lock);
2c810cdd 5488 if (mddev->reshape_backwards
fef9c61f
N
5489 ? logical_sector < conf->reshape_progress
5490 : logical_sector >= conf->reshape_progress) {
b5663ba4
N
5491 previous = 1;
5492 } else {
2c810cdd 5493 if (mddev->reshape_backwards
fef9c61f
N
5494 ? logical_sector < conf->reshape_safe
5495 : logical_sector >= conf->reshape_safe) {
b578d55f
N
5496 spin_unlock_irq(&conf->device_lock);
5497 schedule();
27c0f68f 5498 do_prepare = true;
b578d55f
N
5499 goto retry;
5500 }
5501 }
7ecaa1e6
N
5502 spin_unlock_irq(&conf->device_lock);
5503 }
16a53ecc 5504
112bf897
N
5505 new_sector = raid5_compute_sector(conf, logical_sector,
5506 previous,
911d4ee8 5507 &dd_idx, NULL);
849674e4 5508 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
c46501b2 5509 (unsigned long long)new_sector,
1da177e4
LT
5510 (unsigned long long)logical_sector);
5511
6d036f7d 5512 sh = raid5_get_active_stripe(conf, new_sector, previous,
1eff9d32 5513 (bi->bi_opf & REQ_RAHEAD), 0);
1da177e4 5514 if (sh) {
b0f9ec04 5515 if (unlikely(previous)) {
7ecaa1e6 5516 /* expansion might have moved on while waiting for a
df8e7f76
N
5517 * stripe, so we must do the range check again.
5518 * Expansion could still move past after this
5519 * test, but as we are holding a reference to
5520 * 'sh', we know that if that happens,
5521 * STRIPE_EXPANDING will get set and the expansion
5522 * won't proceed until we finish with the stripe.
7ecaa1e6
N
5523 */
5524 int must_retry = 0;
5525 spin_lock_irq(&conf->device_lock);
2c810cdd 5526 if (mddev->reshape_backwards
b0f9ec04
N
5527 ? logical_sector >= conf->reshape_progress
5528 : logical_sector < conf->reshape_progress)
7ecaa1e6
N
5529 /* mismatch, need to try again */
5530 must_retry = 1;
5531 spin_unlock_irq(&conf->device_lock);
5532 if (must_retry) {
6d036f7d 5533 raid5_release_stripe(sh);
7a3ab908 5534 schedule();
27c0f68f 5535 do_prepare = true;
7ecaa1e6
N
5536 goto retry;
5537 }
5538 }
c46501b2
N
5539 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5540 /* Might have got the wrong stripe_head
5541 * by accident
5542 */
6d036f7d 5543 raid5_release_stripe(sh);
c46501b2
N
5544 goto retry;
5545 }
e62e58a5 5546
ffd96e35 5547 if (rw == WRITE &&
a5c308d4 5548 logical_sector >= mddev->suspend_lo &&
e464eafd 5549 logical_sector < mddev->suspend_hi) {
6d036f7d 5550 raid5_release_stripe(sh);
e62e58a5
N
5551 /* As the suspend_* range is controlled by
5552 * userspace, we want an interruptible
5553 * wait.
5554 */
5555 flush_signals(current);
5556 prepare_to_wait(&conf->wait_for_overlap,
5557 &w, TASK_INTERRUPTIBLE);
5558 if (logical_sector >= mddev->suspend_lo &&
27c0f68f 5559 logical_sector < mddev->suspend_hi) {
e62e58a5 5560 schedule();
27c0f68f
SL
5561 do_prepare = true;
5562 }
e464eafd
N
5563 goto retry;
5564 }
7ecaa1e6
N
5565
5566 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
da41ba65 5567 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
7ecaa1e6
N
5568 /* Stripe is busy expanding or
5569 * add failed due to overlap. Flush everything
1da177e4
LT
5570 * and wait a while
5571 */
482c0834 5572 md_wakeup_thread(mddev->thread);
6d036f7d 5573 raid5_release_stripe(sh);
1da177e4 5574 schedule();
27c0f68f 5575 do_prepare = true;
1da177e4
LT
5576 goto retry;
5577 }
3bddb7f8
SL
5578 if (do_flush) {
5579 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5580 /* we only need flush for one stripe */
5581 do_flush = false;
5582 }
5583
6ed3003c
N
5584 set_bit(STRIPE_HANDLE, &sh->state);
5585 clear_bit(STRIPE_DELAYED, &sh->state);
59fc630b 5586 if ((!sh->batch_head || sh == sh->batch_head) &&
1eff9d32 5587 (bi->bi_opf & REQ_SYNC) &&
729a1866
N
5588 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5589 atomic_inc(&conf->preread_active_stripes);
8811b596 5590 release_stripe_plug(mddev, sh);
1da177e4
LT
5591 } else {
5592 /* cannot get stripe for read-ahead, just give-up */
4246a0b6 5593 bi->bi_error = -EIO;
1da177e4
LT
5594 break;
5595 }
1da177e4 5596 }
27c0f68f 5597 finish_wait(&conf->wait_for_overlap, &w);
7c13edc8 5598
e7836bd6 5599 remaining = raid5_dec_bi_active_stripes(bi);
f6344757 5600 if (remaining == 0) {
1da177e4 5601
16a53ecc 5602 if ( rw == WRITE )
1da177e4 5603 md_write_end(mddev);
6712ecf8 5604
0a82a8d1
LT
5605 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5606 bi, 0);
4246a0b6 5607 bio_endio(bi);
1da177e4 5608 }
1da177e4
LT
5609}
5610
fd01b88c 5611static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
b522adcd 5612
fd01b88c 5613static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
1da177e4 5614{
52c03291
N
5615 /* reshaping is quite different to recovery/resync so it is
5616 * handled quite separately ... here.
5617 *
5618 * On each call to sync_request, we gather one chunk worth of
5619 * destination stripes and flag them as expanding.
5620 * Then we find all the source stripes and request reads.
5621 * As the reads complete, handle_stripe will copy the data
5622 * into the destination stripe and release that stripe.
5623 */
d1688a6d 5624 struct r5conf *conf = mddev->private;
1da177e4 5625 struct stripe_head *sh;
ccfcc3c1 5626 sector_t first_sector, last_sector;
f416885e
N
5627 int raid_disks = conf->previous_raid_disks;
5628 int data_disks = raid_disks - conf->max_degraded;
5629 int new_data_disks = conf->raid_disks - conf->max_degraded;
52c03291
N
5630 int i;
5631 int dd_idx;
c8f517c4 5632 sector_t writepos, readpos, safepos;
ec32a2bd 5633 sector_t stripe_addr;
7a661381 5634 int reshape_sectors;
ab69ae12 5635 struct list_head stripes;
92140480 5636 sector_t retn;
52c03291 5637
fef9c61f
N
5638 if (sector_nr == 0) {
5639 /* If restarting in the middle, skip the initial sectors */
2c810cdd 5640 if (mddev->reshape_backwards &&
fef9c61f
N
5641 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5642 sector_nr = raid5_size(mddev, 0, 0)
5643 - conf->reshape_progress;
6cbd8148
N
5644 } else if (mddev->reshape_backwards &&
5645 conf->reshape_progress == MaxSector) {
5646 /* shouldn't happen, but just in case, finish up.*/
5647 sector_nr = MaxSector;
2c810cdd 5648 } else if (!mddev->reshape_backwards &&
fef9c61f
N
5649 conf->reshape_progress > 0)
5650 sector_nr = conf->reshape_progress;
f416885e 5651 sector_div(sector_nr, new_data_disks);
fef9c61f 5652 if (sector_nr) {
8dee7211
N
5653 mddev->curr_resync_completed = sector_nr;
5654 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
fef9c61f 5655 *skipped = 1;
92140480
N
5656 retn = sector_nr;
5657 goto finish;
fef9c61f 5658 }
52c03291
N
5659 }
5660
7a661381
N
5661 /* We need to process a full chunk at a time.
5662 * If old and new chunk sizes differ, we need to process the
5663 * largest of these
5664 */
3cb5edf4
N
5665
5666 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
7a661381 5667
b5254dd5
N
5668 /* We update the metadata at least every 10 seconds, or when
5669 * the data about to be copied would over-write the source of
5670 * the data at the front of the range. i.e. one new_stripe
5671 * along from reshape_progress new_maps to after where
5672 * reshape_safe old_maps to
52c03291 5673 */
fef9c61f 5674 writepos = conf->reshape_progress;
f416885e 5675 sector_div(writepos, new_data_disks);
c8f517c4
N
5676 readpos = conf->reshape_progress;
5677 sector_div(readpos, data_disks);
fef9c61f 5678 safepos = conf->reshape_safe;
f416885e 5679 sector_div(safepos, data_disks);
2c810cdd 5680 if (mddev->reshape_backwards) {
c74c0d76
N
5681 BUG_ON(writepos < reshape_sectors);
5682 writepos -= reshape_sectors;
c8f517c4 5683 readpos += reshape_sectors;
7a661381 5684 safepos += reshape_sectors;
fef9c61f 5685 } else {
7a661381 5686 writepos += reshape_sectors;
c74c0d76
N
5687 /* readpos and safepos are worst-case calculations.
5688 * A negative number is overly pessimistic, and causes
5689 * obvious problems for unsigned storage. So clip to 0.
5690 */
ed37d83e
N
5691 readpos -= min_t(sector_t, reshape_sectors, readpos);
5692 safepos -= min_t(sector_t, reshape_sectors, safepos);
fef9c61f 5693 }
52c03291 5694
b5254dd5
N
5695 /* Having calculated the 'writepos' possibly use it
5696 * to set 'stripe_addr' which is where we will write to.
5697 */
5698 if (mddev->reshape_backwards) {
5699 BUG_ON(conf->reshape_progress == 0);
5700 stripe_addr = writepos;
5701 BUG_ON((mddev->dev_sectors &
5702 ~((sector_t)reshape_sectors - 1))
5703 - reshape_sectors - stripe_addr
5704 != sector_nr);
5705 } else {
5706 BUG_ON(writepos != sector_nr + reshape_sectors);
5707 stripe_addr = sector_nr;
5708 }
5709
c8f517c4
N
5710 /* 'writepos' is the most advanced device address we might write.
5711 * 'readpos' is the least advanced device address we might read.
5712 * 'safepos' is the least address recorded in the metadata as having
5713 * been reshaped.
b5254dd5
N
5714 * If there is a min_offset_diff, these are adjusted either by
5715 * increasing the safepos/readpos if diff is negative, or
5716 * increasing writepos if diff is positive.
5717 * If 'readpos' is then behind 'writepos', there is no way that we can
c8f517c4
N
5718 * ensure safety in the face of a crash - that must be done by userspace
5719 * making a backup of the data. So in that case there is no particular
5720 * rush to update metadata.
5721 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5722 * update the metadata to advance 'safepos' to match 'readpos' so that
5723 * we can be safe in the event of a crash.
5724 * So we insist on updating metadata if safepos is behind writepos and
5725 * readpos is beyond writepos.
5726 * In any case, update the metadata every 10 seconds.
5727 * Maybe that number should be configurable, but I'm not sure it is
5728 * worth it.... maybe it could be a multiple of safemode_delay???
5729 */
b5254dd5
N
5730 if (conf->min_offset_diff < 0) {
5731 safepos += -conf->min_offset_diff;
5732 readpos += -conf->min_offset_diff;
5733 } else
5734 writepos += conf->min_offset_diff;
5735
2c810cdd 5736 if ((mddev->reshape_backwards
c8f517c4
N
5737 ? (safepos > writepos && readpos < writepos)
5738 : (safepos < writepos && readpos > writepos)) ||
5739 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
52c03291
N
5740 /* Cannot proceed until we've updated the superblock... */
5741 wait_event(conf->wait_for_overlap,
c91abf5a
N
5742 atomic_read(&conf->reshape_stripes)==0
5743 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5744 if (atomic_read(&conf->reshape_stripes) != 0)
5745 return 0;
fef9c61f 5746 mddev->reshape_position = conf->reshape_progress;
75d3da43 5747 mddev->curr_resync_completed = sector_nr;
c8f517c4 5748 conf->reshape_checkpoint = jiffies;
2953079c 5749 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
52c03291 5750 md_wakeup_thread(mddev->thread);
2953079c 5751 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
c91abf5a
N
5752 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5753 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5754 return 0;
52c03291 5755 spin_lock_irq(&conf->device_lock);
fef9c61f 5756 conf->reshape_safe = mddev->reshape_position;
52c03291
N
5757 spin_unlock_irq(&conf->device_lock);
5758 wake_up(&conf->wait_for_overlap);
acb180b0 5759 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
52c03291
N
5760 }
5761
ab69ae12 5762 INIT_LIST_HEAD(&stripes);
7a661381 5763 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
52c03291 5764 int j;
a9f326eb 5765 int skipped_disk = 0;
6d036f7d 5766 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
52c03291
N
5767 set_bit(STRIPE_EXPANDING, &sh->state);
5768 atomic_inc(&conf->reshape_stripes);
5769 /* If any of this stripe is beyond the end of the old
5770 * array, then we need to zero those blocks
5771 */
5772 for (j=sh->disks; j--;) {
5773 sector_t s;
5774 if (j == sh->pd_idx)
5775 continue;
f416885e 5776 if (conf->level == 6 &&
d0dabf7e 5777 j == sh->qd_idx)
f416885e 5778 continue;
6d036f7d 5779 s = raid5_compute_blocknr(sh, j, 0);
b522adcd 5780 if (s < raid5_size(mddev, 0, 0)) {
a9f326eb 5781 skipped_disk = 1;
52c03291
N
5782 continue;
5783 }
5784 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5785 set_bit(R5_Expanded, &sh->dev[j].flags);
5786 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5787 }
a9f326eb 5788 if (!skipped_disk) {
52c03291
N
5789 set_bit(STRIPE_EXPAND_READY, &sh->state);
5790 set_bit(STRIPE_HANDLE, &sh->state);
5791 }
ab69ae12 5792 list_add(&sh->lru, &stripes);
52c03291
N
5793 }
5794 spin_lock_irq(&conf->device_lock);
2c810cdd 5795 if (mddev->reshape_backwards)
7a661381 5796 conf->reshape_progress -= reshape_sectors * new_data_disks;
fef9c61f 5797 else
7a661381 5798 conf->reshape_progress += reshape_sectors * new_data_disks;
52c03291
N
5799 spin_unlock_irq(&conf->device_lock);
5800 /* Ok, those stripe are ready. We can start scheduling
5801 * reads on the source stripes.
5802 * The source stripes are determined by mapping the first and last
5803 * block on the destination stripes.
5804 */
52c03291 5805 first_sector =
ec32a2bd 5806 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
911d4ee8 5807 1, &dd_idx, NULL);
52c03291 5808 last_sector =
0e6e0271 5809 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
09c9e5fa 5810 * new_data_disks - 1),
911d4ee8 5811 1, &dd_idx, NULL);
58c0fed4
AN
5812 if (last_sector >= mddev->dev_sectors)
5813 last_sector = mddev->dev_sectors - 1;
52c03291 5814 while (first_sector <= last_sector) {
6d036f7d 5815 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
52c03291
N
5816 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5817 set_bit(STRIPE_HANDLE, &sh->state);
6d036f7d 5818 raid5_release_stripe(sh);
52c03291
N
5819 first_sector += STRIPE_SECTORS;
5820 }
ab69ae12
N
5821 /* Now that the sources are clearly marked, we can release
5822 * the destination stripes
5823 */
5824 while (!list_empty(&stripes)) {
5825 sh = list_entry(stripes.next, struct stripe_head, lru);
5826 list_del_init(&sh->lru);
6d036f7d 5827 raid5_release_stripe(sh);
ab69ae12 5828 }
c6207277
N
5829 /* If this takes us to the resync_max point where we have to pause,
5830 * then we need to write out the superblock.
5831 */
7a661381 5832 sector_nr += reshape_sectors;
92140480
N
5833 retn = reshape_sectors;
5834finish:
c5e19d90
N
5835 if (mddev->curr_resync_completed > mddev->resync_max ||
5836 (sector_nr - mddev->curr_resync_completed) * 2
c03f6a19 5837 >= mddev->resync_max - mddev->curr_resync_completed) {
c6207277
N
5838 /* Cannot proceed until we've updated the superblock... */
5839 wait_event(conf->wait_for_overlap,
c91abf5a
N
5840 atomic_read(&conf->reshape_stripes) == 0
5841 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5842 if (atomic_read(&conf->reshape_stripes) != 0)
5843 goto ret;
fef9c61f 5844 mddev->reshape_position = conf->reshape_progress;
75d3da43 5845 mddev->curr_resync_completed = sector_nr;
c8f517c4 5846 conf->reshape_checkpoint = jiffies;
2953079c 5847 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
c6207277
N
5848 md_wakeup_thread(mddev->thread);
5849 wait_event(mddev->sb_wait,
2953079c 5850 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
c91abf5a
N
5851 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5852 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5853 goto ret;
c6207277 5854 spin_lock_irq(&conf->device_lock);
fef9c61f 5855 conf->reshape_safe = mddev->reshape_position;
c6207277
N
5856 spin_unlock_irq(&conf->device_lock);
5857 wake_up(&conf->wait_for_overlap);
acb180b0 5858 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
c6207277 5859 }
c91abf5a 5860ret:
92140480 5861 return retn;
52c03291
N
5862}
5863
849674e4
SL
5864static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5865 int *skipped)
52c03291 5866{
d1688a6d 5867 struct r5conf *conf = mddev->private;
52c03291 5868 struct stripe_head *sh;
58c0fed4 5869 sector_t max_sector = mddev->dev_sectors;
57dab0bd 5870 sector_t sync_blocks;
16a53ecc
N
5871 int still_degraded = 0;
5872 int i;
1da177e4 5873
72626685 5874 if (sector_nr >= max_sector) {
1da177e4 5875 /* just being told to finish up .. nothing much to do */
cea9c228 5876
29269553
N
5877 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5878 end_reshape(conf);
5879 return 0;
5880 }
72626685
N
5881
5882 if (mddev->curr_resync < max_sector) /* aborted */
5883 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5884 &sync_blocks, 1);
16a53ecc 5885 else /* completed sync */
72626685
N
5886 conf->fullsync = 0;
5887 bitmap_close_sync(mddev->bitmap);
5888
1da177e4
LT
5889 return 0;
5890 }
ccfcc3c1 5891
64bd660b
N
5892 /* Allow raid5_quiesce to complete */
5893 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5894
52c03291
N
5895 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5896 return reshape_request(mddev, sector_nr, skipped);
f6705578 5897
c6207277
N
5898 /* No need to check resync_max as we never do more than one
5899 * stripe, and as resync_max will always be on a chunk boundary,
5900 * if the check in md_do_sync didn't fire, there is no chance
5901 * of overstepping resync_max here
5902 */
5903
16a53ecc 5904 /* if there is too many failed drives and we are trying
1da177e4
LT
5905 * to resync, then assert that we are finished, because there is
5906 * nothing we can do.
5907 */
3285edf1 5908 if (mddev->degraded >= conf->max_degraded &&
16a53ecc 5909 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
58c0fed4 5910 sector_t rv = mddev->dev_sectors - sector_nr;
57afd89f 5911 *skipped = 1;
1da177e4
LT
5912 return rv;
5913 }
6f608040 5914 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5915 !conf->fullsync &&
5916 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5917 sync_blocks >= STRIPE_SECTORS) {
72626685
N
5918 /* we can skip this block, and probably more */
5919 sync_blocks /= STRIPE_SECTORS;
5920 *skipped = 1;
5921 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5922 }
1da177e4 5923
c40f341f 5924 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
b47490c9 5925
6d036f7d 5926 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
1da177e4 5927 if (sh == NULL) {
6d036f7d 5928 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
1da177e4 5929 /* make sure we don't swamp the stripe cache if someone else
16a53ecc 5930 * is trying to get access
1da177e4 5931 */
66c006a5 5932 schedule_timeout_uninterruptible(1);
1da177e4 5933 }
16a53ecc 5934 /* Need to check if array will still be degraded after recovery/resync
16d9cfab
EM
5935 * Note in case of > 1 drive failures it's possible we're rebuilding
5936 * one drive while leaving another faulty drive in array.
16a53ecc 5937 */
16d9cfab
EM
5938 rcu_read_lock();
5939 for (i = 0; i < conf->raid_disks; i++) {
5940 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5941
5942 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
16a53ecc 5943 still_degraded = 1;
16d9cfab
EM
5944 }
5945 rcu_read_unlock();
16a53ecc
N
5946
5947 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5948
83206d66 5949 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
053f5b65 5950 set_bit(STRIPE_HANDLE, &sh->state);
1da177e4 5951
6d036f7d 5952 raid5_release_stripe(sh);
1da177e4
LT
5953
5954 return STRIPE_SECTORS;
5955}
5956
d1688a6d 5957static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
46031f9a
RBJ
5958{
5959 /* We may not be able to submit a whole bio at once as there
5960 * may not be enough stripe_heads available.
5961 * We cannot pre-allocate enough stripe_heads as we may need
5962 * more than exist in the cache (if we allow ever large chunks).
5963 * So we do one stripe head at a time and record in
5964 * ->bi_hw_segments how many have been done.
5965 *
5966 * We *know* that this entire raid_bio is in one chunk, so
5967 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5968 */
5969 struct stripe_head *sh;
911d4ee8 5970 int dd_idx;
46031f9a
RBJ
5971 sector_t sector, logical_sector, last_sector;
5972 int scnt = 0;
5973 int remaining;
5974 int handled = 0;
5975
4f024f37
KO
5976 logical_sector = raid_bio->bi_iter.bi_sector &
5977 ~((sector_t)STRIPE_SECTORS-1);
112bf897 5978 sector = raid5_compute_sector(conf, logical_sector,
911d4ee8 5979 0, &dd_idx, NULL);
f73a1c7d 5980 last_sector = bio_end_sector(raid_bio);
46031f9a
RBJ
5981
5982 for (; logical_sector < last_sector;
387bb173
NB
5983 logical_sector += STRIPE_SECTORS,
5984 sector += STRIPE_SECTORS,
5985 scnt++) {
46031f9a 5986
e7836bd6 5987 if (scnt < raid5_bi_processed_stripes(raid_bio))
46031f9a
RBJ
5988 /* already done this stripe */
5989 continue;
5990
6d036f7d 5991 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
46031f9a
RBJ
5992
5993 if (!sh) {
5994 /* failed to get a stripe - must wait */
e7836bd6 5995 raid5_set_bi_processed_stripes(raid_bio, scnt);
46031f9a
RBJ
5996 conf->retry_read_aligned = raid_bio;
5997 return handled;
5998 }
5999
da41ba65 6000 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
6d036f7d 6001 raid5_release_stripe(sh);
e7836bd6 6002 raid5_set_bi_processed_stripes(raid_bio, scnt);
387bb173
NB
6003 conf->retry_read_aligned = raid_bio;
6004 return handled;
6005 }
6006
3f9e7c14 6007 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
36d1c647 6008 handle_stripe(sh);
6d036f7d 6009 raid5_release_stripe(sh);
46031f9a
RBJ
6010 handled++;
6011 }
e7836bd6 6012 remaining = raid5_dec_bi_active_stripes(raid_bio);
0a82a8d1
LT
6013 if (remaining == 0) {
6014 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
6015 raid_bio, 0);
4246a0b6 6016 bio_endio(raid_bio);
0a82a8d1 6017 }
46031f9a 6018 if (atomic_dec_and_test(&conf->active_aligned_reads))
b1b46486 6019 wake_up(&conf->wait_for_quiescent);
46031f9a
RBJ
6020 return handled;
6021}
6022
bfc90cb0 6023static int handle_active_stripes(struct r5conf *conf, int group,
566c09c5
SL
6024 struct r5worker *worker,
6025 struct list_head *temp_inactive_list)
46a06401
SL
6026{
6027 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
566c09c5
SL
6028 int i, batch_size = 0, hash;
6029 bool release_inactive = false;
46a06401
SL
6030
6031 while (batch_size < MAX_STRIPE_BATCH &&
851c30c9 6032 (sh = __get_priority_stripe(conf, group)) != NULL)
46a06401
SL
6033 batch[batch_size++] = sh;
6034
566c09c5
SL
6035 if (batch_size == 0) {
6036 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6037 if (!list_empty(temp_inactive_list + i))
6038 break;
a8c34f91
SL
6039 if (i == NR_STRIPE_HASH_LOCKS) {
6040 spin_unlock_irq(&conf->device_lock);
6041 r5l_flush_stripe_to_raid(conf->log);
6042 spin_lock_irq(&conf->device_lock);
566c09c5 6043 return batch_size;
a8c34f91 6044 }
566c09c5
SL
6045 release_inactive = true;
6046 }
46a06401
SL
6047 spin_unlock_irq(&conf->device_lock);
6048
566c09c5
SL
6049 release_inactive_stripe_list(conf, temp_inactive_list,
6050 NR_STRIPE_HASH_LOCKS);
6051
a8c34f91 6052 r5l_flush_stripe_to_raid(conf->log);
566c09c5
SL
6053 if (release_inactive) {
6054 spin_lock_irq(&conf->device_lock);
6055 return 0;
6056 }
6057
46a06401
SL
6058 for (i = 0; i < batch_size; i++)
6059 handle_stripe(batch[i]);
f6bed0ef 6060 r5l_write_stripe_run(conf->log);
46a06401
SL
6061
6062 cond_resched();
6063
6064 spin_lock_irq(&conf->device_lock);
566c09c5
SL
6065 for (i = 0; i < batch_size; i++) {
6066 hash = batch[i]->hash_lock_index;
6067 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6068 }
46a06401
SL
6069 return batch_size;
6070}
46031f9a 6071
851c30c9
SL
6072static void raid5_do_work(struct work_struct *work)
6073{
6074 struct r5worker *worker = container_of(work, struct r5worker, work);
6075 struct r5worker_group *group = worker->group;
6076 struct r5conf *conf = group->conf;
6077 int group_id = group - conf->worker_groups;
6078 int handled;
6079 struct blk_plug plug;
6080
6081 pr_debug("+++ raid5worker active\n");
6082
6083 blk_start_plug(&plug);
6084 handled = 0;
6085 spin_lock_irq(&conf->device_lock);
6086 while (1) {
6087 int batch_size, released;
6088
566c09c5 6089 released = release_stripe_list(conf, worker->temp_inactive_list);
851c30c9 6090
566c09c5
SL
6091 batch_size = handle_active_stripes(conf, group_id, worker,
6092 worker->temp_inactive_list);
bfc90cb0 6093 worker->working = false;
851c30c9
SL
6094 if (!batch_size && !released)
6095 break;
6096 handled += batch_size;
6097 }
6098 pr_debug("%d stripes handled\n", handled);
6099
6100 spin_unlock_irq(&conf->device_lock);
6101 blk_finish_plug(&plug);
6102
6103 pr_debug("--- raid5worker inactive\n");
6104}
6105
1da177e4
LT
6106/*
6107 * This is our raid5 kernel thread.
6108 *
6109 * We scan the hash table for stripes which can be handled now.
6110 * During the scan, completed stripes are saved for us by the interrupt
6111 * handler, so that they will not have to wait for our next wakeup.
6112 */
4ed8731d 6113static void raid5d(struct md_thread *thread)
1da177e4 6114{
4ed8731d 6115 struct mddev *mddev = thread->mddev;
d1688a6d 6116 struct r5conf *conf = mddev->private;
1da177e4 6117 int handled;
e1dfa0a2 6118 struct blk_plug plug;
1da177e4 6119
45b4233c 6120 pr_debug("+++ raid5d active\n");
1da177e4
LT
6121
6122 md_check_recovery(mddev);
1da177e4 6123
c3cce6cd 6124 if (!bio_list_empty(&conf->return_bi) &&
2953079c 6125 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
c3cce6cd
N
6126 struct bio_list tmp = BIO_EMPTY_LIST;
6127 spin_lock_irq(&conf->device_lock);
2953079c 6128 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
c3cce6cd
N
6129 bio_list_merge(&tmp, &conf->return_bi);
6130 bio_list_init(&conf->return_bi);
6131 }
6132 spin_unlock_irq(&conf->device_lock);
6133 return_io(&tmp);
6134 }
6135
e1dfa0a2 6136 blk_start_plug(&plug);
1da177e4
LT
6137 handled = 0;
6138 spin_lock_irq(&conf->device_lock);
6139 while (1) {
46031f9a 6140 struct bio *bio;
773ca82f
SL
6141 int batch_size, released;
6142
566c09c5 6143 released = release_stripe_list(conf, conf->temp_inactive_list);
edbe83ab
N
6144 if (released)
6145 clear_bit(R5_DID_ALLOC, &conf->cache_state);
1da177e4 6146
0021b7bc 6147 if (
7c13edc8
N
6148 !list_empty(&conf->bitmap_list)) {
6149 /* Now is a good time to flush some bitmap updates */
6150 conf->seq_flush++;
700e432d 6151 spin_unlock_irq(&conf->device_lock);
72626685 6152 bitmap_unplug(mddev->bitmap);
700e432d 6153 spin_lock_irq(&conf->device_lock);
7c13edc8 6154 conf->seq_write = conf->seq_flush;
566c09c5 6155 activate_bit_delay(conf, conf->temp_inactive_list);
72626685 6156 }
0021b7bc 6157 raid5_activate_delayed(conf);
72626685 6158
46031f9a
RBJ
6159 while ((bio = remove_bio_from_retry(conf))) {
6160 int ok;
6161 spin_unlock_irq(&conf->device_lock);
6162 ok = retry_aligned_read(conf, bio);
6163 spin_lock_irq(&conf->device_lock);
6164 if (!ok)
6165 break;
6166 handled++;
6167 }
6168
566c09c5
SL
6169 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6170 conf->temp_inactive_list);
773ca82f 6171 if (!batch_size && !released)
1da177e4 6172 break;
46a06401 6173 handled += batch_size;
1da177e4 6174
2953079c 6175 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
46a06401 6176 spin_unlock_irq(&conf->device_lock);
de393cde 6177 md_check_recovery(mddev);
46a06401
SL
6178 spin_lock_irq(&conf->device_lock);
6179 }
1da177e4 6180 }
45b4233c 6181 pr_debug("%d stripes handled\n", handled);
1da177e4
LT
6182
6183 spin_unlock_irq(&conf->device_lock);
2d5b569b
N
6184 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6185 mutex_trylock(&conf->cache_size_mutex)) {
edbe83ab
N
6186 grow_one_stripe(conf, __GFP_NOWARN);
6187 /* Set flag even if allocation failed. This helps
6188 * slow down allocation requests when mem is short
6189 */
6190 set_bit(R5_DID_ALLOC, &conf->cache_state);
2d5b569b 6191 mutex_unlock(&conf->cache_size_mutex);
edbe83ab 6192 }
1da177e4 6193
765d704d
SL
6194 flush_deferred_bios(conf);
6195
0576b1c6
SL
6196 r5l_flush_stripe_to_raid(conf->log);
6197
c9f21aaf 6198 async_tx_issue_pending_all();
e1dfa0a2 6199 blk_finish_plug(&plug);
1da177e4 6200
45b4233c 6201 pr_debug("--- raid5d inactive\n");
1da177e4
LT
6202}
6203
3f294f4f 6204static ssize_t
fd01b88c 6205raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
3f294f4f 6206{
7b1485ba
N
6207 struct r5conf *conf;
6208 int ret = 0;
6209 spin_lock(&mddev->lock);
6210 conf = mddev->private;
96de1e66 6211 if (conf)
edbe83ab 6212 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
7b1485ba
N
6213 spin_unlock(&mddev->lock);
6214 return ret;
3f294f4f
N
6215}
6216
c41d4ac4 6217int
fd01b88c 6218raid5_set_cache_size(struct mddev *mddev, int size)
3f294f4f 6219{
d1688a6d 6220 struct r5conf *conf = mddev->private;
b5470dc5
DW
6221 int err;
6222
c41d4ac4 6223 if (size <= 16 || size > 32768)
3f294f4f 6224 return -EINVAL;
486f0644 6225
edbe83ab 6226 conf->min_nr_stripes = size;
2d5b569b 6227 mutex_lock(&conf->cache_size_mutex);
486f0644
N
6228 while (size < conf->max_nr_stripes &&
6229 drop_one_stripe(conf))
6230 ;
2d5b569b 6231 mutex_unlock(&conf->cache_size_mutex);
486f0644 6232
edbe83ab 6233
b5470dc5
DW
6234 err = md_allow_write(mddev);
6235 if (err)
6236 return err;
486f0644 6237
2d5b569b 6238 mutex_lock(&conf->cache_size_mutex);
486f0644
N
6239 while (size > conf->max_nr_stripes)
6240 if (!grow_one_stripe(conf, GFP_KERNEL))
6241 break;
2d5b569b 6242 mutex_unlock(&conf->cache_size_mutex);
486f0644 6243
c41d4ac4
N
6244 return 0;
6245}
6246EXPORT_SYMBOL(raid5_set_cache_size);
6247
6248static ssize_t
fd01b88c 6249raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
c41d4ac4 6250{
6791875e 6251 struct r5conf *conf;
c41d4ac4
N
6252 unsigned long new;
6253 int err;
6254
6255 if (len >= PAGE_SIZE)
6256 return -EINVAL;
b29bebd6 6257 if (kstrtoul(page, 10, &new))
c41d4ac4 6258 return -EINVAL;
6791875e 6259 err = mddev_lock(mddev);
c41d4ac4
N
6260 if (err)
6261 return err;
6791875e
N
6262 conf = mddev->private;
6263 if (!conf)
6264 err = -ENODEV;
6265 else
6266 err = raid5_set_cache_size(mddev, new);
6267 mddev_unlock(mddev);
6268
6269 return err ?: len;
3f294f4f 6270}
007583c9 6271
96de1e66
N
6272static struct md_sysfs_entry
6273raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6274 raid5_show_stripe_cache_size,
6275 raid5_store_stripe_cache_size);
3f294f4f 6276
d06f191f
MS
6277static ssize_t
6278raid5_show_rmw_level(struct mddev *mddev, char *page)
6279{
6280 struct r5conf *conf = mddev->private;
6281 if (conf)
6282 return sprintf(page, "%d\n", conf->rmw_level);
6283 else
6284 return 0;
6285}
6286
6287static ssize_t
6288raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6289{
6290 struct r5conf *conf = mddev->private;
6291 unsigned long new;
6292
6293 if (!conf)
6294 return -ENODEV;
6295
6296 if (len >= PAGE_SIZE)
6297 return -EINVAL;
6298
6299 if (kstrtoul(page, 10, &new))
6300 return -EINVAL;
6301
6302 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6303 return -EINVAL;
6304
6305 if (new != PARITY_DISABLE_RMW &&
6306 new != PARITY_ENABLE_RMW &&
6307 new != PARITY_PREFER_RMW)
6308 return -EINVAL;
6309
6310 conf->rmw_level = new;
6311 return len;
6312}
6313
6314static struct md_sysfs_entry
6315raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6316 raid5_show_rmw_level,
6317 raid5_store_rmw_level);
6318
6319
8b3e6cdc 6320static ssize_t
fd01b88c 6321raid5_show_preread_threshold(struct mddev *mddev, char *page)
8b3e6cdc 6322{
7b1485ba
N
6323 struct r5conf *conf;
6324 int ret = 0;
6325 spin_lock(&mddev->lock);
6326 conf = mddev->private;
8b3e6cdc 6327 if (conf)
7b1485ba
N
6328 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6329 spin_unlock(&mddev->lock);
6330 return ret;
8b3e6cdc
DW
6331}
6332
6333static ssize_t
fd01b88c 6334raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
8b3e6cdc 6335{
6791875e 6336 struct r5conf *conf;
4ef197d8 6337 unsigned long new;
6791875e
N
6338 int err;
6339
8b3e6cdc
DW
6340 if (len >= PAGE_SIZE)
6341 return -EINVAL;
b29bebd6 6342 if (kstrtoul(page, 10, &new))
8b3e6cdc 6343 return -EINVAL;
6791875e
N
6344
6345 err = mddev_lock(mddev);
6346 if (err)
6347 return err;
6348 conf = mddev->private;
6349 if (!conf)
6350 err = -ENODEV;
edbe83ab 6351 else if (new > conf->min_nr_stripes)
6791875e
N
6352 err = -EINVAL;
6353 else
6354 conf->bypass_threshold = new;
6355 mddev_unlock(mddev);
6356 return err ?: len;
8b3e6cdc
DW
6357}
6358
6359static struct md_sysfs_entry
6360raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6361 S_IRUGO | S_IWUSR,
6362 raid5_show_preread_threshold,
6363 raid5_store_preread_threshold);
6364
d592a996
SL
6365static ssize_t
6366raid5_show_skip_copy(struct mddev *mddev, char *page)
6367{
7b1485ba
N
6368 struct r5conf *conf;
6369 int ret = 0;
6370 spin_lock(&mddev->lock);
6371 conf = mddev->private;
d592a996 6372 if (conf)
7b1485ba
N
6373 ret = sprintf(page, "%d\n", conf->skip_copy);
6374 spin_unlock(&mddev->lock);
6375 return ret;
d592a996
SL
6376}
6377
6378static ssize_t
6379raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6380{
6791875e 6381 struct r5conf *conf;
d592a996 6382 unsigned long new;
6791875e
N
6383 int err;
6384
d592a996
SL
6385 if (len >= PAGE_SIZE)
6386 return -EINVAL;
d592a996
SL
6387 if (kstrtoul(page, 10, &new))
6388 return -EINVAL;
6389 new = !!new;
6791875e
N
6390
6391 err = mddev_lock(mddev);
6392 if (err)
6393 return err;
6394 conf = mddev->private;
6395 if (!conf)
6396 err = -ENODEV;
6397 else if (new != conf->skip_copy) {
6398 mddev_suspend(mddev);
6399 conf->skip_copy = new;
6400 if (new)
dc3b17cc 6401 mddev->queue->backing_dev_info->capabilities |=
6791875e
N
6402 BDI_CAP_STABLE_WRITES;
6403 else
dc3b17cc 6404 mddev->queue->backing_dev_info->capabilities &=
6791875e
N
6405 ~BDI_CAP_STABLE_WRITES;
6406 mddev_resume(mddev);
6407 }
6408 mddev_unlock(mddev);
6409 return err ?: len;
d592a996
SL
6410}
6411
6412static struct md_sysfs_entry
6413raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6414 raid5_show_skip_copy,
6415 raid5_store_skip_copy);
6416
3f294f4f 6417static ssize_t
fd01b88c 6418stripe_cache_active_show(struct mddev *mddev, char *page)
3f294f4f 6419{
d1688a6d 6420 struct r5conf *conf = mddev->private;
96de1e66
N
6421 if (conf)
6422 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6423 else
6424 return 0;
3f294f4f
N
6425}
6426
96de1e66
N
6427static struct md_sysfs_entry
6428raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3f294f4f 6429
b721420e
SL
6430static ssize_t
6431raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6432{
7b1485ba
N
6433 struct r5conf *conf;
6434 int ret = 0;
6435 spin_lock(&mddev->lock);
6436 conf = mddev->private;
b721420e 6437 if (conf)
7b1485ba
N
6438 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6439 spin_unlock(&mddev->lock);
6440 return ret;
b721420e
SL
6441}
6442
60aaf933 6443static int alloc_thread_groups(struct r5conf *conf, int cnt,
6444 int *group_cnt,
6445 int *worker_cnt_per_group,
6446 struct r5worker_group **worker_groups);
b721420e
SL
6447static ssize_t
6448raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6449{
6791875e 6450 struct r5conf *conf;
b721420e
SL
6451 unsigned long new;
6452 int err;
60aaf933 6453 struct r5worker_group *new_groups, *old_groups;
6454 int group_cnt, worker_cnt_per_group;
b721420e
SL
6455
6456 if (len >= PAGE_SIZE)
6457 return -EINVAL;
b721420e
SL
6458 if (kstrtoul(page, 10, &new))
6459 return -EINVAL;
6460
6791875e
N
6461 err = mddev_lock(mddev);
6462 if (err)
6463 return err;
6464 conf = mddev->private;
6465 if (!conf)
6466 err = -ENODEV;
6467 else if (new != conf->worker_cnt_per_group) {
6468 mddev_suspend(mddev);
b721420e 6469
6791875e
N
6470 old_groups = conf->worker_groups;
6471 if (old_groups)
6472 flush_workqueue(raid5_wq);
d206dcfa 6473
6791875e
N
6474 err = alloc_thread_groups(conf, new,
6475 &group_cnt, &worker_cnt_per_group,
6476 &new_groups);
6477 if (!err) {
6478 spin_lock_irq(&conf->device_lock);
6479 conf->group_cnt = group_cnt;
6480 conf->worker_cnt_per_group = worker_cnt_per_group;
6481 conf->worker_groups = new_groups;
6482 spin_unlock_irq(&conf->device_lock);
b721420e 6483
6791875e
N
6484 if (old_groups)
6485 kfree(old_groups[0].workers);
6486 kfree(old_groups);
6487 }
6488 mddev_resume(mddev);
b721420e 6489 }
6791875e 6490 mddev_unlock(mddev);
b721420e 6491
6791875e 6492 return err ?: len;
b721420e
SL
6493}
6494
6495static struct md_sysfs_entry
6496raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6497 raid5_show_group_thread_cnt,
6498 raid5_store_group_thread_cnt);
6499
007583c9 6500static struct attribute *raid5_attrs[] = {
3f294f4f
N
6501 &raid5_stripecache_size.attr,
6502 &raid5_stripecache_active.attr,
8b3e6cdc 6503 &raid5_preread_bypass_threshold.attr,
b721420e 6504 &raid5_group_thread_cnt.attr,
d592a996 6505 &raid5_skip_copy.attr,
d06f191f 6506 &raid5_rmw_level.attr,
2c7da14b 6507 &r5c_journal_mode.attr,
3f294f4f
N
6508 NULL,
6509};
007583c9
N
6510static struct attribute_group raid5_attrs_group = {
6511 .name = NULL,
6512 .attrs = raid5_attrs,
3f294f4f
N
6513};
6514
60aaf933 6515static int alloc_thread_groups(struct r5conf *conf, int cnt,
6516 int *group_cnt,
6517 int *worker_cnt_per_group,
6518 struct r5worker_group **worker_groups)
851c30c9 6519{
566c09c5 6520 int i, j, k;
851c30c9
SL
6521 ssize_t size;
6522 struct r5worker *workers;
6523
60aaf933 6524 *worker_cnt_per_group = cnt;
851c30c9 6525 if (cnt == 0) {
60aaf933 6526 *group_cnt = 0;
6527 *worker_groups = NULL;
851c30c9
SL
6528 return 0;
6529 }
60aaf933 6530 *group_cnt = num_possible_nodes();
851c30c9 6531 size = sizeof(struct r5worker) * cnt;
60aaf933 6532 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6533 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6534 *group_cnt, GFP_NOIO);
6535 if (!*worker_groups || !workers) {
851c30c9 6536 kfree(workers);
60aaf933 6537 kfree(*worker_groups);
851c30c9
SL
6538 return -ENOMEM;
6539 }
6540
60aaf933 6541 for (i = 0; i < *group_cnt; i++) {
851c30c9
SL
6542 struct r5worker_group *group;
6543
0c775d52 6544 group = &(*worker_groups)[i];
851c30c9
SL
6545 INIT_LIST_HEAD(&group->handle_list);
6546 group->conf = conf;
6547 group->workers = workers + i * cnt;
6548
6549 for (j = 0; j < cnt; j++) {
566c09c5
SL
6550 struct r5worker *worker = group->workers + j;
6551 worker->group = group;
6552 INIT_WORK(&worker->work, raid5_do_work);
6553
6554 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6555 INIT_LIST_HEAD(worker->temp_inactive_list + k);
851c30c9
SL
6556 }
6557 }
6558
6559 return 0;
6560}
6561
6562static void free_thread_groups(struct r5conf *conf)
6563{
6564 if (conf->worker_groups)
6565 kfree(conf->worker_groups[0].workers);
6566 kfree(conf->worker_groups);
6567 conf->worker_groups = NULL;
6568}
6569
80c3a6ce 6570static sector_t
fd01b88c 6571raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
80c3a6ce 6572{
d1688a6d 6573 struct r5conf *conf = mddev->private;
80c3a6ce
DW
6574
6575 if (!sectors)
6576 sectors = mddev->dev_sectors;
5e5e3e78 6577 if (!raid_disks)
7ec05478 6578 /* size is defined by the smallest of previous and new size */
5e5e3e78 6579 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
80c3a6ce 6580
3cb5edf4
N
6581 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6582 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
80c3a6ce
DW
6583 return sectors * (raid_disks - conf->max_degraded);
6584}
6585
789b5e03
ON
6586static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6587{
6588 safe_put_page(percpu->spare_page);
46d5b785 6589 if (percpu->scribble)
6590 flex_array_free(percpu->scribble);
789b5e03
ON
6591 percpu->spare_page = NULL;
6592 percpu->scribble = NULL;
6593}
6594
6595static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6596{
6597 if (conf->level == 6 && !percpu->spare_page)
6598 percpu->spare_page = alloc_page(GFP_KERNEL);
6599 if (!percpu->scribble)
46d5b785 6600 percpu->scribble = scribble_alloc(max(conf->raid_disks,
738a2738
N
6601 conf->previous_raid_disks),
6602 max(conf->chunk_sectors,
6603 conf->prev_chunk_sectors)
6604 / STRIPE_SECTORS,
6605 GFP_KERNEL);
789b5e03
ON
6606
6607 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6608 free_scratch_buffer(conf, percpu);
6609 return -ENOMEM;
6610 }
6611
6612 return 0;
6613}
6614
29c6d1bb 6615static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
36d1c647 6616{
29c6d1bb
SAS
6617 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6618
6619 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6620 return 0;
6621}
36d1c647 6622
29c6d1bb
SAS
6623static void raid5_free_percpu(struct r5conf *conf)
6624{
36d1c647
DW
6625 if (!conf->percpu)
6626 return;
6627
29c6d1bb 6628 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
36d1c647
DW
6629 free_percpu(conf->percpu);
6630}
6631
d1688a6d 6632static void free_conf(struct r5conf *conf)
95fc17aa 6633{
d7bd398e
SL
6634 int i;
6635
5c7e81c3
SL
6636 if (conf->log)
6637 r5l_exit_log(conf->log);
30c89465 6638 if (conf->shrinker.nr_deferred)
edbe83ab 6639 unregister_shrinker(&conf->shrinker);
5c7e81c3 6640
851c30c9 6641 free_thread_groups(conf);
95fc17aa 6642 shrink_stripes(conf);
36d1c647 6643 raid5_free_percpu(conf);
d7bd398e
SL
6644 for (i = 0; i < conf->pool_size; i++)
6645 if (conf->disks[i].extra_page)
6646 put_page(conf->disks[i].extra_page);
95fc17aa
DW
6647 kfree(conf->disks);
6648 kfree(conf->stripe_hashtbl);
6649 kfree(conf);
6650}
6651
29c6d1bb 6652static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
36d1c647 6653{
29c6d1bb 6654 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
36d1c647
DW
6655 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6656
29c6d1bb 6657 if (alloc_scratch_buffer(conf, percpu)) {
cc6167b4
N
6658 pr_warn("%s: failed memory allocation for cpu%u\n",
6659 __func__, cpu);
29c6d1bb 6660 return -ENOMEM;
36d1c647 6661 }
29c6d1bb 6662 return 0;
36d1c647 6663}
36d1c647 6664
d1688a6d 6665static int raid5_alloc_percpu(struct r5conf *conf)
36d1c647 6666{
789b5e03 6667 int err = 0;
36d1c647 6668
789b5e03
ON
6669 conf->percpu = alloc_percpu(struct raid5_percpu);
6670 if (!conf->percpu)
36d1c647 6671 return -ENOMEM;
789b5e03 6672
29c6d1bb 6673 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
27a353c0
SL
6674 if (!err) {
6675 conf->scribble_disks = max(conf->raid_disks,
6676 conf->previous_raid_disks);
6677 conf->scribble_sectors = max(conf->chunk_sectors,
6678 conf->prev_chunk_sectors);
6679 }
36d1c647
DW
6680 return err;
6681}
6682
edbe83ab
N
6683static unsigned long raid5_cache_scan(struct shrinker *shrink,
6684 struct shrink_control *sc)
6685{
6686 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
2d5b569b
N
6687 unsigned long ret = SHRINK_STOP;
6688
6689 if (mutex_trylock(&conf->cache_size_mutex)) {
6690 ret= 0;
49895bcc
N
6691 while (ret < sc->nr_to_scan &&
6692 conf->max_nr_stripes > conf->min_nr_stripes) {
2d5b569b
N
6693 if (drop_one_stripe(conf) == 0) {
6694 ret = SHRINK_STOP;
6695 break;
6696 }
6697 ret++;
6698 }
6699 mutex_unlock(&conf->cache_size_mutex);
edbe83ab
N
6700 }
6701 return ret;
6702}
6703
6704static unsigned long raid5_cache_count(struct shrinker *shrink,
6705 struct shrink_control *sc)
6706{
6707 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6708
6709 if (conf->max_nr_stripes < conf->min_nr_stripes)
6710 /* unlikely, but not impossible */
6711 return 0;
6712 return conf->max_nr_stripes - conf->min_nr_stripes;
6713}
6714
d1688a6d 6715static struct r5conf *setup_conf(struct mddev *mddev)
1da177e4 6716{
d1688a6d 6717 struct r5conf *conf;
5e5e3e78 6718 int raid_disk, memory, max_disks;
3cb03002 6719 struct md_rdev *rdev;
1da177e4 6720 struct disk_info *disk;
0232605d 6721 char pers_name[6];
566c09c5 6722 int i;
60aaf933 6723 int group_cnt, worker_cnt_per_group;
6724 struct r5worker_group *new_group;
1da177e4 6725
91adb564
N
6726 if (mddev->new_level != 5
6727 && mddev->new_level != 4
6728 && mddev->new_level != 6) {
cc6167b4
N
6729 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6730 mdname(mddev), mddev->new_level);
91adb564 6731 return ERR_PTR(-EIO);
1da177e4 6732 }
91adb564
N
6733 if ((mddev->new_level == 5
6734 && !algorithm_valid_raid5(mddev->new_layout)) ||
6735 (mddev->new_level == 6
6736 && !algorithm_valid_raid6(mddev->new_layout))) {
cc6167b4
N
6737 pr_warn("md/raid:%s: layout %d not supported\n",
6738 mdname(mddev), mddev->new_layout);
91adb564 6739 return ERR_PTR(-EIO);
99c0fb5f 6740 }
91adb564 6741 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
cc6167b4
N
6742 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6743 mdname(mddev), mddev->raid_disks);
91adb564 6744 return ERR_PTR(-EINVAL);
4bbf3771
N
6745 }
6746
664e7c41
AN
6747 if (!mddev->new_chunk_sectors ||
6748 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6749 !is_power_of_2(mddev->new_chunk_sectors)) {
cc6167b4
N
6750 pr_warn("md/raid:%s: invalid chunk size %d\n",
6751 mdname(mddev), mddev->new_chunk_sectors << 9);
91adb564 6752 return ERR_PTR(-EINVAL);
f6705578
N
6753 }
6754
d1688a6d 6755 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
91adb564 6756 if (conf == NULL)
1da177e4 6757 goto abort;
851c30c9 6758 /* Don't enable multi-threading by default*/
60aaf933 6759 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6760 &new_group)) {
6761 conf->group_cnt = group_cnt;
6762 conf->worker_cnt_per_group = worker_cnt_per_group;
6763 conf->worker_groups = new_group;
6764 } else
851c30c9 6765 goto abort;
f5efd45a 6766 spin_lock_init(&conf->device_lock);
c46501b2 6767 seqcount_init(&conf->gen_lock);
2d5b569b 6768 mutex_init(&conf->cache_size_mutex);
b1b46486 6769 init_waitqueue_head(&conf->wait_for_quiescent);
6ab2a4b8 6770 init_waitqueue_head(&conf->wait_for_stripe);
f5efd45a
DW
6771 init_waitqueue_head(&conf->wait_for_overlap);
6772 INIT_LIST_HEAD(&conf->handle_list);
6773 INIT_LIST_HEAD(&conf->hold_list);
6774 INIT_LIST_HEAD(&conf->delayed_list);
6775 INIT_LIST_HEAD(&conf->bitmap_list);
c3cce6cd 6776 bio_list_init(&conf->return_bi);
773ca82f 6777 init_llist_head(&conf->released_stripes);
f5efd45a
DW
6778 atomic_set(&conf->active_stripes, 0);
6779 atomic_set(&conf->preread_active_stripes, 0);
6780 atomic_set(&conf->active_aligned_reads, 0);
765d704d
SL
6781 bio_list_init(&conf->pending_bios);
6782 spin_lock_init(&conf->pending_bios_lock);
6783 conf->batch_bio_dispatch = true;
6784 rdev_for_each(rdev, mddev) {
6785 if (test_bit(Journal, &rdev->flags))
6786 continue;
6787 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6788 conf->batch_bio_dispatch = false;
6789 break;
6790 }
6791 }
6792
f5efd45a 6793 conf->bypass_threshold = BYPASS_THRESHOLD;
d890fa2b 6794 conf->recovery_disabled = mddev->recovery_disabled - 1;
91adb564
N
6795
6796 conf->raid_disks = mddev->raid_disks;
6797 if (mddev->reshape_position == MaxSector)
6798 conf->previous_raid_disks = mddev->raid_disks;
6799 else
f6705578 6800 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
5e5e3e78 6801 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
f6705578 6802
5e5e3e78 6803 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
b55e6bfc 6804 GFP_KERNEL);
d7bd398e 6805
b55e6bfc
N
6806 if (!conf->disks)
6807 goto abort;
9ffae0cf 6808
d7bd398e
SL
6809 for (i = 0; i < max_disks; i++) {
6810 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
6811 if (!conf->disks[i].extra_page)
6812 goto abort;
6813 }
6814
1da177e4
LT
6815 conf->mddev = mddev;
6816
fccddba0 6817 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
1da177e4 6818 goto abort;
1da177e4 6819
566c09c5
SL
6820 /* We init hash_locks[0] separately to that it can be used
6821 * as the reference lock in the spin_lock_nest_lock() call
6822 * in lock_all_device_hash_locks_irq in order to convince
6823 * lockdep that we know what we are doing.
6824 */
6825 spin_lock_init(conf->hash_locks);
6826 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6827 spin_lock_init(conf->hash_locks + i);
6828
6829 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6830 INIT_LIST_HEAD(conf->inactive_list + i);
6831
6832 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6833 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6834
1e6d690b
SL
6835 atomic_set(&conf->r5c_cached_full_stripes, 0);
6836 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
6837 atomic_set(&conf->r5c_cached_partial_stripes, 0);
6838 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
e33fbb9c
SL
6839 atomic_set(&conf->r5c_flushing_full_stripes, 0);
6840 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
1e6d690b 6841
36d1c647 6842 conf->level = mddev->new_level;
46d5b785 6843 conf->chunk_sectors = mddev->new_chunk_sectors;
36d1c647
DW
6844 if (raid5_alloc_percpu(conf) != 0)
6845 goto abort;
6846
0c55e022 6847 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
1da177e4 6848
dafb20fa 6849 rdev_for_each(rdev, mddev) {
1da177e4 6850 raid_disk = rdev->raid_disk;
5e5e3e78 6851 if (raid_disk >= max_disks
f2076e7d 6852 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
1da177e4
LT
6853 continue;
6854 disk = conf->disks + raid_disk;
6855
17045f52
N
6856 if (test_bit(Replacement, &rdev->flags)) {
6857 if (disk->replacement)
6858 goto abort;
6859 disk->replacement = rdev;
6860 } else {
6861 if (disk->rdev)
6862 goto abort;
6863 disk->rdev = rdev;
6864 }
1da177e4 6865
b2d444d7 6866 if (test_bit(In_sync, &rdev->flags)) {
1da177e4 6867 char b[BDEVNAME_SIZE];
cc6167b4
N
6868 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6869 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
d6b212f4 6870 } else if (rdev->saved_raid_disk != raid_disk)
8c2e870a
NB
6871 /* Cannot rely on bitmap to complete recovery */
6872 conf->fullsync = 1;
1da177e4
LT
6873 }
6874
91adb564 6875 conf->level = mddev->new_level;
584acdd4 6876 if (conf->level == 6) {
16a53ecc 6877 conf->max_degraded = 2;
584acdd4
MS
6878 if (raid6_call.xor_syndrome)
6879 conf->rmw_level = PARITY_ENABLE_RMW;
6880 else
6881 conf->rmw_level = PARITY_DISABLE_RMW;
6882 } else {
16a53ecc 6883 conf->max_degraded = 1;
584acdd4
MS
6884 conf->rmw_level = PARITY_ENABLE_RMW;
6885 }
91adb564 6886 conf->algorithm = mddev->new_layout;
fef9c61f 6887 conf->reshape_progress = mddev->reshape_position;
e183eaed 6888 if (conf->reshape_progress != MaxSector) {
09c9e5fa 6889 conf->prev_chunk_sectors = mddev->chunk_sectors;
e183eaed 6890 conf->prev_algo = mddev->layout;
5cac6bcb
N
6891 } else {
6892 conf->prev_chunk_sectors = conf->chunk_sectors;
6893 conf->prev_algo = conf->algorithm;
e183eaed 6894 }
1da177e4 6895
edbe83ab 6896 conf->min_nr_stripes = NR_STRIPES;
ad5b0f76
SL
6897 if (mddev->reshape_position != MaxSector) {
6898 int stripes = max_t(int,
6899 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
6900 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
6901 conf->min_nr_stripes = max(NR_STRIPES, stripes);
6902 if (conf->min_nr_stripes != NR_STRIPES)
cc6167b4 6903 pr_info("md/raid:%s: force stripe size %d for reshape\n",
ad5b0f76
SL
6904 mdname(mddev), conf->min_nr_stripes);
6905 }
edbe83ab 6906 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
5e5e3e78 6907 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4bda556a 6908 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
edbe83ab 6909 if (grow_stripes(conf, conf->min_nr_stripes)) {
cc6167b4
N
6910 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6911 mdname(mddev), memory);
91adb564
N
6912 goto abort;
6913 } else
cc6167b4 6914 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
edbe83ab
N
6915 /*
6916 * Losing a stripe head costs more than the time to refill it,
6917 * it reduces the queue depth and so can hurt throughput.
6918 * So set it rather large, scaled by number of devices.
6919 */
6920 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6921 conf->shrinker.scan_objects = raid5_cache_scan;
6922 conf->shrinker.count_objects = raid5_cache_count;
6923 conf->shrinker.batch = 128;
6924 conf->shrinker.flags = 0;
6a0f53ff 6925 if (register_shrinker(&conf->shrinker)) {
cc6167b4
N
6926 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6927 mdname(mddev));
6a0f53ff
CY
6928 goto abort;
6929 }
1da177e4 6930
0232605d
N
6931 sprintf(pers_name, "raid%d", mddev->new_level);
6932 conf->thread = md_register_thread(raid5d, mddev, pers_name);
91adb564 6933 if (!conf->thread) {
cc6167b4
N
6934 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6935 mdname(mddev));
16a53ecc
N
6936 goto abort;
6937 }
91adb564
N
6938
6939 return conf;
6940
6941 abort:
6942 if (conf) {
95fc17aa 6943 free_conf(conf);
91adb564
N
6944 return ERR_PTR(-EIO);
6945 } else
6946 return ERR_PTR(-ENOMEM);
6947}
6948
c148ffdc
N
6949static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6950{
6951 switch (algo) {
6952 case ALGORITHM_PARITY_0:
6953 if (raid_disk < max_degraded)
6954 return 1;
6955 break;
6956 case ALGORITHM_PARITY_N:
6957 if (raid_disk >= raid_disks - max_degraded)
6958 return 1;
6959 break;
6960 case ALGORITHM_PARITY_0_6:
f72ffdd6 6961 if (raid_disk == 0 ||
c148ffdc
N
6962 raid_disk == raid_disks - 1)
6963 return 1;
6964 break;
6965 case ALGORITHM_LEFT_ASYMMETRIC_6:
6966 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6967 case ALGORITHM_LEFT_SYMMETRIC_6:
6968 case ALGORITHM_RIGHT_SYMMETRIC_6:
6969 if (raid_disk == raid_disks - 1)
6970 return 1;
6971 }
6972 return 0;
6973}
6974
849674e4 6975static int raid5_run(struct mddev *mddev)
91adb564 6976{
d1688a6d 6977 struct r5conf *conf;
9f7c2220 6978 int working_disks = 0;
c148ffdc 6979 int dirty_parity_disks = 0;
3cb03002 6980 struct md_rdev *rdev;
713cf5a6 6981 struct md_rdev *journal_dev = NULL;
c148ffdc 6982 sector_t reshape_offset = 0;
17045f52 6983 int i;
b5254dd5
N
6984 long long min_offset_diff = 0;
6985 int first = 1;
91adb564 6986
8c6ac868 6987 if (mddev->recovery_cp != MaxSector)
cc6167b4
N
6988 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6989 mdname(mddev));
b5254dd5
N
6990
6991 rdev_for_each(rdev, mddev) {
6992 long long diff;
713cf5a6 6993
f2076e7d 6994 if (test_bit(Journal, &rdev->flags)) {
713cf5a6 6995 journal_dev = rdev;
f2076e7d
SL
6996 continue;
6997 }
b5254dd5
N
6998 if (rdev->raid_disk < 0)
6999 continue;
7000 diff = (rdev->new_data_offset - rdev->data_offset);
7001 if (first) {
7002 min_offset_diff = diff;
7003 first = 0;
7004 } else if (mddev->reshape_backwards &&
7005 diff < min_offset_diff)
7006 min_offset_diff = diff;
7007 else if (!mddev->reshape_backwards &&
7008 diff > min_offset_diff)
7009 min_offset_diff = diff;
7010 }
7011
91adb564
N
7012 if (mddev->reshape_position != MaxSector) {
7013 /* Check that we can continue the reshape.
b5254dd5
N
7014 * Difficulties arise if the stripe we would write to
7015 * next is at or after the stripe we would read from next.
7016 * For a reshape that changes the number of devices, this
7017 * is only possible for a very short time, and mdadm makes
7018 * sure that time appears to have past before assembling
7019 * the array. So we fail if that time hasn't passed.
7020 * For a reshape that keeps the number of devices the same
7021 * mdadm must be monitoring the reshape can keeping the
7022 * critical areas read-only and backed up. It will start
7023 * the array in read-only mode, so we check for that.
91adb564
N
7024 */
7025 sector_t here_new, here_old;
7026 int old_disks;
18b00334 7027 int max_degraded = (mddev->level == 6 ? 2 : 1);
05256d98
N
7028 int chunk_sectors;
7029 int new_data_disks;
91adb564 7030
713cf5a6 7031 if (journal_dev) {
cc6167b4
N
7032 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7033 mdname(mddev));
713cf5a6
SL
7034 return -EINVAL;
7035 }
7036
88ce4930 7037 if (mddev->new_level != mddev->level) {
cc6167b4
N
7038 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7039 mdname(mddev));
91adb564
N
7040 return -EINVAL;
7041 }
91adb564
N
7042 old_disks = mddev->raid_disks - mddev->delta_disks;
7043 /* reshape_position must be on a new-stripe boundary, and one
7044 * further up in new geometry must map after here in old
7045 * geometry.
05256d98
N
7046 * If the chunk sizes are different, then as we perform reshape
7047 * in units of the largest of the two, reshape_position needs
7048 * be a multiple of the largest chunk size times new data disks.
91adb564
N
7049 */
7050 here_new = mddev->reshape_position;
05256d98
N
7051 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7052 new_data_disks = mddev->raid_disks - max_degraded;
7053 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
cc6167b4
N
7054 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7055 mdname(mddev));
91adb564
N
7056 return -EINVAL;
7057 }
05256d98 7058 reshape_offset = here_new * chunk_sectors;
91adb564
N
7059 /* here_new is the stripe we will write to */
7060 here_old = mddev->reshape_position;
05256d98 7061 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
91adb564
N
7062 /* here_old is the first stripe that we might need to read
7063 * from */
67ac6011
N
7064 if (mddev->delta_disks == 0) {
7065 /* We cannot be sure it is safe to start an in-place
b5254dd5 7066 * reshape. It is only safe if user-space is monitoring
67ac6011
N
7067 * and taking constant backups.
7068 * mdadm always starts a situation like this in
7069 * readonly mode so it can take control before
7070 * allowing any writes. So just check for that.
7071 */
b5254dd5
N
7072 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7073 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7074 /* not really in-place - so OK */;
7075 else if (mddev->ro == 0) {
cc6167b4
N
7076 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7077 mdname(mddev));
67ac6011
N
7078 return -EINVAL;
7079 }
2c810cdd 7080 } else if (mddev->reshape_backwards
05256d98
N
7081 ? (here_new * chunk_sectors + min_offset_diff <=
7082 here_old * chunk_sectors)
7083 : (here_new * chunk_sectors >=
7084 here_old * chunk_sectors + (-min_offset_diff))) {
91adb564 7085 /* Reading from the same stripe as writing to - bad */
cc6167b4
N
7086 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7087 mdname(mddev));
91adb564
N
7088 return -EINVAL;
7089 }
cc6167b4 7090 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
91adb564
N
7091 /* OK, we should be able to continue; */
7092 } else {
7093 BUG_ON(mddev->level != mddev->new_level);
7094 BUG_ON(mddev->layout != mddev->new_layout);
664e7c41 7095 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
91adb564 7096 BUG_ON(mddev->delta_disks != 0);
1da177e4 7097 }
91adb564 7098
245f46c2
N
7099 if (mddev->private == NULL)
7100 conf = setup_conf(mddev);
7101 else
7102 conf = mddev->private;
7103
91adb564
N
7104 if (IS_ERR(conf))
7105 return PTR_ERR(conf);
7106
486b0f7b
SL
7107 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7108 if (!journal_dev) {
cc6167b4
N
7109 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7110 mdname(mddev));
486b0f7b
SL
7111 mddev->ro = 1;
7112 set_disk_ro(mddev->gendisk, 1);
7113 } else if (mddev->recovery_cp == MaxSector)
7114 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
7dde2ad3
SL
7115 }
7116
b5254dd5 7117 conf->min_offset_diff = min_offset_diff;
91adb564
N
7118 mddev->thread = conf->thread;
7119 conf->thread = NULL;
7120 mddev->private = conf;
7121
17045f52
N
7122 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7123 i++) {
7124 rdev = conf->disks[i].rdev;
7125 if (!rdev && conf->disks[i].replacement) {
7126 /* The replacement is all we have yet */
7127 rdev = conf->disks[i].replacement;
7128 conf->disks[i].replacement = NULL;
7129 clear_bit(Replacement, &rdev->flags);
7130 conf->disks[i].rdev = rdev;
7131 }
7132 if (!rdev)
c148ffdc 7133 continue;
17045f52
N
7134 if (conf->disks[i].replacement &&
7135 conf->reshape_progress != MaxSector) {
7136 /* replacements and reshape simply do not mix. */
cc6167b4 7137 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
17045f52
N
7138 goto abort;
7139 }
2f115882 7140 if (test_bit(In_sync, &rdev->flags)) {
91adb564 7141 working_disks++;
2f115882
N
7142 continue;
7143 }
c148ffdc
N
7144 /* This disc is not fully in-sync. However if it
7145 * just stored parity (beyond the recovery_offset),
7146 * when we don't need to be concerned about the
7147 * array being dirty.
7148 * When reshape goes 'backwards', we never have
7149 * partially completed devices, so we only need
7150 * to worry about reshape going forwards.
7151 */
7152 /* Hack because v0.91 doesn't store recovery_offset properly. */
7153 if (mddev->major_version == 0 &&
7154 mddev->minor_version > 90)
7155 rdev->recovery_offset = reshape_offset;
5026d7a9 7156
c148ffdc
N
7157 if (rdev->recovery_offset < reshape_offset) {
7158 /* We need to check old and new layout */
7159 if (!only_parity(rdev->raid_disk,
7160 conf->algorithm,
7161 conf->raid_disks,
7162 conf->max_degraded))
7163 continue;
7164 }
7165 if (!only_parity(rdev->raid_disk,
7166 conf->prev_algo,
7167 conf->previous_raid_disks,
7168 conf->max_degraded))
7169 continue;
7170 dirty_parity_disks++;
7171 }
91adb564 7172
17045f52
N
7173 /*
7174 * 0 for a fully functional array, 1 or 2 for a degraded array.
7175 */
2e38a37f 7176 mddev->degraded = raid5_calc_degraded(conf);
91adb564 7177
674806d6 7178 if (has_failed(conf)) {
cc6167b4 7179 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
02c2de8c 7180 mdname(mddev), mddev->degraded, conf->raid_disks);
1da177e4
LT
7181 goto abort;
7182 }
7183
91adb564 7184 /* device size must be a multiple of chunk size */
9d8f0363 7185 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
91adb564
N
7186 mddev->resync_max_sectors = mddev->dev_sectors;
7187
c148ffdc 7188 if (mddev->degraded > dirty_parity_disks &&
1da177e4 7189 mddev->recovery_cp != MaxSector) {
6ff8d8ec 7190 if (mddev->ok_start_degraded)
cc6167b4
N
7191 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7192 mdname(mddev));
6ff8d8ec 7193 else {
cc6167b4
N
7194 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7195 mdname(mddev));
6ff8d8ec
N
7196 goto abort;
7197 }
1da177e4
LT
7198 }
7199
cc6167b4
N
7200 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7201 mdname(mddev), conf->level,
7202 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7203 mddev->new_layout);
1da177e4
LT
7204
7205 print_raid5_conf(conf);
7206
fef9c61f 7207 if (conf->reshape_progress != MaxSector) {
fef9c61f 7208 conf->reshape_safe = conf->reshape_progress;
f6705578
N
7209 atomic_set(&conf->reshape_stripes, 0);
7210 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7211 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7212 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7213 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7214 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 7215 "reshape");
f6705578
N
7216 }
7217
1da177e4 7218 /* Ok, everything is just fine now */
a64c876f
N
7219 if (mddev->to_remove == &raid5_attrs_group)
7220 mddev->to_remove = NULL;
00bcb4ac
N
7221 else if (mddev->kobj.sd &&
7222 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
cc6167b4
N
7223 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7224 mdname(mddev));
4a5add49 7225 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7a5febe9 7226
4a5add49 7227 if (mddev->queue) {
9f7c2220 7228 int chunk_size;
620125f2 7229 bool discard_supported = true;
4a5add49
N
7230 /* read-ahead size must cover two whole stripes, which
7231 * is 2 * (datadisks) * chunksize where 'n' is the
7232 * number of raid devices
7233 */
7234 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7235 int stripe = data_disks *
7236 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
dc3b17cc
JK
7237 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7238 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
91adb564 7239
9f7c2220
N
7240 chunk_size = mddev->chunk_sectors << 9;
7241 blk_queue_io_min(mddev->queue, chunk_size);
7242 blk_queue_io_opt(mddev->queue, chunk_size *
7243 (conf->raid_disks - conf->max_degraded));
c78afc62 7244 mddev->queue->limits.raid_partial_stripes_expensive = 1;
620125f2
SL
7245 /*
7246 * We can only discard a whole stripe. It doesn't make sense to
7247 * discard data disk but write parity disk
7248 */
7249 stripe = stripe * PAGE_SIZE;
4ac6875e
N
7250 /* Round up to power of 2, as discard handling
7251 * currently assumes that */
7252 while ((stripe-1) & stripe)
7253 stripe = (stripe | (stripe-1)) + 1;
620125f2
SL
7254 mddev->queue->limits.discard_alignment = stripe;
7255 mddev->queue->limits.discard_granularity = stripe;
e8d7c332
KK
7256
7257 /*
7258 * We use 16-bit counter of active stripes in bi_phys_segments
7259 * (minus one for over-loaded initialization)
7260 */
7261 blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
7262 blk_queue_max_discard_sectors(mddev->queue,
7263 0xfffe * STRIPE_SECTORS);
7264
620125f2
SL
7265 /*
7266 * unaligned part of discard request will be ignored, so can't
8e0e99ba 7267 * guarantee discard_zeroes_data
620125f2
SL
7268 */
7269 mddev->queue->limits.discard_zeroes_data = 0;
8f6c2e4b 7270
5026d7a9
PA
7271 blk_queue_max_write_same_sectors(mddev->queue, 0);
7272
05616be5 7273 rdev_for_each(rdev, mddev) {
9f7c2220
N
7274 disk_stack_limits(mddev->gendisk, rdev->bdev,
7275 rdev->data_offset << 9);
05616be5
N
7276 disk_stack_limits(mddev->gendisk, rdev->bdev,
7277 rdev->new_data_offset << 9);
620125f2
SL
7278 /*
7279 * discard_zeroes_data is required, otherwise data
7280 * could be lost. Consider a scenario: discard a stripe
7281 * (the stripe could be inconsistent if
7282 * discard_zeroes_data is 0); write one disk of the
7283 * stripe (the stripe could be inconsistent again
7284 * depending on which disks are used to calculate
7285 * parity); the disk is broken; The stripe data of this
7286 * disk is lost.
7287 */
7288 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7289 !bdev_get_queue(rdev->bdev)->
7290 limits.discard_zeroes_data)
7291 discard_supported = false;
8e0e99ba
N
7292 /* Unfortunately, discard_zeroes_data is not currently
7293 * a guarantee - just a hint. So we only allow DISCARD
7294 * if the sysadmin has confirmed that only safe devices
7295 * are in use by setting a module parameter.
7296 */
7297 if (!devices_handle_discard_safely) {
7298 if (discard_supported) {
7299 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7300 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7301 }
7302 discard_supported = false;
7303 }
05616be5 7304 }
620125f2
SL
7305
7306 if (discard_supported &&
e7597e69
JS
7307 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7308 mddev->queue->limits.discard_granularity >= stripe)
620125f2
SL
7309 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7310 mddev->queue);
7311 else
7312 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7313 mddev->queue);
1dffdddd
SL
7314
7315 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
9f7c2220 7316 }
23032a0e 7317
5c7e81c3
SL
7318 if (journal_dev) {
7319 char b[BDEVNAME_SIZE];
7320
cc6167b4
N
7321 pr_debug("md/raid:%s: using device %s as journal\n",
7322 mdname(mddev), bdevname(journal_dev->bdev, b));
5aabf7c4
SL
7323 if (r5l_init_log(conf, journal_dev))
7324 goto abort;
5c7e81c3
SL
7325 }
7326
1da177e4
LT
7327 return 0;
7328abort:
01f96c0a 7329 md_unregister_thread(&mddev->thread);
e4f869d9
N
7330 print_raid5_conf(conf);
7331 free_conf(conf);
1da177e4 7332 mddev->private = NULL;
cc6167b4 7333 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
1da177e4
LT
7334 return -EIO;
7335}
7336
afa0f557 7337static void raid5_free(struct mddev *mddev, void *priv)
1da177e4 7338{
afa0f557 7339 struct r5conf *conf = priv;
1da177e4 7340
95fc17aa 7341 free_conf(conf);
a64c876f 7342 mddev->to_remove = &raid5_attrs_group;
1da177e4
LT
7343}
7344
849674e4 7345static void raid5_status(struct seq_file *seq, struct mddev *mddev)
1da177e4 7346{
d1688a6d 7347 struct r5conf *conf = mddev->private;
1da177e4
LT
7348 int i;
7349
9d8f0363 7350 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
3cb5edf4 7351 conf->chunk_sectors / 2, mddev->layout);
02c2de8c 7352 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5fd13351
N
7353 rcu_read_lock();
7354 for (i = 0; i < conf->raid_disks; i++) {
7355 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7356 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7357 }
7358 rcu_read_unlock();
1da177e4 7359 seq_printf (seq, "]");
1da177e4
LT
7360}
7361
d1688a6d 7362static void print_raid5_conf (struct r5conf *conf)
1da177e4
LT
7363{
7364 int i;
7365 struct disk_info *tmp;
7366
cc6167b4 7367 pr_debug("RAID conf printout:\n");
1da177e4 7368 if (!conf) {
cc6167b4 7369 pr_debug("(conf==NULL)\n");
1da177e4
LT
7370 return;
7371 }
cc6167b4 7372 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
0c55e022
N
7373 conf->raid_disks,
7374 conf->raid_disks - conf->mddev->degraded);
1da177e4
LT
7375
7376 for (i = 0; i < conf->raid_disks; i++) {
7377 char b[BDEVNAME_SIZE];
7378 tmp = conf->disks + i;
7379 if (tmp->rdev)
cc6167b4 7380 pr_debug(" disk %d, o:%d, dev:%s\n",
0c55e022
N
7381 i, !test_bit(Faulty, &tmp->rdev->flags),
7382 bdevname(tmp->rdev->bdev, b));
1da177e4
LT
7383 }
7384}
7385
fd01b88c 7386static int raid5_spare_active(struct mddev *mddev)
1da177e4
LT
7387{
7388 int i;
d1688a6d 7389 struct r5conf *conf = mddev->private;
1da177e4 7390 struct disk_info *tmp;
6b965620
N
7391 int count = 0;
7392 unsigned long flags;
1da177e4
LT
7393
7394 for (i = 0; i < conf->raid_disks; i++) {
7395 tmp = conf->disks + i;
dd054fce
N
7396 if (tmp->replacement
7397 && tmp->replacement->recovery_offset == MaxSector
7398 && !test_bit(Faulty, &tmp->replacement->flags)
7399 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7400 /* Replacement has just become active. */
7401 if (!tmp->rdev
7402 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7403 count++;
7404 if (tmp->rdev) {
7405 /* Replaced device not technically faulty,
7406 * but we need to be sure it gets removed
7407 * and never re-added.
7408 */
7409 set_bit(Faulty, &tmp->rdev->flags);
7410 sysfs_notify_dirent_safe(
7411 tmp->rdev->sysfs_state);
7412 }
7413 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7414 } else if (tmp->rdev
70fffd0b 7415 && tmp->rdev->recovery_offset == MaxSector
b2d444d7 7416 && !test_bit(Faulty, &tmp->rdev->flags)
c04be0aa 7417 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
6b965620 7418 count++;
43c73ca4 7419 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1da177e4
LT
7420 }
7421 }
6b965620 7422 spin_lock_irqsave(&conf->device_lock, flags);
2e38a37f 7423 mddev->degraded = raid5_calc_degraded(conf);
6b965620 7424 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4 7425 print_raid5_conf(conf);
6b965620 7426 return count;
1da177e4
LT
7427}
7428
b8321b68 7429static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 7430{
d1688a6d 7431 struct r5conf *conf = mddev->private;
1da177e4 7432 int err = 0;
b8321b68 7433 int number = rdev->raid_disk;
657e3e4d 7434 struct md_rdev **rdevp;
1da177e4
LT
7435 struct disk_info *p = conf->disks + number;
7436
7437 print_raid5_conf(conf);
f6b6ec5c
SL
7438 if (test_bit(Journal, &rdev->flags) && conf->log) {
7439 struct r5l_log *log;
c2bb6242 7440 /*
f6b6ec5c
SL
7441 * we can't wait pending write here, as this is called in
7442 * raid5d, wait will deadlock.
c2bb6242 7443 */
f6b6ec5c
SL
7444 if (atomic_read(&mddev->writes_pending))
7445 return -EBUSY;
7446 log = conf->log;
7447 conf->log = NULL;
7448 synchronize_rcu();
7449 r5l_exit_log(log);
7450 return 0;
c2bb6242 7451 }
657e3e4d
N
7452 if (rdev == p->rdev)
7453 rdevp = &p->rdev;
7454 else if (rdev == p->replacement)
7455 rdevp = &p->replacement;
7456 else
7457 return 0;
7458
7459 if (number >= conf->raid_disks &&
7460 conf->reshape_progress == MaxSector)
7461 clear_bit(In_sync, &rdev->flags);
7462
7463 if (test_bit(In_sync, &rdev->flags) ||
7464 atomic_read(&rdev->nr_pending)) {
7465 err = -EBUSY;
7466 goto abort;
7467 }
7468 /* Only remove non-faulty devices if recovery
7469 * isn't possible.
7470 */
7471 if (!test_bit(Faulty, &rdev->flags) &&
7472 mddev->recovery_disabled != conf->recovery_disabled &&
7473 !has_failed(conf) &&
dd054fce 7474 (!p->replacement || p->replacement == rdev) &&
657e3e4d
N
7475 number < conf->raid_disks) {
7476 err = -EBUSY;
7477 goto abort;
7478 }
7479 *rdevp = NULL;
d787be40
N
7480 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7481 synchronize_rcu();
7482 if (atomic_read(&rdev->nr_pending)) {
7483 /* lost the race, try later */
7484 err = -EBUSY;
7485 *rdevp = rdev;
7486 }
7487 }
7488 if (p->replacement) {
dd054fce
N
7489 /* We must have just cleared 'rdev' */
7490 p->rdev = p->replacement;
7491 clear_bit(Replacement, &p->replacement->flags);
7492 smp_mb(); /* Make sure other CPUs may see both as identical
7493 * but will never see neither - if they are careful
7494 */
7495 p->replacement = NULL;
7496 clear_bit(WantReplacement, &rdev->flags);
7497 } else
7498 /* We might have just removed the Replacement as faulty-
7499 * clear the bit just in case
7500 */
7501 clear_bit(WantReplacement, &rdev->flags);
1da177e4
LT
7502abort:
7503
7504 print_raid5_conf(conf);
7505 return err;
7506}
7507
fd01b88c 7508static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1da177e4 7509{
d1688a6d 7510 struct r5conf *conf = mddev->private;
199050ea 7511 int err = -EEXIST;
1da177e4
LT
7512 int disk;
7513 struct disk_info *p;
6c2fce2e
NB
7514 int first = 0;
7515 int last = conf->raid_disks - 1;
1da177e4 7516
f6b6ec5c
SL
7517 if (test_bit(Journal, &rdev->flags)) {
7518 char b[BDEVNAME_SIZE];
7519 if (conf->log)
7520 return -EBUSY;
7521
7522 rdev->raid_disk = 0;
7523 /*
7524 * The array is in readonly mode if journal is missing, so no
7525 * write requests running. We should be safe
7526 */
7527 r5l_init_log(conf, rdev);
cc6167b4
N
7528 pr_debug("md/raid:%s: using device %s as journal\n",
7529 mdname(mddev), bdevname(rdev->bdev, b));
f6b6ec5c
SL
7530 return 0;
7531 }
7f0da59b
N
7532 if (mddev->recovery_disabled == conf->recovery_disabled)
7533 return -EBUSY;
7534
dc10c643 7535 if (rdev->saved_raid_disk < 0 && has_failed(conf))
1da177e4 7536 /* no point adding a device */
199050ea 7537 return -EINVAL;
1da177e4 7538
6c2fce2e
NB
7539 if (rdev->raid_disk >= 0)
7540 first = last = rdev->raid_disk;
1da177e4
LT
7541
7542 /*
16a53ecc
N
7543 * find the disk ... but prefer rdev->saved_raid_disk
7544 * if possible.
1da177e4 7545 */
16a53ecc 7546 if (rdev->saved_raid_disk >= 0 &&
6c2fce2e 7547 rdev->saved_raid_disk >= first &&
16a53ecc 7548 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5cfb22a1
N
7549 first = rdev->saved_raid_disk;
7550
7551 for (disk = first; disk <= last; disk++) {
7bfec5f3
N
7552 p = conf->disks + disk;
7553 if (p->rdev == NULL) {
b2d444d7 7554 clear_bit(In_sync, &rdev->flags);
1da177e4 7555 rdev->raid_disk = disk;
199050ea 7556 err = 0;
72626685
N
7557 if (rdev->saved_raid_disk != disk)
7558 conf->fullsync = 1;
d6065f7b 7559 rcu_assign_pointer(p->rdev, rdev);
5cfb22a1 7560 goto out;
1da177e4 7561 }
5cfb22a1
N
7562 }
7563 for (disk = first; disk <= last; disk++) {
7564 p = conf->disks + disk;
7bfec5f3
N
7565 if (test_bit(WantReplacement, &p->rdev->flags) &&
7566 p->replacement == NULL) {
7567 clear_bit(In_sync, &rdev->flags);
7568 set_bit(Replacement, &rdev->flags);
7569 rdev->raid_disk = disk;
7570 err = 0;
7571 conf->fullsync = 1;
7572 rcu_assign_pointer(p->replacement, rdev);
7573 break;
7574 }
7575 }
5cfb22a1 7576out:
1da177e4 7577 print_raid5_conf(conf);
199050ea 7578 return err;
1da177e4
LT
7579}
7580
fd01b88c 7581static int raid5_resize(struct mddev *mddev, sector_t sectors)
1da177e4
LT
7582{
7583 /* no resync is happening, and there is enough space
7584 * on all devices, so we can resize.
7585 * We need to make sure resync covers any new space.
7586 * If the array is shrinking we should possibly wait until
7587 * any io in the removed space completes, but it hardly seems
7588 * worth it.
7589 */
a4a6125a 7590 sector_t newsize;
3cb5edf4
N
7591 struct r5conf *conf = mddev->private;
7592
713cf5a6
SL
7593 if (conf->log)
7594 return -EINVAL;
3cb5edf4 7595 sectors &= ~((sector_t)conf->chunk_sectors - 1);
a4a6125a
N
7596 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7597 if (mddev->external_size &&
7598 mddev->array_sectors > newsize)
b522adcd 7599 return -EINVAL;
a4a6125a
N
7600 if (mddev->bitmap) {
7601 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7602 if (ret)
7603 return ret;
7604 }
7605 md_set_array_sectors(mddev, newsize);
f233ea5c 7606 set_capacity(mddev->gendisk, mddev->array_sectors);
449aad3e 7607 revalidate_disk(mddev->gendisk);
b098636c
N
7608 if (sectors > mddev->dev_sectors &&
7609 mddev->recovery_cp > mddev->dev_sectors) {
58c0fed4 7610 mddev->recovery_cp = mddev->dev_sectors;
1da177e4
LT
7611 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7612 }
58c0fed4 7613 mddev->dev_sectors = sectors;
4b5c7ae8 7614 mddev->resync_max_sectors = sectors;
1da177e4
LT
7615 return 0;
7616}
7617
fd01b88c 7618static int check_stripe_cache(struct mddev *mddev)
01ee22b4
N
7619{
7620 /* Can only proceed if there are plenty of stripe_heads.
7621 * We need a minimum of one full stripe,, and for sensible progress
7622 * it is best to have about 4 times that.
7623 * If we require 4 times, then the default 256 4K stripe_heads will
7624 * allow for chunk sizes up to 256K, which is probably OK.
7625 * If the chunk size is greater, user-space should request more
7626 * stripe_heads first.
7627 */
d1688a6d 7628 struct r5conf *conf = mddev->private;
01ee22b4 7629 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
edbe83ab 7630 > conf->min_nr_stripes ||
01ee22b4 7631 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
edbe83ab 7632 > conf->min_nr_stripes) {
cc6167b4
N
7633 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7634 mdname(mddev),
7635 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7636 / STRIPE_SIZE)*4);
01ee22b4
N
7637 return 0;
7638 }
7639 return 1;
7640}
7641
fd01b88c 7642static int check_reshape(struct mddev *mddev)
29269553 7643{
d1688a6d 7644 struct r5conf *conf = mddev->private;
29269553 7645
713cf5a6
SL
7646 if (conf->log)
7647 return -EINVAL;
88ce4930
N
7648 if (mddev->delta_disks == 0 &&
7649 mddev->new_layout == mddev->layout &&
664e7c41 7650 mddev->new_chunk_sectors == mddev->chunk_sectors)
50ac168a 7651 return 0; /* nothing to do */
674806d6 7652 if (has_failed(conf))
ec32a2bd 7653 return -EINVAL;
fdcfbbb6 7654 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
ec32a2bd
N
7655 /* We might be able to shrink, but the devices must
7656 * be made bigger first.
7657 * For raid6, 4 is the minimum size.
7658 * Otherwise 2 is the minimum
7659 */
7660 int min = 2;
7661 if (mddev->level == 6)
7662 min = 4;
7663 if (mddev->raid_disks + mddev->delta_disks < min)
7664 return -EINVAL;
7665 }
29269553 7666
01ee22b4 7667 if (!check_stripe_cache(mddev))
29269553 7668 return -ENOSPC;
29269553 7669
738a2738
N
7670 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7671 mddev->delta_disks > 0)
7672 if (resize_chunks(conf,
7673 conf->previous_raid_disks
7674 + max(0, mddev->delta_disks),
7675 max(mddev->new_chunk_sectors,
7676 mddev->chunk_sectors)
7677 ) < 0)
7678 return -ENOMEM;
e56108d6
N
7679 return resize_stripes(conf, (conf->previous_raid_disks
7680 + mddev->delta_disks));
63c70c4f
N
7681}
7682
fd01b88c 7683static int raid5_start_reshape(struct mddev *mddev)
63c70c4f 7684{
d1688a6d 7685 struct r5conf *conf = mddev->private;
3cb03002 7686 struct md_rdev *rdev;
63c70c4f 7687 int spares = 0;
c04be0aa 7688 unsigned long flags;
63c70c4f 7689
f416885e 7690 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
63c70c4f
N
7691 return -EBUSY;
7692
01ee22b4
N
7693 if (!check_stripe_cache(mddev))
7694 return -ENOSPC;
7695
30b67645
N
7696 if (has_failed(conf))
7697 return -EINVAL;
7698
c6563a8c 7699 rdev_for_each(rdev, mddev) {
469518a3
N
7700 if (!test_bit(In_sync, &rdev->flags)
7701 && !test_bit(Faulty, &rdev->flags))
29269553 7702 spares++;
c6563a8c 7703 }
63c70c4f 7704
f416885e 7705 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
29269553
N
7706 /* Not enough devices even to make a degraded array
7707 * of that size
7708 */
7709 return -EINVAL;
7710
ec32a2bd
N
7711 /* Refuse to reduce size of the array. Any reductions in
7712 * array size must be through explicit setting of array_size
7713 * attribute.
7714 */
7715 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7716 < mddev->array_sectors) {
cc6167b4
N
7717 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7718 mdname(mddev));
ec32a2bd
N
7719 return -EINVAL;
7720 }
7721
f6705578 7722 atomic_set(&conf->reshape_stripes, 0);
29269553 7723 spin_lock_irq(&conf->device_lock);
c46501b2 7724 write_seqcount_begin(&conf->gen_lock);
29269553 7725 conf->previous_raid_disks = conf->raid_disks;
63c70c4f 7726 conf->raid_disks += mddev->delta_disks;
09c9e5fa
AN
7727 conf->prev_chunk_sectors = conf->chunk_sectors;
7728 conf->chunk_sectors = mddev->new_chunk_sectors;
88ce4930
N
7729 conf->prev_algo = conf->algorithm;
7730 conf->algorithm = mddev->new_layout;
05616be5
N
7731 conf->generation++;
7732 /* Code that selects data_offset needs to see the generation update
7733 * if reshape_progress has been set - so a memory barrier needed.
7734 */
7735 smp_mb();
2c810cdd 7736 if (mddev->reshape_backwards)
fef9c61f
N
7737 conf->reshape_progress = raid5_size(mddev, 0, 0);
7738 else
7739 conf->reshape_progress = 0;
7740 conf->reshape_safe = conf->reshape_progress;
c46501b2 7741 write_seqcount_end(&conf->gen_lock);
29269553
N
7742 spin_unlock_irq(&conf->device_lock);
7743
4d77e3ba
N
7744 /* Now make sure any requests that proceeded on the assumption
7745 * the reshape wasn't running - like Discard or Read - have
7746 * completed.
7747 */
7748 mddev_suspend(mddev);
7749 mddev_resume(mddev);
7750
29269553
N
7751 /* Add some new drives, as many as will fit.
7752 * We know there are enough to make the newly sized array work.
3424bf6a
N
7753 * Don't add devices if we are reducing the number of
7754 * devices in the array. This is because it is not possible
7755 * to correctly record the "partially reconstructed" state of
7756 * such devices during the reshape and confusion could result.
29269553 7757 */
87a8dec9 7758 if (mddev->delta_disks >= 0) {
dafb20fa 7759 rdev_for_each(rdev, mddev)
87a8dec9
N
7760 if (rdev->raid_disk < 0 &&
7761 !test_bit(Faulty, &rdev->flags)) {
7762 if (raid5_add_disk(mddev, rdev) == 0) {
87a8dec9 7763 if (rdev->raid_disk
9d4c7d87 7764 >= conf->previous_raid_disks)
87a8dec9 7765 set_bit(In_sync, &rdev->flags);
9d4c7d87 7766 else
87a8dec9 7767 rdev->recovery_offset = 0;
36fad858
NK
7768
7769 if (sysfs_link_rdev(mddev, rdev))
87a8dec9 7770 /* Failure here is OK */;
50da0840 7771 }
87a8dec9
N
7772 } else if (rdev->raid_disk >= conf->previous_raid_disks
7773 && !test_bit(Faulty, &rdev->flags)) {
7774 /* This is a spare that was manually added */
7775 set_bit(In_sync, &rdev->flags);
87a8dec9 7776 }
29269553 7777
87a8dec9
N
7778 /* When a reshape changes the number of devices,
7779 * ->degraded is measured against the larger of the
7780 * pre and post number of devices.
7781 */
ec32a2bd 7782 spin_lock_irqsave(&conf->device_lock, flags);
2e38a37f 7783 mddev->degraded = raid5_calc_degraded(conf);
ec32a2bd
N
7784 spin_unlock_irqrestore(&conf->device_lock, flags);
7785 }
63c70c4f 7786 mddev->raid_disks = conf->raid_disks;
e516402c 7787 mddev->reshape_position = conf->reshape_progress;
2953079c 7788 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
f6705578 7789
29269553
N
7790 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7791 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
ea358cd0 7792 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
29269553
N
7793 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7794 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7795 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
0da3c619 7796 "reshape");
29269553
N
7797 if (!mddev->sync_thread) {
7798 mddev->recovery = 0;
7799 spin_lock_irq(&conf->device_lock);
ba8805b9 7800 write_seqcount_begin(&conf->gen_lock);
29269553 7801 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
ba8805b9
N
7802 mddev->new_chunk_sectors =
7803 conf->chunk_sectors = conf->prev_chunk_sectors;
7804 mddev->new_layout = conf->algorithm = conf->prev_algo;
05616be5
N
7805 rdev_for_each(rdev, mddev)
7806 rdev->new_data_offset = rdev->data_offset;
7807 smp_wmb();
ba8805b9 7808 conf->generation --;
fef9c61f 7809 conf->reshape_progress = MaxSector;
1e3fa9bd 7810 mddev->reshape_position = MaxSector;
ba8805b9 7811 write_seqcount_end(&conf->gen_lock);
29269553
N
7812 spin_unlock_irq(&conf->device_lock);
7813 return -EAGAIN;
7814 }
c8f517c4 7815 conf->reshape_checkpoint = jiffies;
29269553
N
7816 md_wakeup_thread(mddev->sync_thread);
7817 md_new_event(mddev);
7818 return 0;
7819}
29269553 7820
ec32a2bd
N
7821/* This is called from the reshape thread and should make any
7822 * changes needed in 'conf'
7823 */
d1688a6d 7824static void end_reshape(struct r5conf *conf)
29269553 7825{
29269553 7826
f6705578 7827 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
05616be5 7828 struct md_rdev *rdev;
f6705578 7829
f6705578 7830 spin_lock_irq(&conf->device_lock);
cea9c228 7831 conf->previous_raid_disks = conf->raid_disks;
05616be5
N
7832 rdev_for_each(rdev, conf->mddev)
7833 rdev->data_offset = rdev->new_data_offset;
7834 smp_wmb();
fef9c61f 7835 conf->reshape_progress = MaxSector;
6cbd8148 7836 conf->mddev->reshape_position = MaxSector;
f6705578 7837 spin_unlock_irq(&conf->device_lock);
b0f9ec04 7838 wake_up(&conf->wait_for_overlap);
16a53ecc
N
7839
7840 /* read-ahead size must cover two whole stripes, which is
7841 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7842 */
4a5add49 7843 if (conf->mddev->queue) {
cea9c228 7844 int data_disks = conf->raid_disks - conf->max_degraded;
09c9e5fa 7845 int stripe = data_disks * ((conf->chunk_sectors << 9)
cea9c228 7846 / PAGE_SIZE);
dc3b17cc
JK
7847 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7848 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
16a53ecc 7849 }
29269553 7850 }
29269553
N
7851}
7852
ec32a2bd
N
7853/* This is called from the raid5d thread with mddev_lock held.
7854 * It makes config changes to the device.
7855 */
fd01b88c 7856static void raid5_finish_reshape(struct mddev *mddev)
cea9c228 7857{
d1688a6d 7858 struct r5conf *conf = mddev->private;
cea9c228
N
7859
7860 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7861
ec32a2bd
N
7862 if (mddev->delta_disks > 0) {
7863 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
fe67d19a
HM
7864 if (mddev->queue) {
7865 set_capacity(mddev->gendisk, mddev->array_sectors);
7866 revalidate_disk(mddev->gendisk);
7867 }
ec32a2bd
N
7868 } else {
7869 int d;
908f4fbd 7870 spin_lock_irq(&conf->device_lock);
2e38a37f 7871 mddev->degraded = raid5_calc_degraded(conf);
908f4fbd 7872 spin_unlock_irq(&conf->device_lock);
ec32a2bd
N
7873 for (d = conf->raid_disks ;
7874 d < conf->raid_disks - mddev->delta_disks;
1a67dde0 7875 d++) {
3cb03002 7876 struct md_rdev *rdev = conf->disks[d].rdev;
da7613b8
N
7877 if (rdev)
7878 clear_bit(In_sync, &rdev->flags);
7879 rdev = conf->disks[d].replacement;
7880 if (rdev)
7881 clear_bit(In_sync, &rdev->flags);
1a67dde0 7882 }
cea9c228 7883 }
88ce4930 7884 mddev->layout = conf->algorithm;
09c9e5fa 7885 mddev->chunk_sectors = conf->chunk_sectors;
ec32a2bd
N
7886 mddev->reshape_position = MaxSector;
7887 mddev->delta_disks = 0;
2c810cdd 7888 mddev->reshape_backwards = 0;
cea9c228
N
7889 }
7890}
7891
fd01b88c 7892static void raid5_quiesce(struct mddev *mddev, int state)
72626685 7893{
d1688a6d 7894 struct r5conf *conf = mddev->private;
72626685
N
7895
7896 switch(state) {
e464eafd
N
7897 case 2: /* resume for a suspend */
7898 wake_up(&conf->wait_for_overlap);
7899 break;
7900
72626685 7901 case 1: /* stop all writes */
566c09c5 7902 lock_all_device_hash_locks_irq(conf);
64bd660b
N
7903 /* '2' tells resync/reshape to pause so that all
7904 * active stripes can drain
7905 */
a39f7afd 7906 r5c_flush_cache(conf, INT_MAX);
64bd660b 7907 conf->quiesce = 2;
b1b46486 7908 wait_event_cmd(conf->wait_for_quiescent,
46031f9a
RBJ
7909 atomic_read(&conf->active_stripes) == 0 &&
7910 atomic_read(&conf->active_aligned_reads) == 0,
566c09c5
SL
7911 unlock_all_device_hash_locks_irq(conf),
7912 lock_all_device_hash_locks_irq(conf));
64bd660b 7913 conf->quiesce = 1;
566c09c5 7914 unlock_all_device_hash_locks_irq(conf);
64bd660b
N
7915 /* allow reshape to continue */
7916 wake_up(&conf->wait_for_overlap);
72626685
N
7917 break;
7918
7919 case 0: /* re-enable writes */
566c09c5 7920 lock_all_device_hash_locks_irq(conf);
72626685 7921 conf->quiesce = 0;
b1b46486 7922 wake_up(&conf->wait_for_quiescent);
e464eafd 7923 wake_up(&conf->wait_for_overlap);
566c09c5 7924 unlock_all_device_hash_locks_irq(conf);
72626685
N
7925 break;
7926 }
e6c033f7 7927 r5l_quiesce(conf->log, state);
72626685 7928}
b15c2e57 7929
fd01b88c 7930static void *raid45_takeover_raid0(struct mddev *mddev, int level)
54071b38 7931{
e373ab10 7932 struct r0conf *raid0_conf = mddev->private;
d76c8420 7933 sector_t sectors;
54071b38 7934
f1b29bca 7935 /* for raid0 takeover only one zone is supported */
e373ab10 7936 if (raid0_conf->nr_strip_zones > 1) {
cc6167b4
N
7937 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7938 mdname(mddev));
f1b29bca
DW
7939 return ERR_PTR(-EINVAL);
7940 }
7941
e373ab10
N
7942 sectors = raid0_conf->strip_zone[0].zone_end;
7943 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
3b71bd93 7944 mddev->dev_sectors = sectors;
f1b29bca 7945 mddev->new_level = level;
54071b38
TM
7946 mddev->new_layout = ALGORITHM_PARITY_N;
7947 mddev->new_chunk_sectors = mddev->chunk_sectors;
7948 mddev->raid_disks += 1;
7949 mddev->delta_disks = 1;
7950 /* make sure it will be not marked as dirty */
7951 mddev->recovery_cp = MaxSector;
7952
7953 return setup_conf(mddev);
7954}
7955
fd01b88c 7956static void *raid5_takeover_raid1(struct mddev *mddev)
d562b0c4
N
7957{
7958 int chunksect;
6995f0b2 7959 void *ret;
d562b0c4
N
7960
7961 if (mddev->raid_disks != 2 ||
7962 mddev->degraded > 1)
7963 return ERR_PTR(-EINVAL);
7964
7965 /* Should check if there are write-behind devices? */
7966
7967 chunksect = 64*2; /* 64K by default */
7968
7969 /* The array must be an exact multiple of chunksize */
7970 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7971 chunksect >>= 1;
7972
7973 if ((chunksect<<9) < STRIPE_SIZE)
7974 /* array size does not allow a suitable chunk size */
7975 return ERR_PTR(-EINVAL);
7976
7977 mddev->new_level = 5;
7978 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
664e7c41 7979 mddev->new_chunk_sectors = chunksect;
d562b0c4 7980
6995f0b2 7981 ret = setup_conf(mddev);
32cd7cbb 7982 if (!IS_ERR(ret))
394ed8e4
SL
7983 mddev_clear_unsupported_flags(mddev,
7984 UNSUPPORTED_MDDEV_FLAGS);
6995f0b2 7985 return ret;
d562b0c4
N
7986}
7987
fd01b88c 7988static void *raid5_takeover_raid6(struct mddev *mddev)
fc9739c6
N
7989{
7990 int new_layout;
7991
7992 switch (mddev->layout) {
7993 case ALGORITHM_LEFT_ASYMMETRIC_6:
7994 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7995 break;
7996 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7997 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7998 break;
7999 case ALGORITHM_LEFT_SYMMETRIC_6:
8000 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8001 break;
8002 case ALGORITHM_RIGHT_SYMMETRIC_6:
8003 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8004 break;
8005 case ALGORITHM_PARITY_0_6:
8006 new_layout = ALGORITHM_PARITY_0;
8007 break;
8008 case ALGORITHM_PARITY_N:
8009 new_layout = ALGORITHM_PARITY_N;
8010 break;
8011 default:
8012 return ERR_PTR(-EINVAL);
8013 }
8014 mddev->new_level = 5;
8015 mddev->new_layout = new_layout;
8016 mddev->delta_disks = -1;
8017 mddev->raid_disks -= 1;
8018 return setup_conf(mddev);
8019}
8020
fd01b88c 8021static int raid5_check_reshape(struct mddev *mddev)
b3546035 8022{
88ce4930
N
8023 /* For a 2-drive array, the layout and chunk size can be changed
8024 * immediately as not restriping is needed.
8025 * For larger arrays we record the new value - after validation
8026 * to be used by a reshape pass.
b3546035 8027 */
d1688a6d 8028 struct r5conf *conf = mddev->private;
597a711b 8029 int new_chunk = mddev->new_chunk_sectors;
b3546035 8030
597a711b 8031 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
b3546035
N
8032 return -EINVAL;
8033 if (new_chunk > 0) {
0ba459d2 8034 if (!is_power_of_2(new_chunk))
b3546035 8035 return -EINVAL;
597a711b 8036 if (new_chunk < (PAGE_SIZE>>9))
b3546035 8037 return -EINVAL;
597a711b 8038 if (mddev->array_sectors & (new_chunk-1))
b3546035
N
8039 /* not factor of array size */
8040 return -EINVAL;
8041 }
8042
8043 /* They look valid */
8044
88ce4930 8045 if (mddev->raid_disks == 2) {
597a711b
N
8046 /* can make the change immediately */
8047 if (mddev->new_layout >= 0) {
8048 conf->algorithm = mddev->new_layout;
8049 mddev->layout = mddev->new_layout;
88ce4930
N
8050 }
8051 if (new_chunk > 0) {
597a711b
N
8052 conf->chunk_sectors = new_chunk ;
8053 mddev->chunk_sectors = new_chunk;
88ce4930 8054 }
2953079c 8055 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
88ce4930 8056 md_wakeup_thread(mddev->thread);
b3546035 8057 }
50ac168a 8058 return check_reshape(mddev);
88ce4930
N
8059}
8060
fd01b88c 8061static int raid6_check_reshape(struct mddev *mddev)
88ce4930 8062{
597a711b 8063 int new_chunk = mddev->new_chunk_sectors;
50ac168a 8064
597a711b 8065 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
88ce4930 8066 return -EINVAL;
b3546035 8067 if (new_chunk > 0) {
0ba459d2 8068 if (!is_power_of_2(new_chunk))
88ce4930 8069 return -EINVAL;
597a711b 8070 if (new_chunk < (PAGE_SIZE >> 9))
88ce4930 8071 return -EINVAL;
597a711b 8072 if (mddev->array_sectors & (new_chunk-1))
88ce4930
N
8073 /* not factor of array size */
8074 return -EINVAL;
b3546035 8075 }
88ce4930
N
8076
8077 /* They look valid */
50ac168a 8078 return check_reshape(mddev);
b3546035
N
8079}
8080
fd01b88c 8081static void *raid5_takeover(struct mddev *mddev)
d562b0c4
N
8082{
8083 /* raid5 can take over:
f1b29bca 8084 * raid0 - if there is only one strip zone - make it a raid4 layout
d562b0c4
N
8085 * raid1 - if there are two drives. We need to know the chunk size
8086 * raid4 - trivial - just use a raid4 layout.
8087 * raid6 - Providing it is a *_6 layout
d562b0c4 8088 */
f1b29bca
DW
8089 if (mddev->level == 0)
8090 return raid45_takeover_raid0(mddev, 5);
d562b0c4
N
8091 if (mddev->level == 1)
8092 return raid5_takeover_raid1(mddev);
e9d4758f
N
8093 if (mddev->level == 4) {
8094 mddev->new_layout = ALGORITHM_PARITY_N;
8095 mddev->new_level = 5;
8096 return setup_conf(mddev);
8097 }
fc9739c6
N
8098 if (mddev->level == 6)
8099 return raid5_takeover_raid6(mddev);
d562b0c4
N
8100
8101 return ERR_PTR(-EINVAL);
8102}
8103
fd01b88c 8104static void *raid4_takeover(struct mddev *mddev)
a78d38a1 8105{
f1b29bca
DW
8106 /* raid4 can take over:
8107 * raid0 - if there is only one strip zone
8108 * raid5 - if layout is right
a78d38a1 8109 */
f1b29bca
DW
8110 if (mddev->level == 0)
8111 return raid45_takeover_raid0(mddev, 4);
a78d38a1
N
8112 if (mddev->level == 5 &&
8113 mddev->layout == ALGORITHM_PARITY_N) {
8114 mddev->new_layout = 0;
8115 mddev->new_level = 4;
8116 return setup_conf(mddev);
8117 }
8118 return ERR_PTR(-EINVAL);
8119}
d562b0c4 8120
84fc4b56 8121static struct md_personality raid5_personality;
245f46c2 8122
fd01b88c 8123static void *raid6_takeover(struct mddev *mddev)
245f46c2
N
8124{
8125 /* Currently can only take over a raid5. We map the
8126 * personality to an equivalent raid6 personality
8127 * with the Q block at the end.
8128 */
8129 int new_layout;
8130
8131 if (mddev->pers != &raid5_personality)
8132 return ERR_PTR(-EINVAL);
8133 if (mddev->degraded > 1)
8134 return ERR_PTR(-EINVAL);
8135 if (mddev->raid_disks > 253)
8136 return ERR_PTR(-EINVAL);
8137 if (mddev->raid_disks < 3)
8138 return ERR_PTR(-EINVAL);
8139
8140 switch (mddev->layout) {
8141 case ALGORITHM_LEFT_ASYMMETRIC:
8142 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8143 break;
8144 case ALGORITHM_RIGHT_ASYMMETRIC:
8145 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8146 break;
8147 case ALGORITHM_LEFT_SYMMETRIC:
8148 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8149 break;
8150 case ALGORITHM_RIGHT_SYMMETRIC:
8151 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8152 break;
8153 case ALGORITHM_PARITY_0:
8154 new_layout = ALGORITHM_PARITY_0_6;
8155 break;
8156 case ALGORITHM_PARITY_N:
8157 new_layout = ALGORITHM_PARITY_N;
8158 break;
8159 default:
8160 return ERR_PTR(-EINVAL);
8161 }
8162 mddev->new_level = 6;
8163 mddev->new_layout = new_layout;
8164 mddev->delta_disks = 1;
8165 mddev->raid_disks += 1;
8166 return setup_conf(mddev);
8167}
8168
84fc4b56 8169static struct md_personality raid6_personality =
16a53ecc
N
8170{
8171 .name = "raid6",
8172 .level = 6,
8173 .owner = THIS_MODULE,
849674e4
SL
8174 .make_request = raid5_make_request,
8175 .run = raid5_run,
afa0f557 8176 .free = raid5_free,
849674e4
SL
8177 .status = raid5_status,
8178 .error_handler = raid5_error,
16a53ecc
N
8179 .hot_add_disk = raid5_add_disk,
8180 .hot_remove_disk= raid5_remove_disk,
8181 .spare_active = raid5_spare_active,
849674e4 8182 .sync_request = raid5_sync_request,
16a53ecc 8183 .resize = raid5_resize,
80c3a6ce 8184 .size = raid5_size,
50ac168a 8185 .check_reshape = raid6_check_reshape,
f416885e 8186 .start_reshape = raid5_start_reshape,
cea9c228 8187 .finish_reshape = raid5_finish_reshape,
16a53ecc 8188 .quiesce = raid5_quiesce,
245f46c2 8189 .takeover = raid6_takeover,
5c675f83 8190 .congested = raid5_congested,
16a53ecc 8191};
84fc4b56 8192static struct md_personality raid5_personality =
1da177e4
LT
8193{
8194 .name = "raid5",
2604b703 8195 .level = 5,
1da177e4 8196 .owner = THIS_MODULE,
849674e4
SL
8197 .make_request = raid5_make_request,
8198 .run = raid5_run,
afa0f557 8199 .free = raid5_free,
849674e4
SL
8200 .status = raid5_status,
8201 .error_handler = raid5_error,
1da177e4
LT
8202 .hot_add_disk = raid5_add_disk,
8203 .hot_remove_disk= raid5_remove_disk,
8204 .spare_active = raid5_spare_active,
849674e4 8205 .sync_request = raid5_sync_request,
1da177e4 8206 .resize = raid5_resize,
80c3a6ce 8207 .size = raid5_size,
63c70c4f
N
8208 .check_reshape = raid5_check_reshape,
8209 .start_reshape = raid5_start_reshape,
cea9c228 8210 .finish_reshape = raid5_finish_reshape,
72626685 8211 .quiesce = raid5_quiesce,
d562b0c4 8212 .takeover = raid5_takeover,
5c675f83 8213 .congested = raid5_congested,
1da177e4
LT
8214};
8215
84fc4b56 8216static struct md_personality raid4_personality =
1da177e4 8217{
2604b703
N
8218 .name = "raid4",
8219 .level = 4,
8220 .owner = THIS_MODULE,
849674e4
SL
8221 .make_request = raid5_make_request,
8222 .run = raid5_run,
afa0f557 8223 .free = raid5_free,
849674e4
SL
8224 .status = raid5_status,
8225 .error_handler = raid5_error,
2604b703
N
8226 .hot_add_disk = raid5_add_disk,
8227 .hot_remove_disk= raid5_remove_disk,
8228 .spare_active = raid5_spare_active,
849674e4 8229 .sync_request = raid5_sync_request,
2604b703 8230 .resize = raid5_resize,
80c3a6ce 8231 .size = raid5_size,
3d37890b
N
8232 .check_reshape = raid5_check_reshape,
8233 .start_reshape = raid5_start_reshape,
cea9c228 8234 .finish_reshape = raid5_finish_reshape,
2604b703 8235 .quiesce = raid5_quiesce,
a78d38a1 8236 .takeover = raid4_takeover,
5c675f83 8237 .congested = raid5_congested,
2604b703
N
8238};
8239
8240static int __init raid5_init(void)
8241{
29c6d1bb
SAS
8242 int ret;
8243
851c30c9
SL
8244 raid5_wq = alloc_workqueue("raid5wq",
8245 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8246 if (!raid5_wq)
8247 return -ENOMEM;
29c6d1bb
SAS
8248
8249 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8250 "md/raid5:prepare",
8251 raid456_cpu_up_prepare,
8252 raid456_cpu_dead);
8253 if (ret) {
8254 destroy_workqueue(raid5_wq);
8255 return ret;
8256 }
16a53ecc 8257 register_md_personality(&raid6_personality);
2604b703
N
8258 register_md_personality(&raid5_personality);
8259 register_md_personality(&raid4_personality);
8260 return 0;
1da177e4
LT
8261}
8262
2604b703 8263static void raid5_exit(void)
1da177e4 8264{
16a53ecc 8265 unregister_md_personality(&raid6_personality);
2604b703
N
8266 unregister_md_personality(&raid5_personality);
8267 unregister_md_personality(&raid4_personality);
29c6d1bb 8268 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
851c30c9 8269 destroy_workqueue(raid5_wq);
1da177e4
LT
8270}
8271
8272module_init(raid5_init);
8273module_exit(raid5_exit);
8274MODULE_LICENSE("GPL");
0efb9e61 8275MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
1da177e4 8276MODULE_ALIAS("md-personality-4"); /* RAID5 */
d9d166c2
N
8277MODULE_ALIAS("md-raid5");
8278MODULE_ALIAS("md-raid4");
2604b703
N
8279MODULE_ALIAS("md-level-5");
8280MODULE_ALIAS("md-level-4");
16a53ecc
N
8281MODULE_ALIAS("md-personality-8"); /* RAID6 */
8282MODULE_ALIAS("md-raid6");
8283MODULE_ALIAS("md-level-6");
8284
8285/* This used to be two separate modules, they were: */
8286MODULE_ALIAS("raid5");
8287MODULE_ALIAS("raid6");