Dmitry V. Levin | 2212918 | 2016-05-24 01:32:09 +0000 | [diff] [blame] | 1 | #include "tests.h" |
| 2 | |
| 3 | #ifdef HAVE_LINUX_BTRFS_H |
| 4 | |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 5 | #include <errno.h> |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 6 | #include <fcntl.h> |
Dmitry V. Levin | 28a5f66 | 2016-05-24 01:32:41 +0000 | [diff] [blame] | 7 | #include <inttypes.h> |
| 8 | #include <limits.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <unistd.h> |
| 15 | #include <sys/fcntl.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/vfs.h> |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/btrfs.h> |
| 21 | #include <linux/magic.h> |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 22 | #include "xlat.h" |
| 23 | |
| 24 | #include "xlat/btrfs_balance_args.h" |
| 25 | #include "xlat/btrfs_balance_flags.h" |
| 26 | #include "xlat/btrfs_balance_state.h" |
| 27 | #include "xlat/btrfs_compress_types.h" |
| 28 | #include "xlat/btrfs_defrag_flags.h" |
| 29 | #include "xlat/btrfs_dev_stats_values.h" |
| 30 | #include "xlat/btrfs_dev_stats_flags.h" |
| 31 | #include "xlat/btrfs_qgroup_inherit_flags.h" |
| 32 | #include "xlat/btrfs_qgroup_limit_flags.h" |
| 33 | #include "xlat/btrfs_scrub_flags.h" |
| 34 | #include "xlat/btrfs_send_flags.h" |
| 35 | #include "xlat/btrfs_space_info_flags.h" |
| 36 | #include "xlat/btrfs_snap_flags_v2.h" |
| 37 | #include "xlat/btrfs_tree_objectids.h" |
| 38 | #include "xlat/btrfs_features_compat.h" |
| 39 | #include "xlat/btrfs_features_compat_ro.h" |
| 40 | #include "xlat/btrfs_features_incompat.h" |
| 41 | #include "xlat/btrfs_key_types.h" |
| 42 | |
Dmitry V. Levin | 2212918 | 2016-05-24 01:32:09 +0000 | [diff] [blame] | 43 | #ifdef HAVE_LINUX_FIEMAP_H |
| 44 | # include <linux/fiemap.h> |
| 45 | # include "xlat/fiemap_flags.h" |
| 46 | # include "xlat/fiemap_extent_flags.h" |
| 47 | #endif |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 48 | |
| 49 | #ifndef BTRFS_LABEL_SIZE |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 50 | # define BTRFS_LABEL_SIZE 256 |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | #ifndef BTRFS_NAME_LEN |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 54 | # define BTRFS_NAME_LEN 255 |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 55 | #endif |
| 56 | |
Dmitry V. Levin | 6697d15 | 2016-05-24 02:56:39 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Prior to Linux 3.12, the BTRFS_IOC_DEFAULT_SUBVOL used u64 in |
| 59 | * its definition, which isn't exported by the kernel. |
| 60 | */ |
| 61 | typedef __u64 u64; |
| 62 | |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 63 | static const char *btrfs_test_root; |
| 64 | static int btrfs_test_dir_fd; |
| 65 | static bool verbose = false; |
| 66 | static bool write_ok = false; |
| 67 | |
| 68 | const unsigned char uuid_reference[BTRFS_UUID_SIZE] = { |
| 69 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, |
| 70 | 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, |
| 71 | }; |
| 72 | |
| 73 | const char uuid_reference_string[] = "01234567-89ab-cdef-fedc-ba9876543210"; |
| 74 | |
| 75 | #ifndef BTRFS_IOC_GET_FEATURES |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 76 | # define BTRFS_IOC_GET_FEATURES _IOR(BTRFS_IOCTL_MAGIC, 57, \ |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 77 | struct btrfs_ioctl_feature_flags) |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 78 | # define BTRFS_IOC_SET_FEATURES _IOW(BTRFS_IOCTL_MAGIC, 57, \ |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 79 | struct btrfs_ioctl_feature_flags[2]) |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 80 | # define BTRFS_IOC_GET_SUPPORTED_FEATURES _IOR(BTRFS_IOCTL_MAGIC, 57, \ |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 81 | struct btrfs_ioctl_feature_flags[3]) |
| 82 | #endif |
| 83 | |
| 84 | #ifndef HAVE_STRUCT_BTRFS_IOCTL_FEATURE_FLAGS_COMPAT_FLAGS |
| 85 | struct btrfs_ioctl_feature_flags { |
| 86 | uint64_t compat_flags; |
| 87 | uint64_t compat_ro_flags; |
| 88 | uint64_t incompat_flags; |
| 89 | }; |
| 90 | #endif |
| 91 | |
| 92 | #ifndef HAVE_STRUCT_BTRFS_IOCTL_DEFRAG_RANGE_ARGS_START |
| 93 | struct btrfs_ioctl_defrag_range_args { |
| 94 | uint64_t start; |
| 95 | uint64_t len; |
| 96 | uint64_t flags; |
| 97 | uint32_t extent_thresh; |
| 98 | uint32_t compress_type; |
| 99 | uint32_t unused[4]; |
| 100 | }; |
| 101 | #endif |
| 102 | |
| 103 | #ifndef FIDEDUPERANGE |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 104 | # define FIDEDUPERANGE _IOWR(0x94, 54, struct file_dedupe_range) |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 105 | struct file_dedupe_range_info { |
| 106 | int64_t dest_fd; /* in - destination file */ |
| 107 | uint64_t dest_offset; /* in - start of extent in destination */ |
| 108 | uint64_t bytes_deduped; /* out - total # of bytes we were able |
| 109 | * to dedupe from this file. */ |
| 110 | /* status of this dedupe operation: |
| 111 | * < 0 for error |
| 112 | * == FILE_DEDUPE_RANGE_SAME if dedupe succeeds |
| 113 | * == FILE_DEDUPE_RANGE_DIFFERS if data differs |
| 114 | */ |
| 115 | int32_t status; /* out - see above description */ |
| 116 | uint32_t reserved; /* must be zero */ |
| 117 | }; |
| 118 | |
| 119 | struct file_dedupe_range { |
| 120 | uint64_t src_offset; /* in - start of extent in source */ |
| 121 | uint64_t src_length; /* in - length of extent */ |
| 122 | uint16_t dest_count; /* in - total elements in info array */ |
| 123 | uint16_t reserved1; /* must be zero */ |
| 124 | uint32_t reserved2; /* must be zero */ |
| 125 | struct file_dedupe_range_info info[0]; |
| 126 | }; |
| 127 | #endif |
| 128 | |
| 129 | #ifndef BTRFS_IOC_TREE_SEARCH_V2 |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 130 | # define BTRFS_IOC_TREE_SEARCH_V2 _IOWR(BTRFS_IOCTL_MAGIC, 17, \ |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 131 | struct btrfs_ioctl_search_args_v2) |
| 132 | struct btrfs_ioctl_search_args_v2 { |
| 133 | struct btrfs_ioctl_search_key key; /* in/out - search parameters */ |
| 134 | uint64_t buf_size; /* in - size of buffer |
| 135 | * out - on EOVERFLOW: needed size |
| 136 | * to store item */ |
| 137 | uint64_t buf[0]; /* out - found items */ |
| 138 | }; |
| 139 | #endif |
| 140 | |
| 141 | |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 142 | static const char * |
| 143 | maybe_print_uint64max(uint64_t val) |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 144 | { |
| 145 | if (val == UINT64_MAX) |
| 146 | return " /* UINT64_MAX */"; |
| 147 | return ""; |
| 148 | } |
| 149 | |
| 150 | /* takes highest valid flag bit */ |
Dmitry V. Levin | 2447de4 | 2016-05-24 01:32:55 +0000 | [diff] [blame] | 151 | static uint64_t |
| 152 | max_flags_plus_one(int bit) |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 153 | { |
| 154 | int i; |
| 155 | uint64_t val = 0; |
| 156 | if (bit == -1) |
| 157 | return 1; |
| 158 | for (i = 0; i <= bit + 1 && i < 64; i++) |
| 159 | val |= (1ULL << i); |
| 160 | return val; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Consumes no arguments, returns nothing: |
| 165 | * |
| 166 | * - BTRFS_IOC_TRANS_START |
| 167 | * - BTRFS_IOC_TRANS_END |
| 168 | */ |
| 169 | static void |
| 170 | btrfs_test_trans_ioctls(void) |
| 171 | { |
| 172 | ioctl(-1, BTRFS_IOC_TRANS_START, NULL); |
| 173 | printf("ioctl(-1, BTRFS_IOC_TRANS_START) = -1 EBADF (%m)\n"); |
| 174 | |
| 175 | ioctl(-1, BTRFS_IOC_TRANS_END, NULL); |
| 176 | printf("ioctl(-1, BTRFS_IOC_TRANS_END) = -1 EBADF (%m)\n"); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Consumes no arguments, returns nothing: |
| 181 | * - BTRFS_IOC_SYNC |
| 182 | * |
| 183 | * Consumes argument, returns nothing |
| 184 | * - BTRFS_IOC_WAIT_SYNC |
| 185 | */ |
| 186 | static void |
| 187 | btrfs_test_sync_ioctls(void) |
| 188 | { |
Dmitry V. Levin | 57888fa | 2016-05-24 02:07:47 +0000 | [diff] [blame] | 189 | uint64_t u64val = 0xdeadbeefbadc0ded; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 190 | |
| 191 | ioctl(-1, BTRFS_IOC_SYNC, NULL); |
| 192 | printf("ioctl(-1, BTRFS_IOC_SYNC) = -1 EBADF (%m)\n"); |
| 193 | |
| 194 | ioctl(-1, BTRFS_IOC_WAIT_SYNC, NULL); |
| 195 | printf("ioctl(-1, BTRFS_IOC_WAIT_SYNC, NULL) = -1 EBADF (%m)\n"); |
| 196 | |
| 197 | ioctl(-1, BTRFS_IOC_WAIT_SYNC, &u64val); |
| 198 | printf("ioctl(-1, BTRFS_IOC_WAIT_SYNC, [%" PRIu64 |
| 199 | "]) = -1 EBADF (%m)\n", u64val); |
| 200 | |
| 201 | /* |
| 202 | * The live test of BTRFS_IOC_SYNC happens as a part of the test |
| 203 | * for BTRFS_IOC_LOGICAL_INO |
| 204 | */ |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | btrfs_print_qgroup_inherit(struct btrfs_qgroup_inherit *inherit) |
| 209 | { |
| 210 | printf("{flags="); |
| 211 | printflags(btrfs_qgroup_inherit_flags, inherit->flags, |
| 212 | "BTRFS_QGROUP_INHERIT_???"); |
| 213 | printf(", num_qgroups=%" PRI__u64 |
| 214 | ", num_ref_copies=%" PRI__u64 |
| 215 | ", num_excl_copies=%" PRI__u64", lim={flags=", |
| 216 | inherit->num_qgroups, inherit->num_ref_copies, |
| 217 | inherit->num_excl_copies); |
| 218 | printflags(btrfs_qgroup_limit_flags, |
| 219 | inherit->lim.flags, |
| 220 | "BTRFS_QGROUP_LIMIT_???"); |
| 221 | printf(", max_rfer=%" PRI__u64 ", max_excl=%" PRI__u64 |
| 222 | ", rsv_rfer=%" PRI__u64 ", rsv_excl=%" PRI__u64 |
| 223 | "}, qgroups=", |
| 224 | inherit->lim.max_rfer, inherit->lim.max_excl, |
| 225 | inherit->lim.rsv_rfer, inherit->lim.rsv_excl); |
| 226 | if (verbose) { |
| 227 | unsigned int i; |
| 228 | printf("["); |
| 229 | for (i = 0; i < inherit->num_qgroups; i++) { |
| 230 | if (i > 0) |
| 231 | printf(", "); |
| 232 | printf("%" PRI__u64, inherit->qgroups[i]); |
| 233 | } |
| 234 | printf("]"); |
| 235 | } else |
| 236 | printf("..."); |
| 237 | printf("}"); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | static void |
| 242 | btrfs_print_vol_args_v2(struct btrfs_ioctl_vol_args_v2 *args, int print_qgroups) |
| 243 | { |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 244 | printf("{fd=%d, flags=", (int) args->fd); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 245 | printflags(btrfs_snap_flags_v2, args->flags, "BTRFS_SUBVOL_???"); |
| 246 | |
| 247 | if (args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
| 248 | printf(", size=%" PRI__u64 ", qgroup_inherit=", args->size); |
| 249 | if (args->qgroup_inherit && print_qgroups) |
| 250 | btrfs_print_qgroup_inherit(args->qgroup_inherit); |
| 251 | else if (args->qgroup_inherit) |
| 252 | printf("%p", args->qgroup_inherit); |
| 253 | else |
| 254 | printf("NULL"); |
| 255 | } |
| 256 | printf(", name=\"%s\"}", args->name); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Consumes argument, returns nothing: |
| 261 | * - BTRFS_IOC_SNAP_CREATE |
| 262 | * - BTRFS_IOC_SUBVOL_CREATE |
| 263 | * - BTRFS_IOC_SNAP_DESTROY |
| 264 | * - BTRFS_IOC_DEFAULT_SUBVOL |
| 265 | * |
| 266 | * Consumes argument, returns u64: |
| 267 | * - BTRFS_IOC_SNAP_CREATE_V2 |
| 268 | * - BTRFS_IOC_SUBVOL_CREATE_V2 |
| 269 | */ |
| 270 | |
| 271 | static void |
| 272 | btrfs_test_subvol_ioctls(void) |
| 273 | { |
| 274 | const char *subvol_name = "subvol-name"; |
| 275 | char *long_subvol_name; |
Dmitry V. Levin | 57888fa | 2016-05-24 02:07:47 +0000 | [diff] [blame] | 276 | void *bad_pointer = (void *) (unsigned long) 0xdeadbeeffffffeed; |
| 277 | uint64_t u64val = 0xdeadbeefbadc0ded; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 278 | struct btrfs_ioctl_vol_args vol_args = {}; |
| 279 | struct btrfs_ioctl_vol_args_v2 vol_args_v2 = { |
| 280 | .fd = 2, |
| 281 | .flags = max_flags_plus_one(2), |
| 282 | }; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 283 | |
| 284 | long_subvol_name = malloc(BTRFS_PATH_NAME_MAX); |
| 285 | if (!long_subvol_name) |
| 286 | perror_msg_and_fail("malloc failed"); |
| 287 | memset(long_subvol_name, 'f', BTRFS_PATH_NAME_MAX); |
| 288 | long_subvol_name[BTRFS_PATH_NAME_MAX - 1] = '\0'; |
| 289 | |
| 290 | strcpy(vol_args.name, subvol_name); |
| 291 | |
| 292 | ioctl(-1, BTRFS_IOC_SNAP_CREATE, NULL); |
| 293 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE, NULL) = -1 EBADF (%m)\n"); |
| 294 | |
| 295 | ioctl(-1, BTRFS_IOC_SNAP_CREATE, &vol_args); |
| 296 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE, " |
| 297 | "{fd=0, name=\"%s\"}) = -1 EBADF (%m)\n", vol_args.name); |
| 298 | |
| 299 | ioctl(-1, BTRFS_IOC_SUBVOL_CREATE, &vol_args); |
| 300 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_CREATE, " |
| 301 | "{fd=0, name=\"%s\"}) = -1 EBADF (%m)\n", vol_args.name); |
| 302 | |
| 303 | ioctl(-1, BTRFS_IOC_SNAP_DESTROY, &vol_args); |
| 304 | printf("ioctl(-1, BTRFS_IOC_SNAP_DESTROY," |
| 305 | " {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n", vol_args.name); |
| 306 | |
| 307 | strncpy(vol_args.name, long_subvol_name, BTRFS_PATH_NAME_MAX); |
| 308 | ioctl(-1, BTRFS_IOC_SNAP_CREATE, &vol_args); |
| 309 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE," |
| 310 | " {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n", vol_args.name); |
| 311 | |
| 312 | ioctl(-1, BTRFS_IOC_SUBVOL_CREATE, &vol_args); |
| 313 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_CREATE," |
| 314 | " {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n", vol_args.name); |
| 315 | |
| 316 | ioctl(-1, BTRFS_IOC_SNAP_DESTROY, &vol_args); |
| 317 | printf("ioctl(-1, BTRFS_IOC_SNAP_DESTROY," |
| 318 | " {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n", vol_args.name); |
| 319 | |
| 320 | long_subvol_name = realloc(long_subvol_name, BTRFS_SUBVOL_NAME_MAX); |
| 321 | if (!long_subvol_name) |
| 322 | perror_msg_and_fail("realloc failed"); |
| 323 | |
| 324 | ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, NULL); |
| 325 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, NULL) = -1 EBADF (%m)\n"); |
| 326 | |
| 327 | ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, NULL); |
| 328 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, NULL) = -1 EBADF (%m)\n"); |
| 329 | |
| 330 | strcpy(vol_args_v2.name, subvol_name); |
| 331 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, "); |
| 332 | btrfs_print_vol_args_v2(&vol_args_v2, 1); |
| 333 | ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2); |
| 334 | printf(") = -1 EBADF (%m)\n"); |
| 335 | |
| 336 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, "); |
| 337 | btrfs_print_vol_args_v2(&vol_args_v2, 1); |
| 338 | ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2); |
| 339 | printf(") = -1 EBADF (%m)\n"); |
| 340 | |
| 341 | strncpy(vol_args_v2.name, long_subvol_name, BTRFS_SUBVOL_NAME_MAX); |
| 342 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, "); |
| 343 | btrfs_print_vol_args_v2(&vol_args_v2, 1); |
| 344 | ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2); |
| 345 | printf(") = -1 EBADF (%m)\n"); |
| 346 | |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 347 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, "); |
| 348 | btrfs_print_vol_args_v2(&vol_args_v2, 1); |
| 349 | ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2); |
| 350 | printf(") = -1 EBADF (%m)\n"); |
| 351 | |
| 352 | strcpy(vol_args_v2.name, subvol_name); |
Dmitry V. Levin | 45fcc0c | 2016-05-24 01:32:27 +0000 | [diff] [blame] | 353 | vol_args_v2.qgroup_inherit = bad_pointer; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 354 | |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 355 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, "); |
| 356 | btrfs_print_vol_args_v2(&vol_args_v2, 0); |
| 357 | ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2); |
| 358 | printf(") = -1 EBADF (%m)\n"); |
| 359 | |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 360 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, "); |
| 361 | btrfs_print_vol_args_v2(&vol_args_v2, 0); |
| 362 | ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2); |
| 363 | printf(") = -1 EBADF (%m)\n"); |
| 364 | |
Dmitry V. Levin | 45fcc0c | 2016-05-24 01:32:27 +0000 | [diff] [blame] | 365 | const unsigned int n_qgroups = 8; |
| 366 | unsigned int i; |
| 367 | struct btrfs_qgroup_inherit *inherit; |
| 368 | vol_args_v2.size = |
| 369 | sizeof(*inherit) + n_qgroups * sizeof(inherit->qgroups[0]); |
| 370 | inherit = tail_alloc(vol_args_v2.size); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 371 | |
Dmitry V. Levin | 45fcc0c | 2016-05-24 01:32:27 +0000 | [diff] [blame] | 372 | inherit->flags = 0x3; |
| 373 | inherit->num_ref_copies = 0; |
| 374 | inherit->num_excl_copies = 0; |
| 375 | inherit->num_qgroups = n_qgroups; |
| 376 | for (i = 0; i < n_qgroups; i++) |
| 377 | inherit->qgroups[i] = 1ULL << i; |
| 378 | inherit->lim.flags = 0x7f; |
| 379 | inherit->lim.max_rfer = u64val; |
| 380 | inherit->lim.max_excl = u64val; |
| 381 | inherit->lim.rsv_rfer = u64val; |
| 382 | inherit->lim.rsv_excl = u64val; |
| 383 | vol_args_v2.qgroup_inherit = inherit; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 384 | |
Dmitry V. Levin | 45fcc0c | 2016-05-24 01:32:27 +0000 | [diff] [blame] | 385 | printf("ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, "); |
| 386 | btrfs_print_vol_args_v2(&vol_args_v2, 1); |
| 387 | ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2); |
| 388 | printf(") = -1 EBADF (%m)\n"); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 389 | |
Dmitry V. Levin | 45fcc0c | 2016-05-24 01:32:27 +0000 | [diff] [blame] | 390 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, "); |
| 391 | btrfs_print_vol_args_v2(&vol_args_v2, 1); |
| 392 | ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2); |
| 393 | printf(") = -1 EBADF (%m)\n"); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 394 | |
| 395 | ioctl(-1, BTRFS_IOC_DEFAULT_SUBVOL, NULL); |
| 396 | printf("ioctl(-1, BTRFS_IOC_DEFAULT_SUBVOL, NULL) = -1 EBADF (%m)\n"); |
| 397 | |
| 398 | ioctl(-1, BTRFS_IOC_DEFAULT_SUBVOL, &u64val); |
| 399 | printf("ioctl(-1, BTRFS_IOC_DEFAULT_SUBVOL, [%" |
| 400 | PRIu64 "]) = -1 EBADF (%m)\n", u64val); |
| 401 | |
| 402 | printf("ioctl(-1, BTRFS_IOC_SUBVOL_SETFLAGS, "); |
| 403 | printflags(btrfs_snap_flags_v2, vol_args_v2.flags, |
| 404 | "BTRFS_SUBVOL_???"); |
| 405 | ioctl(-1, BTRFS_IOC_SUBVOL_SETFLAGS, &vol_args_v2.flags); |
| 406 | printf(") = -1 EBADF (%m)\n"); |
| 407 | |
| 408 | if (write_ok) { |
| 409 | struct btrfs_ioctl_vol_args_v2 args_passed; |
| 410 | /* |
| 411 | * Returns transid if flags & BTRFS_SUBVOL_CREATE_ASYNC |
| 412 | * - BTRFS_IOC_SNAP_CREATE_V2 |
| 413 | * - BTRFS_IOC_SUBVOL_CREATE_V2 |
| 414 | */ |
| 415 | int subvolfd; |
| 416 | |
| 417 | strncpy(vol_args_v2.name, subvol_name, |
| 418 | sizeof(vol_args_v2.name)); |
| 419 | vol_args_v2.flags = BTRFS_SUBVOL_CREATE_ASYNC; |
| 420 | vol_args_v2.size = 0; |
| 421 | vol_args_v2.qgroup_inherit = NULL; |
| 422 | args_passed = vol_args_v2; |
| 423 | printf("ioctl(%d, BTRFS_IOC_SUBVOL_CREATE_V2, ", |
| 424 | btrfs_test_dir_fd); |
| 425 | btrfs_print_vol_args_v2(&vol_args_v2, 1); |
| 426 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_SUBVOL_CREATE_V2, |
| 427 | &args_passed); |
| 428 | printf(" => {transid=%" PRI__u64"}) = 0\n", |
| 429 | args_passed.transid); |
| 430 | |
| 431 | subvolfd = openat(btrfs_test_dir_fd, subvol_name, |
| 432 | O_RDONLY|O_DIRECTORY); |
| 433 | if (subvolfd < 0) |
| 434 | perror_msg_and_fail("openat(%s) failed", subvol_name); |
| 435 | |
| 436 | strncpy(vol_args_v2.name, long_subvol_name, BTRFS_NAME_LEN); |
| 437 | vol_args_v2.fd = subvolfd; |
| 438 | args_passed = vol_args_v2; |
| 439 | printf("ioctl(%d, BTRFS_IOC_SNAP_CREATE_V2, ", |
| 440 | btrfs_test_dir_fd); |
| 441 | btrfs_print_vol_args_v2(&args_passed, 1); |
| 442 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_SNAP_CREATE_V2, |
| 443 | &args_passed); |
| 444 | printf(" => {transid=%" PRI__u64"}) = 0\n", |
| 445 | args_passed.transid); |
| 446 | |
| 447 | /* This only works when mounted w/ -ouser_subvol_rm_allowed */ |
| 448 | strncpy(vol_args.name, long_subvol_name, 255); |
| 449 | vol_args.name[255] = 0; |
| 450 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_SNAP_DESTROY, &vol_args); |
| 451 | printf("ioctl(%d, BTRFS_IOC_SNAP_DESTROY, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 452 | "{fd=%d, name=\"%.*s\"}) = 0\n", |
| 453 | btrfs_test_dir_fd, (int) vol_args.fd, 255, long_subvol_name); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 454 | |
| 455 | strcpy(vol_args.name, subvol_name); |
| 456 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_SNAP_DESTROY, &vol_args); |
| 457 | printf("ioctl(%d, BTRFS_IOC_SNAP_DESTROY, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 458 | "{fd=%d, name=\"%s\"}) = 0\n", |
| 459 | btrfs_test_dir_fd, (int) vol_args.fd, subvol_name); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 460 | |
| 461 | close(subvolfd); |
| 462 | } |
| 463 | free(long_subvol_name); |
| 464 | } |
| 465 | |
| 466 | static void |
| 467 | btrfs_print_balance_args(struct btrfs_balance_args *args) |
| 468 | { |
| 469 | printf("{profiles="); |
| 470 | printflags(btrfs_space_info_flags, args->profiles, |
| 471 | "BTRFS_BLOCK_GROUP_???"); |
| 472 | printf(", usage=%"PRI__u64 "%s, devid=%"PRI__u64 "%s, pstart=%"PRI__u64 |
| 473 | "%s, pend=%"PRI__u64 "%s, vstart=%"PRI__u64 "%s, vend=%"PRI__u64 |
| 474 | "%s, target=%"PRI__u64 "%s, flags=", |
| 475 | args->usage, maybe_print_uint64max(args->usage), |
| 476 | args->devid, maybe_print_uint64max(args->devid), |
| 477 | args->pstart, maybe_print_uint64max(args->pstart), |
| 478 | args->pend, maybe_print_uint64max(args->pend), |
| 479 | args->vstart, maybe_print_uint64max(args->vstart), |
| 480 | args->vend, maybe_print_uint64max(args->vend), |
| 481 | args->target, maybe_print_uint64max(args->target)); |
| 482 | printflags(btrfs_balance_args, args->flags, "BTRFS_BALANCE_ARGS_???"); |
| 483 | printf("}"); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Accepts argument, returns nothing |
| 488 | * - BTRFS_IOC_BALANCE |
| 489 | * - BTRFS_IOC_BALANCE_CTL |
| 490 | * |
| 491 | * Accepts argument, returns argument |
| 492 | * - BTRFS_IOC_BALANCE_V2 |
| 493 | */ |
| 494 | static void |
| 495 | btrfs_test_balance_ioctls(void) |
| 496 | { |
| 497 | struct btrfs_ioctl_balance_args args = { |
| 498 | .flags = 0x3f, |
| 499 | .data = { |
| 500 | .profiles = 0x7, |
| 501 | .flags = 0x7, |
| 502 | .devid = 1, |
| 503 | .pend = -1ULL, |
| 504 | .vend = -1ULL, |
| 505 | }, |
| 506 | |
| 507 | .meta = { |
| 508 | .profiles = 0x38, |
| 509 | .flags = 0x38, |
| 510 | .devid = 1, |
| 511 | }, |
| 512 | |
| 513 | .sys = { |
| 514 | .profiles = 0x1c0 | (1ULL << 48), |
| 515 | .flags = 0x4c0, |
| 516 | .devid = 1, |
| 517 | }, |
| 518 | }; |
| 519 | struct btrfs_ioctl_vol_args vol_args = {}; |
| 520 | |
| 521 | ioctl(-1, BTRFS_IOC_BALANCE_CTL, 1); |
| 522 | printf("ioctl(-1, BTRFS_IOC_BALANCE_CTL, " |
| 523 | "BTRFS_BALANCE_CTL_PAUSE) = -1 EBADF (%m)\n"); |
| 524 | |
| 525 | ioctl(-1, BTRFS_IOC_BALANCE_CTL, 2); |
| 526 | printf("ioctl(-1, BTRFS_IOC_BALANCE_CTL, " |
| 527 | "BTRFS_BALANCE_CTL_CANCEL) = -1 EBADF (%m)\n"); |
| 528 | |
| 529 | ioctl(-1, BTRFS_IOC_BALANCE, NULL); |
| 530 | printf("ioctl(-1, BTRFS_IOC_BALANCE) = -1 EBADF (%m)\n"); |
| 531 | |
| 532 | ioctl(-1, BTRFS_IOC_BALANCE, &vol_args); |
| 533 | printf("ioctl(-1, BTRFS_IOC_BALANCE) = -1 EBADF (%m)\n"); |
| 534 | |
| 535 | /* struct btrfs_ioctl_balance_args */ |
| 536 | ioctl(-1, BTRFS_IOC_BALANCE_V2, NULL); |
| 537 | printf("ioctl(-1, BTRFS_IOC_BALANCE_V2, NULL) = -1 EBADF (%m)\n"); |
| 538 | |
| 539 | printf("ioctl(-1, BTRFS_IOC_BALANCE_V2, {flags="); |
| 540 | printflags(btrfs_balance_flags, args.flags, "BTRFS_BALANCE_???"); |
| 541 | printf(", data="); |
| 542 | btrfs_print_balance_args(&args.data); |
| 543 | printf(", meta="); |
| 544 | btrfs_print_balance_args(&args.meta); |
| 545 | printf(", sys="); |
| 546 | btrfs_print_balance_args(&args.sys); |
| 547 | ioctl(-1, BTRFS_IOC_BALANCE_V2, &args); |
| 548 | printf("}) = -1 EBADF (%m)\n"); |
| 549 | |
| 550 | if (write_ok) { |
| 551 | args.flags = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA | |
| 552 | BTRFS_BALANCE_SYSTEM; |
| 553 | args.data.flags = 0; |
| 554 | args.data.profiles = 0; |
| 555 | args.meta.flags = 0; |
| 556 | args.meta.profiles = 0; |
| 557 | args.sys.flags = 0; |
| 558 | args.sys.profiles = 0; |
| 559 | printf("ioctl(%d, BTRFS_IOC_BALANCE_V2, {flags=", |
| 560 | btrfs_test_dir_fd); |
| 561 | |
| 562 | printflags(btrfs_balance_flags, args.flags, |
| 563 | "BTRFS_BALANCE_???"); |
| 564 | printf(", data="); |
| 565 | btrfs_print_balance_args(&args.data); |
| 566 | printf(", meta="); |
| 567 | btrfs_print_balance_args(&args.meta); |
| 568 | printf(", sys="); |
| 569 | btrfs_print_balance_args(&args.sys); |
| 570 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_BALANCE_V2, &args); |
| 571 | printf("} => {flags="); |
| 572 | printflags(btrfs_balance_flags, args.flags, |
| 573 | "BTRFS_BALANCE_???"); |
| 574 | printf(", state="); |
| 575 | printflags(btrfs_balance_state, args.state, |
| 576 | "BTRFS_BALANCE_STATE_???"); |
| 577 | printf(", data="); |
| 578 | btrfs_print_balance_args(&args.data); |
| 579 | printf(", meta="); |
| 580 | btrfs_print_balance_args(&args.meta); |
| 581 | printf(", sys="); |
| 582 | btrfs_print_balance_args(&args.sys); |
| 583 | printf("}) = 0\n"); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Consumes argument, returns nothing: |
| 589 | * - BTRFS_IOC_RESIZE |
| 590 | * |
| 591 | * Requires /dev/btrfs-control, consumes argument, returns nothing: |
| 592 | * - BTRFS_IOC_SCAN_DEV |
| 593 | * - BTRFS_IOC_DEVICES_READY |
| 594 | * |
| 595 | */ |
| 596 | static void |
| 597 | btrfs_test_device_ioctls(void) |
| 598 | { |
| 599 | const char *devid = "1"; |
| 600 | const char *devname = "/dev/sda1"; |
| 601 | struct btrfs_ioctl_vol_args args = { |
| 602 | .fd = 2, |
| 603 | }; |
| 604 | |
| 605 | ioctl(-1, BTRFS_IOC_RESIZE, NULL); |
| 606 | printf("ioctl(-1, BTRFS_IOC_RESIZE, NULL) = -1 EBADF (%m)\n"); |
| 607 | |
| 608 | strcpy(args.name, devid); |
| 609 | ioctl(-1, BTRFS_IOC_RESIZE, &args); |
| 610 | printf("ioctl(-1, BTRFS_IOC_RESIZE, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 611 | "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n", |
| 612 | (int) args.fd, args.name); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 613 | |
| 614 | ioctl(-1, BTRFS_IOC_SCAN_DEV, NULL); |
| 615 | printf("ioctl(-1, BTRFS_IOC_SCAN_DEV, NULL) = -1 EBADF (%m)\n"); |
| 616 | |
| 617 | strcpy(args.name, devname); |
| 618 | ioctl(-1, BTRFS_IOC_SCAN_DEV, &args); |
| 619 | printf("ioctl(-1, BTRFS_IOC_SCAN_DEV, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 620 | "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n", |
| 621 | (int) args.fd, args.name); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 622 | |
| 623 | ioctl(-1, BTRFS_IOC_ADD_DEV, NULL); |
| 624 | printf("ioctl(-1, BTRFS_IOC_ADD_DEV, NULL) = -1 EBADF (%m)\n"); |
| 625 | |
| 626 | ioctl(-1, BTRFS_IOC_ADD_DEV, &args); |
| 627 | printf("ioctl(-1, BTRFS_IOC_ADD_DEV, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 628 | "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n", |
| 629 | (int) args.fd, args.name); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 630 | |
| 631 | ioctl(-1, BTRFS_IOC_RM_DEV, NULL); |
| 632 | printf("ioctl(-1, BTRFS_IOC_RM_DEV, NULL) = -1 EBADF (%m)\n"); |
| 633 | |
| 634 | ioctl(-1, BTRFS_IOC_RM_DEV, &args); |
| 635 | printf("ioctl(-1, BTRFS_IOC_RM_DEV, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 636 | "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n", |
| 637 | (int) args.fd, args.name); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 638 | |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Consumes argument, returns nothing: |
| 643 | * - BTRFS_IOC_CLONE |
| 644 | * - BTRFS_IOC_CLONE_RANGE |
| 645 | */ |
| 646 | static void |
| 647 | btrfs_test_clone_ioctls(void) |
| 648 | { |
| 649 | int clone_fd = 4; |
| 650 | struct btrfs_ioctl_clone_range_args args = { |
| 651 | .src_fd = clone_fd, |
| 652 | .src_offset = 4096, |
| 653 | .src_length = 16384, |
| 654 | .dest_offset = 128 * 1024, |
| 655 | }; |
| 656 | |
| 657 | ioctl(-1, BTRFS_IOC_CLONE, clone_fd); |
| 658 | printf("ioctl(-1, BTRFS_IOC_CLONE or FICLONE, %x) = -1 EBADF (%m)\n", |
| 659 | clone_fd); |
| 660 | |
| 661 | ioctl(-1, BTRFS_IOC_CLONE_RANGE, NULL); |
| 662 | printf("ioctl(-1, BTRFS_IOC_CLONE_RANGE or FICLONERANGE, " |
| 663 | "NULL) = -1 EBADF (%m)\n"); |
| 664 | |
| 665 | ioctl(-1, BTRFS_IOC_CLONE_RANGE, &args); |
| 666 | printf("ioctl(-1, BTRFS_IOC_CLONE_RANGE or FICLONERANGE, " |
Dmitry V. Levin | 3f36611 | 2016-05-24 02:17:49 +0000 | [diff] [blame] | 667 | "{src_fd=%d, src_offset=%" PRI__u64 ", src_length=%" PRI__u64 |
| 668 | ", dest_offset=%" PRI__u64 "}) = -1 EBADF (%m)\n", |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 669 | (int) args.src_fd, args.src_offset, args.src_length, |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 670 | args.dest_offset); |
| 671 | } |
| 672 | |
| 673 | #define BTRFS_COMPRESS_TYPES 2 |
| 674 | #define BTRFS_INVALID_COMPRESS (BTRFS_COMPRESS_TYPES + 1) |
| 675 | |
| 676 | static void |
| 677 | btrfs_print_defrag_range_args(struct btrfs_ioctl_defrag_range_args *args) |
| 678 | { |
| 679 | printf("{start=%" PRIu64", len=%" PRIu64 "%s, flags=", |
| 680 | args->start, args->len, maybe_print_uint64max(args->len)); |
| 681 | |
| 682 | printflags(btrfs_defrag_flags, args->flags, "BTRFS_DEFRAG_RANGE_???"); |
| 683 | printf(", extent_thresh=%u, compress_type=", args->extent_thresh); |
| 684 | printxval(btrfs_compress_types, args->compress_type, |
| 685 | "BTRFS_COMPRESS_???"); |
| 686 | printf("}"); |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Consumes argument, returns nothing: |
| 691 | * - BTRFS_IOC_DEFRAG |
| 692 | * - BTRFS_DEFRAG_RANGE |
| 693 | */ |
| 694 | static void |
| 695 | btrfs_test_defrag_ioctls(void) |
| 696 | { |
| 697 | struct btrfs_ioctl_vol_args vol_args = {}; |
| 698 | struct btrfs_ioctl_defrag_range_args args = { |
| 699 | .start = 0, |
| 700 | .len = -1ULL, |
| 701 | .flags = max_flags_plus_one(1), |
| 702 | .extent_thresh = 128 * 1024, |
| 703 | .compress_type = 2, /* BTRFS_COMPRESS_LZO */ |
| 704 | }; |
| 705 | |
| 706 | /* |
| 707 | * These are documented as using vol_args but don't |
| 708 | * actually consume it. |
| 709 | */ |
| 710 | ioctl(-1, BTRFS_IOC_DEFRAG, NULL); |
| 711 | printf("ioctl(-1, BTRFS_IOC_DEFRAG) = -1 EBADF (%m)\n"); |
| 712 | |
| 713 | ioctl(-1, BTRFS_IOC_DEFRAG, &vol_args); |
| 714 | printf("ioctl(-1, BTRFS_IOC_DEFRAG) = -1 EBADF (%m)\n"); |
| 715 | |
| 716 | /* struct btrfs_ioctl_defrag_range_args */ |
| 717 | ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, NULL); |
| 718 | printf("ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, NULL) = -1 EBADF (%m)\n"); |
| 719 | |
| 720 | printf("ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, "); |
| 721 | btrfs_print_defrag_range_args(&args); |
| 722 | ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, &args); |
| 723 | printf(") = -1 EBADF (%m)\n"); |
| 724 | |
| 725 | args.compress_type = BTRFS_INVALID_COMPRESS; |
| 726 | printf("ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, "); |
| 727 | btrfs_print_defrag_range_args(&args); |
| 728 | ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, &args); |
| 729 | printf(") = -1 EBADF (%m)\n"); |
| 730 | |
| 731 | args.len--; |
| 732 | printf("ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, "); |
| 733 | btrfs_print_defrag_range_args(&args); |
| 734 | ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, &args); |
| 735 | printf(") = -1 EBADF (%m)\n"); |
| 736 | } |
| 737 | |
| 738 | static const char * |
| 739 | xlookup(const struct xlat *xlat, const uint64_t val) |
| 740 | { |
| 741 | for (; xlat->str != NULL; xlat++) |
| 742 | if (xlat->val == val) |
| 743 | return xlat->str; |
| 744 | return NULL; |
| 745 | } |
| 746 | |
| 747 | static void |
| 748 | btrfs_print_objectid(uint64_t objectid) |
| 749 | { |
| 750 | const char *str = xlookup(btrfs_tree_objectids, objectid); |
| 751 | printf("%" PRIu64, objectid); |
| 752 | if (str) |
| 753 | printf(" /* %s */", str); |
| 754 | } |
| 755 | |
| 756 | static void |
| 757 | btrfs_print_key_type(uint32_t type) |
| 758 | { |
| 759 | const char *str = xlookup(btrfs_key_types, type); |
| 760 | printf("%u", type); |
| 761 | if (str) |
| 762 | printf(" /* %s */", str); |
| 763 | } |
| 764 | |
| 765 | static void |
| 766 | btrfs_print_search_key(struct btrfs_ioctl_search_key *key) |
| 767 | { |
| 768 | printf("key={tree_id="); |
| 769 | btrfs_print_objectid(key->tree_id); |
| 770 | if (verbose || key->min_objectid != 256) { |
| 771 | printf(", min_objectid="); |
| 772 | btrfs_print_objectid(key->min_objectid); |
| 773 | } |
| 774 | if (verbose || key->max_objectid != -256ULL) { |
| 775 | printf(", max_objectid="); |
| 776 | btrfs_print_objectid(key->max_objectid); |
| 777 | } |
| 778 | if (key->min_offset) |
| 779 | printf(", min_offset=%" PRI__u64 "%s", |
| 780 | key->min_offset, maybe_print_uint64max(key->min_offset)); |
| 781 | if (key->max_offset) |
| 782 | printf(", max_offset=%" PRI__u64 "%s", |
| 783 | key->max_offset, maybe_print_uint64max(key->max_offset)); |
| 784 | if (key->min_transid) |
| 785 | printf(", min_transid=%" PRI__u64"%s", key->min_transid, |
| 786 | maybe_print_uint64max(key->min_transid)); |
| 787 | if (key->max_transid) |
| 788 | printf(", max_transid=%" PRI__u64"%s", key->max_transid, |
| 789 | maybe_print_uint64max(key->max_transid)); |
| 790 | printf(", min_type="); |
| 791 | btrfs_print_key_type(key->min_type); |
| 792 | printf(", max_type="); |
| 793 | btrfs_print_key_type(key->max_type); |
| 794 | printf(", nr_items=%u}", key->nr_items); |
| 795 | } |
| 796 | |
| 797 | static void |
| 798 | btrfs_print_tree_search_buf(struct btrfs_ioctl_search_key *key, |
| 799 | void *buf, uint64_t buf_size) |
| 800 | { |
| 801 | if (verbose) { |
| 802 | uint64_t i; |
| 803 | uint64_t off = 0; |
| 804 | printf("["); |
| 805 | for (i = 0; i < key->nr_items; i++) { |
| 806 | struct btrfs_ioctl_search_header *sh; |
| 807 | sh = (typeof(sh))(buf + off); |
| 808 | if (i) |
| 809 | printf(", "); |
| 810 | printf("{transid=%" PRI__u64 ", objectid=", |
| 811 | sh->transid); |
| 812 | btrfs_print_objectid(sh->objectid); |
| 813 | printf(", offset=%" PRI__u64 ", type=", sh->offset); |
| 814 | btrfs_print_key_type(sh->type); |
| 815 | printf(", len=%u}", sh->len); |
| 816 | off += sizeof(*sh) + sh->len; |
| 817 | } |
| 818 | printf("]"); |
| 819 | } else |
| 820 | printf("..."); |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Consumes argument, returns argument: |
| 825 | * - BTRFS_IOC_TREE_SEARCH |
| 826 | * - BTRFS_IOC_TREE_SEARCH_V2 |
| 827 | */ |
| 828 | static void |
| 829 | btrfs_test_search_ioctls(void) |
| 830 | { |
| 831 | struct btrfs_ioctl_search_key key_reference = { |
| 832 | .tree_id = 5, |
| 833 | .min_objectid = 256, |
| 834 | .max_objectid = -1ULL, |
| 835 | .min_offset = 0, |
| 836 | .max_offset = -1ULL, |
| 837 | .min_transid = 0, |
| 838 | .max_transid = -1ULL, |
| 839 | .min_type = 0, |
| 840 | .max_type = -1U, |
| 841 | .nr_items = 10, |
| 842 | }; |
| 843 | struct btrfs_ioctl_search_args search_args; |
| 844 | struct btrfs_ioctl_search_args_v2 search_args_v2 = { |
| 845 | .buf_size = 4096, |
| 846 | }; |
| 847 | |
| 848 | ioctl(-1, BTRFS_IOC_TREE_SEARCH, NULL); |
| 849 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH, NULL) = -1 EBADF (%m)\n"); |
| 850 | |
| 851 | ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, NULL); |
| 852 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, NULL) = -1 EBADF (%m)\n"); |
| 853 | |
Jeff Mahoney | 563a758 | 2016-05-26 16:09:28 -0400 | [diff] [blame] | 854 | search_args.key = key_reference; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 855 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH, {"); |
| 856 | btrfs_print_search_key(&search_args.key); |
| 857 | ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args); |
| 858 | printf("}) = -1 EBADF (%m)\n"); |
| 859 | |
| 860 | search_args_v2.key = key_reference; |
| 861 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, {"); |
| 862 | btrfs_print_search_key(&search_args_v2.key); |
| 863 | ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2); |
| 864 | printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n", |
| 865 | (uint64_t)search_args_v2.buf_size); |
| 866 | |
| 867 | key_reference.min_objectid = 6; |
| 868 | key_reference.max_objectid = 7; |
Jeff Mahoney | 563a758 | 2016-05-26 16:09:28 -0400 | [diff] [blame] | 869 | search_args.key = key_reference; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 870 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH, {"); |
| 871 | btrfs_print_search_key(&search_args.key); |
| 872 | ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args); |
| 873 | printf("}) = -1 EBADF (%m)\n"); |
| 874 | |
| 875 | search_args_v2.key = key_reference; |
| 876 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, {"); |
| 877 | btrfs_print_search_key(&search_args_v2.key); |
| 878 | ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2); |
| 879 | printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n", |
| 880 | (uint64_t)search_args_v2.buf_size); |
| 881 | |
| 882 | key_reference.min_offset++; |
| 883 | key_reference.max_offset--; |
Jeff Mahoney | 563a758 | 2016-05-26 16:09:28 -0400 | [diff] [blame] | 884 | search_args.key = key_reference; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 885 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH, {"); |
| 886 | btrfs_print_search_key(&search_args.key); |
| 887 | ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args); |
| 888 | printf("}) = -1 EBADF (%m)\n"); |
| 889 | |
| 890 | search_args_v2.key = key_reference; |
| 891 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, {"); |
| 892 | btrfs_print_search_key(&search_args_v2.key); |
| 893 | ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2); |
| 894 | printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n", |
| 895 | (uint64_t)search_args_v2.buf_size); |
| 896 | |
| 897 | key_reference.min_transid++; |
| 898 | key_reference.max_transid--; |
Jeff Mahoney | 563a758 | 2016-05-26 16:09:28 -0400 | [diff] [blame] | 899 | search_args.key = key_reference; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 900 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH, {"); |
| 901 | btrfs_print_search_key(&search_args.key); |
| 902 | ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args); |
| 903 | printf("}) = -1 EBADF (%m)\n"); |
| 904 | |
| 905 | search_args_v2.key = key_reference; |
| 906 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, {"); |
| 907 | btrfs_print_search_key(&search_args_v2.key); |
| 908 | ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2); |
| 909 | printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n", |
| 910 | (uint64_t)search_args_v2.buf_size); |
| 911 | |
| 912 | key_reference.min_type = 1; |
| 913 | key_reference.max_type = 12; |
Jeff Mahoney | 563a758 | 2016-05-26 16:09:28 -0400 | [diff] [blame] | 914 | search_args.key = key_reference; |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 915 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH, {"); |
| 916 | btrfs_print_search_key(&search_args.key); |
| 917 | ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args); |
| 918 | printf("}) = -1 EBADF (%m)\n"); |
| 919 | |
| 920 | search_args_v2.key = key_reference; |
| 921 | printf("ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, {"); |
| 922 | btrfs_print_search_key(&search_args_v2.key); |
| 923 | ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2); |
| 924 | printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n", |
| 925 | (uint64_t)search_args_v2.buf_size); |
| 926 | |
| 927 | if (btrfs_test_root) { |
| 928 | struct btrfs_ioctl_search_args_v2 *args; |
| 929 | int bufsize = 4096; |
| 930 | |
| 931 | key_reference.tree_id = 5; |
| 932 | key_reference.min_type = 1; |
| 933 | key_reference.max_type = 1; |
| 934 | key_reference.min_objectid = 256; |
| 935 | key_reference.max_objectid = 357; |
| 936 | key_reference.min_offset = 0; |
| 937 | key_reference.max_offset = -1ULL; |
| 938 | |
| 939 | search_args.key = key_reference; |
| 940 | printf("ioctl(%d, BTRFS_IOC_TREE_SEARCH, {", |
| 941 | btrfs_test_dir_fd); |
| 942 | btrfs_print_search_key(&search_args.key); |
| 943 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_TREE_SEARCH, &search_args); |
Jeff Mahoney | fd70b50 | 2016-05-26 23:33:05 -0400 | [diff] [blame] | 944 | printf("} => {key={nr_items=%u}, buf=", |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 945 | search_args.key.nr_items); |
Jeff Mahoney | fd70b50 | 2016-05-26 23:33:05 -0400 | [diff] [blame] | 946 | btrfs_print_tree_search_buf(&search_args.key, search_args.buf, |
| 947 | sizeof(search_args.buf)); |
| 948 | printf("}) = 0\n"); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 949 | |
| 950 | args = malloc(sizeof(*args) + bufsize); |
| 951 | if (!args) |
| 952 | perror_msg_and_fail("malloc failed"); |
| 953 | |
| 954 | args->key = key_reference; |
| 955 | args->buf_size = bufsize; |
| 956 | printf("ioctl(%d, BTRFS_IOC_TREE_SEARCH_V2, {", |
| 957 | btrfs_test_dir_fd); |
| 958 | btrfs_print_search_key(&key_reference); |
| 959 | printf(", buf_size=%" PRIu64 "}", (uint64_t) args->buf_size); |
| 960 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_TREE_SEARCH_V2, args); |
| 961 | printf(" => {key={nr_items=%u}, buf_size=%" PRIu64 ", buf=", |
| 962 | args->key.nr_items, (uint64_t)args->buf_size); |
| 963 | btrfs_print_tree_search_buf(&args->key, args->buf, |
| 964 | args->buf_size); |
| 965 | printf("}) = 0\n"); |
| 966 | |
| 967 | args->key = key_reference; |
| 968 | args->buf_size = sizeof(struct btrfs_ioctl_search_header); |
| 969 | printf("ioctl(%d, BTRFS_IOC_TREE_SEARCH_V2, {", |
| 970 | btrfs_test_dir_fd); |
| 971 | btrfs_print_search_key(&args->key); |
| 972 | printf(", buf_size=%" PRIu64 "}", (uint64_t)args->buf_size); |
| 973 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_TREE_SEARCH_V2, args); |
Jeff Mahoney | fd70b50 | 2016-05-26 23:33:05 -0400 | [diff] [blame] | 974 | printf(" => {buf_size=%" PRIu64 "}) = -1 EOVERFLOW (%m)\n", |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 975 | (uint64_t)args->buf_size); |
| 976 | free(args); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Consumes argument, returns argument: |
| 982 | * - BTRFS_IOC_INO_LOOKUP |
| 983 | */ |
| 984 | static void |
| 985 | btrfs_test_ino_lookup_ioctl(void) |
| 986 | { |
| 987 | struct btrfs_ioctl_ino_lookup_args args = { |
| 988 | .treeid = 5, |
| 989 | .objectid = 256, |
| 990 | }; |
| 991 | |
| 992 | ioctl(-1, BTRFS_IOC_INO_LOOKUP, NULL); |
| 993 | printf("ioctl(-1, BTRFS_IOC_INO_LOOKUP, NULL) = -1 EBADF (%m)\n"); |
| 994 | |
| 995 | printf("ioctl(-1, BTRFS_IOC_INO_LOOKUP, {treeid="); |
| 996 | btrfs_print_objectid(args.treeid); |
| 997 | printf(", objectid="); |
| 998 | btrfs_print_objectid(args.objectid); |
| 999 | ioctl(-1, BTRFS_IOC_INO_LOOKUP, &args); |
| 1000 | printf("}) = -1 EBADF (%m)\n"); |
| 1001 | |
| 1002 | if (btrfs_test_root) { |
| 1003 | printf("ioctl(%d, BTRFS_IOC_INO_LOOKUP, {treeid=", |
| 1004 | btrfs_test_dir_fd); |
| 1005 | btrfs_print_objectid(args.treeid); |
| 1006 | printf(", objectid="); |
| 1007 | btrfs_print_objectid(args.objectid); |
| 1008 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_INO_LOOKUP, &args); |
| 1009 | printf("} => {name=\"%s\"}) = 0\n", args.name); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Consumes argument, returns argument: |
| 1015 | * - BTRFS_IOC_SPACE_INFO |
| 1016 | */ |
| 1017 | static void |
| 1018 | btrfs_test_space_info_ioctl(void) |
| 1019 | { |
| 1020 | struct btrfs_ioctl_space_args args = {}; |
| 1021 | |
| 1022 | ioctl(-1, BTRFS_IOC_SPACE_INFO, NULL); |
| 1023 | printf("ioctl(-1, BTRFS_IOC_SPACE_INFO, NULL) = -1 EBADF (%m)\n"); |
| 1024 | |
| 1025 | ioctl(-1, BTRFS_IOC_SPACE_INFO, &args); |
| 1026 | printf("ioctl(-1, BTRFS_IOC_SPACE_INFO, " |
| 1027 | "{space_slots=%" PRI__u64 "}) = -1 EBADF (%m)\n", |
| 1028 | args.space_slots); |
| 1029 | |
| 1030 | if (btrfs_test_root) { |
| 1031 | struct btrfs_ioctl_space_args args_passed; |
| 1032 | struct btrfs_ioctl_space_args *argsp; |
| 1033 | args_passed = args; |
| 1034 | printf("ioctl(%d, BTRFS_IOC_SPACE_INFO, " |
| 1035 | "{space_slots=%" PRI__u64 "}", |
| 1036 | btrfs_test_dir_fd, args_passed.space_slots); |
| 1037 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_SPACE_INFO, &args_passed); |
| 1038 | printf(" => {total_spaces=%" PRI__u64 "}) = 0\n", |
| 1039 | args_passed.total_spaces); |
| 1040 | |
| 1041 | argsp = malloc(sizeof(args) + |
| 1042 | args_passed.total_spaces * sizeof(args.spaces[0])); |
| 1043 | if (!argsp) |
| 1044 | perror_msg_and_fail("malloc failed"); |
| 1045 | |
| 1046 | *argsp = args; |
| 1047 | argsp->space_slots = args_passed.total_spaces; |
| 1048 | printf("ioctl(%d, BTRFS_IOC_SPACE_INFO, " |
| 1049 | "{space_slots=%" PRI__u64 "}", |
| 1050 | btrfs_test_dir_fd, argsp->space_slots); |
| 1051 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_SPACE_INFO, argsp); |
| 1052 | printf(" => {total_spaces=%" PRI__u64 ", spaces=", |
| 1053 | argsp->total_spaces); |
| 1054 | if (verbose) { |
| 1055 | unsigned int i; |
| 1056 | printf("["); |
| 1057 | for (i = 0; i < argsp->total_spaces; i++) { |
| 1058 | struct btrfs_ioctl_space_info *info; |
| 1059 | info = &argsp->spaces[i]; |
| 1060 | if (i) |
| 1061 | printf(", "); |
| 1062 | printf("{flags="); |
| 1063 | printflags(btrfs_space_info_flags, info->flags, |
| 1064 | "BTRFS_SPACE_INFO_???"); |
| 1065 | printf(", total_bytes=%" PRI__u64 |
| 1066 | ", used_bytes=%" PRI__u64 "}", |
| 1067 | info->total_bytes, info->used_bytes); |
| 1068 | } |
| 1069 | |
| 1070 | printf("]"); |
| 1071 | } else |
| 1072 | printf("..."); |
| 1073 | printf("}) = 0\n"); |
| 1074 | free(argsp); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * Consumes no arguments, returns nothing: |
| 1080 | * - BTRFS_IOC_SCRUB_CANCEL |
| 1081 | * Consumes argument, returns argument: |
| 1082 | - * BTRFS_IOC_SCRUB |
| 1083 | - * BTRFS_IOC_SCRUB_PROGRESS |
| 1084 | */ |
| 1085 | static void |
| 1086 | btrfs_test_scrub_ioctls(void) |
| 1087 | { |
| 1088 | struct btrfs_ioctl_scrub_args args = { |
| 1089 | .devid = 1, |
| 1090 | .start = 0, |
| 1091 | .end = -1ULL, |
| 1092 | .flags = max_flags_plus_one(0), |
| 1093 | }; |
| 1094 | |
| 1095 | ioctl(-1, BTRFS_IOC_SCRUB, NULL); |
| 1096 | printf("ioctl(-1, BTRFS_IOC_SCRUB, NULL) = -1 EBADF (%m)\n"); |
| 1097 | |
| 1098 | ioctl(-1, BTRFS_IOC_SCRUB_CANCEL, NULL); |
| 1099 | printf("ioctl(-1, BTRFS_IOC_SCRUB_CANCEL) = -1 EBADF (%m)\n"); |
| 1100 | |
| 1101 | printf("ioctl(-1, BTRFS_IOC_SCRUB, {devid=%" PRI__u64 ", start=%" |
| 1102 | PRI__u64 "%s, end=%" PRI__u64"%s, flags=", |
| 1103 | args.devid, args.start, maybe_print_uint64max(args.start), |
| 1104 | args.end, maybe_print_uint64max(args.end)); |
| 1105 | printflags(btrfs_scrub_flags, args.flags, "BTRFS_SCRUB_???"); |
| 1106 | ioctl(-1, BTRFS_IOC_SCRUB, &args); |
| 1107 | printf("}) = -1 EBADF (%m)\n"); |
| 1108 | |
| 1109 | ioctl(-1, BTRFS_IOC_SCRUB_PROGRESS, NULL); |
| 1110 | printf("ioctl(-1, BTRFS_IOC_SCRUB_PROGRESS, NULL) = -1 EBADF (%m)\n"); |
| 1111 | |
| 1112 | ioctl(-1, BTRFS_IOC_SCRUB_PROGRESS, &args); |
| 1113 | printf("ioctl(-1, BTRFS_IOC_SCRUB_PROGRESS, " |
| 1114 | "{devid=%" PRI__u64 "}) = -1 EBADF (%m)\n", args.devid); |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * Consumes argument, returns argument: |
| 1119 | * - BTRFS_IOC_DEV_INFO |
| 1120 | */ |
| 1121 | static void |
| 1122 | btrfs_test_dev_info_ioctl(void) |
| 1123 | { |
| 1124 | struct btrfs_ioctl_dev_info_args args = { |
| 1125 | .devid = 1, |
| 1126 | }; |
| 1127 | memcpy(&args.uuid, uuid_reference, BTRFS_UUID_SIZE); |
| 1128 | |
| 1129 | ioctl(-1, BTRFS_IOC_DEV_INFO, NULL); |
| 1130 | printf("ioctl(-1, BTRFS_IOC_DEV_INFO, NULL) = -1 EBADF (%m)\n"); |
| 1131 | |
| 1132 | ioctl(-1, BTRFS_IOC_DEV_INFO, &args); |
| 1133 | printf("ioctl(-1, BTRFS_IOC_DEV_INFO, " |
| 1134 | "{devid=%" PRI__u64", uuid=%s}) = -1 EBADF (%m)\n", |
| 1135 | args.devid, uuid_reference_string); |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Consumes argument, returns argument: |
| 1140 | * - BTRFS_IOC_INO_PATHS |
| 1141 | * - BTRFS_IOC_LOGICAL_INO |
| 1142 | */ |
| 1143 | static void |
| 1144 | btrfs_test_ino_path_ioctls(void) |
| 1145 | { |
| 1146 | char buf[16384]; |
| 1147 | struct btrfs_ioctl_ino_path_args args = { |
| 1148 | .inum = 256, |
| 1149 | .size = sizeof(buf), |
| 1150 | .fspath = (unsigned long)buf, |
| 1151 | }; |
| 1152 | |
| 1153 | ioctl(-1, BTRFS_IOC_INO_PATHS, NULL); |
| 1154 | printf("ioctl(-1, BTRFS_IOC_INO_PATHS, NULL) = -1 EBADF (%m)\n"); |
| 1155 | |
| 1156 | ioctl(-1, BTRFS_IOC_LOGICAL_INO, NULL); |
| 1157 | printf("ioctl(-1, BTRFS_IOC_LOGICAL_INO, NULL) = -1 EBADF (%m)\n"); |
| 1158 | |
| 1159 | ioctl(-1, BTRFS_IOC_INO_PATHS, &args); |
| 1160 | printf("ioctl(-1, BTRFS_IOC_INO_PATHS, " |
| 1161 | "{inum=%" PRI__u64", size=%" PRI__u64 |
| 1162 | ", fspath=0x%" PRI__x64 "}) = -1 EBADF (%m)\n", |
| 1163 | args.inum, args.size, args.fspath); |
| 1164 | |
| 1165 | ioctl(-1, BTRFS_IOC_LOGICAL_INO, &args); |
| 1166 | printf("ioctl(-1, BTRFS_IOC_LOGICAL_INO, {logical=%" PRI__u64 |
| 1167 | ", size=%" PRI__u64", inodes=0x%" PRI__x64 |
| 1168 | "}) = -1 EBADF (%m)\n", args.inum, args.size, args.fspath); |
| 1169 | |
Dmitry V. Levin | 2212918 | 2016-05-24 01:32:09 +0000 | [diff] [blame] | 1170 | #ifdef HAVE_LINUX_FIEMAP_H |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1171 | if (btrfs_test_root) { |
| 1172 | int size; |
| 1173 | struct stat si; |
| 1174 | int ret; |
| 1175 | struct btrfs_data_container *data = (void *)buf; |
| 1176 | struct fiemap *fiemap; |
| 1177 | int fd; |
| 1178 | |
| 1179 | ret = fstat(btrfs_test_dir_fd, &si); |
| 1180 | if (ret) |
| 1181 | perror_msg_and_fail("fstat failed"); |
| 1182 | |
| 1183 | args.inum = si.st_ino; |
| 1184 | printf("ioctl(%d, BTRFS_IOC_INO_PATHS, " |
| 1185 | "{inum=%" PRI__u64", size=%" PRI__u64 |
| 1186 | ", fspath=0x%" PRI__x64"}", |
| 1187 | btrfs_test_dir_fd, args.inum, args.size, |
| 1188 | args.fspath); |
| 1189 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_INO_PATHS, &args); |
| 1190 | printf(" => {fspath={bytes_left=%u, bytes_missing=%u, elem_cnt=%u, elem_missed=%u, val=", |
| 1191 | data->bytes_left, data->bytes_missing, data->elem_cnt, |
| 1192 | data->elem_missed); |
| 1193 | if (verbose) { |
| 1194 | printf("[\"strace-test\"]"); |
| 1195 | } else |
| 1196 | printf("..."); |
| 1197 | printf("}}) = 0\n"); |
| 1198 | |
| 1199 | fd = openat(btrfs_test_dir_fd, "file1", O_RDWR|O_CREAT, 0600); |
| 1200 | if (fd < 0) |
| 1201 | perror_msg_and_fail("openat(file1) failed"); |
| 1202 | |
| 1203 | ret = fstat(fd, &si); |
| 1204 | if (ret) |
| 1205 | perror_msg_and_fail("fstat failed"); |
| 1206 | |
| 1207 | if (write(fd, buf, sizeof(buf)) < 0) |
| 1208 | perror_msg_and_fail("write: fd"); |
| 1209 | |
| 1210 | /* |
| 1211 | * Force delalloc so we can actually |
| 1212 | * search for the extent. |
| 1213 | */ |
| 1214 | fsync(fd); |
| 1215 | ioctl(fd, BTRFS_IOC_SYNC, NULL); |
| 1216 | printf("ioctl(%d, BTRFS_IOC_SYNC) = 0\n", fd); |
| 1217 | |
| 1218 | size = sizeof(*fiemap) + 2 * sizeof(fiemap->fm_extents[0]); |
| 1219 | fiemap = malloc(size); |
| 1220 | if (!fiemap) |
| 1221 | perror_msg_and_fail("malloc failed"); |
| 1222 | memset(fiemap, 0, size); |
| 1223 | |
| 1224 | fiemap->fm_length = sizeof(buf); |
| 1225 | fiemap->fm_extent_count = 2; |
| 1226 | |
| 1227 | /* This is also a live test for FIEMAP */ |
| 1228 | printf("ioctl(%d, FS_IOC_FIEMAP, {fm_start=%" PRI__u64 |
| 1229 | ", fm_length=%" PRI__u64", fm_flags=", |
| 1230 | fd, fiemap->fm_start, fiemap->fm_length); |
| 1231 | printflags(fiemap_flags, fiemap->fm_flags, "FIEMAP_FLAG_???"); |
| 1232 | printf(", fm_extent_count=%u}", fiemap->fm_extent_count); |
| 1233 | ioctl(fd, FS_IOC_FIEMAP, fiemap); |
| 1234 | printf(" => {fm_flags="); |
| 1235 | printflags(fiemap_flags, fiemap->fm_flags, "FIEMAP_FLAG_???"); |
| 1236 | printf(", fm_mapped_extents=%u, fm_extents=", |
| 1237 | fiemap->fm_mapped_extents); |
| 1238 | if (verbose) { |
| 1239 | printf("["); |
| 1240 | unsigned int i; |
| 1241 | for (i = 0; i < fiemap->fm_mapped_extents; i++) { |
| 1242 | struct fiemap_extent *fe; |
| 1243 | fe = &fiemap->fm_extents[i]; |
| 1244 | if (i) |
| 1245 | printf(", "); |
| 1246 | printf("{fe_logical=%" PRI__u64 |
| 1247 | ", fe_physical=%" PRI__u64 |
| 1248 | ", fe_length=%" PRI__u64 |
| 1249 | ", ", |
| 1250 | fe->fe_logical, fe->fe_physical, |
| 1251 | fe->fe_length); |
| 1252 | printflags(fiemap_extent_flags, fe->fe_flags, |
| 1253 | "FIEMAP_EXTENT_???"); |
| 1254 | printf("}"); |
| 1255 | } |
| 1256 | printf("]"); |
| 1257 | } else |
| 1258 | printf("..."); |
| 1259 | printf("}) = 0\n"); |
| 1260 | |
| 1261 | args.inum = fiemap->fm_extents[0].fe_physical; |
| 1262 | printf("ioctl(%d, BTRFS_IOC_LOGICAL_INO, {logical=%" PRI__u64 |
| 1263 | ", size=%" PRI__u64", inodes=0x%" PRI__x64"}", |
| 1264 | fd, args.inum, args.size, args.fspath); |
| 1265 | ioctl(fd, BTRFS_IOC_LOGICAL_INO, &args); |
| 1266 | printf(" => {inodes={bytes_left=%u, bytes_missing=%u, elem_cnt=%u, elem_missed=%u, val=", |
| 1267 | data->bytes_left, data->bytes_missing, data->elem_cnt, |
| 1268 | data->elem_missed); |
| 1269 | if (verbose) { |
Gleb Fotengauer-Malinovskiy | e79f038 | 2016-05-24 18:09:46 +0300 | [diff] [blame] | 1270 | printf("[{inum=%llu, offset=0, root=5}]", |
| 1271 | (unsigned long long) si.st_ino); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1272 | } else |
| 1273 | printf("..."); |
| 1274 | printf("}}) = 0\n"); |
| 1275 | close(fd); |
| 1276 | free(fiemap); |
| 1277 | } |
Dmitry V. Levin | 2212918 | 2016-05-24 01:32:09 +0000 | [diff] [blame] | 1278 | #endif /* HAVE_LINUX_FIEMAP_H */ |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * Consumes argument, returns argument: |
| 1283 | * - BTRFS_IOC_SET_RECEIVED_SUBVOL |
| 1284 | */ |
| 1285 | static void |
| 1286 | btrfs_test_set_received_subvol_ioctl(void) |
| 1287 | { |
| 1288 | struct btrfs_ioctl_received_subvol_args args = { |
| 1289 | .stransid = 0x12345, |
| 1290 | .stime = { |
| 1291 | .sec = 1463193386, |
| 1292 | .nsec = 12345, |
| 1293 | }, |
| 1294 | }; |
| 1295 | memcpy(&args.uuid, uuid_reference, BTRFS_UUID_SIZE); |
| 1296 | |
| 1297 | ioctl(-1, BTRFS_IOC_SET_RECEIVED_SUBVOL, NULL); |
| 1298 | printf("ioctl(-1, BTRFS_IOC_SET_RECEIVED_SUBVOL, " |
| 1299 | "NULL) = -1 EBADF (%m)\n"); |
| 1300 | |
| 1301 | ioctl(-1, BTRFS_IOC_SET_RECEIVED_SUBVOL, &args); |
| 1302 | printf("ioctl(-1, BTRFS_IOC_SET_RECEIVED_SUBVOL, " |
| 1303 | "{uuid=%s, stransid=%" PRI__u64", stime=%" PRI__u64 |
| 1304 | ".%u, flags=0}) = -1 EBADF (%m)\n", |
| 1305 | uuid_reference_string, args.stransid, args.stime.sec, |
| 1306 | args.stime.nsec); |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * Consumes argument, returns nothing (output is via send_fd) |
| 1311 | * - BTRFS_IOC_SEND |
| 1312 | */ |
| 1313 | static void |
| 1314 | btrfs_test_send_ioctl(void) |
| 1315 | { |
| 1316 | uint64_t u64_array[2] = { 256, 257 }; |
| 1317 | struct btrfs_ioctl_send_args args = { |
| 1318 | .send_fd = 4, |
| 1319 | .parent_root = 257, |
| 1320 | .flags = max_flags_plus_one(2), |
| 1321 | }; |
| 1322 | |
| 1323 | ioctl(-1, BTRFS_IOC_SEND, NULL); |
| 1324 | printf("ioctl(-1, BTRFS_IOC_SEND, NULL) = -1 EBADF (%m)\n"); |
| 1325 | |
| 1326 | printf("ioctl(-1, BTRFS_IOC_SEND, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 1327 | "{send_fd=%d, clone_sources_count=%" PRI__u64 |
| 1328 | ", clone_sources=", |
| 1329 | (int) args.send_fd, args.clone_sources_count); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1330 | if (verbose) |
| 1331 | printf("NULL"); |
| 1332 | else |
| 1333 | printf("..."); |
| 1334 | printf(", parent_root="); |
| 1335 | btrfs_print_objectid(args.parent_root); |
| 1336 | printf(", flags="); |
| 1337 | printflags(btrfs_send_flags, args.flags, "BTRFS_SEND_FLAGS_???"); |
| 1338 | ioctl(-1, BTRFS_IOC_SEND, &args); |
| 1339 | printf("}) = -1 EBADF (%m)\n"); |
| 1340 | |
| 1341 | args.clone_sources_count = 2; |
| 1342 | args.clone_sources = (__u64 *)u64_array; |
| 1343 | |
| 1344 | printf("ioctl(-1, BTRFS_IOC_SEND, " |
Dmitry V. Levin | 6ce6d3b | 2016-05-24 01:53:02 +0000 | [diff] [blame] | 1345 | "{send_fd=%d, clone_sources_count=%" PRI__u64 |
| 1346 | ", clone_sources=", |
| 1347 | (int) args.send_fd, args.clone_sources_count); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1348 | if (verbose) { |
| 1349 | printf("["); |
| 1350 | btrfs_print_objectid(u64_array[0]); |
| 1351 | printf(", "); |
| 1352 | btrfs_print_objectid(u64_array[1]); |
| 1353 | printf("]"); |
| 1354 | } else |
| 1355 | printf("..."); |
| 1356 | printf(", parent_root="); |
| 1357 | btrfs_print_objectid(args.parent_root); |
| 1358 | printf(", flags="); |
| 1359 | printflags(btrfs_send_flags, args.flags, "BTRFS_SEND_FLAGS_???"); |
| 1360 | ioctl(-1, BTRFS_IOC_SEND, &args); |
| 1361 | printf("}) = -1 EBADF (%m)\n"); |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * Consumes argument, returns nothing: |
| 1366 | * - BTRFS_IOC_QUOTA_CTL |
| 1367 | */ |
| 1368 | static void |
| 1369 | btrfs_test_quota_ctl_ioctl(void) |
| 1370 | { |
| 1371 | struct btrfs_ioctl_quota_ctl_args args = { |
| 1372 | .cmd = 1, |
| 1373 | }; |
| 1374 | |
| 1375 | ioctl(-1, BTRFS_IOC_QUOTA_CTL, NULL); |
| 1376 | printf("ioctl(-1, BTRFS_IOC_QUOTA_CTL, NULL) = -1 EBADF (%m)\n"); |
| 1377 | |
| 1378 | ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args); |
| 1379 | printf("ioctl(-1, BTRFS_IOC_QUOTA_CTL, " |
| 1380 | "BTRFS_QUOTA_CTL_ENABLE}) = -1 EBADF (%m)\n"); |
| 1381 | |
| 1382 | args.cmd = 2; |
| 1383 | ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args); |
| 1384 | printf("ioctl(-1, BTRFS_IOC_QUOTA_CTL, " |
| 1385 | "BTRFS_QUOTA_CTL_DISABLE}) = -1 EBADF (%m)\n"); |
| 1386 | |
| 1387 | args.cmd = 3; |
| 1388 | ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args); |
| 1389 | printf("ioctl(-1, BTRFS_IOC_QUOTA_CTL, " |
| 1390 | "BTRFS_QUOTA_CTL_RESCAN__NOTUSED}) = -1 EBADF (%m)\n"); |
| 1391 | |
| 1392 | args.cmd = 4; |
| 1393 | ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args); |
| 1394 | printf("ioctl(-1, BTRFS_IOC_QUOTA_CTL, " |
| 1395 | "0x4 /* BTRFS_QUOTA_CTL_??? */}) = -1 EBADF (%m)\n"); |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | * Consumes argument, returns nothing: |
| 1400 | * - BTRFS_IOC_QGROUP_ASSIGN |
| 1401 | */ |
| 1402 | static void |
| 1403 | btrfs_test_qgroup_assign_ioctl(void) |
| 1404 | { |
| 1405 | struct btrfs_ioctl_qgroup_assign_args args = { |
| 1406 | .assign = 1, |
| 1407 | .src = 257, |
| 1408 | .dst = 258, |
| 1409 | }; |
| 1410 | |
| 1411 | ioctl(-1, BTRFS_IOC_QGROUP_ASSIGN, NULL); |
| 1412 | printf("ioctl(-1, BTRFS_IOC_QGROUP_ASSIGN, NULL) = -1 EBADF (%m)\n"); |
| 1413 | |
| 1414 | ioctl(-1, BTRFS_IOC_QGROUP_ASSIGN, &args); |
| 1415 | printf("ioctl(-1, BTRFS_IOC_QGROUP_ASSIGN, " |
| 1416 | "{assign=%" PRI__u64", src=%" PRI__u64", dst=%" PRI__u64 |
| 1417 | "}) = -1 EBADF (%m)\n", args.assign, args.src, args.dst); |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * Consumes argument, returns nothing: |
| 1422 | * - BTRFS_IOC_QGROUP_CREATE |
| 1423 | */ |
| 1424 | static void |
| 1425 | btrfs_test_qgroup_create_ioctl(void) |
| 1426 | { |
| 1427 | struct btrfs_ioctl_qgroup_create_args args = { |
| 1428 | .create = 1, |
| 1429 | .qgroupid = 257, |
| 1430 | }; |
| 1431 | |
| 1432 | ioctl(-1, BTRFS_IOC_QGROUP_CREATE, NULL); |
| 1433 | printf("ioctl(-1, BTRFS_IOC_QGROUP_CREATE, NULL) = -1 EBADF (%m)\n"); |
| 1434 | |
| 1435 | ioctl(-1, BTRFS_IOC_QGROUP_CREATE, &args); |
| 1436 | printf("ioctl(-1, BTRFS_IOC_QGROUP_CREATE, " |
| 1437 | "{create=%" PRI__u64", qgroupid=%" PRI__u64 |
| 1438 | "}) = -1 EBADF (%m)\n", args.create, args.qgroupid); |
| 1439 | } |
| 1440 | |
| 1441 | /* |
| 1442 | * Consumes nothing, returns nothing: |
| 1443 | * - BTRFS_IOC_QUOTA_RESCAN_WAIT |
| 1444 | * Consumes argument, returns nothing: |
| 1445 | * - BTRFS_IOC_QUOTA_RESCAN |
| 1446 | */ |
| 1447 | static void |
| 1448 | btrfs_test_quota_rescan_ioctl(void) |
| 1449 | { |
| 1450 | struct btrfs_ioctl_quota_rescan_args args = { |
| 1451 | .progress = 1, |
| 1452 | }; |
| 1453 | |
| 1454 | ioctl(-1, BTRFS_IOC_QUOTA_RESCAN, NULL); |
| 1455 | printf("ioctl(-1, BTRFS_IOC_QUOTA_RESCAN, NULL) = -1 EBADF (%m)\n"); |
| 1456 | |
| 1457 | ioctl(-1, BTRFS_IOC_QUOTA_RESCAN, &args); |
| 1458 | printf("ioctl(-1, BTRFS_IOC_QUOTA_RESCAN, " |
| 1459 | "{flags=0}) = -1 EBADF (%m)\n"); |
| 1460 | ioctl(-1, BTRFS_IOC_QUOTA_RESCAN_WAIT, NULL); |
| 1461 | printf("ioctl(-1, BTRFS_IOC_QUOTA_RESCAN_WAIT) = -1 EBADF (%m)\n"); |
| 1462 | |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * Consumes argument, returns nothing: |
| 1467 | * - BTRFS_IOC_SET_FSLABEL |
| 1468 | * |
| 1469 | * Consumes no argument, returns argument: |
| 1470 | * - BTRFS_IOC_GET_FS_LABEL |
| 1471 | */ |
| 1472 | static void |
| 1473 | btrfs_test_label_ioctls(void) |
| 1474 | { |
| 1475 | char label[BTRFS_LABEL_SIZE] = "btrfs-label"; |
| 1476 | |
| 1477 | ioctl(-1, BTRFS_IOC_SET_FSLABEL, NULL); |
| 1478 | printf("ioctl(-1, BTRFS_IOC_SET_FSLABEL, NULL) = -1 EBADF (%m)\n"); |
| 1479 | |
| 1480 | ioctl(-1, BTRFS_IOC_SET_FSLABEL, label); |
| 1481 | printf("ioctl(-1, BTRFS_IOC_SET_FSLABEL, \"%s\") = -1 EBADF (%m)\n", |
| 1482 | label); |
| 1483 | |
| 1484 | if (write_ok) { |
| 1485 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_SET_FSLABEL, label); |
| 1486 | printf("ioctl(%d, BTRFS_IOC_SET_FSLABEL, \"%s\") = 0\n", |
| 1487 | btrfs_test_dir_fd, label); |
| 1488 | |
| 1489 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_FSLABEL, label); |
| 1490 | printf("ioctl(%d, BTRFS_IOC_GET_FSLABEL, \"%s\") = 0\n", |
| 1491 | btrfs_test_dir_fd, label); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * Consumes argument, returns argument: |
| 1497 | * - BTRFS_IOC_GET_DEV_STATS |
| 1498 | */ |
| 1499 | static void |
| 1500 | btrfs_test_get_dev_stats_ioctl(void) |
| 1501 | { |
| 1502 | struct btrfs_ioctl_get_dev_stats args = { |
| 1503 | .devid = 1, |
| 1504 | .nr_items = 5, |
| 1505 | .flags = max_flags_plus_one(0), |
| 1506 | }; |
| 1507 | |
| 1508 | ioctl(-1, BTRFS_IOC_GET_DEV_STATS, NULL); |
| 1509 | printf("ioctl(-1, BTRFS_IOC_GET_DEV_STATS, NULL) = -1 EBADF (%m)\n"); |
| 1510 | |
| 1511 | printf("ioctl(-1, BTRFS_IOC_GET_DEV_STATS, {devid=%" PRI__u64 |
| 1512 | ", nr_items=%" PRI__u64", flags=", |
| 1513 | args.devid, args.nr_items); |
| 1514 | printflags(btrfs_dev_stats_flags, args.flags, |
| 1515 | "BTRFS_DEV_STATS_???"); |
| 1516 | ioctl(-1, BTRFS_IOC_GET_DEV_STATS, &args); |
| 1517 | printf("}) = -1 EBADF (%m)\n"); |
| 1518 | |
| 1519 | if (write_ok) { |
| 1520 | unsigned int i; |
| 1521 | args.flags = BTRFS_DEV_STATS_RESET; |
| 1522 | printf("ioctl(%d, BTRFS_IOC_GET_DEV_STATS, {devid=%" PRI__u64 |
| 1523 | ", nr_items=%" PRI__u64", flags=", |
| 1524 | btrfs_test_dir_fd, args.devid, args.nr_items); |
| 1525 | printflags(btrfs_dev_stats_flags, args.flags, |
| 1526 | "BTRFS_DEV_STATS_???"); |
| 1527 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_DEV_STATS, &args); |
| 1528 | printf("} => {nr_items=%" PRI__u64 ", flags=", |
| 1529 | args.nr_items); |
| 1530 | printflags(btrfs_dev_stats_flags, args.flags, |
| 1531 | "BTRFS_DEV_STATS_???"); |
| 1532 | printf(", ["); |
| 1533 | for (i = 0; i < args.nr_items; i++) { |
| 1534 | const char *name = xlookup(btrfs_dev_stats_values, i); |
| 1535 | if (i) |
| 1536 | printf(", "); |
| 1537 | if (name) |
| 1538 | printf("/* %s */ ", name); |
| 1539 | printf("%" PRI__u64, args.values[i]); |
| 1540 | } |
| 1541 | printf("]}) = 0\n"); |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * Consumes argument, returns argument: |
| 1547 | * - BTRFS_IOC_DEV_REPLACE |
| 1548 | * |
| 1549 | * Test environment for this is more difficult; It's better to do it by hand. |
| 1550 | */ |
| 1551 | static void |
| 1552 | btrfs_test_dev_replace_ioctl(void) |
| 1553 | { |
| 1554 | struct btrfs_ioctl_dev_replace_args args = { |
| 1555 | .cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_START, |
| 1556 | .start = { |
| 1557 | .srcdevid = 1, |
| 1558 | }, |
| 1559 | }; |
| 1560 | strcpy((char *)args.start.srcdev_name, "/dev/sda1"); |
| 1561 | strcpy((char *)args.start.tgtdev_name, "/dev/sdb1"); |
| 1562 | |
| 1563 | /* struct btrfs_ioctl_dev_replace_args */ |
| 1564 | ioctl(-1, BTRFS_IOC_DEV_REPLACE, NULL); |
| 1565 | printf("ioctl(-1, BTRFS_IOC_DEV_REPLACE, NULL) = -1 EBADF (%m)\n"); |
| 1566 | |
| 1567 | ioctl(-1, BTRFS_IOC_DEV_REPLACE, &args); |
| 1568 | printf("ioctl(-1, BTRFS_IOC_DEV_REPLACE, " |
| 1569 | "{cmd=BTRFS_IOCTL_DEV_REPLACE_CMD_START, start={srcdevid=%" |
| 1570 | PRI__u64", cont_reading_from_srcdev_mode=%" PRI__u64 |
| 1571 | ", srcdev_name=\"%s\", tgtdev_name=\"%s\"}}) = -1 EBADF (%m)\n", |
| 1572 | args.start.srcdevid, |
| 1573 | args.start.cont_reading_from_srcdev_mode, |
| 1574 | (char *)args.start.srcdev_name, |
| 1575 | (char *)args.start.tgtdev_name); |
| 1576 | |
| 1577 | args.cmd = 1; |
| 1578 | ioctl(-1, BTRFS_IOC_DEV_REPLACE, &args); |
| 1579 | printf("ioctl(-1, BTRFS_IOC_DEV_REPLACE, " |
| 1580 | "{cmd=BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS}) = -1 EBADF (%m)\n"); |
| 1581 | } |
| 1582 | |
| 1583 | static void |
| 1584 | btrfs_test_extent_same_ioctl(void) |
| 1585 | { |
Dmitry V. Levin | ea516df | 2016-05-24 02:40:13 +0000 | [diff] [blame] | 1586 | #ifdef BTRFS_IOC_FILE_EXTENT_SAME |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1587 | struct file_dedupe_range args = { |
| 1588 | .src_offset = 1024, |
| 1589 | .src_length = 10240, |
| 1590 | }; |
| 1591 | struct file_dedupe_range *argsp; |
| 1592 | |
| 1593 | ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, NULL); |
| 1594 | printf("ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE, " |
| 1595 | "NULL) = -1 EBADF (%m)\n"); |
| 1596 | |
| 1597 | printf("ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE, " |
| 1598 | "{src_offset=%" PRIu64 |
| 1599 | ", src_length=%" PRIu64 |
| 1600 | ", dest_count=%hu, info=", |
| 1601 | (uint64_t)args.src_offset, |
| 1602 | (uint64_t)args.src_length, args.dest_count); |
| 1603 | if (verbose) |
| 1604 | printf("[]"); |
| 1605 | else |
| 1606 | printf("..."); |
| 1607 | ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, &args); |
| 1608 | printf("}) = -1 EBADF (%m)\n"); |
| 1609 | |
| 1610 | argsp = malloc(sizeof(*argsp) + sizeof(argsp->info[0]) * 2); |
| 1611 | if (!argsp) |
| 1612 | perror_msg_and_fail("malloc failed"); |
| 1613 | memset(argsp, 0, sizeof(*argsp) + sizeof(argsp->info[0]) * 2); |
| 1614 | |
| 1615 | *argsp = args; |
| 1616 | argsp->dest_count = 2; |
| 1617 | argsp->info[0].dest_fd = 2; |
| 1618 | argsp->info[0].dest_offset = 0; |
| 1619 | argsp->info[1].dest_fd = 2; |
| 1620 | argsp->info[1].dest_offset = 10240; |
| 1621 | |
| 1622 | printf("ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE, " |
| 1623 | "{src_offset=%" PRIu64 |
| 1624 | ", src_length=%" PRIu64 |
| 1625 | ", dest_count=%hu, info=", |
| 1626 | (int64_t)argsp->src_offset, |
| 1627 | (uint64_t)argsp->src_length, argsp->dest_count); |
| 1628 | if (verbose) |
| 1629 | printf("[{dest_fd=%" PRId64 ", dest_offset=%" PRIu64 |
| 1630 | "}, {dest_fd=%" PRId64 ", dest_offset=%"PRIu64 |
| 1631 | "}]", |
| 1632 | (int64_t)argsp->info[0].dest_fd, |
| 1633 | (uint64_t)argsp->info[0].dest_offset, |
| 1634 | (int64_t)argsp->info[1].dest_fd, |
| 1635 | (uint64_t)argsp->info[1].dest_offset); |
| 1636 | else |
| 1637 | printf("..."); |
| 1638 | ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, argsp); |
| 1639 | printf("}) = -1 EBADF (%m)\n"); |
| 1640 | |
| 1641 | if (write_ok) { |
| 1642 | int fd1, fd2; |
| 1643 | char buf[16384]; |
| 1644 | int size = sizeof(*argsp) + sizeof(argsp->info[0]); |
| 1645 | |
| 1646 | memset(buf, 0, sizeof(buf)); |
| 1647 | |
| 1648 | fd1 = openat(btrfs_test_dir_fd, "file1", O_RDWR|O_CREAT, 0600); |
| 1649 | if (fd1 < 0) |
| 1650 | perror_msg_and_fail("open file1 failed"); |
| 1651 | |
| 1652 | fd2 = openat(btrfs_test_dir_fd, "file2", O_RDWR|O_CREAT, 0600); |
| 1653 | if (fd2 < 0) |
| 1654 | perror_msg_and_fail("open file2 failed"); |
| 1655 | |
| 1656 | if (write(fd1, buf, sizeof(buf)) < 0) |
| 1657 | perror_msg_and_fail("write: fd1"); |
| 1658 | if (write(fd2, buf, sizeof(buf)) < 0) |
| 1659 | perror_msg_and_fail("write: fd2"); |
| 1660 | |
| 1661 | close(fd2); |
| 1662 | fd2 = openat(btrfs_test_dir_fd, "file2", O_RDONLY); |
| 1663 | if (fd2 < 0) |
| 1664 | perror_msg_and_fail("open file2 failed"); |
| 1665 | |
| 1666 | argsp = realloc(argsp, size); |
| 1667 | if (!argsp) |
| 1668 | perror_msg_and_fail("realloc failed"); |
| 1669 | memset(argsp, 0, size); |
| 1670 | |
| 1671 | argsp->src_offset = 0; |
| 1672 | argsp->src_length = sizeof(buf); |
| 1673 | argsp->dest_count = 1; |
| 1674 | argsp->info[0].dest_fd = fd2; |
| 1675 | argsp->info[0].dest_offset = 0; |
| 1676 | |
| 1677 | printf("ioctl(%d, BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE, " |
| 1678 | "{src_offset=%" PRIu64 ", src_length=%" PRIu64 |
| 1679 | ", dest_count=%hu, info=", fd1, |
| 1680 | (uint64_t)argsp->src_offset, |
| 1681 | (uint64_t)argsp->src_length, argsp->dest_count); |
| 1682 | if (verbose) |
| 1683 | printf("[{dest_fd=%d, dest_offset=0}]", fd2); |
| 1684 | else |
| 1685 | printf("..."); |
| 1686 | ioctl(fd1, BTRFS_IOC_FILE_EXTENT_SAME, argsp); |
| 1687 | printf("} => {info="); |
| 1688 | if (verbose) |
| 1689 | printf("[{bytes_deduped=%u, status=0}]", |
| 1690 | (unsigned) sizeof(buf)); |
| 1691 | else |
| 1692 | printf("..."); |
| 1693 | printf("}) = 0\n"); |
| 1694 | close(fd1); |
| 1695 | close(fd2); |
| 1696 | unlinkat(btrfs_test_dir_fd, "file1", 0); |
| 1697 | unlinkat(btrfs_test_dir_fd, "file2", 0); |
| 1698 | close(fd1); |
| 1699 | close(fd2); |
| 1700 | } |
| 1701 | free(argsp); |
Dmitry V. Levin | ea516df | 2016-05-24 02:40:13 +0000 | [diff] [blame] | 1702 | #endif /* BTRFS_IOC_FILE_EXTENT_SAME */ |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | static void |
| 1706 | btrfs_print_features(struct btrfs_ioctl_feature_flags *flags) |
| 1707 | { |
| 1708 | printf("{compat_flags="); |
| 1709 | printflags(btrfs_features_compat, flags->compat_flags, |
| 1710 | "BTRFS_FEATURE_COMPAT_???"); |
| 1711 | |
| 1712 | printf(", compat_ro_flags="); |
| 1713 | printflags(btrfs_features_compat_ro, flags->compat_ro_flags, |
| 1714 | "BTRFS_FEATURE_COMPAT_RO_???"); |
| 1715 | |
| 1716 | printf(", incompat_flags="); |
| 1717 | printflags(btrfs_features_incompat, flags->incompat_flags, |
| 1718 | "BTRFS_FEATURE_INCOMPAT_???"); |
| 1719 | printf("}"); |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * Consumes argument, returns nothing: |
| 1724 | * - BTRFS_IOC_SET_FEATURES |
| 1725 | * |
| 1726 | * Consumes nothing, returns argument: |
| 1727 | * - BTRFS_IOC_GET_FEATURES |
| 1728 | * - BTRFS_IOC_GET_SUPPORTED_FEATURES |
| 1729 | */ |
| 1730 | static void |
| 1731 | btrfs_test_features_ioctls(void) |
| 1732 | { |
| 1733 | struct btrfs_ioctl_feature_flags args[2] = { |
| 1734 | { |
| 1735 | .compat_flags = max_flags_plus_one(-1), |
| 1736 | .incompat_flags = max_flags_plus_one(9), |
| 1737 | .compat_ro_flags = max_flags_plus_one(0), |
| 1738 | }, { |
| 1739 | .compat_flags = max_flags_plus_one(-1), |
| 1740 | .incompat_flags = max_flags_plus_one(9), |
| 1741 | .compat_ro_flags = max_flags_plus_one(0), |
| 1742 | }, |
| 1743 | }; |
| 1744 | struct btrfs_ioctl_feature_flags supported_features[3]; |
| 1745 | |
| 1746 | ioctl(-1, BTRFS_IOC_SET_FEATURES, NULL); |
| 1747 | printf("ioctl(-1, BTRFS_IOC_SET_FEATURES, NULL) = -1 EBADF (%m)\n"); |
| 1748 | |
| 1749 | printf("ioctl(-1, BTRFS_IOC_SET_FEATURES, ["); |
| 1750 | btrfs_print_features(&args[0]); |
| 1751 | printf(", "); |
| 1752 | btrfs_print_features(&args[1]); |
| 1753 | ioctl(-1, BTRFS_IOC_SET_FEATURES, &args); |
| 1754 | printf("]) = -1 EBADF (%m)\n"); |
| 1755 | |
| 1756 | if (btrfs_test_root) { |
| 1757 | printf("ioctl(%d, BTRFS_IOC_GET_FEATURES, ", |
| 1758 | btrfs_test_dir_fd); |
| 1759 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_FEATURES, |
| 1760 | &supported_features); |
| 1761 | btrfs_print_features(&supported_features[0]); |
| 1762 | printf(") = 0\n"); |
| 1763 | |
| 1764 | ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_SUPPORTED_FEATURES, |
| 1765 | &supported_features); |
| 1766 | printf("ioctl(%d, BTRFS_IOC_GET_SUPPORTED_FEATURES, ", |
| 1767 | btrfs_test_dir_fd); |
| 1768 | printf("[ /* supported */ "); |
| 1769 | btrfs_print_features(&supported_features[0]); |
| 1770 | printf(", /* safe to set */ "); |
| 1771 | btrfs_print_features(&supported_features[1]); |
| 1772 | printf(", /* safe to clear */ "); |
| 1773 | btrfs_print_features(&supported_features[2]); |
| 1774 | printf("]) = 0\n"); |
| 1775 | } |
| 1776 | } |
| 1777 | |
Dmitry V. Levin | b356ad8 | 2016-05-26 14:26:15 +0000 | [diff] [blame] | 1778 | static void |
| 1779 | btrfs_test_read_ioctls(void) |
| 1780 | { |
| 1781 | static const struct xlat btrfs_read_cmd[] = { |
| 1782 | XLAT(BTRFS_IOC_BALANCE_PROGRESS), |
| 1783 | XLAT(BTRFS_IOC_FS_INFO), |
| 1784 | XLAT(BTRFS_IOC_GET_FEATURES), |
| 1785 | XLAT(BTRFS_IOC_GET_FSLABEL), |
| 1786 | XLAT(BTRFS_IOC_GET_SUPPORTED_FEATURES), |
| 1787 | XLAT(BTRFS_IOC_QGROUP_LIMIT), |
| 1788 | XLAT(BTRFS_IOC_QUOTA_RESCAN_STATUS), |
| 1789 | XLAT(BTRFS_IOC_START_SYNC), |
| 1790 | XLAT(BTRFS_IOC_SUBVOL_GETFLAGS), |
| 1791 | }; |
| 1792 | |
| 1793 | unsigned int i; |
| 1794 | for (i = 0; i < ARRAY_SIZE(btrfs_read_cmd); ++i) { |
| 1795 | ioctl(-1, (unsigned long) btrfs_read_cmd[i].val, 0); |
| 1796 | printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", btrfs_read_cmd[i].str); |
| 1797 | } |
| 1798 | } |
| 1799 | |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1800 | int |
| 1801 | main(int argc, char *argv[]) |
| 1802 | { |
| 1803 | |
| 1804 | int opt; |
| 1805 | int ret; |
| 1806 | const char *path; |
| 1807 | |
| 1808 | while ((opt = getopt(argc, argv, "wv")) != -1) { |
| 1809 | switch (opt) { |
| 1810 | case 'v': |
| 1811 | /* |
| 1812 | * These tests are incomplete, especially when |
| 1813 | * printing arrays of objects are involved. |
| 1814 | */ |
| 1815 | verbose = true; |
| 1816 | break; |
| 1817 | case 'w': |
| 1818 | write_ok = true; |
| 1819 | break; |
| 1820 | default: |
| 1821 | error_msg_and_fail("usage: btrfs [-v] [-w] [path]"); |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | * This will enable optional tests that require a valid file descriptor |
| 1827 | */ |
| 1828 | if (optind < argc) { |
| 1829 | int rootfd; |
| 1830 | struct statfs sfi; |
| 1831 | path = argv[optind]; |
| 1832 | |
| 1833 | ret = statfs(path, &sfi); |
| 1834 | if (ret) |
| 1835 | perror_msg_and_fail("statfs(%s) failed", path); |
| 1836 | |
| 1837 | if ((unsigned) sfi.f_type != BTRFS_SUPER_MAGIC) |
Dmitry V. Levin | 9298c66 | 2016-05-24 01:33:08 +0000 | [diff] [blame] | 1838 | error_msg_and_fail("%s is not a btrfs file system", |
| 1839 | path); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1840 | |
| 1841 | btrfs_test_root = path; |
| 1842 | rootfd = open(path, O_RDONLY|O_DIRECTORY); |
| 1843 | if (rootfd < 0) |
| 1844 | perror_msg_and_fail("open(%s) failed", path); |
| 1845 | |
| 1846 | ret = mkdirat(rootfd, "strace-test", 0755); |
| 1847 | if (ret < 0 && errno != EEXIST) |
| 1848 | perror_msg_and_fail("mkdirat(strace-test) failed"); |
| 1849 | |
| 1850 | btrfs_test_dir_fd = openat(rootfd, "strace-test", |
| 1851 | O_RDONLY|O_DIRECTORY); |
| 1852 | if (btrfs_test_dir_fd < 0) |
| 1853 | perror_msg_and_fail("openat(strace-test) failed"); |
| 1854 | close(rootfd); |
| 1855 | } else |
| 1856 | write_ok = false; |
| 1857 | |
| 1858 | if (btrfs_test_root) { |
| 1859 | fprintf(stderr, "Testing live ioctls on %s (%s)\n", |
| 1860 | btrfs_test_root, write_ok ? "read/write" : "read only"); |
| 1861 | } |
| 1862 | |
Dmitry V. Levin | b356ad8 | 2016-05-26 14:26:15 +0000 | [diff] [blame] | 1863 | btrfs_test_read_ioctls(); |
Jeff Mahoney | 3586679 | 2016-05-18 18:09:42 -0400 | [diff] [blame] | 1864 | btrfs_test_trans_ioctls(); |
| 1865 | btrfs_test_sync_ioctls(); |
| 1866 | btrfs_test_subvol_ioctls(); |
| 1867 | btrfs_test_balance_ioctls(); |
| 1868 | btrfs_test_device_ioctls(); |
| 1869 | btrfs_test_clone_ioctls(); |
| 1870 | btrfs_test_defrag_ioctls(); |
| 1871 | btrfs_test_search_ioctls(); |
| 1872 | btrfs_test_ino_lookup_ioctl(); |
| 1873 | btrfs_test_space_info_ioctl(); |
| 1874 | btrfs_test_scrub_ioctls(); |
| 1875 | btrfs_test_dev_info_ioctl(); |
| 1876 | btrfs_test_ino_path_ioctls(); |
| 1877 | btrfs_test_set_received_subvol_ioctl(); |
| 1878 | btrfs_test_send_ioctl(); |
| 1879 | btrfs_test_quota_ctl_ioctl(); |
| 1880 | btrfs_test_qgroup_assign_ioctl(); |
| 1881 | btrfs_test_qgroup_create_ioctl(); |
| 1882 | btrfs_test_quota_rescan_ioctl(); |
| 1883 | btrfs_test_label_ioctls(); |
| 1884 | btrfs_test_get_dev_stats_ioctl(); |
| 1885 | btrfs_test_dev_replace_ioctl(); |
| 1886 | btrfs_test_extent_same_ioctl(); |
| 1887 | btrfs_test_features_ioctls(); |
| 1888 | |
| 1889 | puts("+++ exited with 0 +++"); |
| 1890 | |
| 1891 | return 0; |
| 1892 | } |
Dmitry V. Levin | 2212918 | 2016-05-24 01:32:09 +0000 | [diff] [blame] | 1893 | |
| 1894 | #else |
| 1895 | |
| 1896 | SKIP_MAIN_UNDEFINED("HAVE_LINUX_BTRFS_H") |
| 1897 | |
| 1898 | #endif |