1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012 Fusion-io All rights reserved.
4 * Copyright (C) 2012 Intel Corp. All rights reserved.
7 #include <linux/sched.h>
9 #include <linux/slab.h>
10 #include <linux/blkdev.h>
11 #include <linux/raid/pq.h>
12 #include <linux/hash.h>
13 #include <linux/list_sort.h>
14 #include <linux/raid/xor.h>
20 #include "async-thread.h"
22 /* set when additional merges to this rbio are not allowed */
23 #define RBIO_RMW_LOCKED_BIT 1
26 * set when this rbio is sitting in the hash, but it is just a cache
29 #define RBIO_CACHE_BIT 2
32 * set when it is safe to trust the stripe_pages for caching
34 #define RBIO_CACHE_READY_BIT 3
36 #define RBIO_CACHE_SIZE 1024
38 #define BTRFS_STRIPE_HASH_TABLE_BITS 11
40 /* Used by the raid56 code to lock stripes for read/modify/write */
41 struct btrfs_stripe_hash {
42 struct list_head hash_list;
46 /* Used by the raid56 code to lock stripes for read/modify/write */
47 struct btrfs_stripe_hash_table {
48 struct list_head stripe_cache;
49 spinlock_t cache_lock;
51 struct btrfs_stripe_hash table[];
56 BTRFS_RBIO_READ_REBUILD,
57 BTRFS_RBIO_PARITY_SCRUB,
58 BTRFS_RBIO_REBUILD_MISSING,
61 struct btrfs_raid_bio {
62 struct btrfs_fs_info *fs_info;
63 struct btrfs_bio *bbio;
65 /* while we're doing rmw on a stripe
66 * we put it into a hash table so we can
67 * lock the stripe and merge more rbios
70 struct list_head hash_list;
73 * LRU list for the stripe cache
75 struct list_head stripe_cache;
78 * for scheduling work in the helper threads
80 struct btrfs_work work;
83 * bio list and bio_list_lock are used
84 * to add more bios into the stripe
85 * in hopes of avoiding the full rmw
87 struct bio_list bio_list;
88 spinlock_t bio_list_lock;
90 /* also protected by the bio_list_lock, the
91 * plug list is used by the plugging code
92 * to collect partial bios while plugged. The
93 * stripe locking code also uses it to hand off
94 * the stripe lock to the next pending IO
96 struct list_head plug_list;
99 * flags that tell us if it is safe to
100 * merge with this bio
104 /* size of each individual stripe on disk */
107 /* number of data stripes (no p/q) */
114 * set if we're doing a parity rebuild
115 * for a read from higher up, which is handled
116 * differently from a parity rebuild as part of
119 enum btrfs_rbio_ops operation;
121 /* first bad stripe */
124 /* second bad stripe (for raid6 use) */
129 * number of pages needed to represent the full
135 * size of all the bios in the bio_list. This
136 * helps us decide if the rbio maps to a full
145 atomic_t stripes_pending;
149 * these are two arrays of pointers. We allocate the
150 * rbio big enough to hold them both and setup their
151 * locations when the rbio is allocated
154 /* pointers to pages that we allocated for
155 * reading/writing stripes directly from the disk (including P/Q)
157 struct page **stripe_pages;
160 * pointers to the pages in the bio_list. Stored
161 * here for faster lookup
163 struct page **bio_pages;
166 * bitmap to record which horizontal stripe has data
168 unsigned long *dbitmap;
170 /* allocated with real_stripes-many pointers for finish_*() calls */
171 void **finish_pointers;
173 /* allocated with stripe_npages-many bits for finish_*() calls */
174 unsigned long *finish_pbitmap;
177 static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
178 static noinline void finish_rmw(struct btrfs_raid_bio *rbio);
179 static void rmw_work(struct btrfs_work *work);
180 static void read_rebuild_work(struct btrfs_work *work);
181 static int fail_bio_stripe(struct btrfs_raid_bio *rbio, struct bio *bio);
182 static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed);
183 static void __free_raid_bio(struct btrfs_raid_bio *rbio);
184 static void index_rbio_pages(struct btrfs_raid_bio *rbio);
185 static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
187 static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
189 static void scrub_parity_work(struct btrfs_work *work);
191 static void start_async_work(struct btrfs_raid_bio *rbio, btrfs_func_t work_func)
193 btrfs_init_work(&rbio->work, work_func, NULL, NULL);
194 btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
198 * the stripe hash table is used for locking, and to collect
199 * bios in hopes of making a full stripe
201 int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
203 struct btrfs_stripe_hash_table *table;
204 struct btrfs_stripe_hash_table *x;
205 struct btrfs_stripe_hash *cur;
206 struct btrfs_stripe_hash *h;
207 int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS;
210 if (info->stripe_hash_table)
214 * The table is large, starting with order 4 and can go as high as
215 * order 7 in case lock debugging is turned on.
217 * Try harder to allocate and fallback to vmalloc to lower the chance
218 * of a failing mount.
220 table = kvzalloc(struct_size(table, table, num_entries), GFP_KERNEL);
224 spin_lock_init(&table->cache_lock);
225 INIT_LIST_HEAD(&table->stripe_cache);
229 for (i = 0; i < num_entries; i++) {
231 INIT_LIST_HEAD(&cur->hash_list);
232 spin_lock_init(&cur->lock);
235 x = cmpxchg(&info->stripe_hash_table, NULL, table);
242 * caching an rbio means to copy anything from the
243 * bio_pages array into the stripe_pages array. We
244 * use the page uptodate bit in the stripe cache array
245 * to indicate if it has valid data
247 * once the caching is done, we set the cache ready
250 static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
257 ret = alloc_rbio_pages(rbio);
261 for (i = 0; i < rbio->nr_pages; i++) {
262 if (!rbio->bio_pages[i])
265 s = kmap(rbio->bio_pages[i]);
266 d = kmap(rbio->stripe_pages[i]);
270 kunmap(rbio->bio_pages[i]);
271 kunmap(rbio->stripe_pages[i]);
272 SetPageUptodate(rbio->stripe_pages[i]);
274 set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
278 * we hash on the first logical address of the stripe
280 static int rbio_bucket(struct btrfs_raid_bio *rbio)
282 u64 num = rbio->bbio->raid_map[0];
285 * we shift down quite a bit. We're using byte
286 * addressing, and most of the lower bits are zeros.
287 * This tends to upset hash_64, and it consistently
288 * returns just one or two different values.
290 * shifting off the lower bits fixes things.
292 return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
296 * stealing an rbio means taking all the uptodate pages from the stripe
297 * array in the source rbio and putting them into the destination rbio
299 static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
305 if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
308 for (i = 0; i < dest->nr_pages; i++) {
309 s = src->stripe_pages[i];
310 if (!s || !PageUptodate(s)) {
314 d = dest->stripe_pages[i];
318 dest->stripe_pages[i] = s;
319 src->stripe_pages[i] = NULL;
324 * merging means we take the bio_list from the victim and
325 * splice it into the destination. The victim should
326 * be discarded afterwards.
328 * must be called with dest->rbio_list_lock held
330 static void merge_rbio(struct btrfs_raid_bio *dest,
331 struct btrfs_raid_bio *victim)
333 bio_list_merge(&dest->bio_list, &victim->bio_list);
334 dest->bio_list_bytes += victim->bio_list_bytes;
335 /* Also inherit the bitmaps from @victim. */
336 bitmap_or(dest->dbitmap, victim->dbitmap, dest->dbitmap,
337 dest->stripe_npages);
338 dest->generic_bio_cnt += victim->generic_bio_cnt;
339 bio_list_init(&victim->bio_list);
343 * used to prune items that are in the cache. The caller
344 * must hold the hash table lock.
346 static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
348 int bucket = rbio_bucket(rbio);
349 struct btrfs_stripe_hash_table *table;
350 struct btrfs_stripe_hash *h;
354 * check the bit again under the hash table lock.
356 if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
359 table = rbio->fs_info->stripe_hash_table;
360 h = table->table + bucket;
362 /* hold the lock for the bucket because we may be
363 * removing it from the hash table
368 * hold the lock for the bio list because we need
369 * to make sure the bio list is empty
371 spin_lock(&rbio->bio_list_lock);
373 if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
374 list_del_init(&rbio->stripe_cache);
375 table->cache_size -= 1;
378 /* if the bio list isn't empty, this rbio is
379 * still involved in an IO. We take it out
380 * of the cache list, and drop the ref that
381 * was held for the list.
383 * If the bio_list was empty, we also remove
384 * the rbio from the hash_table, and drop
385 * the corresponding ref
387 if (bio_list_empty(&rbio->bio_list)) {
388 if (!list_empty(&rbio->hash_list)) {
389 list_del_init(&rbio->hash_list);
390 refcount_dec(&rbio->refs);
391 BUG_ON(!list_empty(&rbio->plug_list));
396 spin_unlock(&rbio->bio_list_lock);
397 spin_unlock(&h->lock);
400 __free_raid_bio(rbio);
404 * prune a given rbio from the cache
406 static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
408 struct btrfs_stripe_hash_table *table;
411 if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
414 table = rbio->fs_info->stripe_hash_table;
416 spin_lock_irqsave(&table->cache_lock, flags);
417 __remove_rbio_from_cache(rbio);
418 spin_unlock_irqrestore(&table->cache_lock, flags);
422 * remove everything in the cache
424 static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
426 struct btrfs_stripe_hash_table *table;
428 struct btrfs_raid_bio *rbio;
430 table = info->stripe_hash_table;
432 spin_lock_irqsave(&table->cache_lock, flags);
433 while (!list_empty(&table->stripe_cache)) {
434 rbio = list_entry(table->stripe_cache.next,
435 struct btrfs_raid_bio,
437 __remove_rbio_from_cache(rbio);
439 spin_unlock_irqrestore(&table->cache_lock, flags);
443 * remove all cached entries and free the hash table
446 void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
448 if (!info->stripe_hash_table)
450 btrfs_clear_rbio_cache(info);
451 kvfree(info->stripe_hash_table);
452 info->stripe_hash_table = NULL;
456 * insert an rbio into the stripe cache. It
457 * must have already been prepared by calling
460 * If this rbio was already cached, it gets
461 * moved to the front of the lru.
463 * If the size of the rbio cache is too big, we
466 static void cache_rbio(struct btrfs_raid_bio *rbio)
468 struct btrfs_stripe_hash_table *table;
471 if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
474 table = rbio->fs_info->stripe_hash_table;
476 spin_lock_irqsave(&table->cache_lock, flags);
477 spin_lock(&rbio->bio_list_lock);
479 /* bump our ref if we were not in the list before */
480 if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
481 refcount_inc(&rbio->refs);
483 if (!list_empty(&rbio->stripe_cache)){
484 list_move(&rbio->stripe_cache, &table->stripe_cache);
486 list_add(&rbio->stripe_cache, &table->stripe_cache);
487 table->cache_size += 1;
490 spin_unlock(&rbio->bio_list_lock);
492 if (table->cache_size > RBIO_CACHE_SIZE) {
493 struct btrfs_raid_bio *found;
495 found = list_entry(table->stripe_cache.prev,
496 struct btrfs_raid_bio,
500 __remove_rbio_from_cache(found);
503 spin_unlock_irqrestore(&table->cache_lock, flags);
507 * helper function to run the xor_blocks api. It is only
508 * able to do MAX_XOR_BLOCKS at a time, so we need to
511 static void run_xor(void **pages, int src_cnt, ssize_t len)
515 void *dest = pages[src_cnt];
518 xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
519 xor_blocks(xor_src_cnt, len, dest, pages + src_off);
521 src_cnt -= xor_src_cnt;
522 src_off += xor_src_cnt;
527 * Returns true if the bio list inside this rbio covers an entire stripe (no
530 static int rbio_is_full(struct btrfs_raid_bio *rbio)
533 unsigned long size = rbio->bio_list_bytes;
536 spin_lock_irqsave(&rbio->bio_list_lock, flags);
537 if (size != rbio->nr_data * rbio->stripe_len)
539 BUG_ON(size > rbio->nr_data * rbio->stripe_len);
540 spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
546 * returns 1 if it is safe to merge two rbios together.
547 * The merging is safe if the two rbios correspond to
548 * the same stripe and if they are both going in the same
549 * direction (read vs write), and if neither one is
550 * locked for final IO
552 * The caller is responsible for locking such that
553 * rmw_locked is safe to test
555 static int rbio_can_merge(struct btrfs_raid_bio *last,
556 struct btrfs_raid_bio *cur)
558 if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
559 test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
563 * we can't merge with cached rbios, since the
564 * idea is that when we merge the destination
565 * rbio is going to run our IO for us. We can
566 * steal from cached rbios though, other functions
569 if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
570 test_bit(RBIO_CACHE_BIT, &cur->flags))
573 if (last->bbio->raid_map[0] !=
574 cur->bbio->raid_map[0])
577 /* we can't merge with different operations */
578 if (last->operation != cur->operation)
581 * We've need read the full stripe from the drive.
582 * check and repair the parity and write the new results.
584 * We're not allowed to add any new bios to the
585 * bio list here, anyone else that wants to
586 * change this stripe needs to do their own rmw.
588 if (last->operation == BTRFS_RBIO_PARITY_SCRUB)
591 if (last->operation == BTRFS_RBIO_REBUILD_MISSING)
594 if (last->operation == BTRFS_RBIO_READ_REBUILD) {
595 int fa = last->faila;
596 int fb = last->failb;
597 int cur_fa = cur->faila;
598 int cur_fb = cur->failb;
600 if (last->faila >= last->failb) {
605 if (cur->faila >= cur->failb) {
610 if (fa != cur_fa || fb != cur_fb)
616 static int rbio_stripe_page_index(struct btrfs_raid_bio *rbio, int stripe,
619 return stripe * rbio->stripe_npages + index;
623 * these are just the pages from the rbio array, not from anything
624 * the FS sent down to us
626 static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe,
629 return rbio->stripe_pages[rbio_stripe_page_index(rbio, stripe, index)];
633 * helper to index into the pstripe
635 static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index)
637 return rbio_stripe_page(rbio, rbio->nr_data, index);
641 * helper to index into the qstripe, returns null
642 * if there is no qstripe
644 static struct page *rbio_qstripe_page(struct btrfs_raid_bio *rbio, int index)
646 if (rbio->nr_data + 1 == rbio->real_stripes)
648 return rbio_stripe_page(rbio, rbio->nr_data + 1, index);
652 * The first stripe in the table for a logical address
653 * has the lock. rbios are added in one of three ways:
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
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.
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.
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.
673 static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
675 struct btrfs_stripe_hash *h;
676 struct btrfs_raid_bio *cur;
677 struct btrfs_raid_bio *pending;
679 struct btrfs_raid_bio *freeit = NULL;
680 struct btrfs_raid_bio *cache_drop = NULL;
683 h = rbio->fs_info->stripe_hash_table->table + rbio_bucket(rbio);
685 spin_lock_irqsave(&h->lock, flags);
686 list_for_each_entry(cur, &h->hash_list, hash_list) {
687 if (cur->bbio->raid_map[0] != rbio->bbio->raid_map[0])
690 spin_lock(&cur->bio_list_lock);
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 refcount_dec(&cur->refs);
700 steal_rbio(cur, rbio);
702 spin_unlock(&cur->bio_list_lock);
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);
718 * We couldn't merge with the running rbio, see if we can merge
719 * with the pending ones. We don't have to check for rmw_locked
720 * because there is no way they are inside finish_rmw right now
722 list_for_each_entry(pending, &cur->plug_list, plug_list) {
723 if (rbio_can_merge(pending, rbio)) {
724 merge_rbio(pending, rbio);
725 spin_unlock(&cur->bio_list_lock);
733 * No merging, put us on the tail of the plug list, our rbio
734 * will be started with the currently running rbio unlocks
736 list_add_tail(&rbio->plug_list, &cur->plug_list);
737 spin_unlock(&cur->bio_list_lock);
742 refcount_inc(&rbio->refs);
743 list_add(&rbio->hash_list, &h->hash_list);
745 spin_unlock_irqrestore(&h->lock, flags);
747 remove_rbio_from_cache(cache_drop);
749 __free_raid_bio(freeit);
754 * called as rmw or parity rebuild is completed. If the plug list has more
755 * rbios waiting for this stripe, the next one on the list will be started
757 static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
760 struct btrfs_stripe_hash *h;
764 bucket = rbio_bucket(rbio);
765 h = rbio->fs_info->stripe_hash_table->table + bucket;
767 if (list_empty(&rbio->plug_list))
770 spin_lock_irqsave(&h->lock, flags);
771 spin_lock(&rbio->bio_list_lock);
773 if (!list_empty(&rbio->hash_list)) {
775 * if we're still cached and there is no other IO
776 * to perform, just leave this rbio here for others
777 * to steal from later
779 if (list_empty(&rbio->plug_list) &&
780 test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
782 clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
783 BUG_ON(!bio_list_empty(&rbio->bio_list));
787 list_del_init(&rbio->hash_list);
788 refcount_dec(&rbio->refs);
791 * we use the plug list to hold all the rbios
792 * waiting for the chance to lock this stripe.
793 * hand the lock over to one of them.
795 if (!list_empty(&rbio->plug_list)) {
796 struct btrfs_raid_bio *next;
797 struct list_head *head = rbio->plug_list.next;
799 next = list_entry(head, struct btrfs_raid_bio,
802 list_del_init(&rbio->plug_list);
804 list_add(&next->hash_list, &h->hash_list);
805 refcount_inc(&next->refs);
806 spin_unlock(&rbio->bio_list_lock);
807 spin_unlock_irqrestore(&h->lock, flags);
809 if (next->operation == BTRFS_RBIO_READ_REBUILD)
810 start_async_work(next, read_rebuild_work);
811 else if (next->operation == BTRFS_RBIO_REBUILD_MISSING) {
812 steal_rbio(rbio, next);
813 start_async_work(next, read_rebuild_work);
814 } else if (next->operation == BTRFS_RBIO_WRITE) {
815 steal_rbio(rbio, next);
816 start_async_work(next, rmw_work);
817 } else if (next->operation == BTRFS_RBIO_PARITY_SCRUB) {
818 steal_rbio(rbio, next);
819 start_async_work(next, scrub_parity_work);
826 spin_unlock(&rbio->bio_list_lock);
827 spin_unlock_irqrestore(&h->lock, flags);
831 remove_rbio_from_cache(rbio);
834 static void __free_raid_bio(struct btrfs_raid_bio *rbio)
838 if (!refcount_dec_and_test(&rbio->refs))
841 WARN_ON(!list_empty(&rbio->stripe_cache));
842 WARN_ON(!list_empty(&rbio->hash_list));
843 WARN_ON(!bio_list_empty(&rbio->bio_list));
845 for (i = 0; i < rbio->nr_pages; i++) {
846 if (rbio->stripe_pages[i]) {
847 __free_page(rbio->stripe_pages[i]);
848 rbio->stripe_pages[i] = NULL;
852 btrfs_put_bbio(rbio->bbio);
856 static void rbio_endio_bio_list(struct bio *cur, blk_status_t err)
863 cur->bi_status = err;
870 * this frees the rbio and runs through all the bios in the
871 * bio_list and calls end_io on them
873 static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, blk_status_t err)
875 struct bio *cur = bio_list_get(&rbio->bio_list);
878 if (rbio->generic_bio_cnt)
879 btrfs_bio_counter_sub(rbio->fs_info, rbio->generic_bio_cnt);
881 * Clear the data bitmap, as the rbio may be cached for later usage.
882 * do this before before unlock_stripe() so there will be no new bio
885 bitmap_clear(rbio->dbitmap, 0, rbio->stripe_npages);
888 * At this moment, rbio->bio_list is empty, however since rbio does not
889 * always have RBIO_RMW_LOCKED_BIT set and rbio is still linked on the
890 * hash list, rbio may be merged with others so that rbio->bio_list
892 * Once unlock_stripe() is done, rbio->bio_list will not be updated any
893 * more and we can call bio_endio() on all queued bios.
896 extra = bio_list_get(&rbio->bio_list);
897 __free_raid_bio(rbio);
899 rbio_endio_bio_list(cur, err);
901 rbio_endio_bio_list(extra, err);
905 * end io function used by finish_rmw. When we finally
906 * get here, we've written a full stripe
908 static void raid_write_end_io(struct bio *bio)
910 struct btrfs_raid_bio *rbio = bio->bi_private;
911 blk_status_t err = bio->bi_status;
915 fail_bio_stripe(rbio, bio);
919 if (!atomic_dec_and_test(&rbio->stripes_pending))
924 /* OK, we have read all the stripes we need to. */
925 max_errors = (rbio->operation == BTRFS_RBIO_PARITY_SCRUB) ?
926 0 : rbio->bbio->max_errors;
927 if (atomic_read(&rbio->error) > max_errors)
930 rbio_orig_end_io(rbio, err);
934 * the read/modify/write code wants to use the original bio for
935 * any pages it included, and then use the rbio for everything
936 * else. This function decides if a given index (stripe number)
937 * and page number in that stripe fall inside the original bio
940 * if you set bio_list_only, you'll get a NULL back for any ranges
941 * that are outside the bio_list
943 * This doesn't take any refs on anything, you get a bare page pointer
944 * and the caller must bump refs as required.
946 * You must call index_rbio_pages once before you can trust
947 * the answers from this function.
949 static struct page *page_in_rbio(struct btrfs_raid_bio *rbio,
950 int index, int pagenr, int bio_list_only)
953 struct page *p = NULL;
955 chunk_page = index * (rbio->stripe_len >> PAGE_SHIFT) + pagenr;
957 spin_lock_irq(&rbio->bio_list_lock);
958 p = rbio->bio_pages[chunk_page];
959 spin_unlock_irq(&rbio->bio_list_lock);
961 if (p || bio_list_only)
964 return rbio->stripe_pages[chunk_page];
968 * number of pages we need for the entire stripe across all the
971 static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
973 return DIV_ROUND_UP(stripe_len, PAGE_SIZE) * nr_stripes;
977 * allocation and initial setup for the btrfs_raid_bio. Not
978 * this does not allocate any pages for rbio->pages.
980 static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
981 struct btrfs_bio *bbio,
984 struct btrfs_raid_bio *rbio;
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);
991 rbio = kzalloc(sizeof(*rbio) +
992 sizeof(*rbio->stripe_pages) * num_pages +
993 sizeof(*rbio->bio_pages) * num_pages +
994 sizeof(*rbio->finish_pointers) * real_stripes +
995 sizeof(*rbio->dbitmap) * BITS_TO_LONGS(stripe_npages) +
996 sizeof(*rbio->finish_pbitmap) *
997 BITS_TO_LONGS(stripe_npages),
1000 return ERR_PTR(-ENOMEM);
1002 bio_list_init(&rbio->bio_list);
1003 INIT_LIST_HEAD(&rbio->plug_list);
1004 spin_lock_init(&rbio->bio_list_lock);
1005 INIT_LIST_HEAD(&rbio->stripe_cache);
1006 INIT_LIST_HEAD(&rbio->hash_list);
1008 rbio->fs_info = fs_info;
1009 rbio->stripe_len = stripe_len;
1010 rbio->nr_pages = num_pages;
1011 rbio->real_stripes = real_stripes;
1012 rbio->stripe_npages = stripe_npages;
1015 refcount_set(&rbio->refs, 1);
1016 atomic_set(&rbio->error, 0);
1017 atomic_set(&rbio->stripes_pending, 0);
1020 * the stripe_pages, bio_pages, etc arrays point to the extra
1021 * memory we allocated past the end of the rbio
1024 #define CONSUME_ALLOC(ptr, count) do { \
1026 p = (unsigned char *)p + sizeof(*(ptr)) * (count); \
1028 CONSUME_ALLOC(rbio->stripe_pages, num_pages);
1029 CONSUME_ALLOC(rbio->bio_pages, num_pages);
1030 CONSUME_ALLOC(rbio->finish_pointers, real_stripes);
1031 CONSUME_ALLOC(rbio->dbitmap, BITS_TO_LONGS(stripe_npages));
1032 CONSUME_ALLOC(rbio->finish_pbitmap, BITS_TO_LONGS(stripe_npages));
1033 #undef CONSUME_ALLOC
1035 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
1036 nr_data = real_stripes - 1;
1037 else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6)
1038 nr_data = real_stripes - 2;
1042 rbio->nr_data = nr_data;
1046 /* allocate pages for all the stripes in the bio, including parity */
1047 static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
1052 for (i = 0; i < rbio->nr_pages; i++) {
1053 if (rbio->stripe_pages[i])
1055 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1058 rbio->stripe_pages[i] = page;
1063 /* only allocate pages for p/q stripes */
1064 static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
1069 i = rbio_stripe_page_index(rbio, rbio->nr_data, 0);
1071 for (; i < rbio->nr_pages; i++) {
1072 if (rbio->stripe_pages[i])
1074 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1077 rbio->stripe_pages[i] = page;
1083 * add a single page from a specific stripe into our list of bios for IO
1084 * this will try to merge into existing bios if possible, and returns
1085 * zero if all went well.
1087 static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
1088 struct bio_list *bio_list,
1091 unsigned long page_index,
1092 unsigned long bio_max_len)
1094 struct bio *last = bio_list->tail;
1097 struct btrfs_bio_stripe *stripe;
1100 stripe = &rbio->bbio->stripes[stripe_nr];
1101 disk_start = stripe->physical + (page_index << PAGE_SHIFT);
1103 /* if the device is missing, just fail this stripe */
1104 if (!stripe->dev->bdev)
1105 return fail_rbio_index(rbio, stripe_nr);
1107 /* see if we can add this page onto our existing bio */
1109 u64 last_end = (u64)last->bi_iter.bi_sector << 9;
1110 last_end += last->bi_iter.bi_size;
1113 * we can't merge these if they are from different
1114 * devices or if they are not contiguous
1116 if (last_end == disk_start && !last->bi_status &&
1117 last->bi_disk == stripe->dev->bdev->bd_disk &&
1118 last->bi_partno == stripe->dev->bdev->bd_partno) {
1119 ret = bio_add_page(last, page, PAGE_SIZE, 0);
1120 if (ret == PAGE_SIZE)
1125 /* put a new bio on the list */
1126 bio = btrfs_io_bio_alloc(bio_max_len >> PAGE_SHIFT ?: 1);
1127 btrfs_io_bio(bio)->device = stripe->dev;
1128 bio->bi_iter.bi_size = 0;
1129 bio_set_dev(bio, stripe->dev->bdev);
1130 bio->bi_iter.bi_sector = disk_start >> 9;
1132 bio_add_page(bio, page, PAGE_SIZE, 0);
1133 bio_list_add(bio_list, bio);
1138 * while we're doing the read/modify/write cycle, we could
1139 * have errors in reading pages off the disk. This checks
1140 * for errors and if we're not able to read the page it'll
1141 * trigger parity reconstruction. The rmw will be finished
1142 * after we've reconstructed the failed stripes
1144 static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
1146 if (rbio->faila >= 0 || rbio->failb >= 0) {
1147 BUG_ON(rbio->faila == rbio->real_stripes - 1);
1148 __raid56_parity_recover(rbio);
1155 * helper function to walk our bio list and populate the bio_pages array with
1156 * the result. This seems expensive, but it is faster than constantly
1157 * searching through the bio list as we setup the IO in finish_rmw or stripe
1160 * This must be called before you trust the answers from page_in_rbio
1162 static void index_rbio_pages(struct btrfs_raid_bio *rbio)
1166 unsigned long stripe_offset;
1167 unsigned long page_index;
1169 spin_lock_irq(&rbio->bio_list_lock);
1170 bio_list_for_each(bio, &rbio->bio_list) {
1171 struct bio_vec bvec;
1172 struct bvec_iter iter;
1175 start = (u64)bio->bi_iter.bi_sector << 9;
1176 stripe_offset = start - rbio->bbio->raid_map[0];
1177 page_index = stripe_offset >> PAGE_SHIFT;
1179 if (bio_flagged(bio, BIO_CLONED))
1180 bio->bi_iter = btrfs_io_bio(bio)->iter;
1182 bio_for_each_segment(bvec, bio, iter) {
1183 rbio->bio_pages[page_index + i] = bvec.bv_page;
1187 spin_unlock_irq(&rbio->bio_list_lock);
1191 * this is called from one of two situations. We either
1192 * have a full stripe from the higher layers, or we've read all
1193 * the missing bits off disk.
1195 * This will calculate the parity and then send down any
1198 static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1200 struct btrfs_bio *bbio = rbio->bbio;
1201 void **pointers = rbio->finish_pointers;
1202 int nr_data = rbio->nr_data;
1206 struct bio_list bio_list;
1210 bio_list_init(&bio_list);
1212 if (rbio->real_stripes - rbio->nr_data == 1)
1213 has_qstripe = false;
1214 else if (rbio->real_stripes - rbio->nr_data == 2)
1219 /* We should have at least one data sector. */
1220 ASSERT(bitmap_weight(rbio->dbitmap, rbio->stripe_npages));
1222 /* at this point we either have a full stripe,
1223 * or we've read the full stripe from the drive.
1224 * recalculate the parity and write the new results.
1226 * We're not allowed to add any new bios to the
1227 * bio list here, anyone else that wants to
1228 * change this stripe needs to do their own rmw.
1230 spin_lock_irq(&rbio->bio_list_lock);
1231 set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1232 spin_unlock_irq(&rbio->bio_list_lock);
1234 atomic_set(&rbio->error, 0);
1237 * now that we've set rmw_locked, run through the
1238 * bio list one last time and map the page pointers
1240 * We don't cache full rbios because we're assuming
1241 * the higher layers are unlikely to use this area of
1242 * the disk again soon. If they do use it again,
1243 * hopefully they will send another full bio.
1245 index_rbio_pages(rbio);
1246 if (!rbio_is_full(rbio))
1247 cache_rbio_pages(rbio);
1249 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
1251 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1253 /* first collect one page from each data stripe */
1254 for (stripe = 0; stripe < nr_data; stripe++) {
1255 p = page_in_rbio(rbio, stripe, pagenr, 0);
1256 pointers[stripe] = kmap(p);
1259 /* then add the parity stripe */
1260 p = rbio_pstripe_page(rbio, pagenr);
1262 pointers[stripe++] = kmap(p);
1267 * raid6, add the qstripe and call the
1268 * library function to fill in our p/q
1270 p = rbio_qstripe_page(rbio, pagenr);
1272 pointers[stripe++] = kmap(p);
1274 raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
1278 copy_page(pointers[nr_data], pointers[0]);
1279 run_xor(pointers + 1, nr_data - 1, PAGE_SIZE);
1283 for (stripe = 0; stripe < rbio->real_stripes; stripe++)
1284 kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
1288 * time to start writing. Make bios for everything from the
1289 * higher layers (the bio_list in our rbio) and our p/q. Ignore
1292 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1293 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1296 /* This vertical stripe has no data, skip it. */
1297 if (!test_bit(pagenr, rbio->dbitmap))
1300 if (stripe < rbio->nr_data) {
1301 page = page_in_rbio(rbio, stripe, pagenr, 1);
1305 page = rbio_stripe_page(rbio, stripe, pagenr);
1308 ret = rbio_add_io_page(rbio, &bio_list,
1309 page, stripe, pagenr, rbio->stripe_len);
1315 if (likely(!bbio->num_tgtdevs))
1318 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1319 if (!bbio->tgtdev_map[stripe])
1322 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1325 /* This vertical stripe has no data, skip it. */
1326 if (!test_bit(pagenr, rbio->dbitmap))
1329 if (stripe < rbio->nr_data) {
1330 page = page_in_rbio(rbio, stripe, pagenr, 1);
1334 page = rbio_stripe_page(rbio, stripe, pagenr);
1337 ret = rbio_add_io_page(rbio, &bio_list, page,
1338 rbio->bbio->tgtdev_map[stripe],
1339 pagenr, rbio->stripe_len);
1346 atomic_set(&rbio->stripes_pending, bio_list_size(&bio_list));
1347 BUG_ON(atomic_read(&rbio->stripes_pending) == 0);
1349 while ((bio = bio_list_pop(&bio_list))) {
1350 bio->bi_private = rbio;
1351 bio->bi_end_io = raid_write_end_io;
1352 bio->bi_opf = REQ_OP_WRITE;
1359 rbio_orig_end_io(rbio, BLK_STS_IOERR);
1361 while ((bio = bio_list_pop(&bio_list)))
1366 * helper to find the stripe number for a given bio. Used to figure out which
1367 * stripe has failed. This expects the bio to correspond to a physical disk,
1368 * so it looks up based on physical sector numbers.
1370 static int find_bio_stripe(struct btrfs_raid_bio *rbio,
1373 u64 physical = bio->bi_iter.bi_sector;
1375 struct btrfs_bio_stripe *stripe;
1379 for (i = 0; i < rbio->bbio->num_stripes; i++) {
1380 stripe = &rbio->bbio->stripes[i];
1381 if (in_range(physical, stripe->physical, rbio->stripe_len) &&
1382 stripe->dev->bdev &&
1383 bio->bi_disk == stripe->dev->bdev->bd_disk &&
1384 bio->bi_partno == stripe->dev->bdev->bd_partno) {
1392 * helper to find the stripe number for a given
1393 * bio (before mapping). Used to figure out which stripe has
1394 * failed. This looks up based on logical block numbers.
1396 static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
1399 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1402 for (i = 0; i < rbio->nr_data; i++) {
1403 u64 stripe_start = rbio->bbio->raid_map[i];
1405 if (in_range(logical, stripe_start, rbio->stripe_len))
1412 * returns -EIO if we had too many failures
1414 static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed)
1416 unsigned long flags;
1419 spin_lock_irqsave(&rbio->bio_list_lock, flags);
1421 /* we already know this stripe is bad, move on */
1422 if (rbio->faila == failed || rbio->failb == failed)
1425 if (rbio->faila == -1) {
1426 /* first failure on this rbio */
1427 rbio->faila = failed;
1428 atomic_inc(&rbio->error);
1429 } else if (rbio->failb == -1) {
1430 /* second failure on this rbio */
1431 rbio->failb = failed;
1432 atomic_inc(&rbio->error);
1437 spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
1443 * helper to fail a stripe based on a physical disk
1446 static int fail_bio_stripe(struct btrfs_raid_bio *rbio,
1449 int failed = find_bio_stripe(rbio, bio);
1454 return fail_rbio_index(rbio, failed);
1458 * this sets each page in the bio uptodate. It should only be used on private
1459 * rbio pages, nothing that comes in from the higher layers
1461 static void set_bio_pages_uptodate(struct bio *bio)
1463 struct bio_vec *bvec;
1464 struct bvec_iter_all iter_all;
1466 ASSERT(!bio_flagged(bio, BIO_CLONED));
1468 bio_for_each_segment_all(bvec, bio, iter_all)
1469 SetPageUptodate(bvec->bv_page);
1473 * end io for the read phase of the rmw cycle. All the bios here are physical
1474 * stripe bios we've read from the disk so we can recalculate the parity of the
1477 * This will usually kick off finish_rmw once all the bios are read in, but it
1478 * may trigger parity reconstruction if we had any errors along the way
1480 static void raid_rmw_end_io(struct bio *bio)
1482 struct btrfs_raid_bio *rbio = bio->bi_private;
1485 fail_bio_stripe(rbio, bio);
1487 set_bio_pages_uptodate(bio);
1491 if (!atomic_dec_and_test(&rbio->stripes_pending))
1494 if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
1498 * this will normally call finish_rmw to start our write
1499 * but if there are any failed stripes we'll reconstruct
1502 validate_rbio_for_rmw(rbio);
1507 rbio_orig_end_io(rbio, BLK_STS_IOERR);
1511 * the stripe must be locked by the caller. It will
1512 * unlock after all the writes are done
1514 static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
1516 int bios_to_read = 0;
1517 struct bio_list bio_list;
1523 bio_list_init(&bio_list);
1525 ret = alloc_rbio_pages(rbio);
1529 index_rbio_pages(rbio);
1531 atomic_set(&rbio->error, 0);
1533 * build a list of bios to read all the missing parts of this
1536 for (stripe = 0; stripe < rbio->nr_data; stripe++) {
1537 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1540 * we want to find all the pages missing from
1541 * the rbio and read them from the disk. If
1542 * page_in_rbio finds a page in the bio list
1543 * we don't need to read it off the stripe.
1545 page = page_in_rbio(rbio, stripe, pagenr, 1);
1549 page = rbio_stripe_page(rbio, stripe, pagenr);
1551 * the bio cache may have handed us an uptodate
1552 * page. If so, be happy and use it
1554 if (PageUptodate(page))
1557 ret = rbio_add_io_page(rbio, &bio_list, page,
1558 stripe, pagenr, rbio->stripe_len);
1564 bios_to_read = bio_list_size(&bio_list);
1565 if (!bios_to_read) {
1567 * this can happen if others have merged with
1568 * us, it means there is nothing left to read.
1569 * But if there are missing devices it may not be
1570 * safe to do the full stripe write yet.
1576 * the bbio may be freed once we submit the last bio. Make sure
1577 * not to touch it after that
1579 atomic_set(&rbio->stripes_pending, bios_to_read);
1580 while ((bio = bio_list_pop(&bio_list))) {
1581 bio->bi_private = rbio;
1582 bio->bi_end_io = raid_rmw_end_io;
1583 bio->bi_opf = REQ_OP_READ;
1585 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
1589 /* the actual write will happen once the reads are done */
1593 rbio_orig_end_io(rbio, BLK_STS_IOERR);
1595 while ((bio = bio_list_pop(&bio_list)))
1601 validate_rbio_for_rmw(rbio);
1606 * if the upper layers pass in a full stripe, we thank them by only allocating
1607 * enough pages to hold the parity, and sending it all down quickly.
1609 static int full_stripe_write(struct btrfs_raid_bio *rbio)
1613 ret = alloc_rbio_parity_pages(rbio);
1615 __free_raid_bio(rbio);
1619 ret = lock_stripe_add(rbio);
1626 * partial stripe writes get handed over to async helpers.
1627 * We're really hoping to merge a few more writes into this
1628 * rbio before calculating new parity
1630 static int partial_stripe_write(struct btrfs_raid_bio *rbio)
1634 ret = lock_stripe_add(rbio);
1636 start_async_work(rbio, rmw_work);
1641 * sometimes while we were reading from the drive to
1642 * recalculate parity, enough new bios come into create
1643 * a full stripe. So we do a check here to see if we can
1644 * go directly to finish_rmw
1646 static int __raid56_parity_write(struct btrfs_raid_bio *rbio)
1648 /* head off into rmw land if we don't have a full stripe */
1649 if (!rbio_is_full(rbio))
1650 return partial_stripe_write(rbio);
1651 return full_stripe_write(rbio);
1655 * We use plugging call backs to collect full stripes.
1656 * Any time we get a partial stripe write while plugged
1657 * we collect it into a list. When the unplug comes down,
1658 * we sort the list by logical block number and merge
1659 * everything we can into the same rbios
1661 struct btrfs_plug_cb {
1662 struct blk_plug_cb cb;
1663 struct btrfs_fs_info *info;
1664 struct list_head rbio_list;
1665 struct btrfs_work work;
1669 * rbios on the plug list are sorted for easier merging.
1671 static int plug_cmp(void *priv, const struct list_head *a,
1672 const struct list_head *b)
1674 struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
1676 struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
1678 u64 a_sector = ra->bio_list.head->bi_iter.bi_sector;
1679 u64 b_sector = rb->bio_list.head->bi_iter.bi_sector;
1681 if (a_sector < b_sector)
1683 if (a_sector > b_sector)
1688 static void run_plug(struct btrfs_plug_cb *plug)
1690 struct btrfs_raid_bio *cur;
1691 struct btrfs_raid_bio *last = NULL;
1694 * sort our plug list then try to merge
1695 * everything we can in hopes of creating full
1698 list_sort(NULL, &plug->rbio_list, plug_cmp);
1699 while (!list_empty(&plug->rbio_list)) {
1700 cur = list_entry(plug->rbio_list.next,
1701 struct btrfs_raid_bio, plug_list);
1702 list_del_init(&cur->plug_list);
1704 if (rbio_is_full(cur)) {
1707 /* we have a full stripe, send it down */
1708 ret = full_stripe_write(cur);
1713 if (rbio_can_merge(last, cur)) {
1714 merge_rbio(last, cur);
1715 __free_raid_bio(cur);
1719 __raid56_parity_write(last);
1724 __raid56_parity_write(last);
1730 * if the unplug comes from schedule, we have to push the
1731 * work off to a helper thread
1733 static void unplug_work(struct btrfs_work *work)
1735 struct btrfs_plug_cb *plug;
1736 plug = container_of(work, struct btrfs_plug_cb, work);
1740 static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
1742 struct btrfs_plug_cb *plug;
1743 plug = container_of(cb, struct btrfs_plug_cb, cb);
1745 if (from_schedule) {
1746 btrfs_init_work(&plug->work, unplug_work, NULL, NULL);
1747 btrfs_queue_work(plug->info->rmw_workers,
1754 /* Add the original bio into rbio->bio_list, and update rbio::dbitmap. */
1755 static void rbio_add_bio(struct btrfs_raid_bio *rbio, struct bio *orig_bio)
1757 const struct btrfs_fs_info *fs_info = rbio->fs_info;
1758 const u64 orig_logical = orig_bio->bi_iter.bi_sector << SECTOR_SHIFT;
1759 const u64 full_stripe_start = rbio->bbio->raid_map[0];
1760 const u32 orig_len = orig_bio->bi_iter.bi_size;
1761 const u32 sectorsize = fs_info->sectorsize;
1764 ASSERT(orig_logical >= full_stripe_start &&
1765 orig_logical + orig_len <= full_stripe_start +
1766 rbio->nr_data * rbio->stripe_len);
1768 bio_list_add(&rbio->bio_list, orig_bio);
1769 rbio->bio_list_bytes += orig_bio->bi_iter.bi_size;
1771 /* Update the dbitmap. */
1772 for (cur_logical = orig_logical; cur_logical < orig_logical + orig_len;
1773 cur_logical += sectorsize) {
1774 int bit = ((u32)(cur_logical - full_stripe_start) >>
1775 PAGE_SHIFT) % rbio->stripe_npages;
1777 set_bit(bit, rbio->dbitmap);
1782 * our main entry point for writes from the rest of the FS.
1784 int raid56_parity_write(struct btrfs_fs_info *fs_info, struct bio *bio,
1785 struct btrfs_bio *bbio, u64 stripe_len)
1787 struct btrfs_raid_bio *rbio;
1788 struct btrfs_plug_cb *plug = NULL;
1789 struct blk_plug_cb *cb;
1792 rbio = alloc_rbio(fs_info, bbio, stripe_len);
1794 btrfs_put_bbio(bbio);
1795 return PTR_ERR(rbio);
1797 rbio->operation = BTRFS_RBIO_WRITE;
1798 rbio_add_bio(rbio, bio);
1800 btrfs_bio_counter_inc_noblocked(fs_info);
1801 rbio->generic_bio_cnt = 1;
1804 * don't plug on full rbios, just get them out the door
1805 * as quickly as we can
1807 if (rbio_is_full(rbio)) {
1808 ret = full_stripe_write(rbio);
1810 btrfs_bio_counter_dec(fs_info);
1814 cb = blk_check_plugged(btrfs_raid_unplug, fs_info, sizeof(*plug));
1816 plug = container_of(cb, struct btrfs_plug_cb, cb);
1818 plug->info = fs_info;
1819 INIT_LIST_HEAD(&plug->rbio_list);
1821 list_add_tail(&rbio->plug_list, &plug->rbio_list);
1824 ret = __raid56_parity_write(rbio);
1826 btrfs_bio_counter_dec(fs_info);
1832 * all parity reconstruction happens here. We've read in everything
1833 * we can find from the drives and this does the heavy lifting of
1834 * sorting the good from the bad.
1836 static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
1840 int faila = -1, failb = -1;
1845 pointers = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
1847 err = BLK_STS_RESOURCE;
1851 faila = rbio->faila;
1852 failb = rbio->failb;
1854 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1855 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
1856 spin_lock_irq(&rbio->bio_list_lock);
1857 set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1858 spin_unlock_irq(&rbio->bio_list_lock);
1861 index_rbio_pages(rbio);
1863 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1865 * Now we just use bitmap to mark the horizontal stripes in
1866 * which we have data when doing parity scrub.
1868 if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB &&
1869 !test_bit(pagenr, rbio->dbitmap))
1872 /* setup our array of pointers with pages
1875 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1877 * if we're rebuilding a read, we have to use
1878 * pages from the bio list
1880 if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1881 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
1882 (stripe == faila || stripe == failb)) {
1883 page = page_in_rbio(rbio, stripe, pagenr, 0);
1885 page = rbio_stripe_page(rbio, stripe, pagenr);
1887 pointers[stripe] = kmap(page);
1890 /* all raid6 handling here */
1891 if (rbio->bbio->map_type & BTRFS_BLOCK_GROUP_RAID6) {
1893 * single failure, rebuild from parity raid5
1897 if (faila == rbio->nr_data) {
1899 * Just the P stripe has failed, without
1900 * a bad data or Q stripe.
1901 * TODO, we should redo the xor here.
1903 err = BLK_STS_IOERR;
1907 * a single failure in raid6 is rebuilt
1908 * in the pstripe code below
1913 /* make sure our ps and qs are in order */
1917 /* if the q stripe is failed, do a pstripe reconstruction
1919 * If both the q stripe and the P stripe are failed, we're
1920 * here due to a crc mismatch and we can't give them the
1923 if (rbio->bbio->raid_map[failb] == RAID6_Q_STRIPE) {
1924 if (rbio->bbio->raid_map[faila] ==
1926 err = BLK_STS_IOERR;
1930 * otherwise we have one bad data stripe and
1931 * a good P stripe. raid5!
1936 if (rbio->bbio->raid_map[failb] == RAID5_P_STRIPE) {
1937 raid6_datap_recov(rbio->real_stripes,
1938 PAGE_SIZE, faila, pointers);
1940 raid6_2data_recov(rbio->real_stripes,
1941 PAGE_SIZE, faila, failb,
1947 /* rebuild from P stripe here (raid5 or raid6) */
1948 BUG_ON(failb != -1);
1950 /* Copy parity block into failed block to start with */
1951 copy_page(pointers[faila], pointers[rbio->nr_data]);
1953 /* rearrange the pointer array */
1954 p = pointers[faila];
1955 for (stripe = faila; stripe < rbio->nr_data - 1; stripe++)
1956 pointers[stripe] = pointers[stripe + 1];
1957 pointers[rbio->nr_data - 1] = p;
1959 /* xor in the rest */
1960 run_xor(pointers, rbio->nr_data - 1, PAGE_SIZE);
1962 /* if we're doing this rebuild as part of an rmw, go through
1963 * and set all of our private rbio pages in the
1964 * failed stripes as uptodate. This way finish_rmw will
1965 * know they can be trusted. If this was a read reconstruction,
1966 * other endio functions will fiddle the uptodate bits
1968 if (rbio->operation == BTRFS_RBIO_WRITE) {
1969 for (i = 0; i < rbio->stripe_npages; i++) {
1971 page = rbio_stripe_page(rbio, faila, i);
1972 SetPageUptodate(page);
1975 page = rbio_stripe_page(rbio, failb, i);
1976 SetPageUptodate(page);
1980 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1982 * if we're rebuilding a read, we have to use
1983 * pages from the bio list
1985 if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1986 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
1987 (stripe == faila || stripe == failb)) {
1988 page = page_in_rbio(rbio, stripe, pagenr, 0);
1990 page = rbio_stripe_page(rbio, stripe, pagenr);
2002 * Similar to READ_REBUILD, REBUILD_MISSING at this point also has a
2003 * valid rbio which is consistent with ondisk content, thus such a
2004 * valid rbio can be cached to avoid further disk reads.
2006 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
2007 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
2009 * - In case of two failures, where rbio->failb != -1:
2011 * Do not cache this rbio since the above read reconstruction
2012 * (raid6_datap_recov() or raid6_2data_recov()) may have
2013 * changed some content of stripes which are not identical to
2014 * on-disk content any more, otherwise, a later write/recover
2015 * may steal stripe_pages from this rbio and end up with
2016 * corruptions or rebuild failures.
2018 * - In case of single failure, where rbio->failb == -1:
2020 * Cache this rbio iff the above read reconstruction is
2021 * executed without problems.
2023 if (err == BLK_STS_OK && rbio->failb < 0)
2024 cache_rbio_pages(rbio);
2026 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
2028 rbio_orig_end_io(rbio, err);
2029 } else if (err == BLK_STS_OK) {
2033 if (rbio->operation == BTRFS_RBIO_WRITE)
2035 else if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB)
2036 finish_parity_scrub(rbio, 0);
2040 rbio_orig_end_io(rbio, err);
2045 * This is called only for stripes we've read from disk to
2046 * reconstruct the parity.
2048 static void raid_recover_end_io(struct bio *bio)
2050 struct btrfs_raid_bio *rbio = bio->bi_private;
2053 * we only read stripe pages off the disk, set them
2054 * up to date if there were no errors
2057 fail_bio_stripe(rbio, bio);
2059 set_bio_pages_uptodate(bio);
2062 if (!atomic_dec_and_test(&rbio->stripes_pending))
2065 if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
2066 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2068 __raid_recover_end_io(rbio);
2072 * reads everything we need off the disk to reconstruct
2073 * the parity. endio handlers trigger final reconstruction
2074 * when the IO is done.
2076 * This is used both for reads from the higher layers and for
2077 * parity construction required to finish a rmw cycle.
2079 static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
2081 int bios_to_read = 0;
2082 struct bio_list bio_list;
2088 bio_list_init(&bio_list);
2090 ret = alloc_rbio_pages(rbio);
2094 atomic_set(&rbio->error, 0);
2097 * Read everything that hasn't failed. However this time we will
2098 * not trust any cached sector.
2099 * As we may read out some stale data but higher layer is not reading
2102 * So here we always re-read everything in recovery path.
2104 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
2105 if (rbio->faila == stripe || rbio->failb == stripe) {
2106 atomic_inc(&rbio->error);
2110 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
2111 ret = rbio_add_io_page(rbio, &bio_list,
2112 rbio_stripe_page(rbio, stripe, pagenr),
2113 stripe, pagenr, rbio->stripe_len);
2119 bios_to_read = bio_list_size(&bio_list);
2120 if (!bios_to_read) {
2122 * we might have no bios to read just because the pages
2123 * were up to date, or we might have no bios to read because
2124 * the devices were gone.
2126 if (atomic_read(&rbio->error) <= rbio->bbio->max_errors) {
2127 __raid_recover_end_io(rbio);
2135 * the bbio may be freed once we submit the last bio. Make sure
2136 * not to touch it after that
2138 atomic_set(&rbio->stripes_pending, bios_to_read);
2139 while ((bio = bio_list_pop(&bio_list))) {
2140 bio->bi_private = rbio;
2141 bio->bi_end_io = raid_recover_end_io;
2142 bio->bi_opf = REQ_OP_READ;
2144 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
2152 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
2153 rbio->operation == BTRFS_RBIO_REBUILD_MISSING)
2154 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2156 while ((bio = bio_list_pop(&bio_list)))
2163 * the main entry point for reads from the higher layers. This
2164 * is really only called when the normal read path had a failure,
2165 * so we assume the bio they send down corresponds to a failed part
2168 int raid56_parity_recover(struct btrfs_fs_info *fs_info, struct bio *bio,
2169 struct btrfs_bio *bbio, u64 stripe_len,
2170 int mirror_num, int generic_io)
2172 struct btrfs_raid_bio *rbio;
2176 ASSERT(bbio->mirror_num == mirror_num);
2177 btrfs_io_bio(bio)->mirror_num = mirror_num;
2180 rbio = alloc_rbio(fs_info, bbio, stripe_len);
2183 btrfs_put_bbio(bbio);
2184 return PTR_ERR(rbio);
2187 rbio->operation = BTRFS_RBIO_READ_REBUILD;
2188 rbio_add_bio(rbio, bio);
2190 rbio->faila = find_logical_bio_stripe(rbio, bio);
2191 if (rbio->faila == -1) {
2193 "%s could not find the bad stripe in raid56 so that we cannot recover any more (bio has logical %llu len %llu, bbio has map_type %llu)",
2194 __func__, (u64)bio->bi_iter.bi_sector << 9,
2195 (u64)bio->bi_iter.bi_size, bbio->map_type);
2197 btrfs_put_bbio(bbio);
2203 btrfs_bio_counter_inc_noblocked(fs_info);
2204 rbio->generic_bio_cnt = 1;
2206 btrfs_get_bbio(bbio);
2211 * for 'mirror == 2', reconstruct from all other stripes.
2212 * for 'mirror_num > 2', select a stripe to fail on every retry.
2214 if (mirror_num > 2) {
2216 * 'mirror == 3' is to fail the p stripe and
2217 * reconstruct from the q stripe. 'mirror > 3' is to
2218 * fail a data stripe and reconstruct from p+q stripe.
2220 rbio->failb = rbio->real_stripes - (mirror_num - 1);
2221 ASSERT(rbio->failb > 0);
2222 if (rbio->failb <= rbio->faila)
2226 ret = lock_stripe_add(rbio);
2229 * __raid56_parity_recover will end the bio with
2230 * any errors it hits. We don't want to return
2231 * its error value up the stack because our caller
2232 * will end up calling bio_endio with any nonzero
2236 __raid56_parity_recover(rbio);
2238 * our rbio has been added to the list of
2239 * rbios that will be handled after the
2240 * currently lock owner is done
2246 static void rmw_work(struct btrfs_work *work)
2248 struct btrfs_raid_bio *rbio;
2250 rbio = container_of(work, struct btrfs_raid_bio, work);
2251 raid56_rmw_stripe(rbio);
2254 static void read_rebuild_work(struct btrfs_work *work)
2256 struct btrfs_raid_bio *rbio;
2258 rbio = container_of(work, struct btrfs_raid_bio, work);
2259 __raid56_parity_recover(rbio);
2263 * The following code is used to scrub/replace the parity stripe
2265 * Caller must have already increased bio_counter for getting @bbio.
2267 * Note: We need make sure all the pages that add into the scrub/replace
2268 * raid bio are correct and not be changed during the scrub/replace. That
2269 * is those pages just hold metadata or file data with checksum.
2272 struct btrfs_raid_bio *
2273 raid56_parity_alloc_scrub_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
2274 struct btrfs_bio *bbio, u64 stripe_len,
2275 struct btrfs_device *scrub_dev,
2276 unsigned long *dbitmap, int stripe_nsectors)
2278 struct btrfs_raid_bio *rbio;
2281 rbio = alloc_rbio(fs_info, bbio, stripe_len);
2284 bio_list_add(&rbio->bio_list, bio);
2286 * This is a special bio which is used to hold the completion handler
2287 * and make the scrub rbio is similar to the other types
2289 ASSERT(!bio->bi_iter.bi_size);
2290 rbio->operation = BTRFS_RBIO_PARITY_SCRUB;
2293 * After mapping bbio with BTRFS_MAP_WRITE, parities have been sorted
2294 * to the end position, so this search can start from the first parity
2297 for (i = rbio->nr_data; i < rbio->real_stripes; i++) {
2298 if (bbio->stripes[i].dev == scrub_dev) {
2303 ASSERT(i < rbio->real_stripes);
2305 /* Now we just support the sectorsize equals to page size */
2306 ASSERT(fs_info->sectorsize == PAGE_SIZE);
2307 ASSERT(rbio->stripe_npages == stripe_nsectors);
2308 bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors);
2311 * We have already increased bio_counter when getting bbio, record it
2312 * so we can free it at rbio_orig_end_io().
2314 rbio->generic_bio_cnt = 1;
2319 /* Used for both parity scrub and missing. */
2320 void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page,
2326 ASSERT(logical >= rbio->bbio->raid_map[0]);
2327 ASSERT(logical + PAGE_SIZE <= rbio->bbio->raid_map[0] +
2328 rbio->stripe_len * rbio->nr_data);
2329 stripe_offset = (int)(logical - rbio->bbio->raid_map[0]);
2330 index = stripe_offset >> PAGE_SHIFT;
2331 rbio->bio_pages[index] = page;
2335 * We just scrub the parity that we have correct data on the same horizontal,
2336 * so we needn't allocate all pages for all the stripes.
2338 static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
2345 for_each_set_bit(bit, rbio->dbitmap, rbio->stripe_npages) {
2346 for (i = 0; i < rbio->real_stripes; i++) {
2347 index = i * rbio->stripe_npages + bit;
2348 if (rbio->stripe_pages[index])
2351 page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2354 rbio->stripe_pages[index] = page;
2360 static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
2363 struct btrfs_bio *bbio = rbio->bbio;
2364 void **pointers = rbio->finish_pointers;
2365 unsigned long *pbitmap = rbio->finish_pbitmap;
2366 int nr_data = rbio->nr_data;
2370 struct page *p_page = NULL;
2371 struct page *q_page = NULL;
2372 struct bio_list bio_list;
2377 bio_list_init(&bio_list);
2379 if (rbio->real_stripes - rbio->nr_data == 1)
2380 has_qstripe = false;
2381 else if (rbio->real_stripes - rbio->nr_data == 2)
2386 if (bbio->num_tgtdevs && bbio->tgtdev_map[rbio->scrubp]) {
2388 bitmap_copy(pbitmap, rbio->dbitmap, rbio->stripe_npages);
2392 * Because the higher layers(scrubber) are unlikely to
2393 * use this area of the disk again soon, so don't cache
2396 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
2401 p_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2404 SetPageUptodate(p_page);
2407 /* RAID6, allocate and map temp space for the Q stripe */
2408 q_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2410 __free_page(p_page);
2413 SetPageUptodate(q_page);
2414 pointers[rbio->real_stripes - 1] = kmap(q_page);
2417 atomic_set(&rbio->error, 0);
2419 /* Map the parity stripe just once */
2420 pointers[nr_data] = kmap(p_page);
2422 for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2425 /* first collect one page from each data stripe */
2426 for (stripe = 0; stripe < nr_data; stripe++) {
2427 p = page_in_rbio(rbio, stripe, pagenr, 0);
2428 pointers[stripe] = kmap(p);
2432 /* RAID6, call the library function to fill in our P/Q */
2433 raid6_call.gen_syndrome(rbio->real_stripes, PAGE_SIZE,
2437 copy_page(pointers[nr_data], pointers[0]);
2438 run_xor(pointers + 1, nr_data - 1, PAGE_SIZE);
2441 /* Check scrubbing parity and repair it */
2442 p = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2444 if (memcmp(parity, pointers[rbio->scrubp], PAGE_SIZE))
2445 copy_page(parity, pointers[rbio->scrubp]);
2447 /* Parity is right, needn't writeback */
2448 bitmap_clear(rbio->dbitmap, pagenr, 1);
2451 for (stripe = 0; stripe < nr_data; stripe++)
2452 kunmap(page_in_rbio(rbio, stripe, pagenr, 0));
2456 __free_page(p_page);
2459 __free_page(q_page);
2464 * time to start writing. Make bios for everything from the
2465 * higher layers (the bio_list in our rbio) and our p/q. Ignore
2468 for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2471 page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2472 ret = rbio_add_io_page(rbio, &bio_list,
2473 page, rbio->scrubp, pagenr, rbio->stripe_len);
2481 for_each_set_bit(pagenr, pbitmap, rbio->stripe_npages) {
2484 page = rbio_stripe_page(rbio, rbio->scrubp, pagenr);
2485 ret = rbio_add_io_page(rbio, &bio_list, page,
2486 bbio->tgtdev_map[rbio->scrubp],
2487 pagenr, rbio->stripe_len);
2493 nr_data = bio_list_size(&bio_list);
2495 /* Every parity is right */
2496 rbio_orig_end_io(rbio, BLK_STS_OK);
2500 atomic_set(&rbio->stripes_pending, nr_data);
2502 while ((bio = bio_list_pop(&bio_list))) {
2503 bio->bi_private = rbio;
2504 bio->bi_end_io = raid_write_end_io;
2505 bio->bi_opf = REQ_OP_WRITE;
2512 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2514 while ((bio = bio_list_pop(&bio_list)))
2518 static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
2520 if (stripe >= 0 && stripe < rbio->nr_data)
2526 * While we're doing the parity check and repair, we could have errors
2527 * in reading pages off the disk. This checks for errors and if we're
2528 * not able to read the page it'll trigger parity reconstruction. The
2529 * parity scrub will be finished after we've reconstructed the failed
2532 static void validate_rbio_for_parity_scrub(struct btrfs_raid_bio *rbio)
2534 if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
2537 if (rbio->faila >= 0 || rbio->failb >= 0) {
2538 int dfail = 0, failp = -1;
2540 if (is_data_stripe(rbio, rbio->faila))
2542 else if (is_parity_stripe(rbio->faila))
2543 failp = rbio->faila;
2545 if (is_data_stripe(rbio, rbio->failb))
2547 else if (is_parity_stripe(rbio->failb))
2548 failp = rbio->failb;
2551 * Because we can not use a scrubbing parity to repair
2552 * the data, so the capability of the repair is declined.
2553 * (In the case of RAID5, we can not repair anything)
2555 if (dfail > rbio->bbio->max_errors - 1)
2559 * If all data is good, only parity is correctly, just
2560 * repair the parity.
2563 finish_parity_scrub(rbio, 0);
2568 * Here means we got one corrupted data stripe and one
2569 * corrupted parity on RAID6, if the corrupted parity
2570 * is scrubbing parity, luckily, use the other one to repair
2571 * the data, or we can not repair the data stripe.
2573 if (failp != rbio->scrubp)
2576 __raid_recover_end_io(rbio);
2578 finish_parity_scrub(rbio, 1);
2583 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2587 * end io for the read phase of the rmw cycle. All the bios here are physical
2588 * stripe bios we've read from the disk so we can recalculate the parity of the
2591 * This will usually kick off finish_rmw once all the bios are read in, but it
2592 * may trigger parity reconstruction if we had any errors along the way
2594 static void raid56_parity_scrub_end_io(struct bio *bio)
2596 struct btrfs_raid_bio *rbio = bio->bi_private;
2599 fail_bio_stripe(rbio, bio);
2601 set_bio_pages_uptodate(bio);
2605 if (!atomic_dec_and_test(&rbio->stripes_pending))
2609 * this will normally call finish_rmw to start our write
2610 * but if there are any failed stripes we'll reconstruct
2613 validate_rbio_for_parity_scrub(rbio);
2616 static void raid56_parity_scrub_stripe(struct btrfs_raid_bio *rbio)
2618 int bios_to_read = 0;
2619 struct bio_list bio_list;
2625 bio_list_init(&bio_list);
2627 ret = alloc_rbio_essential_pages(rbio);
2631 atomic_set(&rbio->error, 0);
2633 * build a list of bios to read all the missing parts of this
2636 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
2637 for_each_set_bit(pagenr, rbio->dbitmap, rbio->stripe_npages) {
2640 * we want to find all the pages missing from
2641 * the rbio and read them from the disk. If
2642 * page_in_rbio finds a page in the bio list
2643 * we don't need to read it off the stripe.
2645 page = page_in_rbio(rbio, stripe, pagenr, 1);
2649 page = rbio_stripe_page(rbio, stripe, pagenr);
2651 * the bio cache may have handed us an uptodate
2652 * page. If so, be happy and use it
2654 if (PageUptodate(page))
2657 ret = rbio_add_io_page(rbio, &bio_list, page,
2658 stripe, pagenr, rbio->stripe_len);
2664 bios_to_read = bio_list_size(&bio_list);
2665 if (!bios_to_read) {
2667 * this can happen if others have merged with
2668 * us, it means there is nothing left to read.
2669 * But if there are missing devices it may not be
2670 * safe to do the full stripe write yet.
2676 * the bbio may be freed once we submit the last bio. Make sure
2677 * not to touch it after that
2679 atomic_set(&rbio->stripes_pending, bios_to_read);
2680 while ((bio = bio_list_pop(&bio_list))) {
2681 bio->bi_private = rbio;
2682 bio->bi_end_io = raid56_parity_scrub_end_io;
2683 bio->bi_opf = REQ_OP_READ;
2685 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
2689 /* the actual write will happen once the reads are done */
2693 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2695 while ((bio = bio_list_pop(&bio_list)))
2701 validate_rbio_for_parity_scrub(rbio);
2704 static void scrub_parity_work(struct btrfs_work *work)
2706 struct btrfs_raid_bio *rbio;
2708 rbio = container_of(work, struct btrfs_raid_bio, work);
2709 raid56_parity_scrub_stripe(rbio);
2712 void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
2714 if (!lock_stripe_add(rbio))
2715 start_async_work(rbio, scrub_parity_work);
2718 /* The following code is used for dev replace of a missing RAID 5/6 device. */
2720 struct btrfs_raid_bio *
2721 raid56_alloc_missing_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
2722 struct btrfs_bio *bbio, u64 length)
2724 struct btrfs_raid_bio *rbio;
2726 rbio = alloc_rbio(fs_info, bbio, length);
2730 rbio->operation = BTRFS_RBIO_REBUILD_MISSING;
2731 bio_list_add(&rbio->bio_list, bio);
2733 * This is a special bio which is used to hold the completion handler
2734 * and make the scrub rbio is similar to the other types
2736 ASSERT(!bio->bi_iter.bi_size);
2738 rbio->faila = find_logical_bio_stripe(rbio, bio);
2739 if (rbio->faila == -1) {
2746 * When we get bbio, we have already increased bio_counter, record it
2747 * so we can free it at rbio_orig_end_io()
2749 rbio->generic_bio_cnt = 1;
2754 void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio)
2756 if (!lock_stripe_add(rbio))
2757 start_async_work(rbio, read_rebuild_work);