blob: 4014c883c3015f5be881fa66cd1eb30f35240d2c [file] [log] [blame]
Dmitry V. Levin22129182016-05-24 01:32:09 +00001#include "tests.h"
2
3#ifdef HAVE_LINUX_BTRFS_H
4
Jeff Mahoney35866792016-05-18 18:09:42 -04005#include <errno.h>
Jeff Mahoney35866792016-05-18 18:09:42 -04006#include <fcntl.h>
Dmitry V. Levin28a5f662016-05-24 01:32:41 +00007#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 Mahoney35866792016-05-18 18:09:42 -040019#include <linux/fs.h>
20#include <linux/btrfs.h>
21#include <linux/magic.h>
Jeff Mahoney35866792016-05-18 18:09:42 -040022#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. Levin22129182016-05-24 01:32:09 +000043#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 Mahoney35866792016-05-18 18:09:42 -040048
49#ifndef BTRFS_LABEL_SIZE
Dmitry V. Levin2447de42016-05-24 01:32:55 +000050# define BTRFS_LABEL_SIZE 256
Jeff Mahoney35866792016-05-18 18:09:42 -040051#endif
52
53#ifndef BTRFS_NAME_LEN
Dmitry V. Levin2447de42016-05-24 01:32:55 +000054# define BTRFS_NAME_LEN 255
Jeff Mahoney35866792016-05-18 18:09:42 -040055#endif
56
Dmitry V. Levin6697d152016-05-24 02:56:39 +000057/*
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 */
61typedef __u64 u64;
62
Jeff Mahoney35866792016-05-18 18:09:42 -040063static const char *btrfs_test_root;
64static int btrfs_test_dir_fd;
65static bool verbose = false;
66static bool write_ok = false;
67
68const 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
73const char uuid_reference_string[] = "01234567-89ab-cdef-fedc-ba9876543210";
74
75#ifndef BTRFS_IOC_GET_FEATURES
Dmitry V. Levin2447de42016-05-24 01:32:55 +000076# define BTRFS_IOC_GET_FEATURES _IOR(BTRFS_IOCTL_MAGIC, 57, \
Jeff Mahoney35866792016-05-18 18:09:42 -040077 struct btrfs_ioctl_feature_flags)
Dmitry V. Levin2447de42016-05-24 01:32:55 +000078# define BTRFS_IOC_SET_FEATURES _IOW(BTRFS_IOCTL_MAGIC, 57, \
Jeff Mahoney35866792016-05-18 18:09:42 -040079 struct btrfs_ioctl_feature_flags[2])
Dmitry V. Levin2447de42016-05-24 01:32:55 +000080# define BTRFS_IOC_GET_SUPPORTED_FEATURES _IOR(BTRFS_IOCTL_MAGIC, 57, \
Jeff Mahoney35866792016-05-18 18:09:42 -040081 struct btrfs_ioctl_feature_flags[3])
82#endif
83
84#ifndef HAVE_STRUCT_BTRFS_IOCTL_FEATURE_FLAGS_COMPAT_FLAGS
85struct 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
93struct 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. Levin2447de42016-05-24 01:32:55 +0000104# define FIDEDUPERANGE _IOWR(0x94, 54, struct file_dedupe_range)
Jeff Mahoney35866792016-05-18 18:09:42 -0400105struct 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
119struct 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. Levin2447de42016-05-24 01:32:55 +0000130# define BTRFS_IOC_TREE_SEARCH_V2 _IOWR(BTRFS_IOCTL_MAGIC, 17, \
Jeff Mahoney35866792016-05-18 18:09:42 -0400131 struct btrfs_ioctl_search_args_v2)
132struct 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. Levin2447de42016-05-24 01:32:55 +0000142static const char *
143maybe_print_uint64max(uint64_t val)
Jeff Mahoney35866792016-05-18 18:09:42 -0400144{
145 if (val == UINT64_MAX)
146 return " /* UINT64_MAX */";
147 return "";
148}
149
150/* takes highest valid flag bit */
Dmitry V. Levin2447de42016-05-24 01:32:55 +0000151static uint64_t
152max_flags_plus_one(int bit)
Jeff Mahoney35866792016-05-18 18:09:42 -0400153{
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 */
169static void
170btrfs_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 */
186static void
187btrfs_test_sync_ioctls(void)
188{
Dmitry V. Levin57888fa2016-05-24 02:07:47 +0000189 uint64_t u64val = 0xdeadbeefbadc0ded;
Jeff Mahoney35866792016-05-18 18:09:42 -0400190
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
207static void
208btrfs_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
241static void
242btrfs_print_vol_args_v2(struct btrfs_ioctl_vol_args_v2 *args, int print_qgroups)
243{
Dmitry V. Levin6ce6d3b2016-05-24 01:53:02 +0000244 printf("{fd=%d, flags=", (int) args->fd);
Jeff Mahoney35866792016-05-18 18:09:42 -0400245 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
271static void
272btrfs_test_subvol_ioctls(void)
273{
274 const char *subvol_name = "subvol-name";
275 char *long_subvol_name;
Dmitry V. Levin57888fa2016-05-24 02:07:47 +0000276 void *bad_pointer = (void *) (unsigned long) 0xdeadbeeffffffeed;
277 uint64_t u64val = 0xdeadbeefbadc0ded;
Jeff Mahoney35866792016-05-18 18:09:42 -0400278 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 Mahoney35866792016-05-18 18:09:42 -0400283
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 Mahoney35866792016-05-18 18:09:42 -0400347 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. Levin45fcc0c2016-05-24 01:32:27 +0000353 vol_args_v2.qgroup_inherit = bad_pointer;
Jeff Mahoney35866792016-05-18 18:09:42 -0400354
Jeff Mahoney35866792016-05-18 18:09:42 -0400355 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 Mahoney35866792016-05-18 18:09:42 -0400360 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. Levin45fcc0c2016-05-24 01:32:27 +0000365 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 Mahoney35866792016-05-18 18:09:42 -0400371
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000372 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 Mahoney35866792016-05-18 18:09:42 -0400384
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000385 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 Mahoney35866792016-05-18 18:09:42 -0400389
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000390 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 Mahoney35866792016-05-18 18:09:42 -0400394
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. Levin6ce6d3b2016-05-24 01:53:02 +0000452 "{fd=%d, name=\"%.*s\"}) = 0\n",
453 btrfs_test_dir_fd, (int) vol_args.fd, 255, long_subvol_name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400454
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. Levin6ce6d3b2016-05-24 01:53:02 +0000458 "{fd=%d, name=\"%s\"}) = 0\n",
459 btrfs_test_dir_fd, (int) vol_args.fd, subvol_name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400460
461 close(subvolfd);
462 }
463 free(long_subvol_name);
464}
465
466static void
467btrfs_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 */
494static void
495btrfs_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 */
596static void
597btrfs_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. Levin6ce6d3b2016-05-24 01:53:02 +0000611 "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
612 (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400613
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. Levin6ce6d3b2016-05-24 01:53:02 +0000620 "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
621 (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400622
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. Levin6ce6d3b2016-05-24 01:53:02 +0000628 "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
629 (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400630
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. Levin6ce6d3b2016-05-24 01:53:02 +0000636 "{fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
637 (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400638
639}
640
641/*
642 * Consumes argument, returns nothing:
643 * - BTRFS_IOC_CLONE
644 * - BTRFS_IOC_CLONE_RANGE
645 */
646static void
647btrfs_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. Levin3f366112016-05-24 02:17:49 +0000667 "{src_fd=%d, src_offset=%" PRI__u64 ", src_length=%" PRI__u64
668 ", dest_offset=%" PRI__u64 "}) = -1 EBADF (%m)\n",
Dmitry V. Levin6ce6d3b2016-05-24 01:53:02 +0000669 (int) args.src_fd, args.src_offset, args.src_length,
Jeff Mahoney35866792016-05-18 18:09:42 -0400670 args.dest_offset);
671}
672
673#define BTRFS_COMPRESS_TYPES 2
674#define BTRFS_INVALID_COMPRESS (BTRFS_COMPRESS_TYPES + 1)
675
676static void
677btrfs_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 */
694static void
695btrfs_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
738static const char *
739xlookup(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
747static void
748btrfs_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
756static void
757btrfs_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
765static void
766btrfs_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
797static void
798btrfs_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 */
828static void
829btrfs_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 Mahoney563a7582016-05-26 16:09:28 -0400854 search_args.key = key_reference;
Jeff Mahoney35866792016-05-18 18:09:42 -0400855 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 Mahoney563a7582016-05-26 16:09:28 -0400869 search_args.key = key_reference;
Jeff Mahoney35866792016-05-18 18:09:42 -0400870 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 Mahoney563a7582016-05-26 16:09:28 -0400884 search_args.key = key_reference;
Jeff Mahoney35866792016-05-18 18:09:42 -0400885 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 Mahoney563a7582016-05-26 16:09:28 -0400899 search_args.key = key_reference;
Jeff Mahoney35866792016-05-18 18:09:42 -0400900 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 Mahoney563a7582016-05-26 16:09:28 -0400914 search_args.key = key_reference;
Jeff Mahoney35866792016-05-18 18:09:42 -0400915 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 Mahoneyfd70b502016-05-26 23:33:05 -0400944 printf("} => {key={nr_items=%u}, buf=",
Jeff Mahoney35866792016-05-18 18:09:42 -0400945 search_args.key.nr_items);
Jeff Mahoneyfd70b502016-05-26 23:33:05 -0400946 btrfs_print_tree_search_buf(&search_args.key, search_args.buf,
947 sizeof(search_args.buf));
948 printf("}) = 0\n");
Jeff Mahoney35866792016-05-18 18:09:42 -0400949
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 Mahoneyfd70b502016-05-26 23:33:05 -0400974 printf(" => {buf_size=%" PRIu64 "}) = -1 EOVERFLOW (%m)\n",
Jeff Mahoney35866792016-05-18 18:09:42 -0400975 (uint64_t)args->buf_size);
976 free(args);
977 }
978}
979
980/*
981 * Consumes argument, returns argument:
982 * - BTRFS_IOC_INO_LOOKUP
983 */
984static void
985btrfs_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 */
1017static void
1018btrfs_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 */
1085static void
1086btrfs_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 */
1121static void
1122btrfs_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 */
1143static void
1144btrfs_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. Levin22129182016-05-24 01:32:09 +00001170#ifdef HAVE_LINUX_FIEMAP_H
Jeff Mahoney35866792016-05-18 18:09:42 -04001171 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-Malinovskiye79f0382016-05-24 18:09:46 +03001270 printf("[{inum=%llu, offset=0, root=5}]",
1271 (unsigned long long) si.st_ino);
Jeff Mahoney35866792016-05-18 18:09:42 -04001272 } else
1273 printf("...");
1274 printf("}}) = 0\n");
1275 close(fd);
1276 free(fiemap);
1277 }
Dmitry V. Levin22129182016-05-24 01:32:09 +00001278#endif /* HAVE_LINUX_FIEMAP_H */
Jeff Mahoney35866792016-05-18 18:09:42 -04001279}
1280
1281/*
1282 * Consumes argument, returns argument:
1283 * - BTRFS_IOC_SET_RECEIVED_SUBVOL
1284 */
1285static void
1286btrfs_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 */
1313static void
1314btrfs_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. Levin6ce6d3b2016-05-24 01:53:02 +00001327 "{send_fd=%d, clone_sources_count=%" PRI__u64
1328 ", clone_sources=",
1329 (int) args.send_fd, args.clone_sources_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001330 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. Levin6ce6d3b2016-05-24 01:53:02 +00001345 "{send_fd=%d, clone_sources_count=%" PRI__u64
1346 ", clone_sources=",
1347 (int) args.send_fd, args.clone_sources_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001348 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 */
1368static void
1369btrfs_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 */
1402static void
1403btrfs_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 */
1424static void
1425btrfs_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 */
1447static void
1448btrfs_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 */
1472static void
1473btrfs_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 */
1499static void
1500btrfs_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 */
1551static void
1552btrfs_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
1583static void
1584btrfs_test_extent_same_ioctl(void)
1585{
Dmitry V. Levinea516df2016-05-24 02:40:13 +00001586#ifdef BTRFS_IOC_FILE_EXTENT_SAME
Jeff Mahoney35866792016-05-18 18:09:42 -04001587 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
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001600 ", dest_count=%hu, info=[]",
Jeff Mahoney35866792016-05-18 18:09:42 -04001601 (uint64_t)args.src_offset,
1602 (uint64_t)args.src_length, args.dest_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001603 ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, &args);
1604 printf("}) = -1 EBADF (%m)\n");
1605
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001606 argsp = malloc(sizeof(*argsp) + sizeof(argsp->info[0]) * 3);
Jeff Mahoney35866792016-05-18 18:09:42 -04001607 if (!argsp)
1608 perror_msg_and_fail("malloc failed");
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001609 memset(argsp, 0, sizeof(*argsp) + sizeof(argsp->info[0]) * 3);
Jeff Mahoney35866792016-05-18 18:09:42 -04001610
1611 *argsp = args;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001612 argsp->dest_count = 3;
Jeff Mahoney35866792016-05-18 18:09:42 -04001613 argsp->info[0].dest_fd = 2;
1614 argsp->info[0].dest_offset = 0;
1615 argsp->info[1].dest_fd = 2;
1616 argsp->info[1].dest_offset = 10240;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001617 argsp->info[2].dest_fd = 2;
1618 argsp->info[2].dest_offset = 20480;
Jeff Mahoney35866792016-05-18 18:09:42 -04001619
1620 printf("ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE, "
1621 "{src_offset=%" PRIu64
1622 ", src_length=%" PRIu64
1623 ", dest_count=%hu, info=",
1624 (int64_t)argsp->src_offset,
1625 (uint64_t)argsp->src_length, argsp->dest_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001626 printf("[{dest_fd=%" PRId64 ", dest_offset=%" PRIu64
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001627 "}, {dest_fd=%" PRId64 ", dest_offset=%"PRIu64 "}",
Jeff Mahoney35866792016-05-18 18:09:42 -04001628 (int64_t)argsp->info[0].dest_fd,
1629 (uint64_t)argsp->info[0].dest_offset,
1630 (int64_t)argsp->info[1].dest_fd,
1631 (uint64_t)argsp->info[1].dest_offset);
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001632 if (verbose)
1633 printf(", {dest_fd=%" PRId64 ", dest_offset=%" PRIu64 "}",
1634 (int64_t)argsp->info[2].dest_fd,
1635 (uint64_t)argsp->info[2].dest_offset);
Jeff Mahoney35866792016-05-18 18:09:42 -04001636 else
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001637 printf(", ...");
1638 printf("]");
Jeff Mahoney35866792016-05-18 18:09:42 -04001639 ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, argsp);
1640 printf("}) = -1 EBADF (%m)\n");
1641
1642 if (write_ok) {
1643 int fd1, fd2;
1644 char buf[16384];
Jeff Mahoney35866792016-05-18 18:09:42 -04001645
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");
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001658 if (write(fd1, buf, sizeof(buf)) < 0)
1659 perror_msg_and_fail("write: fd1");
1660 if (write(fd2, buf, sizeof(buf)) < 0)
1661 perror_msg_and_fail("write: fd2");
Jeff Mahoney35866792016-05-18 18:09:42 -04001662 if (write(fd2, buf, sizeof(buf)) < 0)
1663 perror_msg_and_fail("write: fd2");
1664
1665 close(fd2);
1666 fd2 = openat(btrfs_test_dir_fd, "file2", O_RDONLY);
1667 if (fd2 < 0)
1668 perror_msg_and_fail("open file2 failed");
1669
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001670 memset(argsp, 0, sizeof(*argsp) + sizeof(argsp->info[0]) * 3);
Jeff Mahoney35866792016-05-18 18:09:42 -04001671
1672 argsp->src_offset = 0;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001673 argsp->src_length = 4096;
1674 argsp->dest_count = 3;
Jeff Mahoney35866792016-05-18 18:09:42 -04001675 argsp->info[0].dest_fd = fd2;
1676 argsp->info[0].dest_offset = 0;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001677 argsp->info[1].dest_fd = fd2;
1678 argsp->info[1].dest_offset = 10240;
1679 argsp->info[2].dest_fd = fd2;
1680 argsp->info[2].dest_offset = 20480;
Jeff Mahoney35866792016-05-18 18:09:42 -04001681
1682 printf("ioctl(%d, BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE, "
1683 "{src_offset=%" PRIu64 ", src_length=%" PRIu64
1684 ", dest_count=%hu, info=", fd1,
1685 (uint64_t)argsp->src_offset,
1686 (uint64_t)argsp->src_length, argsp->dest_count);
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001687 printf("[{dest_fd=%" PRId64 ", dest_offset=%" PRIu64
1688 "}, {dest_fd=%" PRId64 ", dest_offset=%"PRIu64 "}",
1689 (int64_t)argsp->info[0].dest_fd,
1690 (uint64_t)argsp->info[0].dest_offset,
1691 (int64_t)argsp->info[1].dest_fd,
1692 (uint64_t)argsp->info[1].dest_offset);
Jeff Mahoney35866792016-05-18 18:09:42 -04001693 if (verbose)
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001694 printf(", {dest_fd=%" PRId64
1695 ", dest_offset=%" PRIu64 "}",
1696 (int64_t)argsp->info[2].dest_fd,
1697 (uint64_t)argsp->info[2].dest_offset);
Jeff Mahoney35866792016-05-18 18:09:42 -04001698 else
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001699 printf(", ...");
1700
Jeff Mahoney35866792016-05-18 18:09:42 -04001701 ioctl(fd1, BTRFS_IOC_FILE_EXTENT_SAME, argsp);
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001702 printf("]} => {info=");
1703 printf("[{bytes_deduped=%" PRIu64 ", status=%d}, "
1704 "{bytes_deduped=%" PRIu64 ", status=%d}",
1705 (uint64_t)argsp->info[0].bytes_deduped,
1706 argsp->info[0].status,
1707 (uint64_t)argsp->info[1].bytes_deduped,
1708 argsp->info[1].status);
Jeff Mahoney35866792016-05-18 18:09:42 -04001709 if (verbose)
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001710 printf(", {bytes_deduped=%" PRIu64 ", status=%d}",
1711 (uint64_t)argsp->info[2].bytes_deduped,
1712 argsp->info[2].status);
Jeff Mahoney35866792016-05-18 18:09:42 -04001713 else
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001714 printf(", ...");
1715 printf("]}) = 0\n");
Jeff Mahoney35866792016-05-18 18:09:42 -04001716 close(fd1);
1717 close(fd2);
1718 unlinkat(btrfs_test_dir_fd, "file1", 0);
1719 unlinkat(btrfs_test_dir_fd, "file2", 0);
1720 close(fd1);
1721 close(fd2);
1722 }
1723 free(argsp);
Dmitry V. Levinea516df2016-05-24 02:40:13 +00001724#endif /* BTRFS_IOC_FILE_EXTENT_SAME */
Jeff Mahoney35866792016-05-18 18:09:42 -04001725}
1726
1727static void
1728btrfs_print_features(struct btrfs_ioctl_feature_flags *flags)
1729{
1730 printf("{compat_flags=");
1731 printflags(btrfs_features_compat, flags->compat_flags,
1732 "BTRFS_FEATURE_COMPAT_???");
1733
1734 printf(", compat_ro_flags=");
1735 printflags(btrfs_features_compat_ro, flags->compat_ro_flags,
1736 "BTRFS_FEATURE_COMPAT_RO_???");
1737
1738 printf(", incompat_flags=");
1739 printflags(btrfs_features_incompat, flags->incompat_flags,
1740 "BTRFS_FEATURE_INCOMPAT_???");
1741 printf("}");
1742}
1743
1744/*
1745 * Consumes argument, returns nothing:
1746 * - BTRFS_IOC_SET_FEATURES
1747 *
1748 * Consumes nothing, returns argument:
1749 * - BTRFS_IOC_GET_FEATURES
1750 * - BTRFS_IOC_GET_SUPPORTED_FEATURES
1751 */
1752static void
1753btrfs_test_features_ioctls(void)
1754{
1755 struct btrfs_ioctl_feature_flags args[2] = {
1756 {
1757 .compat_flags = max_flags_plus_one(-1),
1758 .incompat_flags = max_flags_plus_one(9),
1759 .compat_ro_flags = max_flags_plus_one(0),
1760 }, {
1761 .compat_flags = max_flags_plus_one(-1),
1762 .incompat_flags = max_flags_plus_one(9),
1763 .compat_ro_flags = max_flags_plus_one(0),
1764 },
1765 };
1766 struct btrfs_ioctl_feature_flags supported_features[3];
1767
1768 ioctl(-1, BTRFS_IOC_SET_FEATURES, NULL);
1769 printf("ioctl(-1, BTRFS_IOC_SET_FEATURES, NULL) = -1 EBADF (%m)\n");
1770
1771 printf("ioctl(-1, BTRFS_IOC_SET_FEATURES, [");
1772 btrfs_print_features(&args[0]);
1773 printf(", ");
1774 btrfs_print_features(&args[1]);
1775 ioctl(-1, BTRFS_IOC_SET_FEATURES, &args);
1776 printf("]) = -1 EBADF (%m)\n");
1777
1778 if (btrfs_test_root) {
1779 printf("ioctl(%d, BTRFS_IOC_GET_FEATURES, ",
1780 btrfs_test_dir_fd);
1781 ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_FEATURES,
1782 &supported_features);
1783 btrfs_print_features(&supported_features[0]);
1784 printf(") = 0\n");
1785
1786 ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_SUPPORTED_FEATURES,
1787 &supported_features);
1788 printf("ioctl(%d, BTRFS_IOC_GET_SUPPORTED_FEATURES, ",
1789 btrfs_test_dir_fd);
1790 printf("[ /* supported */ ");
1791 btrfs_print_features(&supported_features[0]);
1792 printf(", /* safe to set */ ");
1793 btrfs_print_features(&supported_features[1]);
1794 printf(", /* safe to clear */ ");
1795 btrfs_print_features(&supported_features[2]);
1796 printf("]) = 0\n");
1797 }
1798}
1799
Dmitry V. Levinb356ad82016-05-26 14:26:15 +00001800static void
1801btrfs_test_read_ioctls(void)
1802{
1803 static const struct xlat btrfs_read_cmd[] = {
1804 XLAT(BTRFS_IOC_BALANCE_PROGRESS),
1805 XLAT(BTRFS_IOC_FS_INFO),
1806 XLAT(BTRFS_IOC_GET_FEATURES),
1807 XLAT(BTRFS_IOC_GET_FSLABEL),
1808 XLAT(BTRFS_IOC_GET_SUPPORTED_FEATURES),
1809 XLAT(BTRFS_IOC_QGROUP_LIMIT),
1810 XLAT(BTRFS_IOC_QUOTA_RESCAN_STATUS),
1811 XLAT(BTRFS_IOC_START_SYNC),
1812 XLAT(BTRFS_IOC_SUBVOL_GETFLAGS),
1813 };
1814
1815 unsigned int i;
1816 for (i = 0; i < ARRAY_SIZE(btrfs_read_cmd); ++i) {
1817 ioctl(-1, (unsigned long) btrfs_read_cmd[i].val, 0);
1818 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", btrfs_read_cmd[i].str);
1819 }
1820}
1821
Jeff Mahoney35866792016-05-18 18:09:42 -04001822int
1823main(int argc, char *argv[])
1824{
1825
1826 int opt;
1827 int ret;
1828 const char *path;
1829
1830 while ((opt = getopt(argc, argv, "wv")) != -1) {
1831 switch (opt) {
1832 case 'v':
1833 /*
1834 * These tests are incomplete, especially when
1835 * printing arrays of objects are involved.
1836 */
1837 verbose = true;
1838 break;
1839 case 'w':
1840 write_ok = true;
1841 break;
1842 default:
1843 error_msg_and_fail("usage: btrfs [-v] [-w] [path]");
1844 }
1845 }
1846
1847 /*
1848 * This will enable optional tests that require a valid file descriptor
1849 */
1850 if (optind < argc) {
1851 int rootfd;
1852 struct statfs sfi;
1853 path = argv[optind];
1854
1855 ret = statfs(path, &sfi);
1856 if (ret)
1857 perror_msg_and_fail("statfs(%s) failed", path);
1858
1859 if ((unsigned) sfi.f_type != BTRFS_SUPER_MAGIC)
Dmitry V. Levin9298c662016-05-24 01:33:08 +00001860 error_msg_and_fail("%s is not a btrfs file system",
1861 path);
Jeff Mahoney35866792016-05-18 18:09:42 -04001862
1863 btrfs_test_root = path;
1864 rootfd = open(path, O_RDONLY|O_DIRECTORY);
1865 if (rootfd < 0)
1866 perror_msg_and_fail("open(%s) failed", path);
1867
1868 ret = mkdirat(rootfd, "strace-test", 0755);
1869 if (ret < 0 && errno != EEXIST)
1870 perror_msg_and_fail("mkdirat(strace-test) failed");
1871
1872 btrfs_test_dir_fd = openat(rootfd, "strace-test",
1873 O_RDONLY|O_DIRECTORY);
1874 if (btrfs_test_dir_fd < 0)
1875 perror_msg_and_fail("openat(strace-test) failed");
1876 close(rootfd);
1877 } else
1878 write_ok = false;
1879
1880 if (btrfs_test_root) {
1881 fprintf(stderr, "Testing live ioctls on %s (%s)\n",
1882 btrfs_test_root, write_ok ? "read/write" : "read only");
1883 }
1884
Dmitry V. Levinb356ad82016-05-26 14:26:15 +00001885 btrfs_test_read_ioctls();
Jeff Mahoney35866792016-05-18 18:09:42 -04001886 btrfs_test_trans_ioctls();
1887 btrfs_test_sync_ioctls();
1888 btrfs_test_subvol_ioctls();
1889 btrfs_test_balance_ioctls();
1890 btrfs_test_device_ioctls();
1891 btrfs_test_clone_ioctls();
1892 btrfs_test_defrag_ioctls();
1893 btrfs_test_search_ioctls();
1894 btrfs_test_ino_lookup_ioctl();
1895 btrfs_test_space_info_ioctl();
1896 btrfs_test_scrub_ioctls();
1897 btrfs_test_dev_info_ioctl();
1898 btrfs_test_ino_path_ioctls();
1899 btrfs_test_set_received_subvol_ioctl();
1900 btrfs_test_send_ioctl();
1901 btrfs_test_quota_ctl_ioctl();
1902 btrfs_test_qgroup_assign_ioctl();
1903 btrfs_test_qgroup_create_ioctl();
1904 btrfs_test_quota_rescan_ioctl();
1905 btrfs_test_label_ioctls();
1906 btrfs_test_get_dev_stats_ioctl();
1907 btrfs_test_dev_replace_ioctl();
1908 btrfs_test_extent_same_ioctl();
1909 btrfs_test_features_ioctls();
1910
1911 puts("+++ exited with 0 +++");
1912
1913 return 0;
1914}
Dmitry V. Levin22129182016-05-24 01:32:09 +00001915
1916#else
1917
1918SKIP_MAIN_UNDEFINED("HAVE_LINUX_BTRFS_H")
1919
1920#endif