David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Fusion IO. All rights reserved. |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/slab.h> |
| 7 | #include "btrfs-tests.h" |
| 8 | #include "../ctree.h" |
| 9 | #include "../extent_io.h" |
| 10 | #include "../disk-io.h" |
| 11 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 12 | static int test_btrfs_split_item(u32 sectorsize, u32 nodesize) |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 13 | { |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 14 | struct btrfs_fs_info *fs_info; |
| 15 | struct btrfs_path *path = NULL; |
| 16 | struct btrfs_root *root = NULL; |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 17 | struct extent_buffer *eb; |
| 18 | struct btrfs_item *item; |
| 19 | char *value = "mary had a little lamb"; |
| 20 | char *split1 = "mary had a little"; |
| 21 | char *split2 = " lamb"; |
| 22 | char *split3 = "mary"; |
| 23 | char *split4 = " had a little"; |
| 24 | char buf[32]; |
| 25 | struct btrfs_key key; |
| 26 | u32 value_len = strlen(value); |
| 27 | int ret = 0; |
| 28 | |
| 29 | test_msg("Running btrfs_split_item tests\n"); |
| 30 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 31 | fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 32 | if (!fs_info) { |
| 33 | test_msg("Could not allocate fs_info\n"); |
| 34 | return -ENOMEM; |
| 35 | } |
| 36 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 37 | root = btrfs_alloc_dummy_root(fs_info); |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 38 | if (IS_ERR(root)) { |
| 39 | test_msg("Could not allocate root\n"); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 40 | ret = PTR_ERR(root); |
| 41 | goto out; |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | path = btrfs_alloc_path(); |
| 45 | if (!path) { |
| 46 | test_msg("Could not allocate path\n"); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 47 | ret = -ENOMEM; |
| 48 | goto out; |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 51 | path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize); |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 52 | if (!eb) { |
| 53 | test_msg("Could not allocate dummy buffer\n"); |
| 54 | ret = -ENOMEM; |
| 55 | goto out; |
| 56 | } |
| 57 | path->slots[0] = 0; |
| 58 | |
| 59 | key.objectid = 0; |
| 60 | key.type = BTRFS_EXTENT_CSUM_KEY; |
| 61 | key.offset = 0; |
| 62 | |
| 63 | setup_items_for_insert(root, path, &key, &value_len, value_len, |
| 64 | value_len + sizeof(struct btrfs_item), 1); |
| 65 | item = btrfs_item_nr(0); |
| 66 | write_extent_buffer(eb, value, btrfs_item_ptr_offset(eb, 0), |
| 67 | value_len); |
| 68 | |
| 69 | key.offset = 3; |
| 70 | |
| 71 | /* |
| 72 | * Passing NULL trans here should be safe because we have plenty of |
| 73 | * space in this leaf to split the item without having to split the |
| 74 | * leaf. |
| 75 | */ |
| 76 | ret = btrfs_split_item(NULL, root, path, &key, 17); |
| 77 | if (ret) { |
| 78 | test_msg("Split item failed %d\n", ret); |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Read the first slot, it should have the original key and contain only |
| 84 | * 'mary had a little' |
| 85 | */ |
| 86 | btrfs_item_key_to_cpu(eb, &key, 0); |
| 87 | if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY || |
| 88 | key.offset != 0) { |
| 89 | test_msg("Invalid key at slot 0\n"); |
| 90 | ret = -EINVAL; |
| 91 | goto out; |
| 92 | } |
| 93 | |
| 94 | item = btrfs_item_nr(0); |
| 95 | if (btrfs_item_size(eb, item) != strlen(split1)) { |
| 96 | test_msg("Invalid len in the first split\n"); |
| 97 | ret = -EINVAL; |
| 98 | goto out; |
| 99 | } |
| 100 | |
| 101 | read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0), |
| 102 | strlen(split1)); |
| 103 | if (memcmp(buf, split1, strlen(split1))) { |
| 104 | test_msg("Data in the buffer doesn't match what it should " |
| 105 | "in the first split have='%.*s' want '%s'\n", |
| 106 | (int)strlen(split1), buf, split1); |
| 107 | ret = -EINVAL; |
| 108 | goto out; |
| 109 | } |
| 110 | |
| 111 | btrfs_item_key_to_cpu(eb, &key, 1); |
| 112 | if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY || |
| 113 | key.offset != 3) { |
| 114 | test_msg("Invalid key at slot 1\n"); |
| 115 | ret = -EINVAL; |
| 116 | goto out; |
| 117 | } |
| 118 | |
| 119 | item = btrfs_item_nr(1); |
| 120 | if (btrfs_item_size(eb, item) != strlen(split2)) { |
| 121 | test_msg("Invalid len in the second split\n"); |
| 122 | ret = -EINVAL; |
| 123 | goto out; |
| 124 | } |
| 125 | |
| 126 | read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1), |
| 127 | strlen(split2)); |
| 128 | if (memcmp(buf, split2, strlen(split2))) { |
| 129 | test_msg("Data in the buffer doesn't match what it should " |
| 130 | "in the second split\n"); |
| 131 | ret = -EINVAL; |
| 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | key.offset = 1; |
| 136 | /* Do it again so we test memmoving the other items in the leaf */ |
| 137 | ret = btrfs_split_item(NULL, root, path, &key, 4); |
| 138 | if (ret) { |
| 139 | test_msg("Second split item failed %d\n", ret); |
| 140 | goto out; |
| 141 | } |
| 142 | |
| 143 | btrfs_item_key_to_cpu(eb, &key, 0); |
| 144 | if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY || |
| 145 | key.offset != 0) { |
| 146 | test_msg("Invalid key at slot 0\n"); |
| 147 | ret = -EINVAL; |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | item = btrfs_item_nr(0); |
| 152 | if (btrfs_item_size(eb, item) != strlen(split3)) { |
| 153 | test_msg("Invalid len in the first split\n"); |
| 154 | ret = -EINVAL; |
| 155 | goto out; |
| 156 | } |
| 157 | |
| 158 | read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 0), |
| 159 | strlen(split3)); |
| 160 | if (memcmp(buf, split3, strlen(split3))) { |
| 161 | test_msg("Data in the buffer doesn't match what it should " |
| 162 | "in the third split"); |
| 163 | ret = -EINVAL; |
| 164 | goto out; |
| 165 | } |
| 166 | |
| 167 | btrfs_item_key_to_cpu(eb, &key, 1); |
| 168 | if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY || |
| 169 | key.offset != 1) { |
| 170 | test_msg("Invalid key at slot 1\n"); |
| 171 | ret = -EINVAL; |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | item = btrfs_item_nr(1); |
| 176 | if (btrfs_item_size(eb, item) != strlen(split4)) { |
| 177 | test_msg("Invalid len in the second split\n"); |
| 178 | ret = -EINVAL; |
| 179 | goto out; |
| 180 | } |
| 181 | |
| 182 | read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 1), |
| 183 | strlen(split4)); |
| 184 | if (memcmp(buf, split4, strlen(split4))) { |
| 185 | test_msg("Data in the buffer doesn't match what it should " |
| 186 | "in the fourth split\n"); |
| 187 | ret = -EINVAL; |
| 188 | goto out; |
| 189 | } |
| 190 | |
| 191 | btrfs_item_key_to_cpu(eb, &key, 2); |
| 192 | if (key.objectid != 0 || key.type != BTRFS_EXTENT_CSUM_KEY || |
| 193 | key.offset != 3) { |
| 194 | test_msg("Invalid key at slot 2\n"); |
| 195 | ret = -EINVAL; |
| 196 | goto out; |
| 197 | } |
| 198 | |
| 199 | item = btrfs_item_nr(2); |
| 200 | if (btrfs_item_size(eb, item) != strlen(split2)) { |
| 201 | test_msg("Invalid len in the second split\n"); |
| 202 | ret = -EINVAL; |
| 203 | goto out; |
| 204 | } |
| 205 | |
| 206 | read_extent_buffer(eb, buf, btrfs_item_ptr_offset(eb, 2), |
| 207 | strlen(split2)); |
| 208 | if (memcmp(buf, split2, strlen(split2))) { |
| 209 | test_msg("Data in the buffer doesn't match what it should " |
| 210 | "in the last chunk\n"); |
| 211 | ret = -EINVAL; |
| 212 | goto out; |
| 213 | } |
| 214 | out: |
| 215 | btrfs_free_path(path); |
Jeff Mahoney | 7c0260e | 2016-06-20 14:14:09 -0400 | [diff] [blame] | 216 | btrfs_free_dummy_root(root); |
| 217 | btrfs_free_dummy_fs_info(fs_info); |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 218 | return ret; |
| 219 | } |
| 220 | |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 221 | int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize) |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 222 | { |
Feifei Xu | ef9f2db | 2016-06-01 19:18:28 +0800 | [diff] [blame] | 223 | test_msg("Running extent buffer operation tests\n"); |
Feifei Xu | b9ef22d | 2016-06-01 19:18:25 +0800 | [diff] [blame] | 224 | return test_btrfs_split_item(sectorsize, nodesize); |
Josef Bacik | 06ea65a | 2013-09-19 16:07:01 -0400 | [diff] [blame] | 225 | } |