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