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