btrfs: alloc_chunk: do not refurbish num_bytes
[sfrench/cifs-2.6.git] / fs / btrfs / volumes.c
index f435d397019eae589f4e9daf88c954229537a60e..405d8977a2b5efcd3582a2af76af5035b847be3c 100644 (file)
@@ -1900,6 +1900,14 @@ int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
                goto out;
        }
 
+       if (btrfs_pinned_by_swapfile(fs_info, device)) {
+               btrfs_warn_in_rcu(fs_info,
+                 "cannot remove device %s (devid %llu) due to active swapfile",
+                                 rcu_str_deref(device->name), device->devid);
+               ret = -ETXTBSY;
+               goto out;
+       }
+
        if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
                ret = BTRFS_ERROR_DEV_TGT_REPLACE;
                goto out;
@@ -2718,8 +2726,15 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
        return ret;
 }
 
-static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
-                                       u64 logical, u64 length)
+/*
+ * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
+ * @logical: Logical block offset in bytes.
+ * @length: Length of extent in bytes.
+ *
+ * Return: Chunk mapping or ERR_PTR.
+ */
+struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
+                                      u64 logical, u64 length)
 {
        struct extent_map_tree *em_tree;
        struct extent_map *em;
@@ -2756,7 +2771,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
        int i, ret = 0;
        struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
 
-       em = get_chunk_map(fs_info, chunk_offset, 1);
+       em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
        if (IS_ERR(em)) {
                /*
                 * This is a logic error, but we don't want to just rely on the
@@ -3638,10 +3653,15 @@ again:
 
                ret = btrfs_relocate_chunk(fs_info, found_key.offset);
                mutex_unlock(&fs_info->delete_unused_bgs_mutex);
-               if (ret && ret != -ENOSPC)
-                       goto error;
                if (ret == -ENOSPC) {
                        enospc_errors++;
+               } else if (ret == -ETXTBSY) {
+                       btrfs_info(fs_info,
+          "skipping relocation of block group %llu due to active swapfile",
+                                  found_key.offset);
+                       ret = 0;
+               } else if (ret) {
+                       goto error;
                } else {
                        spin_lock(&fs_info->balance_lock);
                        bctl->stat.completed++;
@@ -4433,10 +4453,16 @@ again:
 
                ret = btrfs_relocate_chunk(fs_info, chunk_offset);
                mutex_unlock(&fs_info->delete_unused_bgs_mutex);
-               if (ret && ret != -ENOSPC)
-                       goto done;
-               if (ret == -ENOSPC)
+               if (ret == -ENOSPC) {
                        failed++;
+               } else if (ret) {
+                       if (ret == -ETXTBSY) {
+                               btrfs_warn(fs_info,
+                  "could not shrink block group %llu due to active swapfile",
+                                          chunk_offset);
+                       }
+                       goto done;
+               }
        } while (key.offset-- > 0);
 
        if (failed && !retried) {
@@ -4843,10 +4869,9 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
        if (ret)
                goto error_del_extent;
 
-       for (i = 0; i < map->num_stripes; i++) {
-               num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
-               btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
-       }
+       for (i = 0; i < map->num_stripes; i++)
+               btrfs_device_set_bytes_used(map->stripes[i].dev,
+                               map->stripes[i].dev->bytes_used + stripe_size);
 
        atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
 
@@ -4890,7 +4915,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
        int i = 0;
        int ret = 0;
 
-       em = get_chunk_map(fs_info, chunk_offset, chunk_size);
+       em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
        if (IS_ERR(em))
                return PTR_ERR(em);
 
@@ -5032,7 +5057,7 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
        int miss_ndevs = 0;
        int i;
 
-       em = get_chunk_map(fs_info, chunk_offset, 1);
+       em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
        if (IS_ERR(em))
                return 1;
 
@@ -5092,7 +5117,7 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
        struct map_lookup *map;
        int ret;
 
-       em = get_chunk_map(fs_info, logical, len);
+       em = btrfs_get_chunk_map(fs_info, logical, len);
        if (IS_ERR(em))
                /*
                 * We could return errors for these cases, but that could get
@@ -5138,7 +5163,7 @@ unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
        struct map_lookup *map;
        unsigned long len = fs_info->sectorsize;
 
-       em = get_chunk_map(fs_info, logical, len);
+       em = btrfs_get_chunk_map(fs_info, logical, len);
 
        if (!WARN_ON(IS_ERR(em))) {
                map = em->map_lookup;
@@ -5155,7 +5180,7 @@ int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
        struct map_lookup *map;
        int ret = 0;
 
-       em = get_chunk_map(fs_info, logical, len);
+       em = btrfs_get_chunk_map(fs_info, logical, len);
 
        if(!WARN_ON(IS_ERR(em))) {
                map = em->map_lookup;
@@ -5314,7 +5339,7 @@ static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
        /* discard always return a bbio */
        ASSERT(bbio_ret);
 
-       em = get_chunk_map(fs_info, logical, length);
+       em = btrfs_get_chunk_map(fs_info, logical, length);
        if (IS_ERR(em))
                return PTR_ERR(em);
 
@@ -5640,7 +5665,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
                return __btrfs_map_block_for_discard(fs_info, logical,
                                                     *length, bbio_ret);
 
-       em = get_chunk_map(fs_info, logical, *length);
+       em = btrfs_get_chunk_map(fs_info, logical, *length);
        if (IS_ERR(em))
                return PTR_ERR(em);
 
@@ -5943,7 +5968,7 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
        u64 rmap_len;
        int i, j, nr = 0;
 
-       em = get_chunk_map(fs_info, chunk_start, 1);
+       em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
        if (IS_ERR(em))
                return -EIO;
 
@@ -6083,12 +6108,6 @@ static noinline void btrfs_schedule_bio(struct btrfs_device *device,
        int should_queue = 1;
        struct btrfs_pending_bios *pending_bios;
 
-       if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
-           !device->bdev) {
-               bio_io_error(bio);
-               return;
-       }
-
        /* don't bother with additional async steps for reads, right now */
        if (bio_op(bio) == REQ_OP_READ) {
                btrfsic_submit_bio(bio);
@@ -6217,7 +6236,8 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
 
        for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
                dev = bbio->stripes[dev_nr].dev;
-               if (!dev || !dev->bdev ||
+               if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
+                                                  &dev->dev_state) ||
                    (bio_op(first_bio) == REQ_OP_WRITE &&
                    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
                        bbio_error(bbio, first_bio, logical);
@@ -7387,6 +7407,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
        struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
        struct extent_map *em;
        struct map_lookup *map;
+       struct btrfs_device *dev;
        u64 stripe_len;
        bool found = false;
        int ret = 0;
@@ -7436,6 +7457,22 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
                        physical_offset, devid);
                ret = -EUCLEAN;
        }
+
+       /* Make sure no dev extent is beyond device bondary */
+       dev = btrfs_find_device(fs_info, devid, NULL, NULL);
+       if (!dev) {
+               btrfs_err(fs_info, "failed to find devid %llu", devid);
+               ret = -EUCLEAN;
+               goto out;
+       }
+       if (physical_offset + physical_len > dev->disk_total_bytes) {
+               btrfs_err(fs_info,
+"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
+                         devid, physical_offset, physical_len,
+                         dev->disk_total_bytes);
+               ret = -EUCLEAN;
+               goto out;
+       }
 out:
        free_extent_map(em);
        return ret;
@@ -7478,6 +7515,8 @@ int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
        struct btrfs_path *path;
        struct btrfs_root *root = fs_info->dev_root;
        struct btrfs_key key;
+       u64 prev_devid = 0;
+       u64 prev_dev_ext_end = 0;
        int ret = 0;
 
        key.objectid = 1;
@@ -7522,10 +7561,22 @@ int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
                chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
                physical_len = btrfs_dev_extent_length(leaf, dext);
 
+               /* Check if this dev extent overlaps with the previous one */
+               if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
+                       btrfs_err(fs_info,
+"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
+                                 devid, physical_offset, prev_dev_ext_end);
+                       ret = -EUCLEAN;
+                       goto out;
+               }
+
                ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
                                            physical_offset, physical_len);
                if (ret < 0)
                        goto out;
+               prev_devid = devid;
+               prev_dev_ext_end = physical_offset + physical_len;
+
                ret = btrfs_next_item(root, path);
                if (ret < 0)
                        goto out;
@@ -7541,3 +7592,27 @@ out:
        btrfs_free_path(path);
        return ret;
 }
+
+/*
+ * Check whether the given block group or device is pinned by any inode being
+ * used as a swapfile.
+ */
+bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
+{
+       struct btrfs_swapfile_pin *sp;
+       struct rb_node *node;
+
+       spin_lock(&fs_info->swapfile_pins_lock);
+       node = fs_info->swapfile_pins.rb_node;
+       while (node) {
+               sp = rb_entry(node, struct btrfs_swapfile_pin, node);
+               if (ptr < sp->ptr)
+                       node = node->rb_left;
+               else if (ptr > sp->ptr)
+                       node = node->rb_right;
+               else
+                       break;
+       }
+       spin_unlock(&fs_info->swapfile_pins_lock);
+       return node != NULL;
+}