These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / fs / btrfs / raid56.c
index fa72068..1a33d3e 100644 (file)
 #define RBIO_CACHE_SIZE 1024
 
 enum btrfs_rbio_ops {
-       BTRFS_RBIO_WRITE        = 0,
-       BTRFS_RBIO_READ_REBUILD = 1,
-       BTRFS_RBIO_PARITY_SCRUB = 2,
+       BTRFS_RBIO_WRITE,
+       BTRFS_RBIO_READ_REBUILD,
+       BTRFS_RBIO_PARITY_SCRUB,
+       BTRFS_RBIO_REBUILD_MISSING,
 };
 
 struct btrfs_raid_bio {
@@ -602,6 +603,10 @@ static int rbio_can_merge(struct btrfs_raid_bio *last,
            cur->operation == BTRFS_RBIO_PARITY_SCRUB)
                return 0;
 
+       if (last->operation == BTRFS_RBIO_REBUILD_MISSING ||
+           cur->operation == BTRFS_RBIO_REBUILD_MISSING)
+               return 0;
+
        return 1;
 }
 
@@ -793,7 +798,10 @@ static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
 
                        if (next->operation == BTRFS_RBIO_READ_REBUILD)
                                async_read_rebuild(next);
-                       else if (next->operation == BTRFS_RBIO_WRITE) {
+                       else if (next->operation == BTRFS_RBIO_REBUILD_MISSING) {
+                               steal_rbio(rbio, next);
+                               async_read_rebuild(next);
+                       } else if (next->operation == BTRFS_RBIO_WRITE) {
                                steal_rbio(rbio, next);
                                async_rmw_stripe(next);
                        } else if (next->operation == BTRFS_RBIO_PARITY_SCRUB) {
@@ -802,7 +810,11 @@ static noinline void unlock_stripe(struct btrfs_raid_bio *rbio)
                        }
 
                        goto done_nolock;
-               } else  if (waitqueue_active(&h->wait)) {
+                       /*
+                        * The barrier for this waitqueue_active is not needed,
+                        * we're protected by h->lock and can't miss a wakeup.
+                        */
+               } else if (waitqueue_active(&h->wait)) {
                        spin_unlock(&rbio->bio_list_lock);
                        spin_unlock_irqrestore(&h->lock, flags);
                        wake_up(&h->wait);
@@ -851,7 +863,7 @@ static void free_raid_bio(struct btrfs_raid_bio *rbio)
  * this frees the rbio and runs through all the bios in the
  * bio_list and calls end_io on them
  */
-static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, int err, int uptodate)
+static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, int err)
 {
        struct bio *cur = bio_list_get(&rbio->bio_list);
        struct bio *next;
@@ -864,9 +876,8 @@ static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, int err, int uptodate)
        while (cur) {
                next = cur->bi_next;
                cur->bi_next = NULL;
-               if (uptodate)
-                       set_bit(BIO_UPTODATE, &cur->bi_flags);
-               bio_endio(cur, err);
+               cur->bi_error = err;
+               bio_endio(cur);
                cur = next;
        }
 }
@@ -875,9 +886,10 @@ static void rbio_orig_end_io(struct btrfs_raid_bio *rbio, int err, int uptodate)
  * end io function used by finish_rmw.  When we finally
  * get here, we've written a full stripe
  */
-static void raid_write_end_io(struct bio *bio, int err)
+static void raid_write_end_io(struct bio *bio)
 {
        struct btrfs_raid_bio *rbio = bio->bi_private;
+       int err = bio->bi_error;
 
        if (err)
                fail_bio_stripe(rbio, bio);
@@ -893,7 +905,7 @@ static void raid_write_end_io(struct bio *bio, int err)
        if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
                err = -EIO;
 
-       rbio_orig_end_io(rbio, err, 0);
+       rbio_orig_end_io(rbio, err);
        return;
 }
 
