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