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>
21 #include "async-thread.h"
23 /* set when additional merges to this rbio are not allowed */
24 #define RBIO_RMW_LOCKED_BIT 1
27 * set when this rbio is sitting in the hash, but it is just a cache
30 #define RBIO_CACHE_BIT 2
33 * set when it is safe to trust the stripe_pages for caching
35 #define RBIO_CACHE_READY_BIT 3
37 #define RBIO_CACHE_SIZE 1024
39 #define BTRFS_STRIPE_HASH_TABLE_BITS 11
41 /* Used by the raid56 code to lock stripes for read/modify/write */
42 struct btrfs_stripe_hash {
43 struct list_head hash_list;
47 /* Used by the raid56 code to lock stripes for read/modify/write */
48 struct btrfs_stripe_hash_table {
49 struct list_head stripe_cache;
50 spinlock_t cache_lock;
52 struct btrfs_stripe_hash table[];
56 * A bvec like structure to present a sector inside a page.
58 * Unlike bvec we don't need bvlen, as it's fixed to sectorsize.
62 unsigned int pgoff:24;
63 unsigned int uptodate:8;
66 static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
67 static noinline void finish_rmw(struct btrfs_raid_bio *rbio);
68 static void rmw_work(struct work_struct *work);
69 static void read_rebuild_work(struct work_struct *work);
70 static int fail_bio_stripe(struct btrfs_raid_bio *rbio, struct bio *bio);
71 static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed);
72 static void __free_raid_bio(struct btrfs_raid_bio *rbio);
73 static void index_rbio_pages(struct btrfs_raid_bio *rbio);
74 static int alloc_rbio_pages(struct btrfs_raid_bio *rbio);
76 static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
78 static void scrub_parity_work(struct work_struct *work);
80 static void start_async_work(struct btrfs_raid_bio *rbio, work_func_t work_func)
82 INIT_WORK(&rbio->work, work_func);
83 queue_work(rbio->bioc->fs_info->rmw_workers, &rbio->work);
87 * the stripe hash table is used for locking, and to collect
88 * bios in hopes of making a full stripe
90 int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info)
92 struct btrfs_stripe_hash_table *table;
93 struct btrfs_stripe_hash_table *x;
94 struct btrfs_stripe_hash *cur;
95 struct btrfs_stripe_hash *h;
96 int num_entries = 1 << BTRFS_STRIPE_HASH_TABLE_BITS;
99 if (info->stripe_hash_table)
103 * The table is large, starting with order 4 and can go as high as
104 * order 7 in case lock debugging is turned on.
106 * Try harder to allocate and fallback to vmalloc to lower the chance
107 * of a failing mount.
109 table = kvzalloc(struct_size(table, table, num_entries), GFP_KERNEL);
113 spin_lock_init(&table->cache_lock);
114 INIT_LIST_HEAD(&table->stripe_cache);
118 for (i = 0; i < num_entries; i++) {
120 INIT_LIST_HEAD(&cur->hash_list);
121 spin_lock_init(&cur->lock);
124 x = cmpxchg(&info->stripe_hash_table, NULL, table);
130 * caching an rbio means to copy anything from the
131 * bio_sectors array into the stripe_pages array. We
132 * use the page uptodate bit in the stripe cache array
133 * to indicate if it has valid data
135 * once the caching is done, we set the cache ready
138 static void cache_rbio_pages(struct btrfs_raid_bio *rbio)
143 ret = alloc_rbio_pages(rbio);
147 for (i = 0; i < rbio->nr_sectors; i++) {
148 /* Some range not covered by bio (partial write), skip it */
149 if (!rbio->bio_sectors[i].page)
152 ASSERT(rbio->stripe_sectors[i].page);
153 memcpy_page(rbio->stripe_sectors[i].page,
154 rbio->stripe_sectors[i].pgoff,
155 rbio->bio_sectors[i].page,
156 rbio->bio_sectors[i].pgoff,
157 rbio->bioc->fs_info->sectorsize);
158 rbio->stripe_sectors[i].uptodate = 1;
160 set_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
164 * we hash on the first logical address of the stripe
166 static int rbio_bucket(struct btrfs_raid_bio *rbio)
168 u64 num = rbio->bioc->raid_map[0];
171 * we shift down quite a bit. We're using byte
172 * addressing, and most of the lower bits are zeros.
173 * This tends to upset hash_64, and it consistently
174 * returns just one or two different values.
176 * shifting off the lower bits fixes things.
178 return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
181 static bool full_page_sectors_uptodate(struct btrfs_raid_bio *rbio,
182 unsigned int page_nr)
184 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
185 const u32 sectors_per_page = PAGE_SIZE / sectorsize;
188 ASSERT(page_nr < rbio->nr_pages);
190 for (i = sectors_per_page * page_nr;
191 i < sectors_per_page * page_nr + sectors_per_page;
193 if (!rbio->stripe_sectors[i].uptodate)
200 * Update the stripe_sectors[] array to use correct page and pgoff
202 * Should be called every time any page pointer in stripes_pages[] got modified.
204 static void index_stripe_sectors(struct btrfs_raid_bio *rbio)
206 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
210 for (i = 0, offset = 0; i < rbio->nr_sectors; i++, offset += sectorsize) {
211 int page_index = offset >> PAGE_SHIFT;
213 ASSERT(page_index < rbio->nr_pages);
214 rbio->stripe_sectors[i].page = rbio->stripe_pages[page_index];
215 rbio->stripe_sectors[i].pgoff = offset_in_page(offset);
219 static void steal_rbio_page(struct btrfs_raid_bio *src,
220 struct btrfs_raid_bio *dest, int page_nr)
222 const u32 sectorsize = src->bioc->fs_info->sectorsize;
223 const u32 sectors_per_page = PAGE_SIZE / sectorsize;
226 if (dest->stripe_pages[page_nr])
227 __free_page(dest->stripe_pages[page_nr]);
228 dest->stripe_pages[page_nr] = src->stripe_pages[page_nr];
229 src->stripe_pages[page_nr] = NULL;
231 /* Also update the sector->uptodate bits. */
232 for (i = sectors_per_page * page_nr;
233 i < sectors_per_page * page_nr + sectors_per_page; i++)
234 dest->stripe_sectors[i].uptodate = true;
238 * Stealing an rbio means taking all the uptodate pages from the stripe array
239 * in the source rbio and putting them into the destination rbio.
241 * This will also update the involved stripe_sectors[] which are referring to
244 static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
249 if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
252 for (i = 0; i < dest->nr_pages; i++) {
253 s = src->stripe_pages[i];
254 if (!s || !full_page_sectors_uptodate(src, i))
257 steal_rbio_page(src, dest, i);
259 index_stripe_sectors(dest);
260 index_stripe_sectors(src);
264 * merging means we take the bio_list from the victim and
265 * splice it into the destination. The victim should
266 * be discarded afterwards.
268 * must be called with dest->rbio_list_lock held
270 static void merge_rbio(struct btrfs_raid_bio *dest,
271 struct btrfs_raid_bio *victim)
273 bio_list_merge(&dest->bio_list, &victim->bio_list);
274 dest->bio_list_bytes += victim->bio_list_bytes;
275 /* Also inherit the bitmaps from @victim. */
276 bitmap_or(&dest->dbitmap, &victim->dbitmap, &dest->dbitmap,
277 dest->stripe_nsectors);
278 bio_list_init(&victim->bio_list);
282 * used to prune items that are in the cache. The caller
283 * must hold the hash table lock.
285 static void __remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
287 int bucket = rbio_bucket(rbio);
288 struct btrfs_stripe_hash_table *table;
289 struct btrfs_stripe_hash *h;
293 * check the bit again under the hash table lock.
295 if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
298 table = rbio->bioc->fs_info->stripe_hash_table;
299 h = table->table + bucket;
301 /* hold the lock for the bucket because we may be
302 * removing it from the hash table
307 * hold the lock for the bio list because we need
308 * to make sure the bio list is empty
310 spin_lock(&rbio->bio_list_lock);
312 if (test_and_clear_bit(RBIO_CACHE_BIT, &rbio->flags)) {
313 list_del_init(&rbio->stripe_cache);
314 table->cache_size -= 1;
317 /* if the bio list isn't empty, this rbio is
318 * still involved in an IO. We take it out
319 * of the cache list, and drop the ref that
320 * was held for the list.
322 * If the bio_list was empty, we also remove
323 * the rbio from the hash_table, and drop
324 * the corresponding ref
326 if (bio_list_empty(&rbio->bio_list)) {
327 if (!list_empty(&rbio->hash_list)) {
328 list_del_init(&rbio->hash_list);
329 refcount_dec(&rbio->refs);
330 BUG_ON(!list_empty(&rbio->plug_list));
335 spin_unlock(&rbio->bio_list_lock);
336 spin_unlock(&h->lock);
339 __free_raid_bio(rbio);
343 * prune a given rbio from the cache
345 static void remove_rbio_from_cache(struct btrfs_raid_bio *rbio)
347 struct btrfs_stripe_hash_table *table;
350 if (!test_bit(RBIO_CACHE_BIT, &rbio->flags))
353 table = rbio->bioc->fs_info->stripe_hash_table;
355 spin_lock_irqsave(&table->cache_lock, flags);
356 __remove_rbio_from_cache(rbio);
357 spin_unlock_irqrestore(&table->cache_lock, flags);
361 * remove everything in the cache
363 static void btrfs_clear_rbio_cache(struct btrfs_fs_info *info)
365 struct btrfs_stripe_hash_table *table;
367 struct btrfs_raid_bio *rbio;
369 table = info->stripe_hash_table;
371 spin_lock_irqsave(&table->cache_lock, flags);
372 while (!list_empty(&table->stripe_cache)) {
373 rbio = list_entry(table->stripe_cache.next,
374 struct btrfs_raid_bio,
376 __remove_rbio_from_cache(rbio);
378 spin_unlock_irqrestore(&table->cache_lock, flags);
382 * remove all cached entries and free the hash table
385 void btrfs_free_stripe_hash_table(struct btrfs_fs_info *info)
387 if (!info->stripe_hash_table)
389 btrfs_clear_rbio_cache(info);
390 kvfree(info->stripe_hash_table);
391 info->stripe_hash_table = NULL;
395 * insert an rbio into the stripe cache. It
396 * must have already been prepared by calling
399 * If this rbio was already cached, it gets
400 * moved to the front of the lru.
402 * If the size of the rbio cache is too big, we
405 static void cache_rbio(struct btrfs_raid_bio *rbio)
407 struct btrfs_stripe_hash_table *table;
410 if (!test_bit(RBIO_CACHE_READY_BIT, &rbio->flags))
413 table = rbio->bioc->fs_info->stripe_hash_table;
415 spin_lock_irqsave(&table->cache_lock, flags);
416 spin_lock(&rbio->bio_list_lock);
418 /* bump our ref if we were not in the list before */
419 if (!test_and_set_bit(RBIO_CACHE_BIT, &rbio->flags))
420 refcount_inc(&rbio->refs);
422 if (!list_empty(&rbio->stripe_cache)){
423 list_move(&rbio->stripe_cache, &table->stripe_cache);
425 list_add(&rbio->stripe_cache, &table->stripe_cache);
426 table->cache_size += 1;
429 spin_unlock(&rbio->bio_list_lock);
431 if (table->cache_size > RBIO_CACHE_SIZE) {
432 struct btrfs_raid_bio *found;
434 found = list_entry(table->stripe_cache.prev,
435 struct btrfs_raid_bio,
439 __remove_rbio_from_cache(found);
442 spin_unlock_irqrestore(&table->cache_lock, flags);
446 * helper function to run the xor_blocks api. It is only
447 * able to do MAX_XOR_BLOCKS at a time, so we need to
450 static void run_xor(void **pages, int src_cnt, ssize_t len)
454 void *dest = pages[src_cnt];
457 xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
458 xor_blocks(xor_src_cnt, len, dest, pages + src_off);
460 src_cnt -= xor_src_cnt;
461 src_off += xor_src_cnt;
466 * Returns true if the bio list inside this rbio covers an entire stripe (no
469 static int rbio_is_full(struct btrfs_raid_bio *rbio)
472 unsigned long size = rbio->bio_list_bytes;
475 spin_lock_irqsave(&rbio->bio_list_lock, flags);
476 if (size != rbio->nr_data * BTRFS_STRIPE_LEN)
478 BUG_ON(size > rbio->nr_data * BTRFS_STRIPE_LEN);
479 spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
485 * returns 1 if it is safe to merge two rbios together.
486 * The merging is safe if the two rbios correspond to
487 * the same stripe and if they are both going in the same
488 * direction (read vs write), and if neither one is
489 * locked for final IO
491 * The caller is responsible for locking such that
492 * rmw_locked is safe to test
494 static int rbio_can_merge(struct btrfs_raid_bio *last,
495 struct btrfs_raid_bio *cur)
497 if (test_bit(RBIO_RMW_LOCKED_BIT, &last->flags) ||
498 test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags))
502 * we can't merge with cached rbios, since the
503 * idea is that when we merge the destination
504 * rbio is going to run our IO for us. We can
505 * steal from cached rbios though, other functions
508 if (test_bit(RBIO_CACHE_BIT, &last->flags) ||
509 test_bit(RBIO_CACHE_BIT, &cur->flags))
512 if (last->bioc->raid_map[0] != cur->bioc->raid_map[0])
515 /* we can't merge with different operations */
516 if (last->operation != cur->operation)
519 * We've need read the full stripe from the drive.
520 * check and repair the parity and write the new results.
522 * We're not allowed to add any new bios to the
523 * bio list here, anyone else that wants to
524 * change this stripe needs to do their own rmw.
526 if (last->operation == BTRFS_RBIO_PARITY_SCRUB)
529 if (last->operation == BTRFS_RBIO_REBUILD_MISSING)
532 if (last->operation == BTRFS_RBIO_READ_REBUILD) {
533 int fa = last->faila;
534 int fb = last->failb;
535 int cur_fa = cur->faila;
536 int cur_fb = cur->failb;
538 if (last->faila >= last->failb) {
543 if (cur->faila >= cur->failb) {
548 if (fa != cur_fa || fb != cur_fb)
554 static unsigned int rbio_stripe_sector_index(const struct btrfs_raid_bio *rbio,
555 unsigned int stripe_nr,
556 unsigned int sector_nr)
558 ASSERT(stripe_nr < rbio->real_stripes);
559 ASSERT(sector_nr < rbio->stripe_nsectors);
561 return stripe_nr * rbio->stripe_nsectors + sector_nr;
564 /* Return a sector from rbio->stripe_sectors, not from the bio list */
565 static struct sector_ptr *rbio_stripe_sector(const struct btrfs_raid_bio *rbio,
566 unsigned int stripe_nr,
567 unsigned int sector_nr)
569 return &rbio->stripe_sectors[rbio_stripe_sector_index(rbio, stripe_nr,
573 /* Grab a sector inside P stripe */
574 static struct sector_ptr *rbio_pstripe_sector(const struct btrfs_raid_bio *rbio,
575 unsigned int sector_nr)
577 return rbio_stripe_sector(rbio, rbio->nr_data, sector_nr);
580 /* Grab a sector inside Q stripe, return NULL if not RAID6 */
581 static struct sector_ptr *rbio_qstripe_sector(const struct btrfs_raid_bio *rbio,
582 unsigned int sector_nr)
584 if (rbio->nr_data + 1 == rbio->real_stripes)
586 return rbio_stripe_sector(rbio, rbio->nr_data + 1, sector_nr);
590 * The first stripe in the table for a logical address
591 * has the lock. rbios are added in one of three ways:
593 * 1) Nobody has the stripe locked yet. The rbio is given
594 * the lock and 0 is returned. The caller must start the IO
597 * 2) Someone has the stripe locked, but we're able to merge
598 * with the lock owner. The rbio is freed and the IO will
599 * start automatically along with the existing rbio. 1 is returned.
601 * 3) Someone has the stripe locked, but we're not able to merge.
602 * The rbio is added to the lock owner's plug list, or merged into
603 * an rbio already on the plug list. When the lock owner unlocks,
604 * the next rbio on the list is run and the IO is started automatically.
607 * If we return 0, the caller still owns the rbio and must continue with
608 * IO submission. If we return 1, the caller must assume the rbio has
609 * already been freed.
611 static noinline int lock_stripe_add(struct btrfs_raid_bio *rbio)
613 struct btrfs_stripe_hash *h;
614 struct btrfs_raid_bio *cur;
615 struct btrfs_raid_bio *pending;
617 struct btrfs_raid_bio *freeit = NULL;
618 struct btrfs_raid_bio *cache_drop = NULL;
621 h = rbio->bioc->fs_info->stripe_hash_table->table + rbio_bucket(rbio);
623 spin_lock_irqsave(&h->lock, flags);
624 list_for_each_entry(cur, &h->hash_list, hash_list) {
625 if (cur->bioc->raid_map[0] != rbio->bioc->raid_map[0])
628 spin_lock(&cur->bio_list_lock);
630 /* Can we steal this cached rbio's pages? */
631 if (bio_list_empty(&cur->bio_list) &&
632 list_empty(&cur->plug_list) &&
633 test_bit(RBIO_CACHE_BIT, &cur->flags) &&
634 !test_bit(RBIO_RMW_LOCKED_BIT, &cur->flags)) {
635 list_del_init(&cur->hash_list);
636 refcount_dec(&cur->refs);
638 steal_rbio(cur, rbio);
640 spin_unlock(&cur->bio_list_lock);
645 /* Can we merge into the lock owner? */
646 if (rbio_can_merge(cur, rbio)) {
647 merge_rbio(cur, rbio);
648 spin_unlock(&cur->bio_list_lock);
656 * We couldn't merge with the running rbio, see if we can merge
657 * with the pending ones. We don't have to check for rmw_locked
658 * because there is no way they are inside finish_rmw right now
660 list_for_each_entry(pending, &cur->plug_list, plug_list) {
661 if (rbio_can_merge(pending, rbio)) {
662 merge_rbio(pending, rbio);
663 spin_unlock(&cur->bio_list_lock);
671 * No merging, put us on the tail of the plug list, our rbio
672 * will be started with the currently running rbio unlocks
674 list_add_tail(&rbio->plug_list, &cur->plug_list);
675 spin_unlock(&cur->bio_list_lock);
680 refcount_inc(&rbio->refs);
681 list_add(&rbio->hash_list, &h->hash_list);
683 spin_unlock_irqrestore(&h->lock, flags);
685 remove_rbio_from_cache(cache_drop);
687 __free_raid_bio(freeit);
692 * called as rmw or parity rebuild is completed. If the plug list has more
693 * rbios waiting for this stripe, the next one on the list will be started
695 static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
698 struct btrfs_stripe_hash *h;
702 bucket = rbio_bucket(rbio);
703 h = rbio->bioc->fs_info->stripe_hash_table->table + bucket;
705 if (list_empty(&rbio->plug_list))
708 spin_lock_irqsave(&h->lock, flags);
709 spin_lock(&rbio->bio_list_lock);
711 if (!list_empty(&rbio->hash_list)) {
713 * if we're still cached and there is no other IO
714 * to perform, just leave this rbio here for others
715 * to steal from later
717 if (list_empty(&rbio->plug_list) &&
718 test_bit(RBIO_CACHE_BIT, &rbio->flags)) {
720 clear_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
721 BUG_ON(!bio_list_empty(&rbio->bio_list));
725 list_del_init(&rbio->hash_list);
726 refcount_dec(&rbio->refs);
729 * we use the plug list to hold all the rbios
730 * waiting for the chance to lock this stripe.
731 * hand the lock over to one of them.
733 if (!list_empty(&rbio->plug_list)) {
734 struct btrfs_raid_bio *next;
735 struct list_head *head = rbio->plug_list.next;
737 next = list_entry(head, struct btrfs_raid_bio,
740 list_del_init(&rbio->plug_list);
742 list_add(&next->hash_list, &h->hash_list);
743 refcount_inc(&next->refs);
744 spin_unlock(&rbio->bio_list_lock);
745 spin_unlock_irqrestore(&h->lock, flags);
747 if (next->operation == BTRFS_RBIO_READ_REBUILD)
748 start_async_work(next, read_rebuild_work);
749 else if (next->operation == BTRFS_RBIO_REBUILD_MISSING) {
750 steal_rbio(rbio, next);
751 start_async_work(next, read_rebuild_work);
752 } else if (next->operation == BTRFS_RBIO_WRITE) {
753 steal_rbio(rbio, next);
754 start_async_work(next, rmw_work);
755 } else if (next->operation == BTRFS_RBIO_PARITY_SCRUB) {
756 steal_rbio(rbio, next);
757 start_async_work(next, scrub_parity_work);
764 spin_unlock(&rbio->bio_list_lock);
765 spin_unlock_irqrestore(&h->lock, flags);
769 remove_rbio_from_cache(rbio);
772 static void __free_raid_bio(struct btrfs_raid_bio *rbio)
776 if (!refcount_dec_and_test(&rbio->refs))
779 WARN_ON(!list_empty(&rbio->stripe_cache));
780 WARN_ON(!list_empty(&rbio->hash_list));
781 WARN_ON(!bio_list_empty(&rbio->bio_list));
783 for (i = 0; i < rbio->nr_pages; i++) {
784 if (rbio->stripe_pages[i]) {
785 __free_page(rbio->stripe_pages[i]);
786 rbio->stripe_pages[i] = NULL;
790 btrfs_put_bioc(rbio->bioc);
794 static void rbio_endio_bio_list(struct bio *cur, blk_status_t err)
801 cur->bi_status = err;
808 * this frees the rbio and runs through all the bios in the
809 * bio_list and calls end_io on them
811 static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, blk_status_t err)
813 struct bio *cur = bio_list_get(&rbio->bio_list);
817 * Clear the data bitmap, as the rbio may be cached for later usage.
818 * do this before before unlock_stripe() so there will be no new bio
821 bitmap_clear(&rbio->dbitmap, 0, rbio->stripe_nsectors);
824 * At this moment, rbio->bio_list is empty, however since rbio does not
825 * always have RBIO_RMW_LOCKED_BIT set and rbio is still linked on the
826 * hash list, rbio may be merged with others so that rbio->bio_list
828 * Once unlock_stripe() is done, rbio->bio_list will not be updated any
829 * more and we can call bio_endio() on all queued bios.
832 extra = bio_list_get(&rbio->bio_list);
833 __free_raid_bio(rbio);
835 rbio_endio_bio_list(cur, err);
837 rbio_endio_bio_list(extra, err);
841 * end io function used by finish_rmw. When we finally
842 * get here, we've written a full stripe
844 static void raid_write_end_io(struct bio *bio)
846 struct btrfs_raid_bio *rbio = bio->bi_private;
847 blk_status_t err = bio->bi_status;
851 fail_bio_stripe(rbio, bio);
855 if (!atomic_dec_and_test(&rbio->stripes_pending))
860 /* OK, we have read all the stripes we need to. */
861 max_errors = (rbio->operation == BTRFS_RBIO_PARITY_SCRUB) ?
862 0 : rbio->bioc->max_errors;
863 if (atomic_read(&rbio->error) > max_errors)
866 rbio_orig_end_io(rbio, err);
870 * Get a sector pointer specified by its @stripe_nr and @sector_nr
872 * @rbio: The raid bio
873 * @stripe_nr: Stripe number, valid range [0, real_stripe)
874 * @sector_nr: Sector number inside the stripe,
875 * valid range [0, stripe_nsectors)
876 * @bio_list_only: Whether to use sectors inside the bio list only.
878 * The read/modify/write code wants to reuse the original bio page as much
879 * as possible, and only use stripe_sectors as fallback.
881 static struct sector_ptr *sector_in_rbio(struct btrfs_raid_bio *rbio,
882 int stripe_nr, int sector_nr,
885 struct sector_ptr *sector;
888 ASSERT(stripe_nr >= 0 && stripe_nr < rbio->real_stripes);
889 ASSERT(sector_nr >= 0 && sector_nr < rbio->stripe_nsectors);
891 index = stripe_nr * rbio->stripe_nsectors + sector_nr;
892 ASSERT(index >= 0 && index < rbio->nr_sectors);
894 spin_lock_irq(&rbio->bio_list_lock);
895 sector = &rbio->bio_sectors[index];
896 if (sector->page || bio_list_only) {
897 /* Don't return sector without a valid page pointer */
900 spin_unlock_irq(&rbio->bio_list_lock);
903 spin_unlock_irq(&rbio->bio_list_lock);
905 return &rbio->stripe_sectors[index];
909 * allocation and initial setup for the btrfs_raid_bio. Not
910 * this does not allocate any pages for rbio->pages.
912 static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
913 struct btrfs_io_context *bioc)
915 const unsigned int real_stripes = bioc->num_stripes - bioc->num_tgtdevs;
916 const unsigned int stripe_npages = BTRFS_STRIPE_LEN >> PAGE_SHIFT;
917 const unsigned int num_pages = stripe_npages * real_stripes;
918 const unsigned int stripe_nsectors =
919 BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits;
920 const unsigned int num_sectors = stripe_nsectors * real_stripes;
921 struct btrfs_raid_bio *rbio;
924 /* PAGE_SIZE must also be aligned to sectorsize for subpage support */
925 ASSERT(IS_ALIGNED(PAGE_SIZE, fs_info->sectorsize));
927 * Our current stripe len should be fixed to 64k thus stripe_nsectors
928 * (at most 16) should be no larger than BITS_PER_LONG.
930 ASSERT(stripe_nsectors <= BITS_PER_LONG);
932 rbio = kzalloc(sizeof(*rbio) +
933 sizeof(*rbio->stripe_pages) * num_pages +
934 sizeof(*rbio->bio_sectors) * num_sectors +
935 sizeof(*rbio->stripe_sectors) * num_sectors +
936 sizeof(*rbio->finish_pointers) * real_stripes,
939 return ERR_PTR(-ENOMEM);
941 bio_list_init(&rbio->bio_list);
942 INIT_LIST_HEAD(&rbio->plug_list);
943 spin_lock_init(&rbio->bio_list_lock);
944 INIT_LIST_HEAD(&rbio->stripe_cache);
945 INIT_LIST_HEAD(&rbio->hash_list);
946 btrfs_get_bioc(bioc);
948 rbio->nr_pages = num_pages;
949 rbio->nr_sectors = num_sectors;
950 rbio->real_stripes = real_stripes;
951 rbio->stripe_npages = stripe_npages;
952 rbio->stripe_nsectors = stripe_nsectors;
955 refcount_set(&rbio->refs, 1);
956 atomic_set(&rbio->error, 0);
957 atomic_set(&rbio->stripes_pending, 0);
960 * The stripe_pages, bio_sectors, etc arrays point to the extra memory
961 * we allocated past the end of the rbio.
964 #define CONSUME_ALLOC(ptr, count) do { \
966 p = (unsigned char *)p + sizeof(*(ptr)) * (count); \
968 CONSUME_ALLOC(rbio->stripe_pages, num_pages);
969 CONSUME_ALLOC(rbio->bio_sectors, num_sectors);
970 CONSUME_ALLOC(rbio->stripe_sectors, num_sectors);
971 CONSUME_ALLOC(rbio->finish_pointers, real_stripes);
974 ASSERT(btrfs_nr_parity_stripes(bioc->map_type));
975 rbio->nr_data = real_stripes - btrfs_nr_parity_stripes(bioc->map_type);
980 /* allocate pages for all the stripes in the bio, including parity */
981 static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
985 ret = btrfs_alloc_page_array(rbio->nr_pages, rbio->stripe_pages);
988 /* Mapping all sectors */
989 index_stripe_sectors(rbio);
993 /* only allocate pages for p/q stripes */
994 static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
996 const int data_pages = rbio->nr_data * rbio->stripe_npages;
999 ret = btrfs_alloc_page_array(rbio->nr_pages - data_pages,
1000 rbio->stripe_pages + data_pages);
1004 index_stripe_sectors(rbio);
1009 * Add a single sector @sector into our list of bios for IO.
1011 * Return 0 if everything went well.
1012 * Return <0 for error.
1014 static int rbio_add_io_sector(struct btrfs_raid_bio *rbio,
1015 struct bio_list *bio_list,
1016 struct sector_ptr *sector,
1017 unsigned int stripe_nr,
1018 unsigned int sector_nr,
1021 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
1022 struct bio *last = bio_list->tail;
1025 struct btrfs_io_stripe *stripe;
1029 * Note: here stripe_nr has taken device replace into consideration,
1030 * thus it can be larger than rbio->real_stripe.
1031 * So here we check against bioc->num_stripes, not rbio->real_stripes.
1033 ASSERT(stripe_nr >= 0 && stripe_nr < rbio->bioc->num_stripes);
1034 ASSERT(sector_nr >= 0 && sector_nr < rbio->stripe_nsectors);
1035 ASSERT(sector->page);
1037 stripe = &rbio->bioc->stripes[stripe_nr];
1038 disk_start = stripe->physical + sector_nr * sectorsize;
1040 /* if the device is missing, just fail this stripe */
1041 if (!stripe->dev->bdev)
1042 return fail_rbio_index(rbio, stripe_nr);
1044 /* see if we can add this page onto our existing bio */
1046 u64 last_end = last->bi_iter.bi_sector << 9;
1047 last_end += last->bi_iter.bi_size;
1050 * we can't merge these if they are from different
1051 * devices or if they are not contiguous
1053 if (last_end == disk_start && !last->bi_status &&
1054 last->bi_bdev == stripe->dev->bdev) {
1055 ret = bio_add_page(last, sector->page, sectorsize,
1057 if (ret == sectorsize)
1062 /* put a new bio on the list */
1063 bio = bio_alloc(stripe->dev->bdev,
1064 max(BTRFS_STRIPE_LEN >> PAGE_SHIFT, 1),
1066 bio->bi_iter.bi_sector = disk_start >> 9;
1067 bio->bi_private = rbio;
1069 bio_add_page(bio, sector->page, sectorsize, sector->pgoff);
1070 bio_list_add(bio_list, bio);
1075 * while we're doing the read/modify/write cycle, we could
1076 * have errors in reading pages off the disk. This checks
1077 * for errors and if we're not able to read the page it'll
1078 * trigger parity reconstruction. The rmw will be finished
1079 * after we've reconstructed the failed stripes
1081 static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
1083 if (rbio->faila >= 0 || rbio->failb >= 0) {
1084 BUG_ON(rbio->faila == rbio->real_stripes - 1);
1085 __raid56_parity_recover(rbio);
1091 static void index_one_bio(struct btrfs_raid_bio *rbio, struct bio *bio)
1093 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
1094 struct bio_vec bvec;
1095 struct bvec_iter iter;
1096 u32 offset = (bio->bi_iter.bi_sector << SECTOR_SHIFT) -
1097 rbio->bioc->raid_map[0];
1099 bio_for_each_segment(bvec, bio, iter) {
1102 for (bvec_offset = 0; bvec_offset < bvec.bv_len;
1103 bvec_offset += sectorsize, offset += sectorsize) {
1104 int index = offset / sectorsize;
1105 struct sector_ptr *sector = &rbio->bio_sectors[index];
1107 sector->page = bvec.bv_page;
1108 sector->pgoff = bvec.bv_offset + bvec_offset;
1109 ASSERT(sector->pgoff < PAGE_SIZE);
1115 * helper function to walk our bio list and populate the bio_pages array with
1116 * the result. This seems expensive, but it is faster than constantly
1117 * searching through the bio list as we setup the IO in finish_rmw or stripe
1120 * This must be called before you trust the answers from page_in_rbio
1122 static void index_rbio_pages(struct btrfs_raid_bio *rbio)
1126 spin_lock_irq(&rbio->bio_list_lock);
1127 bio_list_for_each(bio, &rbio->bio_list)
1128 index_one_bio(rbio, bio);
1130 spin_unlock_irq(&rbio->bio_list_lock);
1133 static void bio_get_trace_info(struct btrfs_raid_bio *rbio, struct bio *bio,
1134 struct raid56_bio_trace_info *trace_info)
1136 const struct btrfs_io_context *bioc = rbio->bioc;
1141 /* We rely on bio->bi_bdev to find the stripe number. */
1145 for (i = 0; i < bioc->num_stripes; i++) {
1146 if (bio->bi_bdev != bioc->stripes[i].dev->bdev)
1148 trace_info->stripe_nr = i;
1149 trace_info->devid = bioc->stripes[i].dev->devid;
1150 trace_info->offset = (bio->bi_iter.bi_sector << SECTOR_SHIFT) -
1151 bioc->stripes[i].physical;
1156 trace_info->devid = -1;
1157 trace_info->offset = -1;
1158 trace_info->stripe_nr = -1;
1162 * this is called from one of two situations. We either
1163 * have a full stripe from the higher layers, or we've read all
1164 * the missing bits off disk.
1166 * This will calculate the parity and then send down any
1169 static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1171 struct btrfs_io_context *bioc = rbio->bioc;
1172 const u32 sectorsize = bioc->fs_info->sectorsize;
1173 void **pointers = rbio->finish_pointers;
1174 int nr_data = rbio->nr_data;
1175 /* The total sector number inside the full stripe. */
1176 int total_sector_nr;
1178 /* Sector number inside a stripe. */
1181 struct bio_list bio_list;
1185 bio_list_init(&bio_list);
1187 if (rbio->real_stripes - rbio->nr_data == 1)
1188 has_qstripe = false;
1189 else if (rbio->real_stripes - rbio->nr_data == 2)
1194 /* We should have at least one data sector. */
1195 ASSERT(bitmap_weight(&rbio->dbitmap, rbio->stripe_nsectors));
1197 /* at this point we either have a full stripe,
1198 * or we've read the full stripe from the drive.
1199 * recalculate the parity and write the new results.
1201 * We're not allowed to add any new bios to the
1202 * bio list here, anyone else that wants to
1203 * change this stripe needs to do their own rmw.
1205 spin_lock_irq(&rbio->bio_list_lock);
1206 set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1207 spin_unlock_irq(&rbio->bio_list_lock);
1209 atomic_set(&rbio->error, 0);
1212 * now that we've set rmw_locked, run through the
1213 * bio list one last time and map the page pointers
1215 * We don't cache full rbios because we're assuming
1216 * the higher layers are unlikely to use this area of
1217 * the disk again soon. If they do use it again,
1218 * hopefully they will send another full bio.
1220 index_rbio_pages(rbio);
1221 if (!rbio_is_full(rbio))
1222 cache_rbio_pages(rbio);
1224 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
1226 for (sectornr = 0; sectornr < rbio->stripe_nsectors; sectornr++) {
1227 struct sector_ptr *sector;
1229 /* First collect one sector from each data stripe */
1230 for (stripe = 0; stripe < nr_data; stripe++) {
1231 sector = sector_in_rbio(rbio, stripe, sectornr, 0);
1232 pointers[stripe] = kmap_local_page(sector->page) +
1236 /* Then add the parity stripe */
1237 sector = rbio_pstripe_sector(rbio, sectornr);
1238 sector->uptodate = 1;
1239 pointers[stripe++] = kmap_local_page(sector->page) + sector->pgoff;
1243 * RAID6, add the qstripe and call the library function
1244 * to fill in our p/q
1246 sector = rbio_qstripe_sector(rbio, sectornr);
1247 sector->uptodate = 1;
1248 pointers[stripe++] = kmap_local_page(sector->page) +
1251 raid6_call.gen_syndrome(rbio->real_stripes, sectorsize,
1255 memcpy(pointers[nr_data], pointers[0], sectorsize);
1256 run_xor(pointers + 1, nr_data - 1, sectorsize);
1258 for (stripe = stripe - 1; stripe >= 0; stripe--)
1259 kunmap_local(pointers[stripe]);
1263 * Start writing. Make bios for everything from the higher layers (the
1264 * bio_list in our rbio) and our P/Q. Ignore everything else.
1266 for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
1267 total_sector_nr++) {
1268 struct sector_ptr *sector;
1270 stripe = total_sector_nr / rbio->stripe_nsectors;
1271 sectornr = total_sector_nr % rbio->stripe_nsectors;
1273 /* This vertical stripe has no data, skip it. */
1274 if (!test_bit(sectornr, &rbio->dbitmap))
1277 if (stripe < rbio->nr_data) {
1278 sector = sector_in_rbio(rbio, stripe, sectornr, 1);
1282 sector = rbio_stripe_sector(rbio, stripe, sectornr);
1285 ret = rbio_add_io_sector(rbio, &bio_list, sector, stripe,
1286 sectornr, REQ_OP_WRITE);
1291 if (likely(!bioc->num_tgtdevs))
1294 for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
1295 total_sector_nr++) {
1296 struct sector_ptr *sector;
1298 stripe = total_sector_nr / rbio->stripe_nsectors;
1299 sectornr = total_sector_nr % rbio->stripe_nsectors;
1301 if (!bioc->tgtdev_map[stripe]) {
1303 * We can skip the whole stripe completely, note
1304 * total_sector_nr will be increased by one anyway.
1306 ASSERT(sectornr == 0);
1307 total_sector_nr += rbio->stripe_nsectors - 1;
1311 /* This vertical stripe has no data, skip it. */
1312 if (!test_bit(sectornr, &rbio->dbitmap))
1315 if (stripe < rbio->nr_data) {
1316 sector = sector_in_rbio(rbio, stripe, sectornr, 1);
1320 sector = rbio_stripe_sector(rbio, stripe, sectornr);
1323 ret = rbio_add_io_sector(rbio, &bio_list, sector,
1324 rbio->bioc->tgtdev_map[stripe],
1325 sectornr, REQ_OP_WRITE);
1331 atomic_set(&rbio->stripes_pending, bio_list_size(&bio_list));
1332 BUG_ON(atomic_read(&rbio->stripes_pending) == 0);
1334 while ((bio = bio_list_pop(&bio_list))) {
1335 bio->bi_end_io = raid_write_end_io;
1337 if (trace_raid56_write_stripe_enabled()) {
1338 struct raid56_bio_trace_info trace_info = { 0 };
1340 bio_get_trace_info(rbio, bio, &trace_info);
1341 trace_raid56_write_stripe(rbio, bio, &trace_info);
1348 rbio_orig_end_io(rbio, BLK_STS_IOERR);
1350 while ((bio = bio_list_pop(&bio_list)))
1355 * helper to find the stripe number for a given bio. Used to figure out which
1356 * stripe has failed. This expects the bio to correspond to a physical disk,
1357 * so it looks up based on physical sector numbers.
1359 static int find_bio_stripe(struct btrfs_raid_bio *rbio,
1362 u64 physical = bio->bi_iter.bi_sector;
1364 struct btrfs_io_stripe *stripe;
1368 for (i = 0; i < rbio->bioc->num_stripes; i++) {
1369 stripe = &rbio->bioc->stripes[i];
1370 if (in_range(physical, stripe->physical, BTRFS_STRIPE_LEN) &&
1371 stripe->dev->bdev && bio->bi_bdev == stripe->dev->bdev) {
1379 * helper to find the stripe number for a given
1380 * bio (before mapping). Used to figure out which stripe has
1381 * failed. This looks up based on logical block numbers.
1383 static int find_logical_bio_stripe(struct btrfs_raid_bio *rbio,
1386 u64 logical = bio->bi_iter.bi_sector << 9;
1389 for (i = 0; i < rbio->nr_data; i++) {
1390 u64 stripe_start = rbio->bioc->raid_map[i];
1392 if (in_range(logical, stripe_start, BTRFS_STRIPE_LEN))
1399 * returns -EIO if we had too many failures
1401 static int fail_rbio_index(struct btrfs_raid_bio *rbio, int failed)
1403 unsigned long flags;
1406 spin_lock_irqsave(&rbio->bio_list_lock, flags);
1408 /* we already know this stripe is bad, move on */
1409 if (rbio->faila == failed || rbio->failb == failed)
1412 if (rbio->faila == -1) {
1413 /* first failure on this rbio */
1414 rbio->faila = failed;
1415 atomic_inc(&rbio->error);
1416 } else if (rbio->failb == -1) {
1417 /* second failure on this rbio */
1418 rbio->failb = failed;
1419 atomic_inc(&rbio->error);
1424 spin_unlock_irqrestore(&rbio->bio_list_lock, flags);
1430 * helper to fail a stripe based on a physical disk
1433 static int fail_bio_stripe(struct btrfs_raid_bio *rbio,
1436 int failed = find_bio_stripe(rbio, bio);
1441 return fail_rbio_index(rbio, failed);
1445 * For subpage case, we can no longer set page Uptodate directly for
1446 * stripe_pages[], thus we need to locate the sector.
1448 static struct sector_ptr *find_stripe_sector(struct btrfs_raid_bio *rbio,
1454 for (i = 0; i < rbio->nr_sectors; i++) {
1455 struct sector_ptr *sector = &rbio->stripe_sectors[i];
1457 if (sector->page == page && sector->pgoff == pgoff)
1464 * this sets each page in the bio uptodate. It should only be used on private
1465 * rbio pages, nothing that comes in from the higher layers
1467 static void set_bio_pages_uptodate(struct btrfs_raid_bio *rbio, struct bio *bio)
1469 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
1470 struct bio_vec *bvec;
1471 struct bvec_iter_all iter_all;
1473 ASSERT(!bio_flagged(bio, BIO_CLONED));
1475 bio_for_each_segment_all(bvec, bio, iter_all) {
1476 struct sector_ptr *sector;
1479 for (pgoff = bvec->bv_offset; pgoff - bvec->bv_offset < bvec->bv_len;
1480 pgoff += sectorsize) {
1481 sector = find_stripe_sector(rbio, bvec->bv_page, pgoff);
1484 sector->uptodate = 1;
1489 static void raid56_bio_end_io(struct bio *bio)
1491 struct btrfs_raid_bio *rbio = bio->bi_private;
1494 fail_bio_stripe(rbio, bio);
1496 set_bio_pages_uptodate(rbio, bio);
1500 if (atomic_dec_and_test(&rbio->stripes_pending))
1501 queue_work(rbio->bioc->fs_info->endio_raid56_workers,
1502 &rbio->end_io_work);
1506 * End io handler for the read phase of the RMW cycle. All the bios here are
1507 * physical stripe bios we've read from the disk so we can recalculate the
1508 * parity of the stripe.
1510 * This will usually kick off finish_rmw once all the bios are read in, but it
1511 * may trigger parity reconstruction if we had any errors along the way
1513 static void raid56_rmw_end_io_work(struct work_struct *work)
1515 struct btrfs_raid_bio *rbio =
1516 container_of(work, struct btrfs_raid_bio, end_io_work);
1518 if (atomic_read(&rbio->error) > rbio->bioc->max_errors) {
1519 rbio_orig_end_io(rbio, BLK_STS_IOERR);
1524 * This will normally call finish_rmw to start our write but if there
1525 * are any failed stripes we'll reconstruct from parity first.
1527 validate_rbio_for_rmw(rbio);
1531 * the stripe must be locked by the caller. It will
1532 * unlock after all the writes are done
1534 static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
1536 int bios_to_read = 0;
1537 struct bio_list bio_list;
1538 const int nr_data_sectors = rbio->stripe_nsectors * rbio->nr_data;
1540 int total_sector_nr;
1543 bio_list_init(&bio_list);
1545 ret = alloc_rbio_pages(rbio);
1549 index_rbio_pages(rbio);
1551 atomic_set(&rbio->error, 0);
1552 /* Build a list of bios to read all the missing data sectors. */
1553 for (total_sector_nr = 0; total_sector_nr < nr_data_sectors;
1554 total_sector_nr++) {
1555 struct sector_ptr *sector;
1556 int stripe = total_sector_nr / rbio->stripe_nsectors;
1557 int sectornr = total_sector_nr % rbio->stripe_nsectors;
1560 * We want to find all the sectors missing from the rbio and
1561 * read them from the disk. If sector_in_rbio() finds a page
1562 * in the bio list we don't need to read it off the stripe.
1564 sector = sector_in_rbio(rbio, stripe, sectornr, 1);
1568 sector = rbio_stripe_sector(rbio, stripe, sectornr);
1570 * The bio cache may have handed us an uptodate page. If so,
1573 if (sector->uptodate)
1576 ret = rbio_add_io_sector(rbio, &bio_list, sector,
1577 stripe, sectornr, REQ_OP_READ);
1582 bios_to_read = bio_list_size(&bio_list);
1583 if (!bios_to_read) {
1585 * this can happen if others have merged with
1586 * us, it means there is nothing left to read.
1587 * But if there are missing devices it may not be
1588 * safe to do the full stripe write yet.
1594 * The bioc may be freed once we submit the last bio. Make sure not to
1595 * touch it after that.
1597 atomic_set(&rbio->stripes_pending, bios_to_read);
1598 INIT_WORK(&rbio->end_io_work, raid56_rmw_end_io_work);
1599 while ((bio = bio_list_pop(&bio_list))) {
1600 bio->bi_end_io = raid56_bio_end_io;
1602 if (trace_raid56_read_partial_enabled()) {
1603 struct raid56_bio_trace_info trace_info = { 0 };
1605 bio_get_trace_info(rbio, bio, &trace_info);
1606 trace_raid56_read_partial(rbio, bio, &trace_info);
1610 /* the actual write will happen once the reads are done */
1614 rbio_orig_end_io(rbio, BLK_STS_IOERR);
1616 while ((bio = bio_list_pop(&bio_list)))
1622 validate_rbio_for_rmw(rbio);
1627 * if the upper layers pass in a full stripe, we thank them by only allocating
1628 * enough pages to hold the parity, and sending it all down quickly.
1630 static int full_stripe_write(struct btrfs_raid_bio *rbio)
1634 ret = alloc_rbio_parity_pages(rbio);
1638 ret = lock_stripe_add(rbio);
1645 * partial stripe writes get handed over to async helpers.
1646 * We're really hoping to merge a few more writes into this
1647 * rbio before calculating new parity
1649 static int partial_stripe_write(struct btrfs_raid_bio *rbio)
1653 ret = lock_stripe_add(rbio);
1655 start_async_work(rbio, rmw_work);
1660 * sometimes while we were reading from the drive to
1661 * recalculate parity, enough new bios come into create
1662 * a full stripe. So we do a check here to see if we can
1663 * go directly to finish_rmw
1665 static int __raid56_parity_write(struct btrfs_raid_bio *rbio)
1667 /* head off into rmw land if we don't have a full stripe */
1668 if (!rbio_is_full(rbio))
1669 return partial_stripe_write(rbio);
1670 return full_stripe_write(rbio);
1674 * We use plugging call backs to collect full stripes.
1675 * Any time we get a partial stripe write while plugged
1676 * we collect it into a list. When the unplug comes down,
1677 * we sort the list by logical block number and merge
1678 * everything we can into the same rbios
1680 struct btrfs_plug_cb {
1681 struct blk_plug_cb cb;
1682 struct btrfs_fs_info *info;
1683 struct list_head rbio_list;
1684 struct work_struct work;
1688 * rbios on the plug list are sorted for easier merging.
1690 static int plug_cmp(void *priv, const struct list_head *a,
1691 const struct list_head *b)
1693 const struct btrfs_raid_bio *ra = container_of(a, struct btrfs_raid_bio,
1695 const struct btrfs_raid_bio *rb = container_of(b, struct btrfs_raid_bio,
1697 u64 a_sector = ra->bio_list.head->bi_iter.bi_sector;
1698 u64 b_sector = rb->bio_list.head->bi_iter.bi_sector;
1700 if (a_sector < b_sector)
1702 if (a_sector > b_sector)
1707 static void run_plug(struct btrfs_plug_cb *plug)
1709 struct btrfs_raid_bio *cur;
1710 struct btrfs_raid_bio *last = NULL;
1713 * sort our plug list then try to merge
1714 * everything we can in hopes of creating full
1717 list_sort(NULL, &plug->rbio_list, plug_cmp);
1718 while (!list_empty(&plug->rbio_list)) {
1719 cur = list_entry(plug->rbio_list.next,
1720 struct btrfs_raid_bio, plug_list);
1721 list_del_init(&cur->plug_list);
1723 if (rbio_is_full(cur)) {
1726 /* we have a full stripe, send it down */
1727 ret = full_stripe_write(cur);
1732 if (rbio_can_merge(last, cur)) {
1733 merge_rbio(last, cur);
1734 __free_raid_bio(cur);
1738 __raid56_parity_write(last);
1743 __raid56_parity_write(last);
1749 * if the unplug comes from schedule, we have to push the
1750 * work off to a helper thread
1752 static void unplug_work(struct work_struct *work)
1754 struct btrfs_plug_cb *plug;
1755 plug = container_of(work, struct btrfs_plug_cb, work);
1759 static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
1761 struct btrfs_plug_cb *plug;
1762 plug = container_of(cb, struct btrfs_plug_cb, cb);
1764 if (from_schedule) {
1765 INIT_WORK(&plug->work, unplug_work);
1766 queue_work(plug->info->rmw_workers, &plug->work);
1772 /* Add the original bio into rbio->bio_list, and update rbio::dbitmap. */
1773 static void rbio_add_bio(struct btrfs_raid_bio *rbio, struct bio *orig_bio)
1775 const struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
1776 const u64 orig_logical = orig_bio->bi_iter.bi_sector << SECTOR_SHIFT;
1777 const u64 full_stripe_start = rbio->bioc->raid_map[0];
1778 const u32 orig_len = orig_bio->bi_iter.bi_size;
1779 const u32 sectorsize = fs_info->sectorsize;
1782 ASSERT(orig_logical >= full_stripe_start &&
1783 orig_logical + orig_len <= full_stripe_start +
1784 rbio->nr_data * BTRFS_STRIPE_LEN);
1786 bio_list_add(&rbio->bio_list, orig_bio);
1787 rbio->bio_list_bytes += orig_bio->bi_iter.bi_size;
1789 /* Update the dbitmap. */
1790 for (cur_logical = orig_logical; cur_logical < orig_logical + orig_len;
1791 cur_logical += sectorsize) {
1792 int bit = ((u32)(cur_logical - full_stripe_start) >>
1793 fs_info->sectorsize_bits) % rbio->stripe_nsectors;
1795 set_bit(bit, &rbio->dbitmap);
1800 * our main entry point for writes from the rest of the FS.
1802 void raid56_parity_write(struct bio *bio, struct btrfs_io_context *bioc)
1804 struct btrfs_fs_info *fs_info = bioc->fs_info;
1805 struct btrfs_raid_bio *rbio;
1806 struct btrfs_plug_cb *plug = NULL;
1807 struct blk_plug_cb *cb;
1810 rbio = alloc_rbio(fs_info, bioc);
1812 ret = PTR_ERR(rbio);
1815 rbio->operation = BTRFS_RBIO_WRITE;
1816 rbio_add_bio(rbio, bio);
1819 * don't plug on full rbios, just get them out the door
1820 * as quickly as we can
1822 if (rbio_is_full(rbio)) {
1823 ret = full_stripe_write(rbio);
1825 __free_raid_bio(rbio);
1831 cb = blk_check_plugged(btrfs_raid_unplug, fs_info, sizeof(*plug));
1833 plug = container_of(cb, struct btrfs_plug_cb, cb);
1835 plug->info = fs_info;
1836 INIT_LIST_HEAD(&plug->rbio_list);
1838 list_add_tail(&rbio->plug_list, &plug->rbio_list);
1840 ret = __raid56_parity_write(rbio);
1842 __free_raid_bio(rbio);
1850 bio->bi_status = errno_to_blk_status(ret);
1855 * all parity reconstruction happens here. We've read in everything
1856 * we can find from the drives and this does the heavy lifting of
1857 * sorting the good from the bad.
1859 static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
1861 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
1862 int sectornr, stripe;
1865 int faila = -1, failb = -1;
1870 * This array stores the pointer for each sector, thus it has the extra
1871 * pgoff value added from each sector
1873 pointers = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
1875 err = BLK_STS_RESOURCE;
1880 * Store copy of pointers that does not get reordered during
1881 * reconstruction so that kunmap_local works.
1883 unmap_array = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
1885 err = BLK_STS_RESOURCE;
1886 goto cleanup_pointers;
1889 faila = rbio->faila;
1890 failb = rbio->failb;
1892 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1893 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
1894 spin_lock_irq(&rbio->bio_list_lock);
1895 set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
1896 spin_unlock_irq(&rbio->bio_list_lock);
1899 index_rbio_pages(rbio);
1901 for (sectornr = 0; sectornr < rbio->stripe_nsectors; sectornr++) {
1902 struct sector_ptr *sector;
1905 * Now we just use bitmap to mark the horizontal stripes in
1906 * which we have data when doing parity scrub.
1908 if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB &&
1909 !test_bit(sectornr, &rbio->dbitmap))
1913 * Setup our array of pointers with sectors from each stripe
1915 * NOTE: store a duplicate array of pointers to preserve the
1918 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1920 * If we're rebuilding a read, we have to use
1921 * pages from the bio list
1923 if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
1924 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
1925 (stripe == faila || stripe == failb)) {
1926 sector = sector_in_rbio(rbio, stripe, sectornr, 0);
1928 sector = rbio_stripe_sector(rbio, stripe, sectornr);
1930 ASSERT(sector->page);
1931 pointers[stripe] = kmap_local_page(sector->page) +
1933 unmap_array[stripe] = pointers[stripe];
1936 /* All raid6 handling here */
1937 if (rbio->bioc->map_type & BTRFS_BLOCK_GROUP_RAID6) {
1938 /* Single failure, rebuild from parity raid5 style */
1940 if (faila == rbio->nr_data) {
1942 * Just the P stripe has failed, without
1943 * a bad data or Q stripe.
1944 * TODO, we should redo the xor here.
1946 err = BLK_STS_IOERR;
1950 * a single failure in raid6 is rebuilt
1951 * in the pstripe code below
1956 /* make sure our ps and qs are in order */
1960 /* if the q stripe is failed, do a pstripe reconstruction
1962 * If both the q stripe and the P stripe are failed, we're
1963 * here due to a crc mismatch and we can't give them the
1966 if (rbio->bioc->raid_map[failb] == RAID6_Q_STRIPE) {
1967 if (rbio->bioc->raid_map[faila] ==
1969 err = BLK_STS_IOERR;
1973 * otherwise we have one bad data stripe and
1974 * a good P stripe. raid5!
1979 if (rbio->bioc->raid_map[failb] == RAID5_P_STRIPE) {
1980 raid6_datap_recov(rbio->real_stripes,
1981 sectorsize, faila, pointers);
1983 raid6_2data_recov(rbio->real_stripes,
1984 sectorsize, faila, failb,
1990 /* rebuild from P stripe here (raid5 or raid6) */
1991 BUG_ON(failb != -1);
1993 /* Copy parity block into failed block to start with */
1994 memcpy(pointers[faila], pointers[rbio->nr_data], sectorsize);
1996 /* rearrange the pointer array */
1997 p = pointers[faila];
1998 for (stripe = faila; stripe < rbio->nr_data - 1; stripe++)
1999 pointers[stripe] = pointers[stripe + 1];
2000 pointers[rbio->nr_data - 1] = p;
2002 /* xor in the rest */
2003 run_xor(pointers, rbio->nr_data - 1, sectorsize);
2005 /* if we're doing this rebuild as part of an rmw, go through
2006 * and set all of our private rbio pages in the
2007 * failed stripes as uptodate. This way finish_rmw will
2008 * know they can be trusted. If this was a read reconstruction,
2009 * other endio functions will fiddle the uptodate bits
2011 if (rbio->operation == BTRFS_RBIO_WRITE) {
2012 for (i = 0; i < rbio->stripe_nsectors; i++) {
2014 sector = rbio_stripe_sector(rbio, faila, i);
2015 sector->uptodate = 1;
2018 sector = rbio_stripe_sector(rbio, failb, i);
2019 sector->uptodate = 1;
2023 for (stripe = rbio->real_stripes - 1; stripe >= 0; stripe--)
2024 kunmap_local(unmap_array[stripe]);
2035 * Similar to READ_REBUILD, REBUILD_MISSING at this point also has a
2036 * valid rbio which is consistent with ondisk content, thus such a
2037 * valid rbio can be cached to avoid further disk reads.
2039 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
2040 rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
2042 * - In case of two failures, where rbio->failb != -1:
2044 * Do not cache this rbio since the above read reconstruction
2045 * (raid6_datap_recov() or raid6_2data_recov()) may have
2046 * changed some content of stripes which are not identical to
2047 * on-disk content any more, otherwise, a later write/recover
2048 * may steal stripe_pages from this rbio and end up with
2049 * corruptions or rebuild failures.
2051 * - In case of single failure, where rbio->failb == -1:
2053 * Cache this rbio iff the above read reconstruction is
2054 * executed without problems.
2056 if (err == BLK_STS_OK && rbio->failb < 0)
2057 cache_rbio_pages(rbio);
2059 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
2061 rbio_orig_end_io(rbio, err);
2062 } else if (err == BLK_STS_OK) {
2066 if (rbio->operation == BTRFS_RBIO_WRITE)
2068 else if (rbio->operation == BTRFS_RBIO_PARITY_SCRUB)
2069 finish_parity_scrub(rbio, 0);
2073 rbio_orig_end_io(rbio, err);
2078 * This is called only for stripes we've read from disk to reconstruct the
2081 static void raid_recover_end_io_work(struct work_struct *work)
2083 struct btrfs_raid_bio *rbio =
2084 container_of(work, struct btrfs_raid_bio, end_io_work);
2086 if (atomic_read(&rbio->error) > rbio->bioc->max_errors)
2087 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2089 __raid_recover_end_io(rbio);
2093 * reads everything we need off the disk to reconstruct
2094 * the parity. endio handlers trigger final reconstruction
2095 * when the IO is done.
2097 * This is used both for reads from the higher layers and for
2098 * parity construction required to finish a rmw cycle.
2100 static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
2102 int bios_to_read = 0;
2103 struct bio_list bio_list;
2105 int total_sector_nr;
2108 bio_list_init(&bio_list);
2110 ret = alloc_rbio_pages(rbio);
2114 atomic_set(&rbio->error, 0);
2117 * Read everything that hasn't failed. However this time we will
2118 * not trust any cached sector.
2119 * As we may read out some stale data but higher layer is not reading
2122 * So here we always re-read everything in recovery path.
2124 for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
2125 total_sector_nr++) {
2126 int stripe = total_sector_nr / rbio->stripe_nsectors;
2127 int sectornr = total_sector_nr % rbio->stripe_nsectors;
2128 struct sector_ptr *sector;
2130 if (rbio->faila == stripe || rbio->failb == stripe) {
2131 atomic_inc(&rbio->error);
2132 /* Skip the current stripe. */
2133 ASSERT(sectornr == 0);
2134 total_sector_nr += rbio->stripe_nsectors - 1;
2137 sector = rbio_stripe_sector(rbio, stripe, sectornr);
2138 ret = rbio_add_io_sector(rbio, &bio_list, sector, stripe,
2139 sectornr, REQ_OP_READ);
2144 bios_to_read = bio_list_size(&bio_list);
2145 if (!bios_to_read) {
2147 * we might have no bios to read just because the pages
2148 * were up to date, or we might have no bios to read because
2149 * the devices were gone.
2151 if (atomic_read(&rbio->error) <= rbio->bioc->max_errors) {
2152 __raid_recover_end_io(rbio);
2160 * The bioc may be freed once we submit the last bio. Make sure not to
2161 * touch it after that.
2163 atomic_set(&rbio->stripes_pending, bios_to_read);
2164 INIT_WORK(&rbio->end_io_work, raid_recover_end_io_work);
2165 while ((bio = bio_list_pop(&bio_list))) {
2166 bio->bi_end_io = raid56_bio_end_io;
2168 if (trace_raid56_scrub_read_recover_enabled()) {
2169 struct raid56_bio_trace_info trace_info = { 0 };
2171 bio_get_trace_info(rbio, bio, &trace_info);
2172 trace_raid56_scrub_read_recover(rbio, bio, &trace_info);
2180 if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
2181 rbio->operation == BTRFS_RBIO_REBUILD_MISSING)
2182 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2184 while ((bio = bio_list_pop(&bio_list)))
2191 * the main entry point for reads from the higher layers. This
2192 * is really only called when the normal read path had a failure,
2193 * so we assume the bio they send down corresponds to a failed part
2196 void raid56_parity_recover(struct bio *bio, struct btrfs_io_context *bioc,
2199 struct btrfs_fs_info *fs_info = bioc->fs_info;
2200 struct btrfs_raid_bio *rbio;
2202 rbio = alloc_rbio(fs_info, bioc);
2204 bio->bi_status = errno_to_blk_status(PTR_ERR(rbio));
2208 rbio->operation = BTRFS_RBIO_READ_REBUILD;
2209 rbio_add_bio(rbio, bio);
2211 rbio->faila = find_logical_bio_stripe(rbio, bio);
2212 if (rbio->faila == -1) {
2214 "%s could not find the bad stripe in raid56 so that we cannot recover any more (bio has logical %llu len %llu, bioc has map_type %llu)",
2215 __func__, bio->bi_iter.bi_sector << 9,
2216 (u64)bio->bi_iter.bi_size, bioc->map_type);
2217 __free_raid_bio(rbio);
2218 bio->bi_status = BLK_STS_IOERR;
2224 * for 'mirror == 2', reconstruct from all other stripes.
2225 * for 'mirror_num > 2', select a stripe to fail on every retry.
2227 if (mirror_num > 2) {
2229 * 'mirror == 3' is to fail the p stripe and
2230 * reconstruct from the q stripe. 'mirror > 3' is to
2231 * fail a data stripe and reconstruct from p+q stripe.
2233 rbio->failb = rbio->real_stripes - (mirror_num - 1);
2234 ASSERT(rbio->failb > 0);
2235 if (rbio->failb <= rbio->faila)
2239 if (lock_stripe_add(rbio))
2243 * This adds our rbio to the list of rbios that will be handled after
2244 * the current lock owner is done.
2246 __raid56_parity_recover(rbio);
2253 static void rmw_work(struct work_struct *work)
2255 struct btrfs_raid_bio *rbio;
2257 rbio = container_of(work, struct btrfs_raid_bio, work);
2258 raid56_rmw_stripe(rbio);
2261 static void read_rebuild_work(struct work_struct *work)
2263 struct btrfs_raid_bio *rbio;
2265 rbio = container_of(work, struct btrfs_raid_bio, work);
2266 __raid56_parity_recover(rbio);
2270 * The following code is used to scrub/replace the parity stripe
2272 * Caller must have already increased bio_counter for getting @bioc.
2274 * Note: We need make sure all the pages that add into the scrub/replace
2275 * raid bio are correct and not be changed during the scrub/replace. That
2276 * is those pages just hold metadata or file data with checksum.
2279 struct btrfs_raid_bio *raid56_parity_alloc_scrub_rbio(struct bio *bio,
2280 struct btrfs_io_context *bioc,
2281 struct btrfs_device *scrub_dev,
2282 unsigned long *dbitmap, int stripe_nsectors)
2284 struct btrfs_fs_info *fs_info = bioc->fs_info;
2285 struct btrfs_raid_bio *rbio;
2288 rbio = alloc_rbio(fs_info, bioc);
2291 bio_list_add(&rbio->bio_list, bio);
2293 * This is a special bio which is used to hold the completion handler
2294 * and make the scrub rbio is similar to the other types
2296 ASSERT(!bio->bi_iter.bi_size);
2297 rbio->operation = BTRFS_RBIO_PARITY_SCRUB;
2300 * After mapping bioc with BTRFS_MAP_WRITE, parities have been sorted
2301 * to the end position, so this search can start from the first parity
2304 for (i = rbio->nr_data; i < rbio->real_stripes; i++) {
2305 if (bioc->stripes[i].dev == scrub_dev) {
2310 ASSERT(i < rbio->real_stripes);
2312 bitmap_copy(&rbio->dbitmap, dbitmap, stripe_nsectors);
2316 /* Used for both parity scrub and missing. */
2317 void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page,
2318 unsigned int pgoff, u64 logical)
2320 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
2324 ASSERT(logical >= rbio->bioc->raid_map[0]);
2325 ASSERT(logical + sectorsize <= rbio->bioc->raid_map[0] +
2326 BTRFS_STRIPE_LEN * rbio->nr_data);
2327 stripe_offset = (int)(logical - rbio->bioc->raid_map[0]);
2328 index = stripe_offset / sectorsize;
2329 rbio->bio_sectors[index].page = page;
2330 rbio->bio_sectors[index].pgoff = pgoff;
2334 * We just scrub the parity that we have correct data on the same horizontal,
2335 * so we needn't allocate all pages for all the stripes.
2337 static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
2339 const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
2340 int total_sector_nr;
2342 for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
2343 total_sector_nr++) {
2345 int sectornr = total_sector_nr % rbio->stripe_nsectors;
2346 int index = (total_sector_nr * sectorsize) >> PAGE_SHIFT;
2348 if (!test_bit(sectornr, &rbio->dbitmap))
2350 if (rbio->stripe_pages[index])
2352 page = alloc_page(GFP_NOFS);
2355 rbio->stripe_pages[index] = page;
2357 index_stripe_sectors(rbio);
2361 static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
2364 struct btrfs_io_context *bioc = rbio->bioc;
2365 const u32 sectorsize = bioc->fs_info->sectorsize;
2366 void **pointers = rbio->finish_pointers;
2367 unsigned long *pbitmap = &rbio->finish_pbitmap;
2368 int nr_data = rbio->nr_data;
2372 struct sector_ptr p_sector = { 0 };
2373 struct sector_ptr q_sector = { 0 };
2374 struct bio_list bio_list;
2379 bio_list_init(&bio_list);
2381 if (rbio->real_stripes - rbio->nr_data == 1)
2382 has_qstripe = false;
2383 else if (rbio->real_stripes - rbio->nr_data == 2)
2388 if (bioc->num_tgtdevs && bioc->tgtdev_map[rbio->scrubp]) {
2390 bitmap_copy(pbitmap, &rbio->dbitmap, rbio->stripe_nsectors);
2394 * Because the higher layers(scrubber) are unlikely to
2395 * use this area of the disk again soon, so don't cache
2398 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
2403 p_sector.page = alloc_page(GFP_NOFS);
2407 p_sector.uptodate = 1;
2410 /* RAID6, allocate and map temp space for the Q stripe */
2411 q_sector.page = alloc_page(GFP_NOFS);
2412 if (!q_sector.page) {
2413 __free_page(p_sector.page);
2414 p_sector.page = NULL;
2418 q_sector.uptodate = 1;
2419 pointers[rbio->real_stripes - 1] = kmap_local_page(q_sector.page);
2422 atomic_set(&rbio->error, 0);
2424 /* Map the parity stripe just once */
2425 pointers[nr_data] = kmap_local_page(p_sector.page);
2427 for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors) {
2428 struct sector_ptr *sector;
2431 /* first collect one page from each data stripe */
2432 for (stripe = 0; stripe < nr_data; stripe++) {
2433 sector = sector_in_rbio(rbio, stripe, sectornr, 0);
2434 pointers[stripe] = kmap_local_page(sector->page) +
2439 /* RAID6, call the library function to fill in our P/Q */
2440 raid6_call.gen_syndrome(rbio->real_stripes, sectorsize,
2444 memcpy(pointers[nr_data], pointers[0], sectorsize);
2445 run_xor(pointers + 1, nr_data - 1, sectorsize);
2448 /* Check scrubbing parity and repair it */
2449 sector = rbio_stripe_sector(rbio, rbio->scrubp, sectornr);
2450 parity = kmap_local_page(sector->page) + sector->pgoff;
2451 if (memcmp(parity, pointers[rbio->scrubp], sectorsize) != 0)
2452 memcpy(parity, pointers[rbio->scrubp], sectorsize);
2454 /* Parity is right, needn't writeback */
2455 bitmap_clear(&rbio->dbitmap, sectornr, 1);
2456 kunmap_local(parity);
2458 for (stripe = nr_data - 1; stripe >= 0; stripe--)
2459 kunmap_local(pointers[stripe]);
2462 kunmap_local(pointers[nr_data]);
2463 __free_page(p_sector.page);
2464 p_sector.page = NULL;
2465 if (q_sector.page) {
2466 kunmap_local(pointers[rbio->real_stripes - 1]);
2467 __free_page(q_sector.page);
2468 q_sector.page = NULL;
2473 * time to start writing. Make bios for everything from the
2474 * higher layers (the bio_list in our rbio) and our p/q. Ignore
2477 for_each_set_bit(sectornr, &rbio->dbitmap, rbio->stripe_nsectors) {
2478 struct sector_ptr *sector;
2480 sector = rbio_stripe_sector(rbio, rbio->scrubp, sectornr);
2481 ret = rbio_add_io_sector(rbio, &bio_list, sector, rbio->scrubp,
2482 sectornr, REQ_OP_WRITE);
2490 for_each_set_bit(sectornr, pbitmap, rbio->stripe_nsectors) {
2491 struct sector_ptr *sector;
2493 sector = rbio_stripe_sector(rbio, rbio->scrubp, sectornr);
2494 ret = rbio_add_io_sector(rbio, &bio_list, sector,
2495 bioc->tgtdev_map[rbio->scrubp],
2496 sectornr, REQ_OP_WRITE);
2502 nr_data = bio_list_size(&bio_list);
2504 /* Every parity is right */
2505 rbio_orig_end_io(rbio, BLK_STS_OK);
2509 atomic_set(&rbio->stripes_pending, nr_data);
2511 while ((bio = bio_list_pop(&bio_list))) {
2512 bio->bi_end_io = raid_write_end_io;
2514 if (trace_raid56_scrub_write_stripe_enabled()) {
2515 struct raid56_bio_trace_info trace_info = { 0 };
2517 bio_get_trace_info(rbio, bio, &trace_info);
2518 trace_raid56_scrub_write_stripe(rbio, bio, &trace_info);
2525 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2527 while ((bio = bio_list_pop(&bio_list)))
2531 static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
2533 if (stripe >= 0 && stripe < rbio->nr_data)
2539 * While we're doing the parity check and repair, we could have errors
2540 * in reading pages off the disk. This checks for errors and if we're
2541 * not able to read the page it'll trigger parity reconstruction. The
2542 * parity scrub will be finished after we've reconstructed the failed
2545 static void validate_rbio_for_parity_scrub(struct btrfs_raid_bio *rbio)
2547 if (atomic_read(&rbio->error) > rbio->bioc->max_errors)
2550 if (rbio->faila >= 0 || rbio->failb >= 0) {
2551 int dfail = 0, failp = -1;
2553 if (is_data_stripe(rbio, rbio->faila))
2555 else if (is_parity_stripe(rbio->faila))
2556 failp = rbio->faila;
2558 if (is_data_stripe(rbio, rbio->failb))
2560 else if (is_parity_stripe(rbio->failb))
2561 failp = rbio->failb;
2564 * Because we can not use a scrubbing parity to repair
2565 * the data, so the capability of the repair is declined.
2566 * (In the case of RAID5, we can not repair anything)
2568 if (dfail > rbio->bioc->max_errors - 1)
2572 * If all data is good, only parity is correctly, just
2573 * repair the parity.
2576 finish_parity_scrub(rbio, 0);
2581 * Here means we got one corrupted data stripe and one
2582 * corrupted parity on RAID6, if the corrupted parity
2583 * is scrubbing parity, luckily, use the other one to repair
2584 * the data, or we can not repair the data stripe.
2586 if (failp != rbio->scrubp)
2589 __raid_recover_end_io(rbio);
2591 finish_parity_scrub(rbio, 1);
2596 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2600 * end io for the read phase of the rmw cycle. All the bios here are physical
2601 * stripe bios we've read from the disk so we can recalculate the parity of the
2604 * This will usually kick off finish_rmw once all the bios are read in, but it
2605 * may trigger parity reconstruction if we had any errors along the way
2607 static void raid56_parity_scrub_end_io_work(struct work_struct *work)
2609 struct btrfs_raid_bio *rbio =
2610 container_of(work, struct btrfs_raid_bio, end_io_work);
2613 * This will normally call finish_rmw to start our write, but if there
2614 * are any failed stripes we'll reconstruct from parity first
2616 validate_rbio_for_parity_scrub(rbio);
2619 static void raid56_parity_scrub_stripe(struct btrfs_raid_bio *rbio)
2621 int bios_to_read = 0;
2622 struct bio_list bio_list;
2624 int total_sector_nr;
2627 bio_list_init(&bio_list);
2629 ret = alloc_rbio_essential_pages(rbio);
2633 atomic_set(&rbio->error, 0);
2634 /* Build a list of bios to read all the missing parts. */
2635 for (total_sector_nr = 0; total_sector_nr < rbio->nr_sectors;
2636 total_sector_nr++) {
2637 int sectornr = total_sector_nr % rbio->stripe_nsectors;
2638 int stripe = total_sector_nr / rbio->stripe_nsectors;
2639 struct sector_ptr *sector;
2641 /* No data in the vertical stripe, no need to read. */
2642 if (!test_bit(sectornr, &rbio->dbitmap))
2646 * We want to find all the sectors missing from the rbio and
2647 * read them from the disk. If sector_in_rbio() finds a sector
2648 * in the bio list we don't need to read it off the stripe.
2650 sector = sector_in_rbio(rbio, stripe, sectornr, 1);
2654 sector = rbio_stripe_sector(rbio, stripe, sectornr);
2656 * The bio cache may have handed us an uptodate sector. If so,
2659 if (sector->uptodate)
2662 ret = rbio_add_io_sector(rbio, &bio_list, sector, stripe,
2663 sectornr, REQ_OP_READ);
2668 bios_to_read = bio_list_size(&bio_list);
2669 if (!bios_to_read) {
2671 * this can happen if others have merged with
2672 * us, it means there is nothing left to read.
2673 * But if there are missing devices it may not be
2674 * safe to do the full stripe write yet.
2680 * The bioc may be freed once we submit the last bio. Make sure not to
2681 * touch it after that.
2683 atomic_set(&rbio->stripes_pending, bios_to_read);
2684 INIT_WORK(&rbio->end_io_work, raid56_parity_scrub_end_io_work);
2685 while ((bio = bio_list_pop(&bio_list))) {
2686 bio->bi_end_io = raid56_bio_end_io;
2688 if (trace_raid56_scrub_read_enabled()) {
2689 struct raid56_bio_trace_info trace_info = { 0 };
2691 bio_get_trace_info(rbio, bio, &trace_info);
2692 trace_raid56_scrub_read(rbio, bio, &trace_info);
2696 /* the actual write will happen once the reads are done */
2700 rbio_orig_end_io(rbio, BLK_STS_IOERR);
2702 while ((bio = bio_list_pop(&bio_list)))
2708 validate_rbio_for_parity_scrub(rbio);
2711 static void scrub_parity_work(struct work_struct *work)
2713 struct btrfs_raid_bio *rbio;
2715 rbio = container_of(work, struct btrfs_raid_bio, work);
2716 raid56_parity_scrub_stripe(rbio);
2719 void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
2721 if (!lock_stripe_add(rbio))
2722 start_async_work(rbio, scrub_parity_work);
2725 /* The following code is used for dev replace of a missing RAID 5/6 device. */
2727 struct btrfs_raid_bio *
2728 raid56_alloc_missing_rbio(struct bio *bio, struct btrfs_io_context *bioc)
2730 struct btrfs_fs_info *fs_info = bioc->fs_info;
2731 struct btrfs_raid_bio *rbio;
2733 rbio = alloc_rbio(fs_info, bioc);
2737 rbio->operation = BTRFS_RBIO_REBUILD_MISSING;
2738 bio_list_add(&rbio->bio_list, bio);
2740 * This is a special bio which is used to hold the completion handler
2741 * and make the scrub rbio is similar to the other types
2743 ASSERT(!bio->bi_iter.bi_size);
2745 rbio->faila = find_logical_bio_stripe(rbio, bio);
2746 if (rbio->faila == -1) {
2747 btrfs_warn_rl(fs_info,
2748 "can not determine the failed stripe number for full stripe %llu",
2750 __free_raid_bio(rbio);
2757 void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio)
2759 if (!lock_stripe_add(rbio))
2760 start_async_work(rbio, read_rebuild_work);