@@ -1071,7 +1083,7 @@ static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
                 * devices or if they are not contiguous
                 */
                if (last_end == disk_start && stripe->dev->bdev &&
-                   test_bit(BIO_UPTODATE, &last->bi_flags) &&
+                   !last->bi_error &&
                    last->bi_bdev == stripe->dev->bdev) {
                        ret = bio_add_page(last, page, PAGE_CACHE_SIZE, 0);
                        if (ret == PAGE_CACHE_SIZE)
@@ -1087,7 +1099,6 @@ static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
        bio->bi_iter.bi_size = 0;
        bio->bi_bdev = stripe->dev->bdev;
        bio->bi_iter.bi_sector = disk_start >> 9;
-       set_bit(BIO_UPTODATE, &bio->bi_flags);
 
        bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
        bio_list_add(bio_list, bio);
@@ -1312,13 +1323,12 @@ write_data:
 
                bio->bi_private = rbio;
                bio->bi_end_io = raid_write_end_io;
-               BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
                submit_bio(WRITE, bio);
        }
        return;
 
 cleanup:
-       rbio_orig_end_io(rbio, -EIO, 0);
+       rbio_orig_end_io(rbio, -EIO);
 }
 
 /*
@@ -1441,11 +1451,11 @@ static void set_bio_pages_uptodate(struct bio *bio)
  * This will usually kick off finish_rmw once all the bios are read in, but it
  * may trigger parity reconstruction if we had any errors along the way
  */
-static void raid_rmw_end_io(struct bio *bio, int err)
+static void raid_rmw_end_io(struct bio *bio)
 {
        struct btrfs_raid_bio *rbio = bio->bi_private;
 
-       if (err)
+       if (bio->bi_error)
                fail_bio_stripe(rbio, bio);
        else
                set_bio_pages_uptodate(bio);
@@ -1455,7 +1465,6 @@ static void raid_rmw_end_io(struct bio *bio, int err)
        if (!atomic_dec_and_test(&rbio->stripes_pending))
                return;
 
-       err = 0;
        if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
                goto cleanup;
 
@@ -1469,7 +1478,7 @@ static void raid_rmw_end_io(struct bio *bio, int err)
 
 cleanup:
 
-       rbio_orig_end_io(rbio, -EIO, 0);
+       rbio_orig_end_io(rbio, -EIO);
 }
 
 static void async_rmw_stripe(struct btrfs_raid_bio *rbio)
@@ -1572,14 +1581,13 @@ static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
                btrfs_bio_wq_end_io(rbio->fs_info, bio,
                                    BTRFS_WQ_ENDIO_RAID56);
 
-               BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
                submit_bio(READ, bio);
        }
        /* the actual write will happen once the reads are done */
        return 0;
 
 cleanup:
-       rbio_orig_end_io(rbio, -EIO, 0);
+       rbio_orig_end_io(rbio, -EIO);
        return -EIO;
 
 finish:
@@ -1809,7 +1817,8 @@ static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
        faila = rbio->faila;
        failb = rbio->failb;
 
-       if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
+       if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
+           rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
                spin_lock_irq(&rbio->bio_list_lock);
                set_bit(RBIO_RMW_LOCKED_BIT, &rbio->flags);
                spin_unlock_irq(&rbio->bio_list_lock);
@@ -1834,7 +1843,8 @@ static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
                         * if we're rebuilding a read, we have to use
                         * pages from the bio list
                         */
