]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/btrfs/raid56.c
Btrfs, replace: write raid56 parity into the replace target device
[karo-tx-linux.git] / fs / btrfs / raid56.c
1 /*
2  * Copyright (C) 2012 Fusion-io  All rights reserved.
3  * Copyright (C) 2012 Intel Corp. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License v2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 021110-1307, USA.
18  */
19 #include <linux/sched.h>
20 #include <linux/wait.h>
21 #include <linux/bio.h>
22 #include <linux/slab.h>
23 #include <linux/buffer_head.h>
24 #include <linux/blkdev.h>
25 #include <linux/random.h>
26 #include <linux/iocontext.h>
27 #include <linux/capability.h>
28 #include <linux/ratelimit.h>
29 #include <linux/kthread.h>
30 #include <linux/raid/pq.h>
31 #include <linux/hash.h>
32 #include <linux/list_sort.h>
33 #include <linux/raid/xor.h>
34 #include <linux/vmalloc.h>
35 #include <asm/div64.h>
36 #include "ctree.h"
37 #include "extent_map.h"
38 #include "disk-io.h"
39 #include "transaction.h"
40 #include "print-tree.h"
41 #include "volumes.h"
42 #include "raid56.h"
43 #include "async-thread.h"
44 #include "check-integrity.h"
45 #include "rcu-string.h"
46
47 /* set when additional merges to this rbio are not allowed */
48 #define RBIO_RMW_LOCKED_BIT     1
49
50 /*
51  * set when this rbio is sitting in the hash, but it is just a cache
52  * of past RMW
53  */
54 #define RBIO_CACHE_BIT          2
55
56 /*
57  * set when it is safe to trust the stripe_pages for caching
58  */
59 #define RBIO_CACHE_READY_BIT    3
60
61 /*
62  * bbio and raid_map is managed by the caller, so we shouldn't free
63  * them here. And besides that, all rbios with this flag should not
64  * be cached, because we need raid_map to check the rbios' stripe
65  * is the same or not, but it is very likely that the caller has
66  * free raid_map, so don't cache those rbios.
67  */
68 #define RBIO_HOLD_BBIO_MAP_BIT  4
69
70 #define RBIO_CACHE_SIZE 1024
71
72 enum btrfs_rbio_ops {
73         BTRFS_RBIO_WRITE        = 0,
74         BTRFS_RBIO_READ_REBUILD = 1,
75         BTRFS_RBIO_PARITY_SCRUB = 2,
76 };
77
78 struct btrfs_raid_bio {
79         struct btrfs_fs_info *fs_info;
80         struct btrfs_bio *bbio;
81
82         /*
83          * logical block numbers for the start of each stripe
84          * The last one or two are p/q.  These are sorted,
85          * so raid_map[0] is the start of our full stripe
86          */
87         u64 *raid_map;
88
89         /* while we're doing rmw on a stripe
90          * we put it into a hash table so we can
91          * lock the stripe and merge more rbios
92          * into it.
93          */
94         struct list_head hash_list;
95
96         /*
97          * LRU list for the stripe cache
98          */
99         struct list_head stripe_cache;
100
101         /*
102          * for scheduling work in the helper threads
103          */
104         struct btrfs_work work;
105
106         /*
107          * bio list and bio_list_lock are used
108          * to add more bios into the stripe
109          * in hopes of avoiding the full rmw
110          */
111         struct bio_list bio_list;
112         spinlock_t bio_list_lock;
113
114         /* also protected by the bio_list_lock, the
115          * plug list is used by the plugging code
116          * to collect partial bios while plugged.  The
117          * stripe locking code also uses it to hand off
118          * the stripe lock to the next pending IO
119          */
120         struct list_head plug_list;
121
122         /*
123          * flags that tell us if it is safe to
124          * merge with this bio
125          */
126         unsigned long flags;
127
128         /* size of each individual stripe on disk */
129         int stripe_len;
130
131         /* number of data stripes (no p/q) */
132         int nr_data;
133
134         int real_stripes;
135
136         int stripe_npages;
137         /*
138          * set if we're doing a parity rebuild
139          * for a read from higher up, which is handled
140          * differently from a parity rebuild as part of
141          * rmw
142          */
143         enum btrfs_rbio_ops operation;
144
145         /* first bad stripe */
146         int faila;
147
148         /* second bad stripe (for raid6 use) */
149         int failb;
150
151         int scrubp;
152         /*
153          * number of pages needed to represent the full
154          * stripe
155          */
156         int nr_pages;
157
158         /*
159          * size of all the bios in the bio_list.  This
160          * helps us decide if the rbio maps to a full
161          * stripe or not
162          */
163         int bio_list_bytes;
164
165         atomic_t refs;
166
167         atomic_t stripes_pending;
168
169         atomic_t error;
170         /*
171          * these are two arrays of pointers.  We allocate the
172          * rbio big enough to hold them both and setup their
173          * locations when the rbio is allocated
174          */
175
176         /* pointers to pages that we allocated for
177          * reading/writing stripes directly from the disk (including P/Q)
178          */
179         struct page **stripe_pages;
180
181         /*
182          * pointers to the pages in the bio_list.  Stored
183          * here for faster lookup
184          */
185         struct page **bio_pages;
186
187         /*
188          * bitmap to record which horizontal stripe has data
189          */
190         unsigned long *dbitmap;
191 };
192
193 static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
194 static noinline void finish_rmw(struct btrfs_raid_bio *rbio);
195 static void rmw_work(struct btrfs_work *work);
196 static void read_rebuild_work(struct btrfs_work *work);
197 static void async_rmw_stripe(struct btrfs_raid_bio *rbio);
198 static void async_read_rebuild(struct btrfs_raid_bio *rbio);
199 static int fail_bio_stripe(struct btrfs_raid_bio *rbio, struct bio *bio);
200 static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed);
201 static void __free_raid_bio(struct btrfs_raid_bio *rbio);
202 static void index_rbio_pages(struct btrfs_raid_bio *rbio);
203 static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
204
205 static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
206                                          int need_check);
207 static void async_scrub_parity(struct btrfs_raid_bio *rbio);
208
209 /*
210  * the stripe hash table is used for locking, and to collect
211  * bios in hopes of making a full stripe
212  */
213 int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
214 {
215         struct btrfs_stripe_hash_table *table;
216         struct btrfs_stripe_hash_table *x;
217         struct btrfs_stripe_hash *cur;
218         struct btrfs_stripe_hash *h;
219         int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS;
220         int i;
221         int table_size;
222
223         if (info->stripe_hash_table)
224                 return 0;
225
226         /*
227          * The table is large, starting with order 4 and can go as high as
228          * order 7 in case lock debugging is turned on.
229          *
230          * Try harder to allocate and fallback to vmalloc to lower the chance
231          * of a failing mount.
232          */
233         table_size = sizeof(*table) + sizeof(*h) * num_entries;
234         table = kzalloc(table_size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
235         if (!table) {
236                 table = vzalloc(table_size);
237                 if (!table)
238                         return -ENOMEM;
239         }
240
241         spin_lock_init(&table->cache_lock);
242         INIT_LIST_HEAD(&table->stripe_cache);
243
244         h = table->table;
245
246         for (i = 0; i < num_entries; i++) {
247                 cur = h + i;
248                 INIT_LIST_HEAD(&cur->hash_list);
249                 spin_lock_init(&cur->lock);
250                 init_waitqueue_head(&cur->wait);
251         }
252
253         x = cmpxchg(&info->stripe_hash_table, NULL, table);
254         if (x) {
255                 if (is_vmalloc_addr(x))
256                         vfree(x);
257                 else
258                         kfree(x);
259         }
260         return 0;
261 }
262
263 /*
264  * caching an rbio means to copy anything from the
265  * bio_pages array into the stripe_pages array.  We
266  * use the page uptodate bit in the stripe cache array
267  * to indicate if it has valid data
268  *
269  * once the caching is done, we set the cache ready
270  * bit.
271  */
272 static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
273 {
274         int i;
275         char *s;
276         char *d;
277         int ret;
278
279         ret = alloc_rbio_pages(rbio);
280         if (ret)
281                 return;
282
283         for (i = 0; i < rbio->nr_pages; i++) {
284                 if (!rbio->bio_pages[i])
285                         continue;
286
287                 s = kmap(rbio->bio_pages[i]);
288                 d = kmap(rbio->stripe_pages[i]);
289
290                 memcpy(d, s, PAGE_CACHE_SIZE);
291
292                 kunmap(rbio->bio_pages[i]);
293                 kunmap(rbio->stripe_pages[i]);
294                 SetPageUptodate(rbio->stripe_pages[i]);
295         }
296         set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
297 }
298
299 /*
300  * we hash on the first logical address of the stripe
301  */
302 static int rbio_bucket(struct btrfs_raid_bio *rbio)
303 {
304         u64 num = rbio->raid_map[0];
305
306         /*
307          * we shift down quite a bit.  We're using byte
308          * addressing, and most of the lower bits are zeros.
309          * This tends to upset hash_64, and it consistently
310          * returns just one or two different values.
311          *
312          * shifting off the lower bits fixes things.
313          */
314         return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
315 }
316
317 /*
318  * stealing an rbio means taking all the uptodate pages from the stripe
319  * array in the source rbio and putting them into the destination rbio
320  */
321 static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
322 {
323         int i;
324         struct page *s;
325         struct page *d;
326
327         if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
328                 return;
329
330         for (i = 0; i < dest->nr_pages; i++) {
331                 s = src->stripe_pages[i];
332                 if (!s || !PageUptodate(s)) {
333                         continue;
334                 }
335
336                 d = dest->stripe_pages[i];
337                 if (d)
338                         __free_page(d);
339
340                 dest->stripe_pages[i] = s;
341                 src->stripe_pages[i] = NULL;
342         }
343 }
344
345 /*
346  * merging means we take the bio_list from the victim and
347  * splice it into the destination.  The victim should
348  * be discarded afterwards.
349  *
350  * must be called with dest->rbio_list_lock held
351  */
352 static void merge_rbio(struct btrfs_raid_bio *dest,
353                        struct btrfs_raid_bio *victim)
354 {
355         bio_list_merge(&dest->bio_list, &victim->bio_list);
356         dest->bio_list_bytes += victim->bio_list_bytes;
357         bio_list_init(&victim->bio_list);
358 }
359
360 /*
361  * used to prune items that are in the cache.  The caller
362  * must hold the hash table lock.
363  */
364 static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
365 {
366         int bucket = rbio_bucket(rbio);
367         struct btrfs_stripe_hash_table *table;
368         struct btrfs_stripe_hash *h;
369         int freeit = 0;
370
371         /*
372          * check the bit again under the hash table lock.
373          */
374         if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
375                 return;
376
377         table = rbio->fs_info->stripe_hash_table;
378         h = table->table + bucket;
379
380         /* hold the lock for the bucket because we may be
381          * removing it from the hash table
382          */
383         spin_lock(&h->lock);
384
385         /*
386          * hold the lock for the bio list because we need
387          * to make sure the bio list is empty
388          */
389         spin_lock(&rbio->bio_list_lock);
390
391         if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
392                 list_del_init(&rbio->stripe_cache);
393                 table->cache_size -= 1;
394                 freeit = 1;
395
396                 /* if the bio list isn't empty, this rbio is
397                  * still involved in an IO.  We take it out
398                  * of the cache list, and drop the ref that
399                  * was held for the list.
400                  *
401                  * If the bio_list was empty, we also remove
402                  * the rbio from the hash_table, and drop
403                  * the corresponding ref
404                  */
405                 if (bio_list_empty(&rbio->bio_list)) {
406                         if (!list_empty(&rbio->hash_list)) {
407                                 list_del_init(&rbio->hash_list);
408                                 atomic_dec(&rbio->refs);
409                                 BUG_ON(!list_empty(&rbio->plug_list));
410                         }
411                 }
412         }
413
414         spin_unlock(&rbio->bio_list_lock);
415         spin_unlock(&h->lock);
416
417         if (freeit)
418                 __free_raid_bio(rbio);
419 }
420
421 /*
422  * prune a given rbio from the cache
423  */
424 static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
425 {
426         struct btrfs_stripe_hash_table *table;
427         unsigned long flags;
428
429         if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
430                 return;
431
432         table = rbio->fs_info->stripe_hash_table;
433
434         spin_lock_irqsave(&table->cache_lock, flags);
435         __remove_rbio_from_cache(rbio);
436         spin_unlock_irqrestore(&table->cache_lock, flags);
437 }
438
439 /*
440  * remove everything in the cache
441  */
442 static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
443 {
444         struct btrfs_stripe_hash_table *table;
445         unsigned long flags;
446         struct btrfs_raid_bio *rbio;
447
448         table = info->stripe_hash_table;
449
450         spin_lock_irqsave(&table->cache_lock, flags);
451         while (!list_empty(&table->stripe_cache)) {
452                 rbio = list_entry(table->stripe_cache.next,
453                                   struct btrfs_raid_bio,
454                                   stripe_cache);
455                 __remove_rbio_from_cache(rbio);
456         }
457         spin_unlock_irqrestore(&table->cache_lock, flags);
458 }
459
460 /*
461  * remove all cached entries and free the hash table
462  * used by unmount
463  */
464 void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
465 {
466         if (!info->stripe_hash_table)
467                 return;
468         btrfs_clear_rbio_cache(info);
469         if (is_vmalloc_addr(info->stripe_hash_table))
470                 vfree(info->stripe_hash_table);
471         else
472                 kfree(info->stripe_hash_table);
473         info->stripe_hash_table = NULL;
474 }
475
476 /*
477  * insert an rbio into the stripe cache.  It
478  * must have already been prepared by calling
479  * cache_rbio_pages
480  *
481  * If this rbio was already cached, it gets
482  * moved to the front of the lru.
483  *
484  * If the size of the rbio cache is too big, we
485  * prune an item.
486  */
487 static void cache_rbio(struct btrfs_raid_bio *rbio)
488 {
489         struct btrfs_stripe_hash_table *table;
490         unsigned long flags;
491
492         if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
493                 return;
494
495         table = rbio->fs_info->stripe_hash_table;
496
497         spin_lock_irqsave(&table->cache_lock, flags);
498         spin_lock(&rbio->bio_list_lock);
499
500         /* bump our ref if we were not in the list before */
501         if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
502                 atomic_inc(&rbio->refs);
503
504         if (!list_empty(&rbio->stripe_cache)){
505                 list_move(&rbio->stripe_cache, &table->stripe_cache);
506         } else {
507                 list_add(&rbio->stripe_cache, &table->stripe_cache);
508                 table->cache_size += 1;
509         }
510
511         spin_unlock(&rbio->bio_list_lock);
512
513         if (table->cache_size > RBIO_CACHE_SIZE) {
514                 struct btrfs_raid_bio *found;
515
516                 found = list_entry(table->stripe_cache.prev,
517                                   struct btrfs_raid_bio,
518                                   stripe_cache);
519
520                 if (found != rbio)
521                         __remove_rbio_from_cache(found);
522         }
523
524         spin_unlock_irqrestore(&table->cache_lock, flags);
525         return;
526 }
527
528 /*
529  * helper function to run the xor_blocks api.  It is only
530  * able to do MAX_XOR_BLOCKS at a time, so we need to
531  * loop through.
532  */
533 static void run_xor(void **pages, int src_cnt, ssize_t len)
534 {
535         int src_off = 0;
536         int xor_src_cnt = 0;
537         void *dest = pages[src_cnt];
538
539         while(src_cnt > 0) {
540                 xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
541                 xor_blocks(xor_src_cnt, len, dest, pages + src_off);
542
543                 src_cnt -= xor_src_cnt;
544                 src_off += xor_src_cnt;
545         }
546 }
547
548 /*
549  * returns true if the bio list inside this rbio
550  * covers an entire stripe (no rmw required).
551  * Must be called with the bio list lock held, or
552  * at a time when you know it is impossible to add
553  * new bios into the list
554  */
555 static int __rbio_is_full(struct btrfs_raid_bio *rbio)
556 {
557         unsigned long size = rbio->bio_list_bytes;
558         int ret = 1;
559
560         if (size != rbio->nr_data * rbio->stripe_len)
561                 ret = 0;
562
563         BUG_ON(size > rbio->nr_data * rbio->stripe_len);
564         return ret;
565 }
566
567 static int rbio_is_full(struct btrfs_raid_bio *rbio)
568 {
569         unsigned long flags;
570         int ret;
571
572         spin_lock_irqsave(&rbio->bio_list_lock, flags);
573         ret = __rbio_is_full(rbio);
574         spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
575         return ret;
576 }
577
578 /*
579  * returns 1 if it is safe to merge two rbios together.
580  * The merging is safe if the two rbios correspond to
581  * the same stripe and if they are both going in the same
582  * direction (read vs write), and if neither one is
583  * locked for final IO
584  *
585  * The caller is responsible for locking such that
586  * rmw_locked is safe to test
587  */
588 static int rbio_can_merge(struct btrfs_raid_bio *last,
589                           struct btrfs_raid_bio *cur)
590 {
591         if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
592             test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
593                 return 0;
594
595         /*
596          * we can't merge with cached rbios, since the
597          * idea is that when we merge the destination
598          * rbio is going to run our IO for us.  We can
599          * steal from cached rbio's though, other functions
600          * handle that.
601          */
602         if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
603             test_bit(RBIO_CACHE_BIT, &cur->flags))
604                 return 0;
605
606         if (last->raid_map[0] !=
607             cur->raid_map[0])
608                 return 0;
609
610         /* we can't merge with different operations */
611         if (last->operation != cur->operation)
612                 return 0;
613         /*
614          * We've need read the full stripe from the drive.
615          * check and repair the parity and write the new results.
616          *
617          * We're not allowed to add any new bios to the
618          * bio list here, anyone else that wants to
619          * change this stripe needs to do their own rmw.
620          */
621         if (last->operation == BTRFS_RBIO_PARITY_SCRUB ||
622             cur->operation == BTRFS_RBIO_PARITY_SCRUB)
623                 return 0;
624
625         return 1;
626 }
627
628 /*
629  * helper to index into the pstripe
630  */
631 static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index)
632 {
633         index += (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT;
634         return rbio->stripe_pages[index];
635 }
636
637 /*
638  * helper to index into the qstripe, returns null
639  * if there is no qstripe
640  */
641 static struct page *rbio_qstripe_page(struct btrfs_raid_bio *rbio, int index)
642 {
643         if (rbio->nr_data + 1 == rbio->real_stripes)
644                 return NULL;
645
646         index += ((rbio->nr_data + 1) * rbio->stripe_len) >>
647                 PAGE_CACHE_SHIFT;
648         return rbio->stripe_pages[index];
649 }
650
651 /*
652  * The first stripe in the table for a logical address
653  * has the lock.  rbios are added in one of three ways:
654  *
655  * 1) Nobody has the stripe locked yet.  The rbio is given
656  * the lock and 0 is returned.  The caller must start the IO
657  * themselves.
658  *
659  * 2) Someone has the stripe locked, but we're able to merge
660  * with the lock owner.  The rbio is freed and the IO will
661  * start automatically along with the existing rbio.  1 is returned.
662  *
663  * 3) Someone has the stripe locked, but we're not able to merge.
664  * The rbio is added to the lock owner's plug list, or merged into
665  * an rbio already on the plug list.  When the lock owner unlocks,
666  * the next rbio on the list is run and the IO is started automatically.
667  * 1 is returned
668  *
669  * If we return 0, the caller still owns the rbio and must continue with
670  * IO submission.  If we return 1, the caller must assume the rbio has
671  * already been freed.
672  */
673 static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
674 {
675         int bucket = rbio_bucket(rbio);
676         struct btrfs_stripe_hash *h = rbio->fs_info->stripe_hash_table->table + bucket;
677         struct btrfs_raid_bio *cur;
678         struct btrfs_raid_bio *pending;
679         unsigned long flags;
680         DEFINE_WAIT(wait);
681         struct btrfs_raid_bio *freeit = NULL;
682         struct btrfs_raid_bio *cache_drop = NULL;
683         int ret = 0;
684         int walk = 0;
685
686         spin_lock_irqsave(&h->lock, flags);
687         list_for_each_entry(cur, &h->hash_list, hash_list) {
688                 walk++;
689                 if (cur->raid_map[0] == rbio->raid_map[0]) {
690                         spin_lock(&cur->bio_list_lock);
691
692                         /* can we steal this cached rbio's pages? */
693                         if (bio_list_empty(&cur->bio_list) &&
694                             list_empty(&cur->plug_list) &&
695                             test_bit(RBIO_CACHE_BIT, &cur->flags) &&
696                             !test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags)) {
697                                 list_del_init(&cur->hash_list);
698                                 atomic_dec(&cur->refs);
699
700                                 steal_rbio(cur, rbio);
701                                 cache_drop = cur;
702                                 spin_unlock(&cur->bio_list_lock);
703
704                                 goto lockit;
705                         }
706
707                         /* can we merge into the lock owner? */
708                         if (rbio_can_merge(cur, rbio)) {
709                                 merge_rbio(cur, rbio);
710                                 spin_unlock(&cur->bio_list_lock);
711                                 freeit = rbio;
712                                 ret = 1;
713                                 goto out;
714                         }
715
716
717                         /*
718                          * we couldn't merge with the running
719                          * rbio, see if we can merge with the
720                          * pending ones.  We don't have to
721                          * check for rmw_locked because there
722                          * is no way they are inside finish_rmw
723                          * right now
724                          */
725                         list_for_each_entry(pending, &cur->plug_list,
726                                             plug_list) {
727                                 if (rbio_can_merge(pending, rbio)) {
728                                         merge_rbio(pending, rbio);
729                                         spin_unlock(&cur->bio_list_lock);
730                                         freeit = rbio;
731                                         ret = 1;
732                                         goto out;
733                                 }
734                         }
735
736                         /* no merging, put us on the tail of the plug list,
737                          * our rbio will be started with the currently
738                          * running rbio unlocks
739                          */
740                         list_add_tail(&rbio->plug_list, &cur->plug_list);
741                         spin_unlock(&cur->bio_list_lock);
742                         ret = 1;
743                         goto out;
744                 }
745         }
746 lockit:
747         atomic_inc(&rbio->refs);
748         list_add(&rbio->hash_list, &h->hash_list);
749 out:
750         spin_unlock_irqrestore(&h->lock, flags);
751         if (cache_drop)
752                 remove_rbio_from_cache(cache_drop);
753         if (freeit)
754                 __free_raid_bio(freeit);
755         return ret;
756 }
757
758 /*
759  * called as rmw or parity rebuild is completed.  If the plug list has more
760  * rbios waiting for this stripe, the next one on the list will be started
761  */
762 static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
763 {
764         int bucket;
765         struct btrfs_stripe_hash *h;
766         unsigned long flags;
767         int keep_cache = 0;
768
769         bucket = rbio_bucket(rbio);
770         h = rbio->fs_info->stripe_hash_table->table + bucket;
771
772         if (list_empty(&rbio->plug_list))
773                 cache_rbio(rbio);
774
775         spin_lock_irqsave(&h->lock, flags);
776         spin_lock(&rbio->bio_list_lock);
777
778         if (!list_empty(&rbio->hash_list)) {
779                 /*
780                  * if we're still cached and there is no other IO
781                  * to perform, just leave this rbio here for others
782                  * to steal from later
783                  */
784                 if (list_empty(&rbio->plug_list) &&
785                     test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
786                         keep_cache = 1;
787                         clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
788                         BUG_ON(!bio_list_empty(&rbio->bio_list));
789                         goto done;
790                 }
791
792                 list_del_init(&rbio->hash_list);
793                 atomic_dec(&rbio->refs);
794
795                 /*
796                  * we use the plug list to hold all the rbios
797                  * waiting for the chance to lock this stripe.
798                  * hand the lock over to one of them.
799                  */
800                 if (!list_empty(&rbio->plug_list)) {
801                         struct btrfs_raid_bio *next;
802                         struct list_head *head = rbio->plug_list.next;
803
804                         next = list_entry(head, struct btrfs_raid_bio,
805                                           plug_list);
806
807                         list_del_init(&rbio->plug_list);
808
809                         list_add(&next->hash_list, &h->hash_list);
810                         atomic_inc(&next->refs);
811                         spin_unlock(&rbio->bio_list_lock);
812                         spin_unlock_irqrestore(&h->lock, flags);
813
814                         if (next->operation == BTRFS_RBIO_READ_REBUILD)
815                                 async_read_rebuild(next);
816                         else if (next->operation == BTRFS_RBIO_WRITE) {
817                                 steal_rbio(rbio, next);
818                                 async_rmw_stripe(next);
819                         } else if (next->operation == BTRFS_RBIO_PARITY_SCRUB) {
820                                 steal_rbio(rbio, next);
821                                 async_scrub_parity(next);
822                         }
823
824                         goto done_nolock;
825                 } else  if (waitqueue_active(&h->wait)) {
826                         spin_unlock(&rbio->bio_list_lock);
827                         spin_unlock_irqrestore(&h->lock, flags);
828                         wake_up(&h->wait);
829                         goto done_nolock;
830                 }
831         }
832 done:
833         spin_unlock(&rbio->bio_list_lock);
834         spin_unlock_irqrestore(&h->lock, flags);
835
836 done_nolock:
837         if (!keep_cache)
838                 remove_rbio_from_cache(rbio);
839 }
840
841 static inline void
842 __free_bbio_and_raid_map(struct btrfs_bio *bbio, u64 *raid_map, int need)
843 {
844         if (need) {
845                 kfree(raid_map);
846                 kfree(bbio);
847         }
848 }
849
850 static inline void free_bbio_and_raid_map(struct btrfs_raid_bio *rbio)
851 {
852         __free_bbio_and_raid_map(rbio->bbio, rbio->raid_map,
853                         !test_bit(RBIO_HOLD_BBIO_MAP_BIT, &rbio->flags));
854 }
855
856 static void __free_raid_bio(struct btrfs_raid_bio *rbio)
857 {
858         int i;
859
860         WARN_ON(atomic_read(&rbio->refs) < 0);
861         if (!atomic_dec_and_test(&rbio->refs))
862                 return;
863
864         WARN_ON(!list_empty(&rbio->stripe_cache));
865         WARN_ON(!list_empty(&rbio->hash_list));
866         WARN_ON(!bio_list_empty(&rbio->bio_list));
867
868         for (i = 0; i < rbio->nr_pages; i++) {
869                 if (rbio->stripe_pages[i]) {
870                         __free_page(rbio->stripe_pages[i]);
871                         rbio->stripe_pages[i] = NULL;
872                 }
873         }
874
875         free_bbio_and_raid_map(rbio);
876
877         kfree(rbio);
878 }
879
880 static void free_raid_bio(struct btrfs_raid_bio *rbio)
881 {
882         unlock_stripe(rbio);
883         __free_raid_bio(rbio);
884 }
885
886 /*
887  * this frees the rbio and runs through all the bios in the
888  * bio_list and calls end_io on them
889  */
890 static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, int err, int uptodate)
891 {
892         struct bio *cur = bio_list_get(&rbio->bio_list);
893         struct bio *next;
894         free_raid_bio(rbio);
895
896         while (cur) {
897                 next = cur->bi_next;
898                 cur->bi_next = NULL;
899                 if (uptodate)
900                         set_bit(BIO_UPTODATE, &cur->bi_flags);
901                 bio_endio(cur, err);
902                 cur = next;
903         }
904 }
905
906 /*
907  * end io function used by finish_rmw.  When we finally
908  * get here, we've written a full stripe
909  */
910 static void raid_write_end_io(struct bio *bio, int err)
911 {
912         struct btrfs_raid_bio *rbio = bio->bi_private;
913
914         if (err)
915                 fail_bio_stripe(rbio, bio);
916
917         bio_put(bio);
918
919         if (!atomic_dec_and_test(&rbio->stripes_pending))
920                 return;
921
922         err = 0;
923
924         /* OK, we have read all the stripes we need to. */
925         if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
926                 err = -EIO;
927
928         rbio_orig_end_io(rbio, err, 0);
929         return;
930 }
931
932 /*
933  * the read/modify/write code wants to use the original bio for
934  * any pages it included, and then use the rbio for everything
935  * else.  This function decides if a given index (stripe number)
936  * and page number in that stripe fall inside the original bio
937  * or the rbio.
938  *
939  * if you set bio_list_only, you'll get a NULL back for any ranges
940  * that are outside the bio_list
941  *
942  * This doesn't take any refs on anything, you get a bare page pointer
943  * and the caller must bump refs as required.
944  *
945  * You must call index_rbio_pages once before you can trust
946  * the answers from this function.
947  */
948 static struct page *page_in_rbio(struct btrfs_raid_bio *rbio,
949                                  int index, int pagenr, int bio_list_only)
950 {
951         int chunk_page;
952         struct page *p = NULL;
953
954         chunk_page = index * (rbio->stripe_len >> PAGE_SHIFT) + pagenr;
955
956         spin_lock_irq(&rbio->bio_list_lock);
957         p = rbio->bio_pages[chunk_page];
958         spin_unlock_irq(&rbio->bio_list_lock);
959
960         if (p || bio_list_only)
961                 return p;
962
963         return rbio->stripe_pages[chunk_page];
964 }
965
966 /*
967  * number of pages we need for the entire stripe across all the
968  * drives
969  */
970 static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
971 {
972         unsigned long nr = stripe_len * nr_stripes;
973         return DIV_ROUND_UP(nr, PAGE_CACHE_SIZE);
974 }
975
976 /*
977  * allocation and initial setup for the btrfs_raid_bio.  Not
978  * this does not allocate any pages for rbio->pages.
979  */
980 static struct btrfs_raid_bio *alloc_rbio(struct btrfs_root *root,
981                           struct btrfs_bio *bbio, u64 *raid_map,
982                           u64 stripe_len)
983 {
984         struct btrfs_raid_bio *rbio;
985         int nr_data = 0;
986         int real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
987         int num_pages = rbio_nr_pages(stripe_len, real_stripes);
988         int stripe_npages = DIV_ROUND_UP(stripe_len, PAGE_SIZE);
989         void *p;
990
991         rbio = kzalloc(sizeof(*rbio) + num_pages * sizeof(struct page *) * 2 +
992                        DIV_ROUND_UP(stripe_npages, BITS_PER_LONG / 8),
993                         GFP_NOFS);
994         if (!rbio)
995                 return ERR_PTR(-ENOMEM);
996
997         bio_list_init(&rbio->bio_list);
998         INIT_LIST_HEAD(&rbio->plug_list);
999         spin_lock_init(&rbio->bio_list_lock);
1000         INIT_LIST_HEAD(&rbio->stripe_cache);
1001         INIT_LIST_HEAD(&rbio->hash_list);
1002         rbio->bbio = bbio;
1003         rbio->raid_map = raid_map;
1004         rbio->fs_info = root->fs_info;
1005         rbio->stripe_len = stripe_len;
1006         rbio->nr_pages = num_pages;
1007         rbio->real_stripes = real_stripes;
1008         rbio->stripe_npages = stripe_npages;
1009         rbio->faila = -1;
1010         rbio->failb = -1;
1011         atomic_set(&rbio->refs, 1);
1012         atomic_set(&rbio->error, 0);
1013         atomic_set(&rbio->stripes_pending, 0);
1014
1015         /*
1016          * the stripe_pages and bio_pages array point to the extra
1017          * memory we allocated past the end of the rbio
1018          */
1019         p = rbio + 1;
1020         rbio->stripe_pages = p;
1021         rbio->bio_pages = p + sizeof(struct page *) * num_pages;
1022         rbio->dbitmap = p + sizeof(struct page *) * num_pages * 2;
1023
1024         if (raid_map[real_stripes - 1] == RAID6_Q_STRIPE)
1025                 nr_data = real_stripes - 2;
1026         else
1027                 nr_data = real_stripes - 1;
1028
1029         rbio->nr_data = nr_data;
1030         return rbio;
1031 }
1032
1033 /* allocate pages for all the stripes in the bio, including parity */
1034 static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
1035 {
1036         int i;
1037         struct page *page;
1038
1039         for (i = 0; i < rbio->nr_pages; i++) {
1040                 if (rbio->stripe_pages[i])
1041                         continue;
1042                 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1043                 if (!page)
1044                         return -ENOMEM;
1045                 rbio->stripe_pages[i] = page;
1046                 ClearPageUptodate(page);
1047         }
1048         return 0;
1049 }
1050
1051 /* allocate pages for just the p/q stripes */
1052 static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
1053 {
1054         int i;
1055         struct page *page;
1056
1057         i = (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT;
1058
1059         for (; i < rbio->nr_pages; i++) {
1060                 if (rbio->stripe_pages[i])
1061                         continue;
1062                 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1063                 if (!page)
1064                         return -ENOMEM;
1065                 rbio->stripe_pages[i] = page;
1066         }
1067         return 0;
1068 }
1069
1070 /*
1071  * add a single page from a specific stripe into our list of bios for IO
1072  * this will try to merge into existing bios if possible, and returns
1073  * zero if all went well.
1074  */
1075 static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
1076                             struct bio_list *bio_list,
1077                             struct page *page,
1078                             int stripe_nr,
1079                             unsigned long page_index,
1080                             unsigned long bio_max_len)
1081 {
1082         struct bio *last = bio_list->tail;
1083         u64 last_end = 0;
1084         int ret;
1085         struct bio *bio;
1086         struct btrfs_bio_stripe *stripe;
1087         u64 disk_start;
1088
1089         stripe = &rbio->bbio->stripes[stripe_nr];
1090         disk_start = stripe->physical + (page_index << PAGE_CACHE_SHIFT);
1091
1092         /* if the device is missing, just fail this stripe */
1093         if (!stripe->dev->bdev)
1094                 return fail_rbio_index(rbio, stripe_nr);
1095
1096         /* see if we can add this page onto our existing bio */
1097         if (last) {
1098                 last_end = (u64)last->bi_iter.bi_sector << 9;
1099                 last_end += last->bi_iter.bi_size;
1100
1101                 /*
1102                  * we can't merge these if they are from different
1103                  * devices or if they are not contiguous
1104                  */
1105                 if (last_end == disk_start && stripe->dev->bdev &&
1106                     test_bit(BIO_UPTODATE, &last->bi_flags) &&
1107                     last->bi_bdev == stripe->dev->bdev) {
1108                         ret = bio_add_page(last, page, PAGE_CACHE_SIZE, 0);
1109                         if (ret == PAGE_CACHE_SIZE)
1110                                 return 0;
1111                 }
1112         }
1113
1114         /* put a new bio on the list */
1115         bio = btrfs_io_bio_alloc(GFP_NOFS, bio_max_len >> PAGE_SHIFT?:1);
1116         if (!bio)
1117                 return -ENOMEM;
1118
1119         bio->bi_iter.bi_size = 0;
1120         bio->bi_bdev = stripe->dev->bdev;
1121         bio->bi_iter.bi_sector = disk_start >> 9;
1122         set_bit(BIO_UPTODATE, &bio->bi_flags);
1123
1124         bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
1125         bio_list_add(bio_list, bio);
1126         return 0;
1127 }
1128
1129 /*
1130  * while we're doing the read/modify/write cycle, we could
1131  * have errors in reading pages off the disk.  This checks
1132  * for errors and if we're not able to read the page it'll
1133  * trigger parity reconstruction.  The rmw will be finished
1134  * after we've reconstructed the failed stripes
1135  */
1136 static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
1137 {
1138         if (rbio->faila >= 0 || rbio->failb >= 0) {
1139                 BUG_ON(rbio->faila == rbio->real_stripes - 1);
1140                 __raid56_parity_recover(rbio);
1141         } else {
1142                 finish_rmw(rbio);
1143         }
1144 }
1145
1146 /*
1147  * these are just the pages from the rbio array, not from anything
1148  * the FS sent down to us
1149  */
1150 static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe, int page)
1151 {
1152         int index;
1153         index = stripe * (rbio->stripe_len >> PAGE_CACHE_SHIFT);
1154         index += page;
1155         return rbio->stripe_pages[index];
1156 }
1157
1158 /*
1159  * helper function to walk our bio list and populate the bio_pages array with
1160  * the result.  This seems expensive, but it is faster than constantly
1161  * searching through the bio list as we setup the IO in finish_rmw or stripe
1162  * reconstruction.
1163  *
1164  * This must be called before you trust the answers from page_in_rbio
1165  */
1166 static void index_rbio_pages(struct btrfs_raid_bio *rbio)
1167 {
1168         struct bio *bio;
1169         u64 start;
1170         unsigned long stripe_offset;
1171         unsigned long page_index;
1172         struct page *p;
1173         int i;
1174
1175         spin_lock_irq(&rbio->bio_list_lock);
1176         bio_list_for_each(bio, &rbio->bio_list) {
1177                 start = (u64)bio->bi_iter.bi_sector << 9;
1178                 stripe_offset = start - rbio->raid_map[0];
1179                 page_index = stripe_offset >> PAGE_CACHE_SHIFT;
1180
1181                 for (i = 0; i < bio->bi_vcnt; i++) {
1182                         p = bio->bi_io_vec[i].bv_page;
1183                         rbio->bio_pages[page_index + i] = p;
1184                 }
1185         }
1186         spin_unlock_irq(&rbio->bio_list_lock);
1187 }
1188
1189 /*
1190  * this is called from one of two situations.  We either
1191  * have a full stripe from the higher layers, or we've read all
1192  * the missing bits off disk.
1193  *
1194  * This will calculate the parity and then send down any
1195  * changed blocks.
1196  */
1197 static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1198 {
1199         struct btrfs_bio *bbio = rbio->bbio;
1200         void *pointers[rbio->real_stripes];
1201         int stripe_len = rbio->stripe_len;
1202         int nr_data = rbio->nr_data;
1203         int stripe;
1204         int pagenr;
1205         int p_stripe = -1;
1206         int q_stripe = -1;
1207         struct bio_list bio_list;
1208         struct bio *bio;
1209         int pages_per_stripe = stripe_len >> PAGE_CACHE_SHIFT;
1210         int ret;
1211
1212         bio_list_init(&bio_list);
1213
1214         if (rbio->real_stripes - rbio->nr_data == 1) {
1215                 p_stripe = rbio->real_stripes - 1;
1216         } else if (rbio->real_stripes - rbio->nr_data == 2) {
1217                 p_stripe = rbio->real_stripes - 2;
1218                 q_stripe = rbio->real_stripes - 1;
1219         } else {
1220                 BUG();
1221         }
1222
1223         /* at this point we either have a full stripe,
1224          * or we've read the full stripe from the drive.
1225          * recalculate the parity and write the new results.
1226          *
1227          * We're not allowed to add any new bios to the
1228          * bio list here, anyone else that wants to
1229          * change this stripe needs to do their own rmw.
1230          */
1231         spin_lock_irq(&rbio->bio_list_lock);
1232         set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1233         spin_unlock_irq(&rbio->bio_list_lock);
1234
1235         atomic_set(&rbio->error, 0);
1236
1237         /*
1238          * now that we've set rmw_locked, run through the
1239          * bio list one last time and map the page pointers
1240          *
1241          * We don't cache full rbios because we're assuming
1242          * the higher layers are unlikely to use this area of
1243          * the disk again soon.  If they do use it again,
1244          * hopefully they will send another full bio.
1245          */
1246         index_rbio_pages(rbio);
1247         if (!rbio_is_full(rbio))
1248                 cache_rbio_pages(rbio);
1249         else
1250                 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
1251
1252         for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) {
1253                 struct page *p;
1254                 /* first collect one page from each data stripe */
1255                 for (stripe = 0; stripe < nr_data; stripe++) {
1256                         p = page_in_rbio(rbio, stripe, pagenr, 0);
1257                         pointers[stripe] = kmap(p);
1258                 }
1259
1260                 /* then add the parity stripe */
1261                 p = rbio_pstripe_page(rbio, pagenr);
1262                 SetPageUptodate(p);
1263                 pointers[stripe++] = kmap(p);
1264
1265                 if (q_stripe != -1) {
1266
1267                         /*
1268                          * raid6, add the qstripe and call the
1269                          * library function to fill in our p/q
1270                          */
1271                         p = rbio_qstripe_page(rbio, pagenr);
1272                         SetPageUptodate(p);
1273                         pointers[stripe++] = kmap(p);
1274
1275                         raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
1276                                                 pointers);
1277                 } else {
1278                         /* raid5 */
1279                         memcpy(pointers[nr_data], pointers[0], PAGE_SIZE);
1280                         run_xor(pointers + 1, nr_data - 1, PAGE_CACHE_SIZE);
1281                 }
1282
1283
1284                 for (stripe = 0; stripe < rbio->real_stripes; stripe++)
1285                         kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
1286         }
1287
1288         /*
1289          * time to start writing.  Make bios for everything from the
1290          * higher layers (the bio_list in our rbio) and our p/q.  Ignore
1291          * everything else.
1292          */
1293         for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1294                 for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) {
1295                         struct page *page;
1296                         if (stripe < rbio->nr_data) {
1297                                 page = page_in_rbio(rbio, stripe, pagenr, 1);
1298                                 if (!page)
1299                                         continue;
1300                         } else {
1301                                page = rbio_stripe_page(rbio, stripe, pagenr);
1302                         }
1303
1304                         ret = rbio_add_io_page(rbio, &bio_list,
1305                                        page, stripe, pagenr, rbio->stripe_len);
1306                         if (ret)
1307                                 goto cleanup;
1308                 }
1309         }
1310
1311         if (likely(!bbio->num_tgtdevs))
1312                 goto write_data;
1313
1314         for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1315                 if (!bbio->tgtdev_map[stripe])
1316                         continue;
1317
1318                 for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) {
1319                         struct page *page;
1320                         if (stripe < rbio->nr_data) {
1321                                 page = page_in_rbio(rbio, stripe, pagenr, 1);
1322                                 if (!page)
1323                                         continue;
1324                         } else {
1325                                page = rbio_stripe_page(rbio, stripe, pagenr);
1326                         }
1327
1328                         ret = rbio_add_io_page(rbio, &bio_list, page,
1329                                                rbio->bbio->tgtdev_map[stripe],
1330                                                pagenr, rbio->stripe_len);
1331                         if (ret)
1332                                 goto cleanup;
1333                 }
1334         }
1335
1336 write_data:
1337         atomic_set(&rbio->stripes_pending, bio_list_size(&bio_list));
1338         BUG_ON(atomic_read(&rbio->stripes_pending) == 0);
1339
1340         while (1) {
1341                 bio = bio_list_pop(&bio_list);
1342                 if (!bio)
1343                         break;
1344
1345                 bio->bi_private = rbio;
1346                 bio->bi_end_io = raid_write_end_io;
1347                 BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
1348                 submit_bio(WRITE, bio);
1349         }
1350         return;
1351
1352 cleanup:
1353         rbio_orig_end_io(rbio, -EIO, 0);
1354 }
1355
1356 /*
1357  * helper to find the stripe number for a given bio.  Used to figure out which
1358  * stripe has failed.  This expects the bio to correspond to a physical disk,
1359  * so it looks up based on physical sector numbers.
1360  */
1361 static int find_bio_stripe(struct btrfs_raid_bio *rbio,
1362                            struct bio *bio)
1363 {
1364         u64 physical = bio->bi_iter.bi_sector;
1365         u64 stripe_start;
1366         int i;
1367         struct btrfs_bio_stripe *stripe;
1368
1369         physical <<= 9;
1370
1371         for (i = 0; i < rbio->bbio->num_stripes; i++) {
1372                 stripe = &rbio->bbio->stripes[i];
1373                 stripe_start = stripe->physical;
1374                 if (physical >= stripe_start &&
1375                     physical < stripe_start + rbio->stripe_len &&
1376                     bio->bi_bdev == stripe->dev->bdev) {
1377                         return i;
1378                 }
1379         }
1380         return -1;
1381 }
1382
1383 /*
1384  * helper to find the stripe number for a given
1385  * bio (before mapping).  Used to figure out which stripe has
1386  * failed.  This looks up based on logical block numbers.
1387  */
1388 static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
1389                                    struct bio *bio)
1390 {
1391         u64 logical = bio->bi_iter.bi_sector;
1392         u64 stripe_start;
1393         int i;
1394
1395         logical <<= 9;
1396
1397         for (i = 0; i < rbio->nr_data; i++) {
1398                 stripe_start = rbio->raid_map[i];
1399                 if (logical >= stripe_start &&
1400                     logical < stripe_start + rbio->stripe_len) {
1401                         return i;
1402                 }
1403         }
1404         return -1;
1405 }
1406
1407 /*
1408  * returns -EIO if we had too many failures
1409  */
1410 static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed)
1411 {
1412         unsigned long flags;
1413         int ret = 0;
1414
1415         spin_lock_irqsave(&rbio->bio_list_lock, flags);
1416
1417         /* we already know this stripe is bad, move on */
1418         if (rbio->faila == failed || rbio->failb == failed)
1419                 goto out;
1420
1421         if (rbio->faila == -1) {
1422                 /* first failure on this rbio */
1423                 rbio->faila = failed;
1424                 atomic_inc(&rbio->error);
1425         } else if (rbio->failb == -1) {
1426                 /* second failure on this rbio */
1427                 rbio->failb = failed;
1428                 atomic_inc(&rbio->error);
1429         } else {
1430                 ret = -EIO;
1431         }
1432 out:
1433         spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
1434
1435         return ret;
1436 }
1437
1438 /*
1439  * helper to fail a stripe based on a physical disk
1440  * bio.
1441  */
1442 static int fail_bio_stripe(struct btrfs_raid_bio *rbio,
1443                            struct bio *bio)
1444 {
1445         int failed = find_bio_stripe(rbio, bio);
1446
1447         if (failed < 0)
1448                 return -EIO;
1449
1450         return fail_rbio_index(rbio, failed);
1451 }
1452
1453 /*
1454  * this sets each page in the bio uptodate.  It should only be used on private
1455  * rbio pages, nothing that comes in from the higher layers
1456  */
1457 static void set_bio_pages_uptodate(struct bio *bio)
1458 {
1459         int i;
1460         struct page *p;
1461
1462         for (i = 0; i < bio->bi_vcnt; i++) {
1463                 p = bio->bi_io_vec[i].bv_page;
1464                 SetPageUptodate(p);
1465         }
1466 }
1467
1468 /*
1469  * end io for the read phase of the rmw cycle.  All the bios here are physical
1470  * stripe bios we've read from the disk so we can recalculate the parity of the
1471  * stripe.
1472  *
1473  * This will usually kick off finish_rmw once all the bios are read in, but it
1474  * may trigger parity reconstruction if we had any errors along the way
1475  */
1476 static void raid_rmw_end_io(struct bio *bio, int err)
1477 {
1478         struct btrfs_raid_bio *rbio = bio->bi_private;
1479
1480         if (err)
1481                 fail_bio_stripe(rbio, bio);
1482         else
1483                 set_bio_pages_uptodate(bio);
1484
1485         bio_put(bio);
1486
1487         if (!atomic_dec_and_test(&rbio->stripes_pending))
1488                 return;
1489
1490         err = 0;
1491         if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
1492                 goto cleanup;
1493
1494         /*
1495          * this will normally call finish_rmw to start our write
1496          * but if there are any failed stripes we'll reconstruct
1497          * from parity first
1498          */
1499         validate_rbio_for_rmw(rbio);
1500         return;
1501
1502 cleanup:
1503
1504         rbio_orig_end_io(rbio, -EIO, 0);
1505 }
1506
1507 static void async_rmw_stripe(struct btrfs_raid_bio *rbio)
1508 {
1509         btrfs_init_work(&rbio->work, btrfs_rmw_helper,
1510                         rmw_work, NULL, NULL);
1511
1512         btrfs_queue_work(rbio->fs_info->rmw_workers,
1513                          &rbio->work);
1514 }
1515
1516 static void async_read_rebuild(struct btrfs_raid_bio *rbio)
1517 {
1518         btrfs_init_work(&rbio->work, btrfs_rmw_helper,
1519                         read_rebuild_work, NULL, NULL);
1520
1521         btrfs_queue_work(rbio->fs_info->rmw_workers,
1522                          &rbio->work);
1523 }
1524
1525 /*
1526  * the stripe must be locked by the caller.  It will
1527  * unlock after all the writes are done
1528  */
1529 static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
1530 {
1531         int bios_to_read = 0;
1532         struct bio_list bio_list;
1533         int ret;
1534         int nr_pages = DIV_ROUND_UP(rbio->stripe_len, PAGE_CACHE_SIZE);
1535         int pagenr;
1536         int stripe;
1537         struct bio *bio;
1538
1539         bio_list_init(&bio_list);
1540
1541         ret = alloc_rbio_pages(rbio);
1542         if (ret)
1543                 goto cleanup;
1544
1545         index_rbio_pages(rbio);
1546
1547         atomic_set(&rbio->error, 0);
1548         /*
1549          * build a list of bios to read all the missing parts of this
1550          * stripe
1551          */
1552         for (stripe = 0; stripe < rbio->nr_data; stripe++) {
1553                 for (pagenr = 0; pagenr < nr_pages; pagenr++) {
1554                         struct page *page;
1555                         /*
1556                          * we want to find all the pages missing from
1557                          * the rbio and read them from the disk.  If
1558                          * page_in_rbio finds a page in the bio list
1559                          * we don't need to read it off the stripe.
1560                          */
1561                         page = page_in_rbio(rbio, stripe, pagenr, 1);
1562                         if (page)
1563                                 continue;
1564
1565                         page = rbio_stripe_page(rbio, stripe, pagenr);
1566                         /*
1567                          * the bio cache may have handed us an uptodate
1568                          * page.  If so, be happy and use it
1569                          */
1570                         if (PageUptodate(page))
1571                                 continue;
1572
1573                         ret = rbio_add_io_page(rbio, &bio_list, page,
1574                                        stripe, pagenr, rbio->stripe_len);
1575                         if (ret)
1576                                 goto cleanup;
1577                 }
1578         }
1579
1580         bios_to_read = bio_list_size(&bio_list);
1581         if (!bios_to_read) {
1582                 /*
1583                  * this can happen if others have merged with
1584                  * us, it means there is nothing left to read.
1585                  * But if there are missing devices it may not be
1586                  * safe to do the full stripe write yet.
1587                  */
1588                 goto finish;
1589         }
1590
1591         /*
1592          * the bbio may be freed once we submit the last bio.  Make sure
1593          * not to touch it after that
1594          */
1595         atomic_set(&rbio->stripes_pending, bios_to_read);
1596         while (1) {
1597                 bio = bio_list_pop(&bio_list);
1598                 if (!bio)
1599                         break;
1600
1601                 bio->bi_private = rbio;
1602                 bio->bi_end_io = raid_rmw_end_io;
1603
1604                 btrfs_bio_wq_end_io(rbio->fs_info, bio,
1605                                     BTRFS_WQ_ENDIO_RAID56);
1606
1607                 BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
1608                 submit_bio(READ, bio);
1609         }
1610         /* the actual write will happen once the reads are done */
1611         return 0;
1612
1613 cleanup:
1614         rbio_orig_end_io(rbio, -EIO, 0);
1615         return -EIO;
1616
1617 finish:
1618         validate_rbio_for_rmw(rbio);
1619         return 0;
1620 }
1621
1622 /*
1623  * if the upper layers pass in a full stripe, we thank them by only allocating
1624  * enough pages to hold the parity, and sending it all down quickly.
1625  */
1626 static int full_stripe_write(struct btrfs_raid_bio *rbio)
1627 {
1628         int ret;
1629
1630         ret = alloc_rbio_parity_pages(rbio);
1631         if (ret) {
1632                 __free_raid_bio(rbio);
1633                 return ret;
1634         }
1635
1636         ret = lock_stripe_add(rbio);
1637         if (ret == 0)
1638                 finish_rmw(rbio);
1639         return 0;
1640 }
1641
1642 /*
1643  * partial stripe writes get handed over to async helpers.
1644  * We're really hoping to merge a few more writes into this
1645  * rbio before calculating new parity
1646  */
1647 static int partial_stripe_write(struct btrfs_raid_bio *rbio)
1648 {
1649         int ret;
1650
1651         ret = lock_stripe_add(rbio);
1652         if (ret == 0)
1653                 async_rmw_stripe(rbio);
1654         return 0;
1655 }
1656
1657 /*
1658  * sometimes while we were reading from the drive to
1659  * recalculate parity, enough new bios come into create
1660  * a full stripe.  So we do a check here to see if we can
1661  * go directly to finish_rmw
1662  */
1663 static int __raid56_parity_write(struct btrfs_raid_bio *rbio)
1664 {
1665         /* head off into rmw land if we don't have a full stripe */
1666         if (!rbio_is_full(rbio))
1667                 return partial_stripe_write(rbio);
1668         return full_stripe_write(rbio);
1669 }
1670
1671 /*
1672  * We use plugging call backs to collect full stripes.
1673  * Any time we get a partial stripe write while plugged
1674  * we collect it into a list.  When the unplug comes down,
1675  * we sort the list by logical block number and merge
1676  * everything we can into the same rbios
1677  */
1678 struct btrfs_plug_cb {
1679         struct blk_plug_cb cb;
1680         struct btrfs_fs_info *info;
1681         struct list_head rbio_list;
1682         struct btrfs_work work;
1683 };
1684
1685 /*
1686  * rbios on the plug list are sorted for easier merging.
1687  */
1688 static int plug_cmp(void *priv, struct list_head *a, struct list_head *b)
1689 {
1690         struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
1691                                                  plug_list);
1692         struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
1693                                                  plug_list);
1694         u64 a_sector = ra->bio_list.head->bi_iter.bi_sector;
1695         u64 b_sector = rb->bio_list.head->bi_iter.bi_sector;
1696
1697         if (a_sector < b_sector)
1698                 return -1;
1699         if (a_sector > b_sector)
1700                 return 1;
1701         return 0;
1702 }
1703
1704 static void run_plug(struct btrfs_plug_cb *plug)
1705 {
1706         struct btrfs_raid_bio *cur;
1707         struct btrfs_raid_bio *last = NULL;
1708
1709         /*
1710          * sort our plug list then try to merge
1711          * everything we can in hopes of creating full
1712          * stripes.
1713          */
1714         list_sort(NULL, &plug->rbio_list, plug_cmp);
1715         while (!list_empty(&plug->rbio_list)) {
1716                 cur = list_entry(plug->rbio_list.next,
1717                                  struct btrfs_raid_bio, plug_list);
1718                 list_del_init(&cur->plug_list);
1719
1720                 if (rbio_is_full(cur)) {
1721                         /* we have a full stripe, send it down */
1722                         full_stripe_write(cur);
1723                         continue;
1724                 }
1725                 if (last) {
1726                         if (rbio_can_merge(last, cur)) {
1727                                 merge_rbio(last, cur);
1728                                 __free_raid_bio(cur);
1729                                 continue;
1730
1731                         }
1732                         __raid56_parity_write(last);
1733                 }
1734                 last = cur;
1735         }
1736         if (last) {
1737                 __raid56_parity_write(last);
1738         }
1739         kfree(plug);
1740 }
1741
1742 /*
1743  * if the unplug comes from schedule, we have to push the
1744  * work off to a helper thread
1745  */
1746 static void unplug_work(struct btrfs_work *work)
1747 {
1748         struct btrfs_plug_cb *plug;
1749         plug = container_of(work, struct btrfs_plug_cb, work);
1750         run_plug(plug);
1751 }
1752
1753 static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
1754 {
1755         struct btrfs_plug_cb *plug;
1756         plug = container_of(cb, struct btrfs_plug_cb, cb);
1757
1758         if (from_schedule) {
1759                 btrfs_init_work(&plug->work, btrfs_rmw_helper,
1760                                 unplug_work, NULL, NULL);
1761                 btrfs_queue_work(plug->info->rmw_workers,
1762                                  &plug->work);
1763                 return;
1764         }
1765         run_plug(plug);
1766 }
1767
1768 /*
1769  * our main entry point for writes from the rest of the FS.
1770  */
1771 int raid56_parity_write(struct btrfs_root *root, struct bio *bio,
1772                         struct btrfs_bio *bbio, u64 *raid_map,
1773                         u64 stripe_len)
1774 {
1775         struct btrfs_raid_bio *rbio;
1776         struct btrfs_plug_cb *plug = NULL;
1777         struct blk_plug_cb *cb;
1778
1779         rbio = alloc_rbio(root, bbio, raid_map, stripe_len);
1780         if (IS_ERR(rbio)) {
1781                 __free_bbio_and_raid_map(bbio, raid_map, 1);
1782                 return PTR_ERR(rbio);
1783         }
1784         bio_list_add(&rbio->bio_list, bio);
1785         rbio->bio_list_bytes = bio->bi_iter.bi_size;
1786         rbio->operation = BTRFS_RBIO_WRITE;
1787
1788         /*
1789          * don't plug on full rbios, just get them out the door
1790          * as quickly as we can
1791          */
1792         if (rbio_is_full(rbio))
1793                 return full_stripe_write(rbio);
1794
1795         cb = blk_check_plugged(btrfs_raid_unplug, root->fs_info,
1796                                sizeof(*plug));
1797         if (cb) {
1798                 plug = container_of(cb, struct btrfs_plug_cb, cb);
1799                 if (!plug->info) {
1800                         plug->info = root->fs_info;
1801                         INIT_LIST_HEAD(&plug->rbio_list);
1802                 }
1803                 list_add_tail(&rbio->plug_list, &plug->rbio_list);
1804         } else {
1805                 return __raid56_parity_write(rbio);
1806         }
1807         return 0;
1808 }
1809
1810 /*
1811  * all parity reconstruction happens here.  We've read in everything
1812  * we can find from the drives and this does the heavy lifting of
1813  * sorting the good from the bad.
1814  */
1815 static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
1816 {
1817         int pagenr, stripe;
1818         void **pointers;
1819         int faila = -1, failb = -1;
1820         int nr_pages = DIV_ROUND_UP(rbio->stripe_len, PAGE_CACHE_SIZE);
1821         struct page *page;
1822         int err;
1823         int i;
1824
1825         pointers = kzalloc(rbio->real_stripes * sizeof(void *),
1826                            GFP_NOFS);
1827         if (!pointers) {
1828                 err = -ENOMEM;
1829                 goto cleanup_io;
1830         }
1831
1832         faila = rbio->faila;
1833         failb = rbio->failb;
1834
1835         if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
1836                 spin_lock_irq(&rbio->bio_list_lock);
1837                 set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1838                 spin_unlock_irq(&rbio->bio_list_lock);
1839         }
1840
1841         index_rbio_pages(rbio);
1842
1843         for (pagenr = 0; pagenr < nr_pages; pagenr++) {
1844                 /*
1845                  * Now we just use bitmap to mark the horizontal stripes in
1846                  * which we have data when doing parity scrub.
1847                  */
1848                 if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB &&
1849                     !test_bit(pagenr, rbio->dbitmap))
1850                         continue;
1851
1852                 /* setup our array of pointers with pages
1853                  * from each stripe
1854                  */
1855                 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1856                         /*
1857                          * if we're rebuilding a read, we have to use
1858                          * pages from the bio list
1859                          */
1860                         if (rbio->operation == BTRFS_RBIO_READ_REBUILD &&
1861                             (stripe == faila || stripe == failb)) {
1862                                 page = page_in_rbio(rbio, stripe, pagenr, 0);
1863                         } else {
1864                                 page = rbio_stripe_page(rbio, stripe, pagenr);
1865                         }
1866                         pointers[stripe] = kmap(page);
1867                 }
1868
1869                 /* all raid6 handling here */
1870                 if (rbio->raid_map[rbio->real_stripes - 1] ==
1871                     RAID6_Q_STRIPE) {
1872
1873                         /*
1874                          * single failure, rebuild from parity raid5
1875                          * style
1876                          */
1877                         if (failb < 0) {
1878                                 if (faila == rbio->nr_data) {
1879                                         /*
1880                                          * Just the P stripe has failed, without
1881                                          * a bad data or Q stripe.
1882                                          * TODO, we should redo the xor here.
1883                                          */
1884                                         err = -EIO;
1885                                         goto cleanup;
1886                                 }
1887                                 /*
1888                                  * a single failure in raid6 is rebuilt
1889                                  * in the pstripe code below
1890                                  */
1891                                 goto pstripe;
1892                         }
1893
1894                         /* make sure our ps and qs are in order */
1895                         if (faila > failb) {
1896                                 int tmp = failb;
1897                                 failb = faila;
1898                                 faila = tmp;
1899                         }
1900
1901                         /* if the q stripe is failed, do a pstripe reconstruction
1902                          * from the xors.
1903                          * If both the q stripe and the P stripe are failed, we're
1904                          * here due to a crc mismatch and we can't give them the
1905                          * data they want
1906                          */
1907                         if (rbio->raid_map[failb] == RAID6_Q_STRIPE) {
1908                                 if (rbio->raid_map[faila] == RAID5_P_STRIPE) {
1909                                         err = -EIO;
1910                                         goto cleanup;
1911                                 }
1912                                 /*
1913                                  * otherwise we have one bad data stripe and
1914                                  * a good P stripe.  raid5!
1915                                  */
1916                                 goto pstripe;
1917                         }
1918
1919                         if (rbio->raid_map[failb] == RAID5_P_STRIPE) {
1920                                 raid6_datap_recov(rbio->real_stripes,
1921                                                   PAGE_SIZE, faila, pointers);
1922                         } else {
1923                                 raid6_2data_recov(rbio->real_stripes,
1924                                                   PAGE_SIZE, faila, failb,
1925                                                   pointers);
1926                         }
1927                 } else {
1928                         void *p;
1929
1930                         /* rebuild from P stripe here (raid5 or raid6) */
1931                         BUG_ON(failb != -1);
1932 pstripe:
1933                         /* Copy parity block into failed block to start with */
1934                         memcpy(pointers[faila],
1935                                pointers[rbio->nr_data],
1936                                PAGE_CACHE_SIZE);
1937
1938                         /* rearrange the pointer array */
1939                         p = pointers[faila];
1940                         for (stripe = faila; stripe < rbio->nr_data - 1; stripe++)
1941                                 pointers[stripe] = pointers[stripe + 1];
1942                         pointers[rbio->nr_data - 1] = p;
1943
1944                         /* xor in the rest */
1945                         run_xor(pointers, rbio->nr_data - 1, PAGE_CACHE_SIZE);
1946                 }
1947                 /* if we're doing this rebuild as part of an rmw, go through
1948                  * and set all of our private rbio pages in the
1949                  * failed stripes as uptodate.  This way finish_rmw will
1950                  * know they can be trusted.  If this was a read reconstruction,
1951                  * other endio functions will fiddle the uptodate bits
1952                  */
1953                 if (rbio->operation == BTRFS_RBIO_WRITE) {
1954                         for (i = 0;  i < nr_pages; i++) {
1955                                 if (faila != -1) {
1956                                         page = rbio_stripe_page(rbio, faila, i);
1957                                         SetPageUptodate(page);
1958                                 }
1959                                 if (failb != -1) {
1960                                         page = rbio_stripe_page(rbio, failb, i);
1961                                         SetPageUptodate(page);
1962                                 }
1963                         }
1964                 }
1965                 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1966                         /*
1967                          * if we're rebuilding a read, we have to use
1968                          * pages from the bio list
1969                          */
1970                         if (rbio->operation == BTRFS_RBIO_READ_REBUILD &&
1971                             (stripe == faila || stripe == failb)) {
1972                                 page = page_in_rbio(rbio, stripe, pagenr, 0);
1973                         } else {
1974                                 page = rbio_stripe_page(rbio, stripe, pagenr);
1975                         }
1976                         kunmap(page);
1977                 }
1978         }
1979
1980         err = 0;
1981 cleanup:
1982         kfree(pointers);
1983
1984 cleanup_io:
1985         if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
1986                 if (err == 0 &&
1987                     !test_bit(RBIO_HOLD_BBIO_MAP_BIT, &rbio->flags))
1988                         cache_rbio_pages(rbio);
1989                 else
1990                         clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
1991
1992                 rbio_orig_end_io(rbio, err, err == 0);
1993         } else if (err == 0) {
1994                 rbio->faila = -1;
1995                 rbio->failb = -1;
1996
1997                 if (rbio->operation == BTRFS_RBIO_WRITE)
1998                         finish_rmw(rbio);
1999                 else if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB)
2000                         finish_parity_scrub(rbio, 0);
2001                 else
2002                         BUG();
2003         } else {
2004                 rbio_orig_end_io(rbio, err, 0);
2005         }
2006 }
2007
2008 /*
2009  * This is called only for stripes we've read from disk to
2010  * reconstruct the parity.
2011  */
2012 static void raid_recover_end_io(struct bio *bio, int err)
2013 {
2014         struct btrfs_raid_bio *rbio = bio->bi_private;
2015
2016         /*
2017          * we only read stripe pages off the disk, set them
2018          * up to date if there were no errors
2019          */
2020         if (err)
2021                 fail_bio_stripe(rbio, bio);
2022         else
2023                 set_bio_pages_uptodate(bio);
2024         bio_put(bio);
2025
2026         if (!atomic_dec_and_test(&rbio->stripes_pending))
2027                 return;
2028
2029         if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
2030                 rbio_orig_end_io(rbio, -EIO, 0);
2031         else
2032                 __raid_recover_end_io(rbio);
2033 }
2034
2035 /*
2036  * reads everything we need off the disk to reconstruct
2037  * the parity. endio handlers trigger final reconstruction
2038  * when the IO is done.
2039  *
2040  * This is used both for reads from the higher layers and for
2041  * parity construction required to finish a rmw cycle.
2042  */
2043 static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
2044 {
2045         int bios_to_read = 0;
2046         struct bio_list bio_list;
2047         int ret;
2048         int nr_pages = DIV_ROUND_UP(rbio->stripe_len, PAGE_CACHE_SIZE);
2049         int pagenr;
2050         int stripe;
2051         struct bio *bio;
2052
2053         bio_list_init(&bio_list);
2054
2055         ret = alloc_rbio_pages(rbio);
2056         if (ret)
2057                 goto cleanup;
2058
2059         atomic_set(&rbio->error, 0);
2060
2061         /*
2062          * read everything that hasn't failed.  Thanks to the
2063          * stripe cache, it is possible that some or all of these
2064          * pages are going to be uptodate.
2065          */
2066         for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
2067                 if (rbio->faila == stripe || rbio->failb == stripe) {
2068                         atomic_inc(&rbio->error);
2069                         continue;
2070                 }
2071
2072                 for (pagenr = 0; pagenr < nr_pages; pagenr++) {
2073                         struct page *p;
2074
2075                         /*
2076                          * the rmw code may have already read this
2077                          * page in
2078                          */
2079                         p = rbio_stripe_page(rbio, stripe, pagenr);
2080                         if (PageUptodate(p))
2081                                 continue;
2082
2083                         ret = rbio_add_io_page(rbio, &bio_list,
2084                                        rbio_stripe_page(rbio, stripe, pagenr),
2085                                        stripe, pagenr, rbio->stripe_len);
2086                         if (ret < 0)
2087                                 goto cleanup;
2088                 }
2089         }
2090
2091         bios_to_read = bio_list_size(&bio_list);
2092         if (!bios_to_read) {
2093                 /*
2094                  * we might have no bios to read just because the pages
2095                  * were up to date, or we might have no bios to read because
2096                  * the devices were gone.
2097                  */
2098                 if (atomic_read(&rbio->error) <= rbio->bbio->max_errors) {
2099                         __raid_recover_end_io(rbio);
2100                         goto out;
2101                 } else {
2102                         goto cleanup;
2103                 }
2104         }
2105
2106         /*
2107          * the bbio may be freed once we submit the last bio.  Make sure
2108          * not to touch it after that
2109          */
2110         atomic_set(&rbio->stripes_pending, bios_to_read);
2111         while (1) {
2112                 bio = bio_list_pop(&bio_list);
2113                 if (!bio)
2114                         break;
2115
2116                 bio->bi_private = rbio;
2117                 bio->bi_end_io = raid_recover_end_io;
2118
2119                 btrfs_bio_wq_end_io(rbio->fs_info, bio,
2120                                     BTRFS_WQ_ENDIO_RAID56);
2121
2122                 BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
2123                 submit_bio(READ, bio);
2124         }
2125 out:
2126         return 0;
2127
2128 cleanup:
2129         if (rbio->operation == BTRFS_RBIO_READ_REBUILD)
2130                 rbio_orig_end_io(rbio, -EIO, 0);
2131         return -EIO;
2132 }
2133
2134 /*
2135  * the main entry point for reads from the higher layers.  This
2136  * is really only called when the normal read path had a failure,
2137  * so we assume the bio they send down corresponds to a failed part
2138  * of the drive.
2139  */
2140 int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,
2141                           struct btrfs_bio *bbio, u64 *raid_map,
2142                           u64 stripe_len, int mirror_num, int hold_bbio)
2143 {
2144         struct btrfs_raid_bio *rbio;
2145         int ret;
2146
2147         rbio = alloc_rbio(root, bbio, raid_map, stripe_len);
2148         if (IS_ERR(rbio)) {
2149                 __free_bbio_and_raid_map(bbio, raid_map, !hold_bbio);
2150                 return PTR_ERR(rbio);
2151         }
2152
2153         if (hold_bbio)
2154                 set_bit(RBIO_HOLD_BBIO_MAP_BIT, &rbio->flags);
2155         rbio->operation = BTRFS_RBIO_READ_REBUILD;
2156         bio_list_add(&rbio->bio_list, bio);
2157         rbio->bio_list_bytes = bio->bi_iter.bi_size;
2158
2159         rbio->faila = find_logical_bio_stripe(rbio, bio);
2160         if (rbio->faila == -1) {
2161                 BUG();
2162                 __free_bbio_and_raid_map(bbio, raid_map, !hold_bbio);
2163                 kfree(rbio);
2164                 return -EIO;
2165         }
2166
2167         /*
2168          * reconstruct from the q stripe if they are
2169          * asking for mirror 3
2170          */
2171         if (mirror_num == 3)
2172                 rbio->failb = rbio->real_stripes - 2;
2173
2174         ret = lock_stripe_add(rbio);
2175
2176         /*
2177          * __raid56_parity_recover will end the bio with
2178          * any errors it hits.  We don't want to return
2179          * its error value up the stack because our caller
2180          * will end up calling bio_endio with any nonzero
2181          * return
2182          */
2183         if (ret == 0)
2184                 __raid56_parity_recover(rbio);
2185         /*
2186          * our rbio has been added to the list of
2187          * rbios that will be handled after the
2188          * currently lock owner is done
2189          */
2190         return 0;
2191
2192 }
2193
2194 static void rmw_work(struct btrfs_work *work)
2195 {
2196         struct btrfs_raid_bio *rbio;
2197
2198         rbio = container_of(work, struct btrfs_raid_bio, work);
2199         raid56_rmw_stripe(rbio);
2200 }
2201
2202 static void read_rebuild_work(struct btrfs_work *work)
2203 {
2204         struct btrfs_raid_bio *rbio;
2205
2206         rbio = container_of(work, struct btrfs_raid_bio, work);
2207         __raid56_parity_recover(rbio);
2208 }
2209
2210 /*
2211  * The following code is used to scrub/replace the parity stripe
2212  *
2213  * Note: We need make sure all the pages that add into the scrub/replace
2214  * raid bio are correct and not be changed during the scrub/replace. That
2215  * is those pages just hold metadata or file data with checksum.
2216  */
2217
2218 struct btrfs_raid_bio *
2219 raid56_parity_alloc_scrub_rbio(struct btrfs_root *root, struct bio *bio,
2220                                struct btrfs_bio *bbio, u64 *raid_map,
2221                                u64 stripe_len, struct btrfs_device *scrub_dev,
2222                                unsigned long *dbitmap, int stripe_nsectors)
2223 {
2224         struct btrfs_raid_bio *rbio;
2225         int i;
2226
2227         rbio = alloc_rbio(root, bbio, raid_map, stripe_len);
2228         if (IS_ERR(rbio))
2229                 return NULL;
2230         bio_list_add(&rbio->bio_list, bio);
2231         /*
2232          * This is a special bio which is used to hold the completion handler
2233          * and make the scrub rbio is similar to the other types
2234          */
2235         ASSERT(!bio->bi_iter.bi_size);
2236         rbio->operation = BTRFS_RBIO_PARITY_SCRUB;
2237
2238         for (i = 0; i < rbio->real_stripes; i++) {
2239                 if (bbio->stripes[i].dev == scrub_dev) {
2240                         rbio->scrubp = i;
2241                         break;
2242                 }
2243         }
2244
2245         /* Now we just support the sectorsize equals to page size */
2246         ASSERT(root->sectorsize == PAGE_SIZE);
2247         ASSERT(rbio->stripe_npages == stripe_nsectors);
2248         bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors);
2249
2250         return rbio;
2251 }
2252
2253 void raid56_parity_add_scrub_pages(struct btrfs_raid_bio *rbio,
2254                                    struct page *page, u64 logical)
2255 {
2256         int stripe_offset;
2257         int index;
2258
2259         ASSERT(logical >= rbio->raid_map[0]);
2260         ASSERT(logical + PAGE_SIZE <= rbio->raid_map[0] +
2261                                 rbio->stripe_len * rbio->nr_data);
2262         stripe_offset = (int)(logical - rbio->raid_map[0]);
2263         index = stripe_offset >> PAGE_CACHE_SHIFT;
2264         rbio->bio_pages[index] = page;
2265 }
2266
2267 /*
2268  * We just scrub the parity that we have correct data on the same horizontal,
2269  * so we needn't allocate all pages for all the stripes.
2270  */
2271 static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
2272 {
2273         int i;
2274         int bit;
2275         int index;
2276         struct page *page;
2277
2278         for_each_set_bit(bit, rbio->dbitmap, rbio->stripe_npages) {
2279                 for (i = 0; i < rbio->real_stripes; i++) {
2280                         index = i * rbio->stripe_npages + bit;
2281                         if (rbio->stripe_pages[index])
2282                                 continue;
2283
2284                         page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2285                         if (!page)
2286                                 return -ENOMEM;
2287                         rbio->stripe_pages[index] = page;
2288                         ClearPageUptodate(page);
2289                 }
2290         }
2291         return 0;
2292 }
2293
2294 /*
2295  * end io function used by finish_rmw.  When we finally
2296  * get here, we've written a full stripe
2297  */
2298 static void raid_write_parity_end_io(struct bio *bio, int err)
2299 {
2300         struct btrfs_raid_bio *rbio = bio->bi_private;
2301
2302         if (err)
2303                 fail_bio_stripe(rbio, bio);
2304
2305         bio_put(bio);
2306
2307         if (!atomic_dec_and_test(&rbio->stripes_pending))
2308                 return;
2309
2310         err = 0;
2311
2312         if (atomic_read(&rbio->error))
2313                 err = -EIO;
2314
2315         rbio_orig_end_io(rbio, err, 0);
2316 }
2317
2318 static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
2319                                          int need_check)
2320 {
2321         struct btrfs_bio *bbio = rbio->bbio;
2322         void *pointers[rbio->real_stripes];
2323         DECLARE_BITMAP(pbitmap, rbio->stripe_npages);
2324         int nr_data = rbio->nr_data;
2325         int stripe;
2326         int pagenr;
2327         int p_stripe = -1;
2328         int q_stripe = -1;
2329         struct page *p_page = NULL;
2330         struct page *q_page = NULL;
2331         struct bio_list bio_list;
2332         struct bio *bio;
2333         int is_replace = 0;
2334         int ret;
2335
2336         bio_list_init(&bio_list);
2337
2338         if (rbio->real_stripes - rbio->nr_data == 1) {
2339                 p_stripe = rbio->real_stripes - 1;
2340         } else if (rbio->real_stripes - rbio->nr_data == 2) {
2341                 p_stripe = rbio->real_stripes - 2;
2342                 q_stripe = rbio->real_stripes - 1;
2343         } else {
2344                 BUG();
2345         }
2346
2347         if (bbio->num_tgtdevs && bbio->tgtdev_map[rbio->scrubp]) {
2348                 is_replace = 1;
2349                 bitmap_copy(pbitmap, rbio->dbitmap, rbio->stripe_npages);
2350         }
2351
2352         /*
2353          * Because the higher layers(scrubber) are unlikely to
2354          * use this area of the disk again soon, so don't cache
2355          * it.
2356          */
2357         clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
2358
2359         if (!need_check)
2360                 goto writeback;
2361
2362         p_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2363         if (!p_page)
2364                 goto cleanup;
2365         SetPageUptodate(p_page);
2366
2367         if (q_stripe != -1) {
2368                 q_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2369                 if (!q_page) {
2370                         __free_page(p_page);
2371                         goto cleanup;
2372                 }
2373                 SetPageUptodate(q_page);
2374         }
2375
2376         atomic_set(&rbio->error, 0);
2377
2378         for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2379                 struct page *p;
2380                 void *parity;
2381                 /* first collect one page from each data stripe */
2382                 for (stripe = 0; stripe < nr_data; stripe++) {
2383                         p = page_in_rbio(rbio, stripe, pagenr, 0);
2384                         pointers[stripe] = kmap(p);
2385                 }
2386
2387                 /* then add the parity stripe */
2388                 pointers[stripe++] = kmap(p_page);
2389
2390                 if (q_stripe != -1) {
2391
2392                         /*
2393                          * raid6, add the qstripe and call the
2394                          * library function to fill in our p/q
2395                          */
2396                         pointers[stripe++] = kmap(q_page);
2397
2398                         raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
2399                                                 pointers);
2400                 } else {
2401                         /* raid5 */
2402                         memcpy(pointers[nr_data], pointers[0], PAGE_SIZE);
2403                         run_xor(pointers + 1, nr_data - 1, PAGE_CACHE_SIZE);
2404                 }
2405
2406                 /* Check scrubbing pairty and repair it */
2407                 p = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2408                 parity = kmap(p);
2409                 if (memcmp(parity, pointers[rbio->scrubp], PAGE_CACHE_SIZE))
2410                         memcpy(parity, pointers[rbio->scrubp], PAGE_CACHE_SIZE);
2411                 else
2412                         /* Parity is right, needn't writeback */
2413                         bitmap_clear(rbio->dbitmap, pagenr, 1);
2414                 kunmap(p);
2415
2416                 for (stripe = 0; stripe < rbio->real_stripes; stripe++)
2417                         kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
2418         }
2419
2420         __free_page(p_page);
2421         if (q_page)
2422                 __free_page(q_page);
2423
2424 writeback:
2425         /*
2426          * time to start writing.  Make bios for everything from the
2427          * higher layers (the bio_list in our rbio) and our p/q.  Ignore
2428          * everything else.
2429          */
2430         for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2431                 struct page *page;
2432
2433                 page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2434                 ret = rbio_add_io_page(rbio, &bio_list,
2435                                page, rbio->scrubp, pagenr, rbio->stripe_len);
2436                 if (ret)
2437                         goto cleanup;
2438         }
2439
2440         if (!is_replace)
2441                 goto submit_write;
2442
2443         for_each_set_bit(pagenr, pbitmap, rbio->stripe_npages) {
2444                 struct page *page;
2445
2446                 page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2447                 ret = rbio_add_io_page(rbio, &bio_list, page,
2448                                        bbio->tgtdev_map[rbio->scrubp],
2449                                        pagenr, rbio->stripe_len);
2450                 if (ret)
2451                         goto cleanup;
2452         }
2453
2454 submit_write:
2455         nr_data = bio_list_size(&bio_list);
2456         if (!nr_data) {
2457                 /* Every parity is right */
2458                 rbio_orig_end_io(rbio, 0, 0);
2459                 return;
2460         }
2461
2462         atomic_set(&rbio->stripes_pending, nr_data);
2463
2464         while (1) {
2465                 bio = bio_list_pop(&bio_list);
2466                 if (!bio)
2467                         break;
2468
2469                 bio->bi_private = rbio;
2470                 bio->bi_end_io = raid_write_parity_end_io;
2471                 BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
2472                 submit_bio(WRITE, bio);
2473         }
2474         return;
2475
2476 cleanup:
2477         rbio_orig_end_io(rbio, -EIO, 0);
2478 }
2479
2480 static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
2481 {
2482         if (stripe >= 0 && stripe < rbio->nr_data)
2483                 return 1;
2484         return 0;
2485 }
2486
2487 /*
2488  * While we're doing the parity check and repair, we could have errors
2489  * in reading pages off the disk.  This checks for errors and if we're
2490  * not able to read the page it'll trigger parity reconstruction.  The
2491  * parity scrub will be finished after we've reconstructed the failed
2492  * stripes
2493  */
2494 static void validate_rbio_for_parity_scrub(struct btrfs_raid_bio *rbio)
2495 {
2496         if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
2497                 goto cleanup;
2498
2499         if (rbio->faila >= 0 || rbio->failb >= 0) {
2500                 int dfail = 0, failp = -1;
2501
2502                 if (is_data_stripe(rbio, rbio->faila))
2503                         dfail++;
2504                 else if (is_parity_stripe(rbio->faila))
2505                         failp = rbio->faila;
2506
2507                 if (is_data_stripe(rbio, rbio->failb))
2508                         dfail++;
2509                 else if (is_parity_stripe(rbio->failb))
2510                         failp = rbio->failb;
2511
2512                 /*
2513                  * Because we can not use a scrubbing parity to repair
2514                  * the data, so the capability of the repair is declined.
2515                  * (In the case of RAID5, we can not repair anything)
2516                  */
2517                 if (dfail > rbio->bbio->max_errors - 1)
2518                         goto cleanup;
2519
2520                 /*
2521                  * If all data is good, only parity is correctly, just
2522                  * repair the parity.
2523                  */
2524                 if (dfail == 0) {
2525                         finish_parity_scrub(rbio, 0);
2526                         return;
2527                 }
2528
2529                 /*
2530                  * Here means we got one corrupted data stripe and one
2531                  * corrupted parity on RAID6, if the corrupted parity
2532                  * is scrubbing parity, luckly, use the other one to repair
2533                  * the data, or we can not repair the data stripe.
2534                  */
2535                 if (failp != rbio->scrubp)
2536                         goto cleanup;
2537
2538                 __raid_recover_end_io(rbio);
2539         } else {
2540                 finish_parity_scrub(rbio, 1);
2541         }
2542         return;
2543
2544 cleanup:
2545         rbio_orig_end_io(rbio, -EIO, 0);
2546 }
2547
2548 /*
2549  * end io for the read phase of the rmw cycle.  All the bios here are physical
2550  * stripe bios we've read from the disk so we can recalculate the parity of the
2551  * stripe.
2552  *
2553  * This will usually kick off finish_rmw once all the bios are read in, but it
2554  * may trigger parity reconstruction if we had any errors along the way
2555  */
2556 static void raid56_parity_scrub_end_io(struct bio *bio, int err)
2557 {
2558         struct btrfs_raid_bio *rbio = bio->bi_private;
2559
2560         if (err)
2561                 fail_bio_stripe(rbio, bio);
2562         else
2563                 set_bio_pages_uptodate(bio);
2564
2565         bio_put(bio);
2566
2567         if (!atomic_dec_and_test(&rbio->stripes_pending))
2568                 return;
2569
2570         /*
2571          * this will normally call finish_rmw to start our write
2572          * but if there are any failed stripes we'll reconstruct
2573          * from parity first
2574          */
2575         validate_rbio_for_parity_scrub(rbio);
2576 }
2577
2578 static void raid56_parity_scrub_stripe(struct btrfs_raid_bio *rbio)
2579 {
2580         int bios_to_read = 0;
2581         struct bio_list bio_list;
2582         int ret;
2583         int pagenr;
2584         int stripe;
2585         struct bio *bio;
2586
2587         ret = alloc_rbio_essential_pages(rbio);
2588         if (ret)
2589                 goto cleanup;
2590
2591         bio_list_init(&bio_list);
2592
2593         atomic_set(&rbio->error, 0);
2594         /*
2595          * build a list of bios to read all the missing parts of this
2596          * stripe
2597          */
2598         for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
2599                 for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2600                         struct page *page;
2601                         /*
2602                          * we want to find all the pages missing from
2603                          * the rbio and read them from the disk.  If
2604                          * page_in_rbio finds a page in the bio list
2605                          * we don't need to read it off the stripe.
2606                          */
2607                         page = page_in_rbio(rbio, stripe, pagenr, 1);
2608                         if (page)
2609                                 continue;
2610
2611                         page = rbio_stripe_page(rbio, stripe, pagenr);
2612                         /*
2613                          * the bio cache may have handed us an uptodate
2614                          * page.  If so, be happy and use it
2615                          */
2616                         if (PageUptodate(page))
2617                                 continue;
2618
2619                         ret = rbio_add_io_page(rbio, &bio_list, page,
2620                                        stripe, pagenr, rbio->stripe_len);
2621                         if (ret)
2622                                 goto cleanup;
2623                 }
2624         }
2625
2626         bios_to_read = bio_list_size(&bio_list);
2627         if (!bios_to_read) {
2628                 /*
2629                  * this can happen if others have merged with
2630                  * us, it means there is nothing left to read.
2631                  * But if there are missing devices it may not be
2632                  * safe to do the full stripe write yet.
2633                  */
2634                 goto finish;
2635         }
2636
2637         /*
2638          * the bbio may be freed once we submit the last bio.  Make sure
2639          * not to touch it after that
2640          */
2641         atomic_set(&rbio->stripes_pending, bios_to_read);
2642         while (1) {
2643                 bio = bio_list_pop(&bio_list);
2644                 if (!bio)
2645                         break;
2646
2647                 bio->bi_private = rbio;
2648                 bio->bi_end_io = raid56_parity_scrub_end_io;
2649
2650                 btrfs_bio_wq_end_io(rbio->fs_info, bio,
2651                                     BTRFS_WQ_ENDIO_RAID56);
2652
2653                 BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
2654                 submit_bio(READ, bio);
2655         }
2656         /* the actual write will happen once the reads are done */
2657         return;
2658
2659 cleanup:
2660         rbio_orig_end_io(rbio, -EIO, 0);
2661         return;
2662
2663 finish:
2664         validate_rbio_for_parity_scrub(rbio);
2665 }
2666
2667 static void scrub_parity_work(struct btrfs_work *work)
2668 {
2669         struct btrfs_raid_bio *rbio;
2670
2671         rbio = container_of(work, struct btrfs_raid_bio, work);
2672         raid56_parity_scrub_stripe(rbio);
2673 }
2674
2675 static void async_scrub_parity(struct btrfs_raid_bio *rbio)
2676 {
2677         btrfs_init_work(&rbio->work, btrfs_rmw_helper,
2678                         scrub_parity_work, NULL, NULL);
2679
2680         btrfs_queue_work(rbio->fs_info->rmw_workers,
2681                          &rbio->work);
2682 }
2683
2684 void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
2685 {
2686         if (!lock_stripe_add(rbio))
2687                 async_scrub_parity(rbio);
2688 }