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