-                       if (rbio->operation == BTRFS_RBIO_READ_REBUILD &&
+                       if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
+                            rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
                            (stripe == faila || stripe == failb)) {
                                page = page_in_rbio(rbio, stripe, pagenr, 0);
                        } else {
@@ -1943,7 +1953,8 @@ pstripe:
                         * if we're rebuilding a read, we have to use
                         * pages from the bio list
                         */
-                       if (rbio->operation == BTRFS_RBIO_READ_REBUILD &&
+                       if ((rbio->operation == BTRFS_RBIO_READ_REBUILD ||
+                            rbio->operation == BTRFS_RBIO_REBUILD_MISSING) &&
                            (stripe == faila || stripe == failb)) {
                                page = page_in_rbio(rbio, stripe, pagenr, 0);
                        } else {
@@ -1964,7 +1975,9 @@ cleanup_io:
                else
                        clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
 
-               rbio_orig_end_io(rbio, err, err == 0);
+               rbio_orig_end_io(rbio, err);
+       } else if (rbio->operation == BTRFS_RBIO_REBUILD_MISSING) {
+               rbio_orig_end_io(rbio, err);
        } else if (err == 0) {
                rbio->faila = -1;
                rbio->failb = -1;
@@ -1976,7 +1989,7 @@ cleanup_io:
                else
                        BUG();
        } else {
-               rbio_orig_end_io(rbio, err, 0);
+               rbio_orig_end_io(rbio, err);
        }
 }
 
@@ -1984,7 +1997,7 @@ cleanup_io:
  * This is called only for stripes we've read from disk to
  * reconstruct the parity.
  */
-static void raid_recover_end_io(struct bio *bio, int err)
+static void raid_recover_end_io(struct bio *bio)
 {
        struct btrfs_raid_bio *rbio = bio->bi_private;
 
@@ -1992,7 +2005,7 @@ static void raid_recover_end_io(struct bio *bio, int err)
         * we only read stripe pages off the disk, set them
         * up to date if there were no errors
         */
-       if (err)
+       if (bio->bi_error)
                fail_bio_stripe(rbio, bio);
        else
                set_bio_pages_uptodate(bio);
@@ -2002,7 +2015,7 @@ static void raid_recover_end_io(struct bio *bio, int err)
                return;
 
        if (atomic_read(&rbio->error) > rbio->bbio->max_errors)
-               rbio_orig_end_io(rbio, -EIO, 0);
+               rbio_orig_end_io(rbio, -EIO);
        else
                __raid_recover_end_io(rbio);
 }
@@ -2094,15 +2107,15 @@ static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
                btrfs_bio_wq_end_io(rbio->fs_info, bio,
                                    BTRFS_WQ_ENDIO_RAID56);
 
-               BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
                submit_bio(READ, bio);
        }
 out:
        return 0;
 
 cleanup:
-       if (rbio->operation == BTRFS_RBIO_READ_REBUILD)
-               rbio_orig_end_io(rbio, -EIO, 0);
+       if (rbio->operation == BTRFS_RBIO_READ_REBUILD ||
+           rbio->operation == BTRFS_RBIO_REBUILD_MISSING)
+               rbio_orig_end_io(rbio, -EIO);
        return -EIO;
 }
 
@@ -2232,8 +2245,9 @@ raid56_parity_alloc_scrub_rbio(struct btrfs_root *root, struct bio *bio,
        return rbio;
 }
 
-void raid56_parity_add_scrub_pages(struct btrfs_raid_bio *rbio,
-                                  struct page *page, u64 logical)
+/* Used for both parity scrub and missing. */
+void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page,
+                           u64 logical)
 {
        int stripe_offset;
        int index;
@@ -2277,11 +2291,12 @@ static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
  * end io function used by finish_rmw.  When we finally
  * get here, we've written a full stripe
  */
-static void raid_write_parity_end_io(struct bio *bio, int err)
+static void raid_write_parity_end_io(struct bio *bio)
 {
        struct btrfs_raid_bio *rbio = bio->bi_private;
+       int err = bio->bi_error;
 
-       if (err)
+       if (bio->bi_error)
                fail_bio_stripe(rbio, bio);
 
        bio_put(bio);
@@ -2294,7 +2309,7 @@ static void raid_write_parity_end_io(struct bio *bio, int err)
        if (atomic_read(&rbio->error))
                err = -EIO;
 
-       rbio_orig_end_io(rbio, err, 0);
+       rbio_orig_end_io(rbio, err);
 }
 
 static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
@@ -2437,7 +2452,7 @@ submit_write:
        nr_data = bio_list_size(&bio_list);
        if (!nr_data) {
                /* Every parity is right */
-               rbio_orig_end_io(rbio, 0, 0);
+               rbio_orig_end_io(rbio, 0);
                return;
        }
 
@@ -2450,13 +2465,12 @@ submit_write:
 
                bio->bi_private = rbio;
                bio->bi_end_io = raid_write_parity_end_io;
-               BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
                submit_bio(WRITE, bio);
        }
        return;
 
 cleanup:
