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