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
5 * Copyright (C) 2002, 2003 H. Peter Anvin
6 *
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!
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
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.
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
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
35 * the number of the batch it will be in. This is seq_flush+1.
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 */
45
46#include <linux/blkdev.h>
47#include <linux/kthread.h>
48#include <linux/raid/pq.h>
49#include <linux/async_tx.h>
50#include <linux/module.h>
51#include <linux/async.h>
52#include <linux/seq_file.h>
53#include <linux/cpu.h>
54#include <linux/slab.h>
55#include <linux/ratelimit.h>
56#include <linux/nodemask.h>
57
58#include <trace/events/block.h>
59#include <linux/list_sort.h>
60
61#include "md.h"
62#include "raid5.h"
63#include "raid0.h"
64#include "md-bitmap.h"
65#include "raid5-log.h"
66
67#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
68
69#define cpu_to_group(cpu) cpu_to_node(cpu)
70#define ANY_GROUP NUMA_NO_NODE
71
72static bool devices_handle_discard_safely = false;
73module_param(devices_handle_discard_safely, bool, 0644);
74MODULE_PARM_DESC(devices_handle_discard_safely,
75 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
76static struct workqueue_struct *raid5_wq;
77
78static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
79{
80 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
81 return &conf->stripe_hashtbl[hash];
82}
83
84static inline int stripe_hash_locks_hash(sector_t sect)
85{
86 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
87}
88
89static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
90{
91 spin_lock_irq(conf->hash_locks + hash);
92 spin_lock(&conf->device_lock);
93}
94
95static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
96{
97 spin_unlock(&conf->device_lock);
98 spin_unlock_irq(conf->hash_locks + hash);
99}
100
101static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
102{
103 int i;
104 spin_lock_irq(conf->hash_locks);
105 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
106 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
107 spin_lock(&conf->device_lock);
108}
109
110static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
111{
112 int i;
113 spin_unlock(&conf->device_lock);
114 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
115 spin_unlock(conf->hash_locks + i);
116 spin_unlock_irq(conf->hash_locks);
117}
118
119/* Find first data disk in a raid6 stripe */
120static inline int raid6_d0(struct stripe_head *sh)
121{
122 if (sh->ddf_layout)
123 /* ddf always start from first device */
124 return 0;
125 /* md starts just after Q block */
126 if (sh->qd_idx == sh->disks - 1)
127 return 0;
128 else
129 return sh->qd_idx + 1;
130}
131static inline int raid6_next_disk(int disk, int raid_disks)
132{
133 disk++;
134 return (disk < raid_disks) ? disk : 0;
135}
136
137/* When walking through the disks in a raid5, starting at raid6_d0,
138 * We need to map each disk to a 'slot', where the data disks are slot
139 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
140 * is raid_disks-1. This help does that mapping.
141 */
142static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
143 int *count, int syndrome_disks)
144{
145 int slot = *count;
146
147 if (sh->ddf_layout)
148 (*count)++;
149 if (idx == sh->pd_idx)
150 return syndrome_disks;
151 if (idx == sh->qd_idx)
152 return syndrome_disks + 1;
153 if (!sh->ddf_layout)
154 (*count)++;
155 return slot;
156}
157
158static void print_raid5_conf (struct r5conf *conf);
159
160static int stripe_operations_active(struct stripe_head *sh)
161{
162 return sh->check_state || sh->reconstruct_state ||
163 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
164 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
165}
166
167static bool stripe_is_lowprio(struct stripe_head *sh)
168{
169 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
170 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
171 !test_bit(STRIPE_R5C_CACHING, &sh->state);
172}
173
174static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
175{
176 struct r5conf *conf = sh->raid_conf;
177 struct r5worker_group *group;
178 int thread_cnt;
179 int i, cpu = sh->cpu;
180
181 if (!cpu_online(cpu)) {
182 cpu = cpumask_any(cpu_online_mask);
183 sh->cpu = cpu;
184 }
185
186 if (list_empty(&sh->lru)) {
187 struct r5worker_group *group;
188 group = conf->worker_groups + cpu_to_group(cpu);
189 if (stripe_is_lowprio(sh))
190 list_add_tail(&sh->lru, &group->loprio_list);
191 else
192 list_add_tail(&sh->lru, &group->handle_list);
193 group->stripes_cnt++;
194 sh->group = group;
195 }
196
197 if (conf->worker_cnt_per_group == 0) {
198 md_wakeup_thread(conf->mddev->thread);
199 return;
200 }
201
202 group = conf->worker_groups + cpu_to_group(sh->cpu);
203
204 group->workers[0].working = true;
205 /* at least one worker should run to avoid race */
206 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
207
208 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
209 /* wakeup more workers */
210 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
211 if (group->workers[i].working == false) {
212 group->workers[i].working = true;
213 queue_work_on(sh->cpu, raid5_wq,
214 &group->workers[i].work);
215 thread_cnt--;
216 }
217 }
218}
219
220static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
221 struct list_head *temp_inactive_list)
222{
223 int i;
224 int injournal = 0; /* number of date pages with R5_InJournal */
225
226 BUG_ON(!list_empty(&sh->lru));
227 BUG_ON(atomic_read(&conf->active_stripes)==0);
228
229 if (r5c_is_writeback(conf->log))
230 for (i = sh->disks; i--; )
231 if (test_bit(R5_InJournal, &sh->dev[i].flags))
232 injournal++;
233 /*
234 * In the following cases, the stripe cannot be released to cached
235 * lists. Therefore, we make the stripe write out and set
236 * STRIPE_HANDLE:
237 * 1. when quiesce in r5c write back;
238 * 2. when resync is requested fot the stripe.
239 */
240 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
241 (conf->quiesce && r5c_is_writeback(conf->log) &&
242 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
243 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
244 r5c_make_stripe_write_out(sh);
245 set_bit(STRIPE_HANDLE, &sh->state);
246 }
247
248 if (test_bit(STRIPE_HANDLE, &sh->state)) {
249 if (test_bit(STRIPE_DELAYED, &sh->state) &&
250 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
251 list_add_tail(&sh->lru, &conf->delayed_list);
252 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
253 sh->bm_seq - conf->seq_write > 0)
254 list_add_tail(&sh->lru, &conf->bitmap_list);
255 else {
256 clear_bit(STRIPE_DELAYED, &sh->state);
257 clear_bit(STRIPE_BIT_DELAY, &sh->state);
258 if (conf->worker_cnt_per_group == 0) {
259 if (stripe_is_lowprio(sh))
260 list_add_tail(&sh->lru,
261 &conf->loprio_list);
262 else
263 list_add_tail(&sh->lru,
264 &conf->handle_list);
265 } else {
266 raid5_wakeup_stripe_thread(sh);
267 return;
268 }
269 }
270 md_wakeup_thread(conf->mddev->thread);
271 } else {
272 BUG_ON(stripe_operations_active(sh));
273 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
274 if (atomic_dec_return(&conf->preread_active_stripes)
275 < IO_THRESHOLD)
276 md_wakeup_thread(conf->mddev->thread);
277 atomic_dec(&conf->active_stripes);
278 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
279 if (!r5c_is_writeback(conf->log))
280 list_add_tail(&sh->lru, temp_inactive_list);
281 else {
282 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
283 if (injournal == 0)
284 list_add_tail(&sh->lru, temp_inactive_list);
285 else if (injournal == conf->raid_disks - conf->max_degraded) {
286 /* full stripe */
287 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
288 atomic_inc(&conf->r5c_cached_full_stripes);
289 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
290 atomic_dec(&conf->r5c_cached_partial_stripes);
291 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
292 r5c_check_cached_full_stripe(conf);
293 } else
294 /*
295 * STRIPE_R5C_PARTIAL_STRIPE is set in
296 * r5c_try_caching_write(). No need to
297 * set it again.
298 */
299 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
300 }
301 }
302 }
303}
304
305static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
306 struct list_head *temp_inactive_list)
307{
308 if (atomic_dec_and_test(&sh->count))
309 do_release_stripe(conf, sh, temp_inactive_list);
310}
311
312/*
313 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
314 *
315 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
316 * given time. Adding stripes only takes device lock, while deleting stripes
317 * only takes hash lock.
318 */
319static void release_inactive_stripe_list(struct r5conf *conf,
320 struct list_head *temp_inactive_list,
321 int hash)
322{
323 int size;
324 bool do_wakeup = false;
325 unsigned long flags;
326
327 if (hash == NR_STRIPE_HASH_LOCKS) {
328 size = NR_STRIPE_HASH_LOCKS;
329 hash = NR_STRIPE_HASH_LOCKS - 1;
330 } else
331 size = 1;
332 while (size) {
333 struct list_head *list = &temp_inactive_list[size - 1];
334
335 /*
336 * We don't hold any lock here yet, raid5_get_active_stripe() might
337 * remove stripes from the list
338 */
339 if (!list_empty_careful(list)) {
340 spin_lock_irqsave(conf->hash_locks + hash, flags);
341 if (list_empty(conf->inactive_list + hash) &&
342 !list_empty(list))
343 atomic_dec(&conf->empty_inactive_list_nr);
344 list_splice_tail_init(list, conf->inactive_list + hash);
345 do_wakeup = true;
346 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
347 }
348 size--;
349 hash--;
350 }
351
352 if (do_wakeup) {
353 wake_up(&conf->wait_for_stripe);
354 if (atomic_read(&conf->active_stripes) == 0)
355 wake_up(&conf->wait_for_quiescent);
356 if (conf->retry_read_aligned)
357 md_wakeup_thread(conf->mddev->thread);
358 }
359}
360
361/* should hold conf->device_lock already */
362static int release_stripe_list(struct r5conf *conf,
363 struct list_head *temp_inactive_list)
364{
365 struct stripe_head *sh, *t;
366 int count = 0;
367 struct llist_node *head;
368
369 head = llist_del_all(&conf->released_stripes);
370 head = llist_reverse_order(head);
371 llist_for_each_entry_safe(sh, t, head, release_list) {
372 int hash;
373
374 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
375 smp_mb();
376 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
377 /*
378 * Don't worry the bit is set here, because if the bit is set
379 * again, the count is always > 1. This is true for
380 * STRIPE_ON_UNPLUG_LIST bit too.
381 */
382 hash = sh->hash_lock_index;
383 __release_stripe(conf, sh, &temp_inactive_list[hash]);
384 count++;
385 }
386
387 return count;
388}
389
390void raid5_release_stripe(struct stripe_head *sh)
391{
392 struct r5conf *conf = sh->raid_conf;
393 unsigned long flags;
394 struct list_head list;
395 int hash;
396 bool wakeup;
397
398 /* Avoid release_list until the last reference.
399 */
400 if (atomic_add_unless(&sh->count, -1, 1))
401 return;
402
403 if (unlikely(!conf->mddev->thread) ||
404 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
405 goto slow_path;
406 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
407 if (wakeup)
408 md_wakeup_thread(conf->mddev->thread);
409 return;
410slow_path:
411 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
412 if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
413 INIT_LIST_HEAD(&list);
414 hash = sh->hash_lock_index;
415 do_release_stripe(conf, sh, &list);
416 spin_unlock_irqrestore(&conf->device_lock, flags);
417 release_inactive_stripe_list(conf, &list, hash);
418 }
419}
420
421static inline void remove_hash(struct stripe_head *sh)
422{
423 pr_debug("remove_hash(), stripe %llu\n",
424 (unsigned long long)sh->sector);
425
426 hlist_del_init(&sh->hash);
427}
428
429static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
430{
431 struct hlist_head *hp = stripe_hash(conf, sh->sector);
432
433 pr_debug("insert_hash(), stripe %llu\n",
434 (unsigned long long)sh->sector);
435
436 hlist_add_head(&sh->hash, hp);
437}
438
439/* find an idle stripe, make sure it is unhashed, and return it. */
440static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
441{
442 struct stripe_head *sh = NULL;
443 struct list_head *first;
444
445 if (list_empty(conf->inactive_list + hash))
446 goto out;
447 first = (conf->inactive_list + hash)->next;
448 sh = list_entry(first, struct stripe_head, lru);
449 list_del_init(first);
450 remove_hash(sh);
451 atomic_inc(&conf->active_stripes);
452 BUG_ON(hash != sh->hash_lock_index);
453 if (list_empty(conf->inactive_list + hash))
454 atomic_inc(&conf->empty_inactive_list_nr);
455out:
456 return sh;
457}
458
459static void shrink_buffers(struct stripe_head *sh)
460{
461 struct page *p;
462 int i;
463 int num = sh->raid_conf->pool_size;
464
465 for (i = 0; i < num ; i++) {
466 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
467 p = sh->dev[i].page;
468 if (!p)
469 continue;
470 sh->dev[i].page = NULL;
471 put_page(p);
472 }
473}
474
475static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
476{
477 int i;
478 int num = sh->raid_conf->pool_size;
479
480 for (i = 0; i < num; i++) {
481 struct page *page;
482
483 if (!(page = alloc_page(gfp))) {
484 return 1;
485 }
486 sh->dev[i].page = page;
487 sh->dev[i].orig_page = page;
488 }
489
490 return 0;
491}
492
493static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
494 struct stripe_head *sh);
495
496static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
497{
498 struct r5conf *conf = sh->raid_conf;
499 int i, seq;
500
501 BUG_ON(atomic_read(&sh->count) != 0);
502 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
503 BUG_ON(stripe_operations_active(sh));
504 BUG_ON(sh->batch_head);
505
506 pr_debug("init_stripe called, stripe %llu\n",
507 (unsigned long long)sector);
508retry:
509 seq = read_seqcount_begin(&conf->gen_lock);
510 sh->generation = conf->generation - previous;
511 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
512 sh->sector = sector;
513 stripe_set_idx(sector, conf, previous, sh);
514 sh->state = 0;
515
516 for (i = sh->disks; i--; ) {
517 struct r5dev *dev = &sh->dev[i];
518
519 if (dev->toread || dev->read || dev->towrite || dev->written ||
520 test_bit(R5_LOCKED, &dev->flags)) {
521 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
522 (unsigned long long)sh->sector, i, dev->toread,
523 dev->read, dev->towrite, dev->written,
524 test_bit(R5_LOCKED, &dev->flags));
525 WARN_ON(1);
526 }
527 dev->flags = 0;
528 dev->sector = raid5_compute_blocknr(sh, i, previous);
529 }
530 if (read_seqcount_retry(&conf->gen_lock, seq))
531 goto retry;
532 sh->overwrite_disks = 0;
533 insert_hash(conf, sh);
534 sh->cpu = smp_processor_id();
535 set_bit(STRIPE_BATCH_READY, &sh->state);
536}
537
538static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
539 short generation)
540{
541 struct stripe_head *sh;
542
543 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
544 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
545 if (sh->sector == sector && sh->generation == generation)
546 return sh;
547 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
548 return NULL;
549}
550
551/*
552 * Need to check if array has failed when deciding whether to:
553 * - start an array
554 * - remove non-faulty devices
555 * - add a spare
556 * - allow a reshape
557 * This determination is simple when no reshape is happening.
558 * However if there is a reshape, we need to carefully check
559 * both the before and after sections.
560 * This is because some failed devices may only affect one
561 * of the two sections, and some non-in_sync devices may
562 * be insync in the section most affected by failed devices.
563 */
564int raid5_calc_degraded(struct r5conf *conf)
565{
566 int degraded, degraded2;
567 int i;
568
569 rcu_read_lock();
570 degraded = 0;
571 for (i = 0; i < conf->previous_raid_disks; i++) {
572 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
573 if (rdev && test_bit(Faulty, &rdev->flags))
574 rdev = rcu_dereference(conf->disks[i].replacement);
575 if (!rdev || test_bit(Faulty, &rdev->flags))
576 degraded++;
577 else if (test_bit(In_sync, &rdev->flags))
578 ;
579 else
580 /* not in-sync or faulty.
581 * If the reshape increases the number of devices,
582 * this is being recovered by the reshape, so
583 * this 'previous' section is not in_sync.
584 * If the number of devices is being reduced however,
585 * the device can only be part of the array if
586 * we are reverting a reshape, so this section will
587 * be in-sync.
588 */
589 if (conf->raid_disks >= conf->previous_raid_disks)
590 degraded++;
591 }
592 rcu_read_unlock();
593 if (conf->raid_disks == conf->previous_raid_disks)
594 return degraded;
595 rcu_read_lock();
596 degraded2 = 0;
597 for (i = 0; i < conf->raid_disks; i++) {
598 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
599 if (rdev && test_bit(Faulty, &rdev->flags))
600 rdev = rcu_dereference(conf->disks[i].replacement);
601 if (!rdev || test_bit(Faulty, &rdev->flags))
602 degraded2++;
603 else if (test_bit(In_sync, &rdev->flags))
604 ;
605 else
606 /* not in-sync or faulty.
607 * If reshape increases the number of devices, this
608 * section has already been recovered, else it
609 * almost certainly hasn't.
610 */
611 if (conf->raid_disks <= conf->previous_raid_disks)
612 degraded2++;
613 }
614 rcu_read_unlock();
615 if (degraded2 > degraded)
616 return degraded2;
617 return degraded;
618}
619
620static int has_failed(struct r5conf *conf)
621{
622 int degraded;
623
624 if (conf->mddev->reshape_position == MaxSector)
625 return conf->mddev->degraded > conf->max_degraded;
626
627 degraded = raid5_calc_degraded(conf);
628 if (degraded > conf->max_degraded)
629 return 1;
630 return 0;
631}
632
633struct stripe_head *
634raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
635 int previous, int noblock, int noquiesce)
636{
637 struct stripe_head *sh;
638 int hash = stripe_hash_locks_hash(sector);
639 int inc_empty_inactive_list_flag;
640
641 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
642
643 spin_lock_irq(conf->hash_locks + hash);
644
645 do {
646 wait_event_lock_irq(conf->wait_for_quiescent,
647 conf->quiesce == 0 || noquiesce,
648 *(conf->hash_locks + hash));
649 sh = __find_stripe(conf, sector, conf->generation - previous);
650 if (!sh) {
651 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
652 sh = get_free_stripe(conf, hash);
653 if (!sh && !test_bit(R5_DID_ALLOC,
654 &conf->cache_state))
655 set_bit(R5_ALLOC_MORE,
656 &conf->cache_state);
657 }
658 if (noblock && sh == NULL)
659 break;
660
661 r5c_check_stripe_cache_usage(conf);
662 if (!sh) {
663 set_bit(R5_INACTIVE_BLOCKED,
664 &conf->cache_state);
665 r5l_wake_reclaim(conf->log, 0);
666 wait_event_lock_irq(
667 conf->wait_for_stripe,
668 !list_empty(conf->inactive_list + hash) &&
669 (atomic_read(&conf->active_stripes)
670 < (conf->max_nr_stripes * 3 / 4)
671 || !test_bit(R5_INACTIVE_BLOCKED,
672 &conf->cache_state)),
673 *(conf->hash_locks + hash));
674 clear_bit(R5_INACTIVE_BLOCKED,
675 &conf->cache_state);
676 } else {
677 init_stripe(sh, sector, previous);
678 atomic_inc(&sh->count);
679 }
680 } else if (!atomic_inc_not_zero(&sh->count)) {
681 spin_lock(&conf->device_lock);
682 if (!atomic_read(&sh->count)) {
683 if (!test_bit(STRIPE_HANDLE, &sh->state))
684 atomic_inc(&conf->active_stripes);
685 BUG_ON(list_empty(&sh->lru) &&
686 !test_bit(STRIPE_EXPANDING, &sh->state));
687 inc_empty_inactive_list_flag = 0;
688 if (!list_empty(conf->inactive_list + hash))
689 inc_empty_inactive_list_flag = 1;
690 list_del_init(&sh->lru);
691 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
692 atomic_inc(&conf->empty_inactive_list_nr);
693 if (sh->group) {
694 sh->group->stripes_cnt--;
695 sh->group = NULL;
696 }
697 }
698 atomic_inc(&sh->count);
699 spin_unlock(&conf->device_lock);
700 }
701 } while (sh == NULL);
702
703 spin_unlock_irq(conf->hash_locks + hash);
704 return sh;
705}
706
707static bool is_full_stripe_write(struct stripe_head *sh)
708{
709 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
710 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
711}
712
713static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
714{
715 if (sh1 > sh2) {
716 spin_lock_irq(&sh2->stripe_lock);
717 spin_lock_nested(&sh1->stripe_lock, 1);
718 } else {
719 spin_lock_irq(&sh1->stripe_lock);
720 spin_lock_nested(&sh2->stripe_lock, 1);
721 }
722}
723
724static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
725{
726 spin_unlock(&sh1->stripe_lock);
727 spin_unlock_irq(&sh2->stripe_lock);
728}
729
730/* Only freshly new full stripe normal write stripe can be added to a batch list */
731static bool stripe_can_batch(struct stripe_head *sh)
732{
733 struct r5conf *conf = sh->raid_conf;
734
735 if (raid5_has_log(conf) || raid5_has_ppl(conf))
736 return false;
737 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
738 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
739 is_full_stripe_write(sh);
740}
741
742/* we only do back search */
743static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
744{
745 struct stripe_head *head;
746 sector_t head_sector, tmp_sec;
747 int hash;
748 int dd_idx;
749 int inc_empty_inactive_list_flag;
750
751 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
752 tmp_sec = sh->sector;
753 if (!sector_div(tmp_sec, conf->chunk_sectors))
754 return;
755 head_sector = sh->sector - STRIPE_SECTORS;
756
757 hash = stripe_hash_locks_hash(head_sector);
758 spin_lock_irq(conf->hash_locks + hash);
759 head = __find_stripe(conf, head_sector, conf->generation);
760 if (head && !atomic_inc_not_zero(&head->count)) {
761 spin_lock(&conf->device_lock);
762 if (!atomic_read(&head->count)) {
763 if (!test_bit(STRIPE_HANDLE, &head->state))
764 atomic_inc(&conf->active_stripes);
765 BUG_ON(list_empty(&head->lru) &&
766 !test_bit(STRIPE_EXPANDING, &head->state));
767 inc_empty_inactive_list_flag = 0;
768 if (!list_empty(conf->inactive_list + hash))
769 inc_empty_inactive_list_flag = 1;
770 list_del_init(&head->lru);
771 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
772 atomic_inc(&conf->empty_inactive_list_nr);
773 if (head->group) {
774 head->group->stripes_cnt--;
775 head->group = NULL;
776 }
777 }
778 atomic_inc(&head->count);
779 spin_unlock(&conf->device_lock);
780 }
781 spin_unlock_irq(conf->hash_locks + hash);
782
783 if (!head)
784 return;
785 if (!stripe_can_batch(head))
786 goto out;
787
788 lock_two_stripes(head, sh);
789 /* clear_batch_ready clear the flag */
790 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
791 goto unlock_out;
792
793 if (sh->batch_head)
794 goto unlock_out;
795
796 dd_idx = 0;
797 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
798 dd_idx++;
799 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
800 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
801 goto unlock_out;
802
803 if (head->batch_head) {
804 spin_lock(&head->batch_head->batch_lock);
805 /* This batch list is already running */
806 if (!stripe_can_batch(head)) {
807 spin_unlock(&head->batch_head->batch_lock);
808 goto unlock_out;
809 }
810 /*
811 * We must assign batch_head of this stripe within the
812 * batch_lock, otherwise clear_batch_ready of batch head
813 * stripe could clear BATCH_READY bit of this stripe and
814 * this stripe->batch_head doesn't get assigned, which
815 * could confuse clear_batch_ready for this stripe
816 */
817 sh->batch_head = head->batch_head;
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 } else {
826 head->batch_head = head;
827 sh->batch_head = head->batch_head;
828 spin_lock(&head->batch_lock);
829 list_add_tail(&sh->batch_list, &head->batch_list);
830 spin_unlock(&head->batch_lock);
831 }
832
833 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
834 if (atomic_dec_return(&conf->preread_active_stripes)
835 < IO_THRESHOLD)
836 md_wakeup_thread(conf->mddev->thread);
837
838 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
839 int seq = sh->bm_seq;
840 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
841 sh->batch_head->bm_seq > seq)
842 seq = sh->batch_head->bm_seq;
843 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
844 sh->batch_head->bm_seq = seq;
845 }
846
847 atomic_inc(&sh->count);
848unlock_out:
849 unlock_two_stripes(head, sh);
850out:
851 raid5_release_stripe(head);
852}
853
854/* Determine if 'data_offset' or 'new_data_offset' should be used
855 * in this stripe_head.
856 */
857static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
858{
859 sector_t progress = conf->reshape_progress;
860 /* Need a memory barrier to make sure we see the value
861 * of conf->generation, or ->data_offset that was set before
862 * reshape_progress was updated.
863 */
864 smp_rmb();
865 if (progress == MaxSector)
866 return 0;
867 if (sh->generation == conf->generation - 1)
868 return 0;
869 /* We are in a reshape, and this is a new-generation stripe,
870 * so use new_data_offset.
871 */
872 return 1;
873}
874
875static void dispatch_bio_list(struct bio_list *tmp)
876{
877 struct bio *bio;
878
879 while ((bio = bio_list_pop(tmp)))
880 generic_make_request(bio);
881}
882
883static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b)
884{
885 const struct r5pending_data *da = list_entry(a,
886 struct r5pending_data, sibling);
887 const struct r5pending_data *db = list_entry(b,
888 struct r5pending_data, sibling);
889 if (da->sector > db->sector)
890 return 1;
891 if (da->sector < db->sector)
892 return -1;
893 return 0;
894}
895
896static void dispatch_defer_bios(struct r5conf *conf, int target,
897 struct bio_list *list)
898{
899 struct r5pending_data *data;
900 struct list_head *first, *next = NULL;
901 int cnt = 0;
902
903 if (conf->pending_data_cnt == 0)
904 return;
905
906 list_sort(NULL, &conf->pending_list, cmp_stripe);
907
908 first = conf->pending_list.next;
909
910 /* temporarily move the head */
911 if (conf->next_pending_data)
912 list_move_tail(&conf->pending_list,
913 &conf->next_pending_data->sibling);
914
915 while (!list_empty(&conf->pending_list)) {
916 data = list_first_entry(&conf->pending_list,
917 struct r5pending_data, sibling);
918 if (&data->sibling == first)
919 first = data->sibling.next;
920 next = data->sibling.next;
921
922 bio_list_merge(list, &data->bios);
923 list_move(&data->sibling, &conf->free_list);
924 cnt++;
925 if (cnt >= target)
926 break;
927 }
928 conf->pending_data_cnt -= cnt;
929 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
930
931 if (next != &conf->pending_list)
932 conf->next_pending_data = list_entry(next,
933 struct r5pending_data, sibling);
934 else
935 conf->next_pending_data = NULL;
936 /* list isn't empty */
937 if (first != &conf->pending_list)
938 list_move_tail(&conf->pending_list, first);
939}
940
941static void flush_deferred_bios(struct r5conf *conf)
942{
943 struct bio_list tmp = BIO_EMPTY_LIST;
944
945 if (conf->pending_data_cnt == 0)
946 return;
947
948 spin_lock(&conf->pending_bios_lock);
949 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
950 BUG_ON(conf->pending_data_cnt != 0);
951 spin_unlock(&conf->pending_bios_lock);
952
953 dispatch_bio_list(&tmp);
954}
955
956static void defer_issue_bios(struct r5conf *conf, sector_t sector,
957 struct bio_list *bios)
958{
959 struct bio_list tmp = BIO_EMPTY_LIST;
960 struct r5pending_data *ent;
961
962 spin_lock(&conf->pending_bios_lock);
963 ent = list_first_entry(&conf->free_list, struct r5pending_data,
964 sibling);
965 list_move_tail(&ent->sibling, &conf->pending_list);
966 ent->sector = sector;
967 bio_list_init(&ent->bios);
968 bio_list_merge(&ent->bios, bios);
969 conf->pending_data_cnt++;
970 if (conf->pending_data_cnt >= PENDING_IO_MAX)
971 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
972
973 spin_unlock(&conf->pending_bios_lock);
974
975 dispatch_bio_list(&tmp);
976}
977
978static void
979raid5_end_read_request(struct bio *bi);
980static void
981raid5_end_write_request(struct bio *bi);
982
983static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
984{
985 struct r5conf *conf = sh->raid_conf;
986 int i, disks = sh->disks;
987 struct stripe_head *head_sh = sh;
988 struct bio_list pending_bios = BIO_EMPTY_LIST;
989 bool should_defer;
990
991 might_sleep();
992
993 if (log_stripe(sh, s) == 0)
994 return;
995
996 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
997
998 for (i = disks; i--; ) {
999 int op, op_flags = 0;
1000 int replace_only = 0;
1001 struct bio *bi, *rbi;
1002 struct md_rdev *rdev, *rrdev = NULL;
1003
1004 sh = head_sh;
1005 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
1006 op = REQ_OP_WRITE;
1007 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
1008 op_flags = REQ_FUA;
1009 if (test_bit(R5_Discard, &sh->dev[i].flags))
1010 op = REQ_OP_DISCARD;
1011 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1012 op = REQ_OP_READ;
1013 else if (test_and_clear_bit(R5_WantReplace,
1014 &sh->dev[i].flags)) {
1015 op = REQ_OP_WRITE;
1016 replace_only = 1;
1017 } else
1018 continue;
1019 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
1020 op_flags |= REQ_SYNC;
1021
1022again:
1023 bi = &sh->dev[i].req;
1024 rbi = &sh->dev[i].rreq; /* For writing to replacement */
1025
1026 rcu_read_lock();
1027 rrdev = rcu_dereference(conf->disks[i].replacement);
1028 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1029 rdev = rcu_dereference(conf->disks[i].rdev);
1030 if (!rdev) {
1031 rdev = rrdev;
1032 rrdev = NULL;
1033 }
1034 if (op_is_write(op)) {
1035 if (replace_only)
1036 rdev = NULL;
1037 if (rdev == rrdev)
1038 /* We raced and saw duplicates */
1039 rrdev = NULL;
1040 } else {
1041 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
1042 rdev = rrdev;
1043 rrdev = NULL;
1044 }
1045
1046 if (rdev && test_bit(Faulty, &rdev->flags))
1047 rdev = NULL;
1048 if (rdev)
1049 atomic_inc(&rdev->nr_pending);
1050 if (rrdev && test_bit(Faulty, &rrdev->flags))
1051 rrdev = NULL;
1052 if (rrdev)
1053 atomic_inc(&rrdev->nr_pending);
1054 rcu_read_unlock();
1055
1056 /* We have already checked bad blocks for reads. Now
1057 * need to check for writes. We never accept write errors
1058 * on the replacement, so we don't to check rrdev.
1059 */
1060 while (op_is_write(op) && rdev &&
1061 test_bit(WriteErrorSeen, &rdev->flags)) {
1062 sector_t first_bad;
1063 int bad_sectors;
1064 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
1065 &first_bad, &bad_sectors);
1066 if (!bad)
1067 break;
1068
1069 if (bad < 0) {
1070 set_bit(BlockedBadBlocks, &rdev->flags);
1071 if (!conf->mddev->external &&
1072 conf->mddev->sb_flags) {
1073 /* It is very unlikely, but we might
1074 * still need to write out the
1075 * bad block log - better give it
1076 * a chance*/
1077 md_check_recovery(conf->mddev);
1078 }
1079 /*
1080 * Because md_wait_for_blocked_rdev
1081 * will dec nr_pending, we must
1082 * increment it first.
1083 */
1084 atomic_inc(&rdev->nr_pending);
1085 md_wait_for_blocked_rdev(rdev, conf->mddev);
1086 } else {
1087 /* Acknowledged bad block - skip the write */
1088 rdev_dec_pending(rdev, conf->mddev);
1089 rdev = NULL;
1090 }
1091 }
1092
1093 if (rdev) {
1094 if (s->syncing || s->expanding || s->expanded
1095 || s->replacing)
1096 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1097
1098 set_bit(STRIPE_IO_STARTED, &sh->state);
1099
1100 bio_set_dev(bi, rdev->bdev);
1101 bio_set_op_attrs(bi, op, op_flags);
1102 bi->bi_end_io = op_is_write(op)
1103 ? raid5_end_write_request
1104 : raid5_end_read_request;
1105 bi->bi_private = sh;
1106
1107 pr_debug("%s: for %llu schedule op %d on disc %d\n",
1108 __func__, (unsigned long long)sh->sector,
1109 bi->bi_opf, i);
1110 atomic_inc(&sh->count);
1111 if (sh != head_sh)
1112 atomic_inc(&head_sh->count);
1113 if (use_new_offset(conf, sh))
1114 bi->bi_iter.bi_sector = (sh->sector
1115 + rdev->new_data_offset);
1116 else
1117 bi->bi_iter.bi_sector = (sh->sector
1118 + rdev->data_offset);
1119 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
1120 bi->bi_opf |= REQ_NOMERGE;
1121
1122 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1123 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1124
1125 if (!op_is_write(op) &&
1126 test_bit(R5_InJournal, &sh->dev[i].flags))
1127 /*
1128 * issuing read for a page in journal, this
1129 * must be preparing for prexor in rmw; read
1130 * the data into orig_page
1131 */
1132 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1133 else
1134 sh->dev[i].vec.bv_page = sh->dev[i].page;
1135 bi->bi_vcnt = 1;
1136 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1137 bi->bi_io_vec[0].bv_offset = 0;
1138 bi->bi_iter.bi_size = STRIPE_SIZE;
1139 bi->bi_write_hint = sh->dev[i].write_hint;
1140 if (!rrdev)
1141 sh->dev[i].write_hint = RWF_WRITE_LIFE_NOT_SET;
1142 /*
1143 * If this is discard request, set bi_vcnt 0. We don't
1144 * want to confuse SCSI because SCSI will replace payload
1145 */
1146 if (op == REQ_OP_DISCARD)
1147 bi->bi_vcnt = 0;
1148 if (rrdev)
1149 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
1150
1151 if (conf->mddev->gendisk)
1152 trace_block_bio_remap(bi->bi_disk->queue,
1153 bi, disk_devt(conf->mddev->gendisk),
1154 sh->dev[i].sector);
1155 if (should_defer && op_is_write(op))
1156 bio_list_add(&pending_bios, bi);
1157 else
1158 generic_make_request(bi);
1159 }
1160 if (rrdev) {
1161 if (s->syncing || s->expanding || s->expanded
1162 || s->replacing)
1163 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1164
1165 set_bit(STRIPE_IO_STARTED, &sh->state);
1166
1167 bio_set_dev(rbi, rrdev->bdev);
1168 bio_set_op_attrs(rbi, op, op_flags);
1169 BUG_ON(!op_is_write(op));
1170 rbi->bi_end_io = raid5_end_write_request;
1171 rbi->bi_private = sh;
1172
1173 pr_debug("%s: for %llu schedule op %d on "
1174 "replacement disc %d\n",
1175 __func__, (unsigned long long)sh->sector,
1176 rbi->bi_opf, i);
1177 atomic_inc(&sh->count);
1178 if (sh != head_sh)
1179 atomic_inc(&head_sh->count);
1180 if (use_new_offset(conf, sh))
1181 rbi->bi_iter.bi_sector = (sh->sector
1182 + rrdev->new_data_offset);
1183 else
1184 rbi->bi_iter.bi_sector = (sh->sector
1185 + rrdev->data_offset);
1186 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1187 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1188 sh->dev[i].rvec.bv_page = sh->dev[i].page;
1189 rbi->bi_vcnt = 1;
1190 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1191 rbi->bi_io_vec[0].bv_offset = 0;
1192 rbi->bi_iter.bi_size = STRIPE_SIZE;
1193 rbi->bi_write_hint = sh->dev[i].write_hint;
1194 sh->dev[i].write_hint = RWF_WRITE_LIFE_NOT_SET;
1195 /*
1196 * If this is discard request, set bi_vcnt 0. We don't
1197 * want to confuse SCSI because SCSI will replace payload
1198 */
1199 if (op == REQ_OP_DISCARD)
1200 rbi->bi_vcnt = 0;
1201 if (conf->mddev->gendisk)
1202 trace_block_bio_remap(rbi->bi_disk->queue,
1203 rbi, disk_devt(conf->mddev->gendisk),
1204 sh->dev[i].sector);
1205 if (should_defer && op_is_write(op))
1206 bio_list_add(&pending_bios, rbi);
1207 else
1208 generic_make_request(rbi);
1209 }
1210 if (!rdev && !rrdev) {
1211 if (op_is_write(op))
1212 set_bit(STRIPE_DEGRADED, &sh->state);
1213 pr_debug("skip op %d on disc %d for sector %llu\n",
1214 bi->bi_opf, i, (unsigned long long)sh->sector);
1215 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1216 set_bit(STRIPE_HANDLE, &sh->state);
1217 }
1218
1219 if (!head_sh->batch_head)
1220 continue;
1221 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1222 batch_list);
1223 if (sh != head_sh)
1224 goto again;
1225 }
1226
1227 if (should_defer && !bio_list_empty(&pending_bios))
1228 defer_issue_bios(conf, head_sh->sector, &pending_bios);
1229}
1230
1231static struct dma_async_tx_descriptor *
1232async_copy_data(int frombio, struct bio *bio, struct page **page,
1233 sector_t sector, struct dma_async_tx_descriptor *tx,
1234 struct stripe_head *sh, int no_skipcopy)
1235{
1236 struct bio_vec bvl;
1237 struct bvec_iter iter;
1238 struct page *bio_page;
1239 int page_offset;
1240 struct async_submit_ctl submit;
1241 enum async_tx_flags flags = 0;
1242
1243 if (bio->bi_iter.bi_sector >= sector)
1244 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
1245 else
1246 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
1247
1248 if (frombio)
1249 flags |= ASYNC_TX_FENCE;
1250 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1251
1252 bio_for_each_segment(bvl, bio, iter) {
1253 int len = bvl.bv_len;
1254 int clen;
1255 int b_offset = 0;
1256
1257 if (page_offset < 0) {
1258 b_offset = -page_offset;
1259 page_offset += b_offset;
1260 len -= b_offset;
1261 }
1262
1263 if (len > 0 && page_offset + len > STRIPE_SIZE)
1264 clen = STRIPE_SIZE - page_offset;
1265 else
1266 clen = len;
1267
1268 if (clen > 0) {
1269 b_offset += bvl.bv_offset;
1270 bio_page = bvl.bv_page;
1271 if (frombio) {
1272 if (sh->raid_conf->skip_copy &&
1273 b_offset == 0 && page_offset == 0 &&
1274 clen == STRIPE_SIZE &&
1275 !no_skipcopy)
1276 *page = bio_page;
1277 else
1278 tx = async_memcpy(*page, bio_page, page_offset,
1279 b_offset, clen, &submit);
1280 } else
1281 tx = async_memcpy(bio_page, *page, b_offset,
1282 page_offset, clen, &submit);
1283 }
1284 /* chain the operations */
1285 submit.depend_tx = tx;
1286
1287 if (clen < len) /* hit end of page */
1288 break;
1289 page_offset += len;
1290 }
1291
1292 return tx;
1293}
1294
1295static void ops_complete_biofill(void *stripe_head_ref)
1296{
1297 struct stripe_head *sh = stripe_head_ref;
1298 int i;
1299
1300 pr_debug("%s: stripe %llu\n", __func__,
1301 (unsigned long long)sh->sector);
1302
1303 /* clear completed biofills */
1304 for (i = sh->disks; i--; ) {
1305 struct r5dev *dev = &sh->dev[i];
1306
1307 /* acknowledge completion of a biofill operation */
1308 /* and check if we need to reply to a read request,
1309 * new R5_Wantfill requests are held off until
1310 * !STRIPE_BIOFILL_RUN
1311 */
1312 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
1313 struct bio *rbi, *rbi2;
1314
1315 BUG_ON(!dev->read);
1316 rbi = dev->read;
1317 dev->read = NULL;
1318 while (rbi && rbi->bi_iter.bi_sector <
1319 dev->sector + STRIPE_SECTORS) {
1320 rbi2 = r5_next_bio(rbi, dev->sector);
1321 bio_endio(rbi);
1322 rbi = rbi2;
1323 }
1324 }
1325 }
1326 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
1327
1328 set_bit(STRIPE_HANDLE, &sh->state);
1329 raid5_release_stripe(sh);
1330}
1331
1332static void ops_run_biofill(struct stripe_head *sh)
1333{
1334 struct dma_async_tx_descriptor *tx = NULL;
1335 struct async_submit_ctl submit;
1336 int i;
1337
1338 BUG_ON(sh->batch_head);
1339 pr_debug("%s: stripe %llu\n", __func__,
1340 (unsigned long long)sh->sector);
1341
1342 for (i = sh->disks; i--; ) {
1343 struct r5dev *dev = &sh->dev[i];
1344 if (test_bit(R5_Wantfill, &dev->flags)) {
1345 struct bio *rbi;
1346 spin_lock_irq(&sh->stripe_lock);
1347 dev->read = rbi = dev->toread;
1348 dev->toread = NULL;
1349 spin_unlock_irq(&sh->stripe_lock);
1350 while (rbi && rbi->bi_iter.bi_sector <
1351 dev->sector + STRIPE_SECTORS) {
1352 tx = async_copy_data(0, rbi, &dev->page,
1353 dev->sector, tx, sh, 0);
1354 rbi = r5_next_bio(rbi, dev->sector);
1355 }
1356 }
1357 }
1358
1359 atomic_inc(&sh->count);
1360 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1361 async_trigger_callback(&submit);
1362}
1363
1364static void mark_target_uptodate(struct stripe_head *sh, int target)
1365{
1366 struct r5dev *tgt;
1367
1368 if (target < 0)
1369 return;
1370
1371 tgt = &sh->dev[target];
1372 set_bit(R5_UPTODATE, &tgt->flags);
1373 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1374 clear_bit(R5_Wantcompute, &tgt->flags);
1375}
1376
1377static void ops_complete_compute(void *stripe_head_ref)
1378{
1379 struct stripe_head *sh = stripe_head_ref;
1380
1381 pr_debug("%s: stripe %llu\n", __func__,
1382 (unsigned long long)sh->sector);
1383
1384 /* mark the computed target(s) as uptodate */
1385 mark_target_uptodate(sh, sh->ops.target);
1386 mark_target_uptodate(sh, sh->ops.target2);
1387
1388 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1389 if (sh->check_state == check_state_compute_run)
1390 sh->check_state = check_state_compute_result;
1391 set_bit(STRIPE_HANDLE, &sh->state);
1392 raid5_release_stripe(sh);
1393}
1394
1395/* return a pointer to the address conversion region of the scribble buffer */
1396static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1397{
1398 return percpu->scribble + i * percpu->scribble_obj_size;
1399}
1400
1401/* return a pointer to the address conversion region of the scribble buffer */
1402static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1403 struct raid5_percpu *percpu, int i)
1404{
1405 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
1406}
1407
1408static struct dma_async_tx_descriptor *
1409ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1410{
1411 int disks = sh->disks;
1412 struct page **xor_srcs = to_addr_page(percpu, 0);
1413 int target = sh->ops.target;
1414 struct r5dev *tgt = &sh->dev[target];
1415 struct page *xor_dest = tgt->page;
1416 int count = 0;
1417 struct dma_async_tx_descriptor *tx;
1418 struct async_submit_ctl submit;
1419 int i;
1420
1421 BUG_ON(sh->batch_head);
1422
1423 pr_debug("%s: stripe %llu block: %d\n",
1424 __func__, (unsigned long long)sh->sector, target);
1425 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1426
1427 for (i = disks; i--; )
1428 if (i != target)
1429 xor_srcs[count++] = sh->dev[i].page;
1430
1431 atomic_inc(&sh->count);
1432
1433 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
1434 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
1435 if (unlikely(count == 1))
1436 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1437 else
1438 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1439
1440 return tx;
1441}
1442
1443/* set_syndrome_sources - populate source buffers for gen_syndrome
1444 * @srcs - (struct page *) array of size sh->disks
1445 * @sh - stripe_head to parse
1446 *
1447 * Populates srcs in proper layout order for the stripe and returns the
1448 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1449 * destination buffer is recorded in srcs[count] and the Q destination
1450 * is recorded in srcs[count+1]].
1451 */
1452static int set_syndrome_sources(struct page **srcs,
1453 struct stripe_head *sh,
1454 int srctype)
1455{
1456 int disks = sh->disks;
1457 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1458 int d0_idx = raid6_d0(sh);
1459 int count;
1460 int i;
1461
1462 for (i = 0; i < disks; i++)
1463 srcs[i] = NULL;
1464
1465 count = 0;
1466 i = d0_idx;
1467 do {
1468 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1469 struct r5dev *dev = &sh->dev[i];
1470
1471 if (i == sh->qd_idx || i == sh->pd_idx ||
1472 (srctype == SYNDROME_SRC_ALL) ||
1473 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1474 (test_bit(R5_Wantdrain, &dev->flags) ||
1475 test_bit(R5_InJournal, &dev->flags))) ||
1476 (srctype == SYNDROME_SRC_WRITTEN &&
1477 (dev->written ||
1478 test_bit(R5_InJournal, &dev->flags)))) {
1479 if (test_bit(R5_InJournal, &dev->flags))
1480 srcs[slot] = sh->dev[i].orig_page;
1481 else
1482 srcs[slot] = sh->dev[i].page;
1483 }
1484 i = raid6_next_disk(i, disks);
1485 } while (i != d0_idx);
1486
1487 return syndrome_disks;
1488}
1489
1490static struct dma_async_tx_descriptor *
1491ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1492{
1493 int disks = sh->disks;
1494 struct page **blocks = to_addr_page(percpu, 0);
1495 int target;
1496 int qd_idx = sh->qd_idx;
1497 struct dma_async_tx_descriptor *tx;
1498 struct async_submit_ctl submit;
1499 struct r5dev *tgt;
1500 struct page *dest;
1501 int i;
1502 int count;
1503
1504 BUG_ON(sh->batch_head);
1505 if (sh->ops.target < 0)
1506 target = sh->ops.target2;
1507 else if (sh->ops.target2 < 0)
1508 target = sh->ops.target;
1509 else
1510 /* we should only have one valid target */
1511 BUG();
1512 BUG_ON(target < 0);
1513 pr_debug("%s: stripe %llu block: %d\n",
1514 __func__, (unsigned long long)sh->sector, target);
1515
1516 tgt = &sh->dev[target];
1517 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1518 dest = tgt->page;
1519
1520 atomic_inc(&sh->count);
1521
1522 if (target == qd_idx) {
1523 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
1524 blocks[count] = NULL; /* regenerating p is not necessary */
1525 BUG_ON(blocks[count+1] != dest); /* q should already be set */
1526 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1527 ops_complete_compute, sh,
1528 to_addr_conv(sh, percpu, 0));
1529 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1530 } else {
1531 /* Compute any data- or p-drive using XOR */
1532 count = 0;
1533 for (i = disks; i-- ; ) {
1534 if (i == target || i == qd_idx)
1535 continue;
1536 blocks[count++] = sh->dev[i].page;
1537 }
1538
1539 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1540 NULL, ops_complete_compute, sh,
1541 to_addr_conv(sh, percpu, 0));
1542 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1543 }
1544
1545 return tx;
1546}
1547
1548static struct dma_async_tx_descriptor *
1549ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1550{
1551 int i, count, disks = sh->disks;
1552 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1553 int d0_idx = raid6_d0(sh);
1554 int faila = -1, failb = -1;
1555 int target = sh->ops.target;
1556 int target2 = sh->ops.target2;
1557 struct r5dev *tgt = &sh->dev[target];
1558 struct r5dev *tgt2 = &sh->dev[target2];
1559 struct dma_async_tx_descriptor *tx;
1560 struct page **blocks = to_addr_page(percpu, 0);
1561 struct async_submit_ctl submit;
1562
1563 BUG_ON(sh->batch_head);
1564 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1565 __func__, (unsigned long long)sh->sector, target, target2);
1566 BUG_ON(target < 0 || target2 < 0);
1567 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1568 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1569
1570 /* we need to open-code set_syndrome_sources to handle the
1571 * slot number conversion for 'faila' and 'failb'
1572 */
1573 for (i = 0; i < disks ; i++)
1574 blocks[i] = NULL;
1575 count = 0;
1576 i = d0_idx;
1577 do {
1578 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1579
1580 blocks[slot] = sh->dev[i].page;
1581
1582 if (i == target)
1583 faila = slot;
1584 if (i == target2)
1585 failb = slot;
1586 i = raid6_next_disk(i, disks);
1587 } while (i != d0_idx);
1588
1589 BUG_ON(faila == failb);
1590 if (failb < faila)
1591 swap(faila, failb);
1592 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1593 __func__, (unsigned long long)sh->sector, faila, failb);
1594
1595 atomic_inc(&sh->count);
1596
1597 if (failb == syndrome_disks+1) {
1598 /* Q disk is one of the missing disks */
1599 if (faila == syndrome_disks) {
1600 /* Missing P+Q, just recompute */
1601 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1602 ops_complete_compute, sh,
1603 to_addr_conv(sh, percpu, 0));
1604 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
1605 STRIPE_SIZE, &submit);
1606 } else {
1607 struct page *dest;
1608 int data_target;
1609 int qd_idx = sh->qd_idx;
1610
1611 /* Missing D+Q: recompute D from P, then recompute Q */
1612 if (target == qd_idx)
1613 data_target = target2;
1614 else
1615 data_target = target;
1616
1617 count = 0;
1618 for (i = disks; i-- ; ) {
1619 if (i == data_target || i == qd_idx)
1620 continue;
1621 blocks[count++] = sh->dev[i].page;
1622 }
1623 dest = sh->dev[data_target].page;
1624 init_async_submit(&submit,
1625 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1626 NULL, NULL, NULL,
1627 to_addr_conv(sh, percpu, 0));
1628 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1629 &submit);
1630
1631 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
1632 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1633 ops_complete_compute, sh,
1634 to_addr_conv(sh, percpu, 0));
1635 return async_gen_syndrome(blocks, 0, count+2,
1636 STRIPE_SIZE, &submit);
1637 }
1638 } else {
1639 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1640 ops_complete_compute, sh,
1641 to_addr_conv(sh, percpu, 0));
1642 if (failb == syndrome_disks) {
1643 /* We're missing D+P. */
1644 return async_raid6_datap_recov(syndrome_disks+2,
1645 STRIPE_SIZE, faila,
1646 blocks, &submit);
1647 } else {
1648 /* We're missing D+D. */
1649 return async_raid6_2data_recov(syndrome_disks+2,
1650 STRIPE_SIZE, faila, failb,
1651 blocks, &submit);
1652 }
1653 }
1654}
1655
1656static void ops_complete_prexor(void *stripe_head_ref)
1657{
1658 struct stripe_head *sh = stripe_head_ref;
1659
1660 pr_debug("%s: stripe %llu\n", __func__,
1661 (unsigned long long)sh->sector);
1662
1663 if (r5c_is_writeback(sh->raid_conf->log))
1664 /*
1665 * raid5-cache write back uses orig_page during prexor.
1666 * After prexor, it is time to free orig_page
1667 */
1668 r5c_release_extra_page(sh);
1669}
1670
1671static struct dma_async_tx_descriptor *
1672ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1673 struct dma_async_tx_descriptor *tx)
1674{
1675 int disks = sh->disks;
1676 struct page **xor_srcs = to_addr_page(percpu, 0);
1677 int count = 0, pd_idx = sh->pd_idx, i;
1678 struct async_submit_ctl submit;
1679
1680 /* existing parity data subtracted */
1681 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1682
1683 BUG_ON(sh->batch_head);
1684 pr_debug("%s: stripe %llu\n", __func__,
1685 (unsigned long long)sh->sector);
1686
1687 for (i = disks; i--; ) {
1688 struct r5dev *dev = &sh->dev[i];
1689 /* Only process blocks that are known to be uptodate */
1690 if (test_bit(R5_InJournal, &dev->flags))
1691 xor_srcs[count++] = dev->orig_page;
1692 else if (test_bit(R5_Wantdrain, &dev->flags))
1693 xor_srcs[count++] = dev->page;
1694 }
1695
1696 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1697 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1698 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1699
1700 return tx;
1701}
1702
1703static struct dma_async_tx_descriptor *
1704ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1705 struct dma_async_tx_descriptor *tx)
1706{
1707 struct page **blocks = to_addr_page(percpu, 0);
1708 int count;
1709 struct async_submit_ctl submit;
1710
1711 pr_debug("%s: stripe %llu\n", __func__,
1712 (unsigned long long)sh->sector);
1713
1714 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1715
1716 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1717 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1718 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1719
1720 return tx;
1721}
1722
1723static struct dma_async_tx_descriptor *
1724ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1725{
1726 struct r5conf *conf = sh->raid_conf;
1727 int disks = sh->disks;
1728 int i;
1729 struct stripe_head *head_sh = sh;
1730
1731 pr_debug("%s: stripe %llu\n", __func__,
1732 (unsigned long long)sh->sector);
1733
1734 for (i = disks; i--; ) {
1735 struct r5dev *dev;
1736 struct bio *chosen;
1737
1738 sh = head_sh;
1739 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
1740 struct bio *wbi;
1741
1742again:
1743 dev = &sh->dev[i];
1744 /*
1745 * clear R5_InJournal, so when rewriting a page in
1746 * journal, it is not skipped by r5l_log_stripe()
1747 */
1748 clear_bit(R5_InJournal, &dev->flags);
1749 spin_lock_irq(&sh->stripe_lock);
1750 chosen = dev->towrite;
1751 dev->towrite = NULL;
1752 sh->overwrite_disks = 0;
1753 BUG_ON(dev->written);
1754 wbi = dev->written = chosen;
1755 spin_unlock_irq(&sh->stripe_lock);
1756 WARN_ON(dev->page != dev->orig_page);
1757
1758 while (wbi && wbi->bi_iter.bi_sector <
1759 dev->sector + STRIPE_SECTORS) {
1760 if (wbi->bi_opf & REQ_FUA)
1761 set_bit(R5_WantFUA, &dev->flags);
1762 if (wbi->bi_opf & REQ_SYNC)
1763 set_bit(R5_SyncIO, &dev->flags);
1764 if (bio_op(wbi) == REQ_OP_DISCARD)
1765 set_bit(R5_Discard, &dev->flags);
1766 else {
1767 tx = async_copy_data(1, wbi, &dev->page,
1768 dev->sector, tx, sh,
1769 r5c_is_writeback(conf->log));
1770 if (dev->page != dev->orig_page &&
1771 !r5c_is_writeback(conf->log)) {
1772 set_bit(R5_SkipCopy, &dev->flags);
1773 clear_bit(R5_UPTODATE, &dev->flags);
1774 clear_bit(R5_OVERWRITE, &dev->flags);
1775 }
1776 }
1777 wbi = r5_next_bio(wbi, dev->sector);
1778 }
1779
1780 if (head_sh->batch_head) {
1781 sh = list_first_entry(&sh->batch_list,
1782 struct stripe_head,
1783 batch_list);
1784 if (sh == head_sh)
1785 continue;
1786 goto again;
1787 }
1788 }
1789 }
1790
1791 return tx;
1792}
1793
1794static void ops_complete_reconstruct(void *stripe_head_ref)
1795{
1796 struct stripe_head *sh = stripe_head_ref;
1797 int disks = sh->disks;
1798 int pd_idx = sh->pd_idx;
1799 int qd_idx = sh->qd_idx;
1800 int i;
1801 bool fua = false, sync = false, discard = false;
1802
1803 pr_debug("%s: stripe %llu\n", __func__,
1804 (unsigned long long)sh->sector);
1805
1806 for (i = disks; i--; ) {
1807 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1808 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1809 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
1810 }
1811
1812 for (i = disks; i--; ) {
1813 struct r5dev *dev = &sh->dev[i];
1814
1815 if (dev->written || i == pd_idx || i == qd_idx) {
1816 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
1817 set_bit(R5_UPTODATE, &dev->flags);
1818 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1819 set_bit(R5_Expanded, &dev->flags);
1820 }
1821 if (fua)
1822 set_bit(R5_WantFUA, &dev->flags);
1823 if (sync)
1824 set_bit(R5_SyncIO, &dev->flags);
1825 }
1826 }
1827
1828 if (sh->reconstruct_state == reconstruct_state_drain_run)
1829 sh->reconstruct_state = reconstruct_state_drain_result;
1830 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1831 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1832 else {
1833 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1834 sh->reconstruct_state = reconstruct_state_result;
1835 }
1836
1837 set_bit(STRIPE_HANDLE, &sh->state);
1838 raid5_release_stripe(sh);
1839}
1840
1841static void
1842ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1843 struct dma_async_tx_descriptor *tx)
1844{
1845 int disks = sh->disks;
1846 struct page **xor_srcs;
1847 struct async_submit_ctl submit;
1848 int count, pd_idx = sh->pd_idx, i;
1849 struct page *xor_dest;
1850 int prexor = 0;
1851 unsigned long flags;
1852 int j = 0;
1853 struct stripe_head *head_sh = sh;
1854 int last_stripe;
1855
1856 pr_debug("%s: stripe %llu\n", __func__,
1857 (unsigned long long)sh->sector);
1858
1859 for (i = 0; i < sh->disks; i++) {
1860 if (pd_idx == i)
1861 continue;
1862 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1863 break;
1864 }
1865 if (i >= sh->disks) {
1866 atomic_inc(&sh->count);
1867 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1868 ops_complete_reconstruct(sh);
1869 return;
1870 }
1871again:
1872 count = 0;
1873 xor_srcs = to_addr_page(percpu, j);
1874 /* check if prexor is active which means only process blocks
1875 * that are part of a read-modify-write (written)
1876 */
1877 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1878 prexor = 1;
1879 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1880 for (i = disks; i--; ) {
1881 struct r5dev *dev = &sh->dev[i];
1882 if (head_sh->dev[i].written ||
1883 test_bit(R5_InJournal, &head_sh->dev[i].flags))
1884 xor_srcs[count++] = dev->page;
1885 }
1886 } else {
1887 xor_dest = sh->dev[pd_idx].page;
1888 for (i = disks; i--; ) {
1889 struct r5dev *dev = &sh->dev[i];
1890 if (i != pd_idx)
1891 xor_srcs[count++] = dev->page;
1892 }
1893 }
1894
1895 /* 1/ if we prexor'd then the dest is reused as a source
1896 * 2/ if we did not prexor then we are redoing the parity
1897 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1898 * for the synchronous xor case
1899 */
1900 last_stripe = !head_sh->batch_head ||
1901 list_first_entry(&sh->batch_list,
1902 struct stripe_head, batch_list) == head_sh;
1903 if (last_stripe) {
1904 flags = ASYNC_TX_ACK |
1905 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1906
1907 atomic_inc(&head_sh->count);
1908 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1909 to_addr_conv(sh, percpu, j));
1910 } else {
1911 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1912 init_async_submit(&submit, flags, tx, NULL, NULL,
1913 to_addr_conv(sh, percpu, j));
1914 }
1915
1916 if (unlikely(count == 1))
1917 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1918 else
1919 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1920 if (!last_stripe) {
1921 j++;
1922 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1923 batch_list);
1924 goto again;
1925 }
1926}
1927
1928static void
1929ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1930 struct dma_async_tx_descriptor *tx)
1931{
1932 struct async_submit_ctl submit;
1933 struct page **blocks;
1934 int count, i, j = 0;
1935 struct stripe_head *head_sh = sh;
1936 int last_stripe;
1937 int synflags;
1938 unsigned long txflags;
1939
1940 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1941
1942 for (i = 0; i < sh->disks; i++) {
1943 if (sh->pd_idx == i || sh->qd_idx == i)
1944 continue;
1945 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1946 break;
1947 }
1948 if (i >= sh->disks) {
1949 atomic_inc(&sh->count);
1950 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1951 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1952 ops_complete_reconstruct(sh);
1953 return;
1954 }
1955
1956again:
1957 blocks = to_addr_page(percpu, j);
1958
1959 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1960 synflags = SYNDROME_SRC_WRITTEN;
1961 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1962 } else {
1963 synflags = SYNDROME_SRC_ALL;
1964 txflags = ASYNC_TX_ACK;
1965 }
1966
1967 count = set_syndrome_sources(blocks, sh, synflags);
1968 last_stripe = !head_sh->batch_head ||
1969 list_first_entry(&sh->batch_list,
1970 struct stripe_head, batch_list) == head_sh;
1971
1972 if (last_stripe) {
1973 atomic_inc(&head_sh->count);
1974 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
1975 head_sh, to_addr_conv(sh, percpu, j));
1976 } else
1977 init_async_submit(&submit, 0, tx, NULL, NULL,
1978 to_addr_conv(sh, percpu, j));
1979 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1980 if (!last_stripe) {
1981 j++;
1982 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1983 batch_list);
1984 goto again;
1985 }
1986}
1987
1988static void ops_complete_check(void *stripe_head_ref)
1989{
1990 struct stripe_head *sh = stripe_head_ref;
1991
1992 pr_debug("%s: stripe %llu\n", __func__,
1993 (unsigned long long)sh->sector);
1994
1995 sh->check_state = check_state_check_result;
1996 set_bit(STRIPE_HANDLE, &sh->state);
1997 raid5_release_stripe(sh);
1998}
1999
2000static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
2001{
2002 int disks = sh->disks;
2003 int pd_idx = sh->pd_idx;
2004 int qd_idx = sh->qd_idx;
2005 struct page *xor_dest;
2006 struct page **xor_srcs = to_addr_page(percpu, 0);
2007 struct dma_async_tx_descriptor *tx;
2008 struct async_submit_ctl submit;
2009 int count;
2010 int i;
2011
2012 pr_debug("%s: stripe %llu\n", __func__,
2013 (unsigned long long)sh->sector);
2014
2015 BUG_ON(sh->batch_head);
2016 count = 0;
2017 xor_dest = sh->dev[pd_idx].page;
2018 xor_srcs[count++] = xor_dest;
2019 for (i = disks; i--; ) {
2020 if (i == pd_idx || i == qd_idx)
2021 continue;
2022 xor_srcs[count++] = sh->dev[i].page;
2023 }
2024
2025 init_async_submit(&submit, 0, NULL, NULL, NULL,
2026 to_addr_conv(sh, percpu, 0));
2027 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
2028 &sh->ops.zero_sum_result, &submit);
2029
2030 atomic_inc(&sh->count);
2031 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2032 tx = async_trigger_callback(&submit);
2033}
2034
2035static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2036{
2037 struct page **srcs = to_addr_page(percpu, 0);
2038 struct async_submit_ctl submit;
2039 int count;
2040
2041 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2042 (unsigned long long)sh->sector, checkp);
2043
2044 BUG_ON(sh->batch_head);
2045 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
2046 if (!checkp)
2047 srcs[count] = NULL;
2048
2049 atomic_inc(&sh->count);
2050 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
2051 sh, to_addr_conv(sh, percpu, 0));
2052 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
2053 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
2054}
2055
2056static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
2057{
2058 int overlap_clear = 0, i, disks = sh->disks;
2059 struct dma_async_tx_descriptor *tx = NULL;
2060 struct r5conf *conf = sh->raid_conf;
2061 int level = conf->level;
2062 struct raid5_percpu *percpu;
2063 unsigned long cpu;
2064
2065 cpu = get_cpu();
2066 percpu = per_cpu_ptr(conf->percpu, cpu);
2067 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
2068 ops_run_biofill(sh);
2069 overlap_clear++;
2070 }
2071
2072 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
2073 if (level < 6)
2074 tx = ops_run_compute5(sh, percpu);
2075 else {
2076 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2077 tx = ops_run_compute6_1(sh, percpu);
2078 else
2079 tx = ops_run_compute6_2(sh, percpu);
2080 }
2081 /* terminate the chain if reconstruct is not set to be run */
2082 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
2083 async_tx_ack(tx);
2084 }
2085
2086 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2087 if (level < 6)
2088 tx = ops_run_prexor5(sh, percpu, tx);
2089 else
2090 tx = ops_run_prexor6(sh, percpu, tx);
2091 }
2092
2093 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2094 tx = ops_run_partial_parity(sh, percpu, tx);
2095
2096 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
2097 tx = ops_run_biodrain(sh, tx);
2098 overlap_clear++;
2099 }
2100
2101 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2102 if (level < 6)
2103 ops_run_reconstruct5(sh, percpu, tx);
2104 else
2105 ops_run_reconstruct6(sh, percpu, tx);
2106 }
2107
2108 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2109 if (sh->check_state == check_state_run)
2110 ops_run_check_p(sh, percpu);
2111 else if (sh->check_state == check_state_run_q)
2112 ops_run_check_pq(sh, percpu, 0);
2113 else if (sh->check_state == check_state_run_pq)
2114 ops_run_check_pq(sh, percpu, 1);
2115 else
2116 BUG();
2117 }
2118
2119 if (overlap_clear && !sh->batch_head)
2120 for (i = disks; i--; ) {
2121 struct r5dev *dev = &sh->dev[i];
2122 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2123 wake_up(&sh->raid_conf->wait_for_overlap);
2124 }
2125 put_cpu();
2126}
2127
2128static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2129{
2130 if (sh->ppl_page)
2131 __free_page(sh->ppl_page);
2132 kmem_cache_free(sc, sh);
2133}
2134
2135static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2136 int disks, struct r5conf *conf)
2137{
2138 struct stripe_head *sh;
2139 int i;
2140
2141 sh = kmem_cache_zalloc(sc, gfp);
2142 if (sh) {
2143 spin_lock_init(&sh->stripe_lock);
2144 spin_lock_init(&sh->batch_lock);
2145 INIT_LIST_HEAD(&sh->batch_list);
2146 INIT_LIST_HEAD(&sh->lru);
2147 INIT_LIST_HEAD(&sh->r5c);
2148 INIT_LIST_HEAD(&sh->log_list);
2149 atomic_set(&sh->count, 1);
2150 sh->raid_conf = conf;
2151 sh->log_start = MaxSector;
2152 for (i = 0; i < disks; i++) {
2153 struct r5dev *dev = &sh->dev[i];
2154
2155 bio_init(&dev->req, &dev->vec, 1);
2156 bio_init(&dev->rreq, &dev->rvec, 1);
2157 }
2158
2159 if (raid5_has_ppl(conf)) {
2160 sh->ppl_page = alloc_page(gfp);
2161 if (!sh->ppl_page) {
2162 free_stripe(sc, sh);
2163 sh = NULL;
2164 }
2165 }
2166 }
2167 return sh;
2168}
2169static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
2170{
2171 struct stripe_head *sh;
2172
2173 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
2174 if (!sh)
2175 return 0;
2176
2177 if (grow_buffers(sh, gfp)) {
2178 shrink_buffers(sh);
2179 free_stripe(conf->slab_cache, sh);
2180 return 0;
2181 }
2182 sh->hash_lock_index =
2183 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
2184 /* we just created an active stripe so... */
2185 atomic_inc(&conf->active_stripes);
2186
2187 raid5_release_stripe(sh);
2188 conf->max_nr_stripes++;
2189 return 1;
2190}
2191
2192static int grow_stripes(struct r5conf *conf, int num)
2193{
2194 struct kmem_cache *sc;
2195 size_t namelen = sizeof(conf->cache_name[0]);
2196 int devs = max(conf->raid_disks, conf->previous_raid_disks);
2197
2198 if (conf->mddev->gendisk)
2199 snprintf(conf->cache_name[0], namelen,
2200 "raid%d-%s", conf->level, mdname(conf->mddev));
2201 else
2202 snprintf(conf->cache_name[0], namelen,
2203 "raid%d-%p", conf->level, conf->mddev);
2204 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
2205
2206 conf->active_name = 0;
2207 sc = kmem_cache_create(conf->cache_name[conf->active_name],
2208 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
2209 0, 0, NULL);
2210 if (!sc)
2211 return 1;
2212 conf->slab_cache = sc;
2213 conf->pool_size = devs;
2214 while (num--)
2215 if (!grow_one_stripe(conf, GFP_KERNEL))
2216 return 1;
2217
2218 return 0;
2219}
2220
2221/**
2222 * scribble_len - return the required size of the scribble region
2223 * @num - total number of disks in the array
2224 *
2225 * The size must be enough to contain:
2226 * 1/ a struct page pointer for each device in the array +2
2227 * 2/ room to convert each entry in (1) to its corresponding dma
2228 * (dma_map_page()) or page (page_address()) address.
2229 *
2230 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2231 * calculate over all devices (not just the data blocks), using zeros in place
2232 * of the P and Q blocks.
2233 */
2234static int scribble_alloc(struct raid5_percpu *percpu,
2235 int num, int cnt, gfp_t flags)
2236{
2237 size_t obj_size =
2238 sizeof(struct page *) * (num+2) +
2239 sizeof(addr_conv_t) * (num+2);
2240 void *scribble;
2241
2242 scribble = kvmalloc_array(cnt, obj_size, flags);
2243 if (!scribble)
2244 return -ENOMEM;
2245
2246 kvfree(percpu->scribble);
2247
2248 percpu->scribble = scribble;
2249 percpu->scribble_obj_size = obj_size;
2250 return 0;
2251}
2252
2253static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2254{
2255 unsigned long cpu;
2256 int err = 0;
2257
2258 /*
2259 * Never shrink. And mddev_suspend() could deadlock if this is called
2260 * from raid5d. In that case, scribble_disks and scribble_sectors
2261 * should equal to new_disks and new_sectors
2262 */
2263 if (conf->scribble_disks >= new_disks &&
2264 conf->scribble_sectors >= new_sectors)
2265 return 0;
2266 mddev_suspend(conf->mddev);
2267 get_online_cpus();
2268
2269 for_each_present_cpu(cpu) {
2270 struct raid5_percpu *percpu;
2271
2272 percpu = per_cpu_ptr(conf->percpu, cpu);
2273 err = scribble_alloc(percpu, new_disks,
2274 new_sectors / STRIPE_SECTORS,
2275 GFP_NOIO);
2276 if (err)
2277 break;
2278 }
2279
2280 put_online_cpus();
2281 mddev_resume(conf->mddev);
2282 if (!err) {
2283 conf->scribble_disks = new_disks;
2284 conf->scribble_sectors = new_sectors;
2285 }
2286 return err;
2287}
2288
2289static int resize_stripes(struct r5conf *conf, int newsize)
2290{
2291 /* Make all the stripes able to hold 'newsize' devices.
2292 * New slots in each stripe get 'page' set to a new page.
2293 *
2294 * This happens in stages:
2295 * 1/ create a new kmem_cache and allocate the required number of
2296 * stripe_heads.
2297 * 2/ gather all the old stripe_heads and transfer the pages across
2298 * to the new stripe_heads. This will have the side effect of
2299 * freezing the array as once all stripe_heads have been collected,
2300 * no IO will be possible. Old stripe heads are freed once their
2301 * pages have been transferred over, and the old kmem_cache is
2302 * freed when all stripes are done.
2303 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2304 * we simple return a failure status - no need to clean anything up.
2305 * 4/ allocate new pages for the new slots in the new stripe_heads.
2306 * If this fails, we don't bother trying the shrink the
2307 * stripe_heads down again, we just leave them as they are.
2308 * As each stripe_head is processed the new one is released into
2309 * active service.
2310 *
2311 * Once step2 is started, we cannot afford to wait for a write,
2312 * so we use GFP_NOIO allocations.
2313 */
2314 struct stripe_head *osh, *nsh;
2315 LIST_HEAD(newstripes);
2316 struct disk_info *ndisks;
2317 int err = 0;
2318 struct kmem_cache *sc;
2319 int i;
2320 int hash, cnt;
2321
2322 md_allow_write(conf->mddev);
2323
2324 /* Step 1 */
2325 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2326 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
2327 0, 0, NULL);
2328 if (!sc)
2329 return -ENOMEM;
2330
2331 /* Need to ensure auto-resizing doesn't interfere */
2332 mutex_lock(&conf->cache_size_mutex);
2333
2334 for (i = conf->max_nr_stripes; i; i--) {
2335 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
2336 if (!nsh)
2337 break;
2338
2339 list_add(&nsh->lru, &newstripes);
2340 }
2341 if (i) {
2342 /* didn't get enough, give up */
2343 while (!list_empty(&newstripes)) {
2344 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2345 list_del(&nsh->lru);
2346 free_stripe(sc, nsh);
2347 }
2348 kmem_cache_destroy(sc);
2349 mutex_unlock(&conf->cache_size_mutex);
2350 return -ENOMEM;
2351 }
2352 /* Step 2 - Must use GFP_NOIO now.
2353 * OK, we have enough stripes, start collecting inactive
2354 * stripes and copying them over
2355 */
2356 hash = 0;
2357 cnt = 0;
2358 list_for_each_entry(nsh, &newstripes, lru) {
2359 lock_device_hash_lock(conf, hash);
2360 wait_event_cmd(conf->wait_for_stripe,
2361 !list_empty(conf->inactive_list + hash),
2362 unlock_device_hash_lock(conf, hash),
2363 lock_device_hash_lock(conf, hash));
2364 osh = get_free_stripe(conf, hash);
2365 unlock_device_hash_lock(conf, hash);
2366
2367 for(i=0; i<conf->pool_size; i++) {
2368 nsh->dev[i].page = osh->dev[i].page;
2369 nsh->dev[i].orig_page = osh->dev[i].page;
2370 }
2371 nsh->hash_lock_index = hash;
2372 free_stripe(conf->slab_cache, osh);
2373 cnt++;
2374 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2375 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2376 hash++;
2377 cnt = 0;
2378 }
2379 }
2380 kmem_cache_destroy(conf->slab_cache);
2381
2382 /* Step 3.
2383 * At this point, we are holding all the stripes so the array
2384 * is completely stalled, so now is a good time to resize
2385 * conf->disks and the scribble region
2386 */
2387 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
2388 if (ndisks) {
2389 for (i = 0; i < conf->pool_size; i++)
2390 ndisks[i] = conf->disks[i];
2391
2392 for (i = conf->pool_size; i < newsize; i++) {
2393 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2394 if (!ndisks[i].extra_page)
2395 err = -ENOMEM;
2396 }
2397
2398 if (err) {
2399 for (i = conf->pool_size; i < newsize; i++)
2400 if (ndisks[i].extra_page)
2401 put_page(ndisks[i].extra_page);
2402 kfree(ndisks);
2403 } else {
2404 kfree(conf->disks);
2405 conf->disks = ndisks;
2406 }
2407 } else
2408 err = -ENOMEM;
2409
2410 mutex_unlock(&conf->cache_size_mutex);
2411
2412 conf->slab_cache = sc;
2413 conf->active_name = 1-conf->active_name;
2414
2415 /* Step 4, return new stripes to service */
2416 while(!list_empty(&newstripes)) {
2417 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2418 list_del_init(&nsh->lru);
2419
2420 for (i=conf->raid_disks; i < newsize; i++)
2421 if (nsh->dev[i].page == NULL) {
2422 struct page *p = alloc_page(GFP_NOIO);
2423 nsh->dev[i].page = p;
2424 nsh->dev[i].orig_page = p;
2425 if (!p)
2426 err = -ENOMEM;
2427 }
2428 raid5_release_stripe(nsh);
2429 }
2430 /* critical section pass, GFP_NOIO no longer needed */
2431
2432 if (!err)
2433 conf->pool_size = newsize;
2434 return err;
2435}
2436
2437static int drop_one_stripe(struct r5conf *conf)
2438{
2439 struct stripe_head *sh;
2440 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
2441
2442 spin_lock_irq(conf->hash_locks + hash);
2443 sh = get_free_stripe(conf, hash);
2444 spin_unlock_irq(conf->hash_locks + hash);
2445 if (!sh)
2446 return 0;
2447 BUG_ON(atomic_read(&sh->count));
2448 shrink_buffers(sh);
2449 free_stripe(conf->slab_cache, sh);
2450 atomic_dec(&conf->active_stripes);
2451 conf->max_nr_stripes--;
2452 return 1;
2453}
2454
2455static void shrink_stripes(struct r5conf *conf)
2456{
2457 while (conf->max_nr_stripes &&
2458 drop_one_stripe(conf))
2459 ;
2460
2461 kmem_cache_destroy(conf->slab_cache);
2462 conf->slab_cache = NULL;
2463}
2464
2465static void raid5_end_read_request(struct bio * bi)
2466{
2467 struct stripe_head *sh = bi->bi_private;
2468 struct r5conf *conf = sh->raid_conf;
2469 int disks = sh->disks, i;
2470 char b[BDEVNAME_SIZE];
2471 struct md_rdev *rdev = NULL;
2472 sector_t s;
2473
2474 for (i=0 ; i<disks; i++)
2475 if (bi == &sh->dev[i].req)
2476 break;
2477
2478 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
2479 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2480 bi->bi_status);
2481 if (i == disks) {
2482 bio_reset(bi);
2483 BUG();
2484 return;
2485 }
2486 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2487 /* If replacement finished while this request was outstanding,
2488 * 'replacement' might be NULL already.
2489 * In that case it moved down to 'rdev'.
2490 * rdev is not removed until all requests are finished.
2491 */
2492 rdev = conf->disks[i].replacement;
2493 if (!rdev)
2494 rdev = conf->disks[i].rdev;
2495
2496 if (use_new_offset(conf, sh))
2497 s = sh->sector + rdev->new_data_offset;
2498 else
2499 s = sh->sector + rdev->data_offset;
2500 if (!bi->bi_status) {
2501 set_bit(R5_UPTODATE, &sh->dev[i].flags);
2502 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2503 /* Note that this cannot happen on a
2504 * replacement device. We just fail those on
2505 * any error
2506 */
2507 pr_info_ratelimited(
2508 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
2509 mdname(conf->mddev), STRIPE_SECTORS,
2510 (unsigned long long)s,
2511 bdevname(rdev->bdev, b));
2512 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2513 clear_bit(R5_ReadError, &sh->dev[i].flags);
2514 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2515 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2516 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2517
2518 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2519 /*
2520 * end read for a page in journal, this
2521 * must be preparing for prexor in rmw
2522 */
2523 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2524
2525 if (atomic_read(&rdev->read_errors))
2526 atomic_set(&rdev->read_errors, 0);
2527 } else {
2528 const char *bdn = bdevname(rdev->bdev, b);
2529 int retry = 0;
2530 int set_bad = 0;
2531
2532 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
2533 atomic_inc(&rdev->read_errors);
2534 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2535 pr_warn_ratelimited(
2536 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
2537 mdname(conf->mddev),
2538 (unsigned long long)s,
2539 bdn);
2540 else if (conf->mddev->degraded >= conf->max_degraded) {
2541 set_bad = 1;
2542 pr_warn_ratelimited(
2543 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
2544 mdname(conf->mddev),
2545 (unsigned long long)s,
2546 bdn);
2547 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
2548 /* Oh, no!!! */
2549 set_bad = 1;
2550 pr_warn_ratelimited(
2551 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
2552 mdname(conf->mddev),
2553 (unsigned long long)s,
2554 bdn);
2555 } else if (atomic_read(&rdev->read_errors)
2556 > conf->max_nr_stripes)
2557 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2558 mdname(conf->mddev), bdn);
2559 else
2560 retry = 1;
2561 if (set_bad && test_bit(In_sync, &rdev->flags)
2562 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2563 retry = 1;
2564 if (retry)
2565 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2566 set_bit(R5_ReadError, &sh->dev[i].flags);
2567 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2568 } else
2569 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2570 else {
2571 clear_bit(R5_ReadError, &sh->dev[i].flags);
2572 clear_bit(R5_ReWrite, &sh->dev[i].flags);
2573 if (!(set_bad
2574 && test_bit(In_sync, &rdev->flags)
2575 && rdev_set_badblocks(
2576 rdev, sh->sector, STRIPE_SECTORS, 0)))
2577 md_error(conf->mddev, rdev);
2578 }
2579 }
2580 rdev_dec_pending(rdev, conf->mddev);
2581 bio_reset(bi);
2582 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2583 set_bit(STRIPE_HANDLE, &sh->state);
2584 raid5_release_stripe(sh);
2585}
2586
2587static void raid5_end_write_request(struct bio *bi)
2588{
2589 struct stripe_head *sh = bi->bi_private;
2590 struct r5conf *conf = sh->raid_conf;
2591 int disks = sh->disks, i;
2592 struct md_rdev *uninitialized_var(rdev);
2593 sector_t first_bad;
2594 int bad_sectors;
2595 int replacement = 0;
2596
2597 for (i = 0 ; i < disks; i++) {
2598 if (bi == &sh->dev[i].req) {
2599 rdev = conf->disks[i].rdev;
2600 break;
2601 }
2602 if (bi == &sh->dev[i].rreq) {
2603 rdev = conf->disks[i].replacement;
2604 if (rdev)
2605 replacement = 1;
2606 else
2607 /* rdev was removed and 'replacement'
2608 * replaced it. rdev is not removed
2609 * until all requests are finished.
2610 */
2611 rdev = conf->disks[i].rdev;
2612 break;
2613 }
2614 }
2615 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
2616 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2617 bi->bi_status);
2618 if (i == disks) {
2619 bio_reset(bi);
2620 BUG();
2621 return;
2622 }
2623
2624 if (replacement) {
2625 if (bi->bi_status)
2626 md_error(conf->mddev, rdev);
2627 else if (is_badblock(rdev, sh->sector,
2628 STRIPE_SECTORS,
2629 &first_bad, &bad_sectors))
2630 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2631 } else {
2632 if (bi->bi_status) {
2633 set_bit(STRIPE_DEGRADED, &sh->state);
2634 set_bit(WriteErrorSeen, &rdev->flags);
2635 set_bit(R5_WriteError, &sh->dev[i].flags);
2636 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2637 set_bit(MD_RECOVERY_NEEDED,
2638 &rdev->mddev->recovery);
2639 } else if (is_badblock(rdev, sh->sector,
2640 STRIPE_SECTORS,
2641 &first_bad, &bad_sectors)) {
2642 set_bit(R5_MadeGood, &sh->dev[i].flags);
2643 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2644 /* That was a successful write so make
2645 * sure it looks like we already did
2646 * a re-write.
2647 */
2648 set_bit(R5_ReWrite, &sh->dev[i].flags);
2649 }
2650 }
2651 rdev_dec_pending(rdev, conf->mddev);
2652
2653 if (sh->batch_head && bi->bi_status && !replacement)
2654 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2655
2656 bio_reset(bi);
2657 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2658 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2659 set_bit(STRIPE_HANDLE, &sh->state);
2660 raid5_release_stripe(sh);
2661
2662 if (sh->batch_head && sh != sh->batch_head)
2663 raid5_release_stripe(sh->batch_head);
2664}
2665
2666static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
2667{
2668 char b[BDEVNAME_SIZE];
2669 struct r5conf *conf = mddev->private;
2670 unsigned long flags;
2671 pr_debug("raid456: error called\n");
2672
2673 spin_lock_irqsave(&conf->device_lock, flags);
2674
2675 if (test_bit(In_sync, &rdev->flags) &&
2676 mddev->degraded == conf->max_degraded) {
2677 /*
2678 * Don't allow to achieve failed state
2679 * Don't try to recover this device
2680 */
2681 conf->recovery_disabled = mddev->recovery_disabled;
2682 spin_unlock_irqrestore(&conf->device_lock, flags);
2683 return;
2684 }
2685
2686 set_bit(Faulty, &rdev->flags);
2687 clear_bit(In_sync, &rdev->flags);
2688 mddev->degraded = raid5_calc_degraded(conf);
2689 spin_unlock_irqrestore(&conf->device_lock, flags);
2690 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2691
2692 set_bit(Blocked, &rdev->flags);
2693 set_mask_bits(&mddev->sb_flags, 0,
2694 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
2695 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2696 "md/raid:%s: Operation continuing on %d devices.\n",
2697 mdname(mddev),
2698 bdevname(rdev->bdev, b),
2699 mdname(mddev),
2700 conf->raid_disks - mddev->degraded);
2701 r5c_update_on_rdev_error(mddev, rdev);
2702}
2703
2704/*
2705 * Input: a 'big' sector number,
2706 * Output: index of the data and parity disk, and the sector # in them.
2707 */
2708sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2709 int previous, int *dd_idx,
2710 struct stripe_head *sh)
2711{
2712 sector_t stripe, stripe2;
2713 sector_t chunk_number;
2714 unsigned int chunk_offset;
2715 int pd_idx, qd_idx;
2716 int ddf_layout = 0;
2717 sector_t new_sector;
2718 int algorithm = previous ? conf->prev_algo
2719 : conf->algorithm;
2720 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2721 : conf->chunk_sectors;
2722 int raid_disks = previous ? conf->previous_raid_disks
2723 : conf->raid_disks;
2724 int data_disks = raid_disks - conf->max_degraded;
2725
2726 /* First compute the information on this sector */
2727
2728 /*
2729 * Compute the chunk number and the sector offset inside the chunk
2730 */
2731 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2732 chunk_number = r_sector;
2733
2734 /*
2735 * Compute the stripe number
2736 */
2737 stripe = chunk_number;
2738 *dd_idx = sector_div(stripe, data_disks);
2739 stripe2 = stripe;
2740 /*
2741 * Select the parity disk based on the user selected algorithm.
2742 */
2743 pd_idx = qd_idx = -1;
2744 switch(conf->level) {
2745 case 4:
2746 pd_idx = data_disks;
2747 break;
2748 case 5:
2749 switch (algorithm) {
2750 case ALGORITHM_LEFT_ASYMMETRIC:
2751 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2752 if (*dd_idx >= pd_idx)
2753 (*dd_idx)++;
2754 break;
2755 case ALGORITHM_RIGHT_ASYMMETRIC:
2756 pd_idx = sector_div(stripe2, raid_disks);
2757 if (*dd_idx >= pd_idx)
2758 (*dd_idx)++;
2759 break;
2760 case ALGORITHM_LEFT_SYMMETRIC:
2761 pd_idx = data_disks - sector_div(stripe2, raid_disks);
2762 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2763 break;
2764 case ALGORITHM_RIGHT_SYMMETRIC:
2765 pd_idx = sector_div(stripe2, raid_disks);
2766 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
2767 break;
2768 case ALGORITHM_PARITY_0:
2769 pd_idx = 0;
2770 (*dd_idx)++;
2771 break;
2772 case ALGORITHM_PARITY_N:
2773 pd_idx = data_disks;
2774 break;
2775 default:
2776 BUG();
2777 }
2778 break;
2779 case 6:
2780
2781 switch (algorithm) {
2782 case ALGORITHM_LEFT_ASYMMETRIC:
2783 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
2784 qd_idx = pd_idx + 1;
2785 if (pd_idx == raid_disks-1) {
2786 (*