-       rbio_orig_end_io(rbio, -EIO, 0);
+       rbio_orig_end_io(rbio, -EIO);
 }
 
 static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
@@ -2524,7 +2538,7 @@ static void validate_rbio_for_parity_scrub(struct btrfs_raid_bio *rbio)
        return;
 
 cleanup:
-       rbio_orig_end_io(rbio, -EIO, 0);
+       rbio_orig_end_io(rbio, -EIO);
 }
 
 /*
@@ -2535,11 +2549,11 @@ cleanup:
  * This will usually kick off finish_rmw once all the bios are read in, but it
  * may trigger parity reconstruction if we had any errors along the way
  */
-static void raid56_parity_scrub_end_io(struct bio *bio, int err)
+static void raid56_parity_scrub_end_io(struct bio *bio)
 {
        struct btrfs_raid_bio *rbio = bio->bi_private;
 
-       if (err)
+       if (bio->bi_error)
                fail_bio_stripe(rbio, bio);
        else
                set_bio_pages_uptodate(bio);
@@ -2632,14 +2646,13 @@ static void raid56_parity_scrub_stripe(struct btrfs_raid_bio *rbio)
                btrfs_bio_wq_end_io(rbio->fs_info, bio,
                                    BTRFS_WQ_ENDIO_RAID56);
 
-               BUG_ON(!test_bit(BIO_UPTODATE, &bio->bi_flags));
                submit_bio(READ, bio);
        }
        /* the actual write will happen once the reads are done */
        return;
 
 cleanup:
-       rbio_orig_end_io(rbio, -EIO, 0);
+       rbio_orig_end_io(rbio, -EIO);
        return;
 
 finish:
@@ -2668,3 +2681,55 @@ void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
        if (!lock_stripe_add(rbio))
                async_scrub_parity(rbio);
 }
+
+/* The following code is used for dev replace of a missing RAID 5/6 device. */
+
+struct btrfs_raid_bio *
+raid56_alloc_missing_rbio(struct btrfs_root *root, struct bio *bio,
+                         struct btrfs_bio *bbio, u64 length)
+{
+       struct btrfs_raid_bio *rbio;
+
+       rbio = alloc_rbio(root, bbio, length);
+       if (IS_ERR(rbio))
+               return NULL;
+
+       rbio->operation = BTRFS_RBIO_REBUILD_MISSING;
+       bio_list_add(&rbio->bio_list, bio);
+       /*
+        * This is a special bio which is used to hold the completion handler
+        * and make the scrub rbio is similar to the other types
+        */
+       ASSERT(!bio->bi_iter.bi_size);
+
+       rbio->faila = find_logical_bio_stripe(rbio, bio);
+       if (rbio->faila == -1) {
+               BUG();
+               kfree(rbio);
+               return NULL;
+       }
+
+       return rbio;
+}
+
+static void missing_raid56_work(struct btrfs_work *work)
+{
+       struct btrfs_raid_bio *rbio;
+
+       rbio = container_of(work, struct btrfs_raid_bio, work);
+       __raid56_parity_recover(rbio);
+}
+
+static void async_missing_raid56(struct btrfs_raid_bio *rbio)
+{
+       btrfs_init_work(&rbio->work, btrfs_rmw_helper,
+                       missing_raid56_work, NULL, NULL);
+
+       btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
+}
+
+void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio)
+{
+       if (!lock_stripe_add(rbio))
+               async_missing_raid56(rbio);
+}