blob: f23d7c0a10a237ddf058918ac71f2e9b631bd4e0 [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>
Dmitry V. Levin28a5f662016-05-24 01:32:41 +00009#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
Dmitry V. Levin28a5f662016-05-24 01:32:41 +000014#include <sys/ioctl.h>
15#include <sys/stat.h>
Elliott Hughes03a418e2018-06-15 13:11:40 -070016#include <sys/sysmacros.h>
Dmitry V. Levin28a5f662016-05-24 01:32:41 +000017#include <sys/vfs.h>
Jeff Mahoney35866792016-05-18 18:09:42 -040018#include <linux/fs.h>
19#include <linux/btrfs.h>
20#include <linux/magic.h>
Jeff Mahoney35866792016-05-18 18:09:42 -040021#include "xlat.h"
22
23#include "xlat/btrfs_balance_args.h"
24#include "xlat/btrfs_balance_flags.h"
25#include "xlat/btrfs_balance_state.h"
26#include "xlat/btrfs_compress_types.h"
Elliott Hughes03a418e2018-06-15 13:11:40 -070027#include "xlat/btrfs_cont_reading_from_srcdev_mode.h"
Jeff Mahoney35866792016-05-18 18:09:42 -040028#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
Elliott Hughesc1873762018-12-19 15:13:36 -080057#ifndef FS_IOC_GETFSLABEL
58# define FS_IOC_GETFSLABEL BTRFS_IOC_GET_FSLABEL
59#endif
60
61#ifndef FS_IOC_SETFSLABEL
62# define FS_IOC_SETFSLABEL BTRFS_IOC_SET_FSLABEL
63#endif
64
Dmitry V. Levin6697d152016-05-24 02:56:39 +000065/*
66 * Prior to Linux 3.12, the BTRFS_IOC_DEFAULT_SUBVOL used u64 in
67 * its definition, which isn't exported by the kernel.
68 */
69typedef __u64 u64;
70
Jeff Mahoney35866792016-05-18 18:09:42 -040071static const char *btrfs_test_root;
72static int btrfs_test_dir_fd;
Elliott Hughesdc75b012017-07-05 13:54:44 -070073static bool verbose;
74static bool write_ok;
Elliott Hughes03a418e2018-06-15 13:11:40 -070075static bool verbose_xlat;
76
77static const char *path;
78static const char dir_name_fmt[] = "strace-test-%d";
79static char dir_name[sizeof(dir_name_fmt) + sizeof(int) * 3];
Jeff Mahoney35866792016-05-18 18:09:42 -040080
81const unsigned char uuid_reference[BTRFS_UUID_SIZE] = {
82 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
83 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
84};
85
86const char uuid_reference_string[] = "01234567-89ab-cdef-fedc-ba9876543210";
87
Dmitry V. Levinfe7a4512016-06-17 03:02:46 +030088#ifndef BTRFS_IOC_QUOTA_RESCAN
89struct btrfs_ioctl_quota_rescan_args {
90 uint64_t flags, progress, reserved[6];
91};
Elliott Hughesdc75b012017-07-05 13:54:44 -070092# define BTRFS_IOC_QUOTA_RESCAN \
93 _IOW(BTRFS_IOCTL_MAGIC, 44, struct btrfs_ioctl_quota_rescan_args)
94# define BTRFS_IOC_QUOTA_RESCAN_STATUS \
95 _IOR(BTRFS_IOCTL_MAGIC, 45, struct btrfs_ioctl_quota_rescan_args)
Dmitry V. Levinfe7a4512016-06-17 03:02:46 +030096#endif
97
98#ifndef BTRFS_IOC_QUOTA_RESCAN_WAIT
99# define BTRFS_IOC_QUOTA_RESCAN_WAIT _IO(BTRFS_IOCTL_MAGIC, 46)
100#endif
101
Jeff Mahoney35866792016-05-18 18:09:42 -0400102#ifndef BTRFS_IOC_GET_FEATURES
Elliott Hughesdc75b012017-07-05 13:54:44 -0700103# define BTRFS_IOC_GET_FEATURES \
104 _IOR(BTRFS_IOCTL_MAGIC, 57, struct btrfs_ioctl_feature_flags)
105# define BTRFS_IOC_SET_FEATURES \
106 _IOW(BTRFS_IOCTL_MAGIC, 57, struct btrfs_ioctl_feature_flags[2])
107# define BTRFS_IOC_GET_SUPPORTED_FEATURES \
108 _IOR(BTRFS_IOCTL_MAGIC, 57, struct btrfs_ioctl_feature_flags[3])
Jeff Mahoney35866792016-05-18 18:09:42 -0400109#endif
110
111#ifndef HAVE_STRUCT_BTRFS_IOCTL_FEATURE_FLAGS_COMPAT_FLAGS
112struct btrfs_ioctl_feature_flags {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700113 uint64_t compat_flags;
114 uint64_t compat_ro_flags;
115 uint64_t incompat_flags;
Jeff Mahoney35866792016-05-18 18:09:42 -0400116};
117#endif
118
119#ifndef HAVE_STRUCT_BTRFS_IOCTL_DEFRAG_RANGE_ARGS_START
120struct btrfs_ioctl_defrag_range_args {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700121 uint64_t start;
122 uint64_t len;
123 uint64_t flags;
124 uint32_t extent_thresh;
125 uint32_t compress_type;
126 uint32_t unused[4];
Jeff Mahoney35866792016-05-18 18:09:42 -0400127};
128#endif
129
130#ifndef FIDEDUPERANGE
Dmitry V. Levin2447de42016-05-24 01:32:55 +0000131# define FIDEDUPERANGE _IOWR(0x94, 54, struct file_dedupe_range)
Jeff Mahoney35866792016-05-18 18:09:42 -0400132struct file_dedupe_range_info {
133 int64_t dest_fd; /* in - destination file */
134 uint64_t dest_offset; /* in - start of extent in destination */
135 uint64_t bytes_deduped; /* out - total # of bytes we were able
136 * to dedupe from this file. */
137 /* status of this dedupe operation:
138 * < 0 for error
139 * == FILE_DEDUPE_RANGE_SAME if dedupe succeeds
140 * == FILE_DEDUPE_RANGE_DIFFERS if data differs
141 */
142 int32_t status; /* out - see above description */
143 uint32_t reserved; /* must be zero */
144};
145
146struct file_dedupe_range {
147 uint64_t src_offset; /* in - start of extent in source */
148 uint64_t src_length; /* in - length of extent */
149 uint16_t dest_count; /* in - total elements in info array */
150 uint16_t reserved1; /* must be zero */
151 uint32_t reserved2; /* must be zero */
152 struct file_dedupe_range_info info[0];
153};
154#endif
155
156#ifndef BTRFS_IOC_TREE_SEARCH_V2
Elliott Hughesdc75b012017-07-05 13:54:44 -0700157# define BTRFS_IOC_TREE_SEARCH_V2 \
158 _IOWR(BTRFS_IOCTL_MAGIC, 17, struct btrfs_ioctl_search_args_v2)
Jeff Mahoney35866792016-05-18 18:09:42 -0400159struct btrfs_ioctl_search_args_v2 {
160 struct btrfs_ioctl_search_key key; /* in/out - search parameters */
161 uint64_t buf_size; /* in - size of buffer
162 * out - on EOVERFLOW: needed size
163 * to store item */
Elliott Hughesdc75b012017-07-05 13:54:44 -0700164 uint64_t buf[0]; /* out - found items */
Jeff Mahoney35866792016-05-18 18:09:42 -0400165};
166#endif
167
168
Dmitry V. Levin2447de42016-05-24 01:32:55 +0000169static const char *
Elliott Hughes03a418e2018-06-15 13:11:40 -0700170sprint_xlat_(uint32_t val, const char *xlat)
Jeff Mahoney35866792016-05-18 18:09:42 -0400171{
Elliott Hughes03a418e2018-06-15 13:11:40 -0700172 static char str[256];
173 int ret;
174
175 if (verbose_xlat) {
176 ret = snprintf(str, sizeof(str), "%#x /* %s */", val, xlat);
177
178 if (ret < 0)
179 perror_msg_and_fail("sprint_ioc(%#x, %s)", val, xlat);
180 if ((unsigned) ret >= sizeof(str))
181 error_msg_and_fail("sprint_ioc(%#x, %s): buffer "
182 "overflow", val, xlat);
183
184 return str;
185 }
186
187 return xlat;
188}
189
190#define ioc(x_) sprint_xlat_(x_, #x_)
191
192void
193prfl_btrfs(const struct xlat *xlat, const unsigned long long val,
194 const char *str)
195{
196 if (verbose_xlat && val)
197 printf("%#llx /* ", val);
198 printflags(xlat, val, str);
199 if (verbose_xlat && val)
200 printf(" */");
201}
202
203void
204prxval_btrfs(const struct xlat *xlat, const unsigned long long val,
205 const char *str, bool known)
206{
207 if (verbose_xlat && known)
208 printf("%#llx /* ", val);
209 printxval(xlat, val, str);
210 if (verbose_xlat && known)
211 printf(" */");
212}
213
214static void
215print_uint64(const char *prefix, uint64_t val)
216{
217 if (val == UINT64_MAX) {
218 if (verbose_xlat)
219 printf("%s%" PRIu64 " /* UINT64_MAX */", prefix, val);
220 else
221 printf("%sUINT64_MAX", prefix);
222 } else {
223 printf("%s%" PRIu64, prefix, val);
224 }
Jeff Mahoney35866792016-05-18 18:09:42 -0400225}
226
227/* takes highest valid flag bit */
Dmitry V. Levin2447de42016-05-24 01:32:55 +0000228static uint64_t
229max_flags_plus_one(int bit)
Jeff Mahoney35866792016-05-18 18:09:42 -0400230{
231 int i;
232 uint64_t val = 0;
233 if (bit == -1)
234 return 1;
235 for (i = 0; i <= bit + 1 && i < 64; i++)
236 val |= (1ULL << i);
237 return val;
238}
239
240/*
241 * Consumes no arguments, returns nothing:
242 *
243 * - BTRFS_IOC_TRANS_START
244 * - BTRFS_IOC_TRANS_END
245 */
246static void
247btrfs_test_trans_ioctls(void)
248{
249 ioctl(-1, BTRFS_IOC_TRANS_START, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700250 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_TRANS_START));
Jeff Mahoney35866792016-05-18 18:09:42 -0400251
252 ioctl(-1, BTRFS_IOC_TRANS_END, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700253 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_TRANS_END));
Jeff Mahoney35866792016-05-18 18:09:42 -0400254}
255
256/*
257 * Consumes no arguments, returns nothing:
258 * - BTRFS_IOC_SYNC
259 *
260 * Consumes argument, returns nothing
261 * - BTRFS_IOC_WAIT_SYNC
262 */
263static void
264btrfs_test_sync_ioctls(void)
265{
Elliott Hughesd35df492017-02-15 15:19:05 -0800266 uint64_t u64val = 0xdeadbeefbadc0dedULL;
Jeff Mahoney35866792016-05-18 18:09:42 -0400267
268 ioctl(-1, BTRFS_IOC_SYNC, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700269 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_SYNC));
Jeff Mahoney35866792016-05-18 18:09:42 -0400270
271 ioctl(-1, BTRFS_IOC_WAIT_SYNC, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700272 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
273 ioc(BTRFS_IOC_WAIT_SYNC));
Jeff Mahoney35866792016-05-18 18:09:42 -0400274
275 ioctl(-1, BTRFS_IOC_WAIT_SYNC, &u64val);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700276 printf("ioctl(-1, %s, [%" PRIu64 "]) = -1 EBADF (%m)\n",
277 ioc(BTRFS_IOC_WAIT_SYNC), u64val);
Jeff Mahoney35866792016-05-18 18:09:42 -0400278
279 /*
280 * The live test of BTRFS_IOC_SYNC happens as a part of the test
281 * for BTRFS_IOC_LOGICAL_INO
282 */
283}
284
285static void
286btrfs_print_qgroup_inherit(struct btrfs_qgroup_inherit *inherit)
287{
288 printf("{flags=");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700289 prfl_btrfs(btrfs_qgroup_inherit_flags, inherit->flags,
Jeff Mahoney35866792016-05-18 18:09:42 -0400290 "BTRFS_QGROUP_INHERIT_???");
291 printf(", num_qgroups=%" PRI__u64
292 ", num_ref_copies=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700293 ", num_excl_copies=%" PRI__u64 ", lim={flags=",
Jeff Mahoney35866792016-05-18 18:09:42 -0400294 inherit->num_qgroups, inherit->num_ref_copies,
295 inherit->num_excl_copies);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700296 prfl_btrfs(btrfs_qgroup_limit_flags,
Jeff Mahoney35866792016-05-18 18:09:42 -0400297 inherit->lim.flags,
298 "BTRFS_QGROUP_LIMIT_???");
299 printf(", max_rfer=%" PRI__u64 ", max_excl=%" PRI__u64
300 ", rsv_rfer=%" PRI__u64 ", rsv_excl=%" PRI__u64
Elliott Hughes03a418e2018-06-15 13:11:40 -0700301 "}, ",
Jeff Mahoney35866792016-05-18 18:09:42 -0400302 inherit->lim.max_rfer, inherit->lim.max_excl,
303 inherit->lim.rsv_rfer, inherit->lim.rsv_excl);
304 if (verbose) {
305 unsigned int i;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700306 printf("qgroups=[");
Jeff Mahoney35866792016-05-18 18:09:42 -0400307 for (i = 0; i < inherit->num_qgroups; i++) {
308 if (i > 0)
309 printf(", ");
310 printf("%" PRI__u64, inherit->qgroups[i]);
311 }
312 printf("]");
313 } else
314 printf("...");
315 printf("}");
316}
317
318
319static void
320btrfs_print_vol_args_v2(struct btrfs_ioctl_vol_args_v2 *args, int print_qgroups)
321{
Dmitry V. Levin6ce6d3b2016-05-24 01:53:02 +0000322 printf("{fd=%d, flags=", (int) args->fd);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700323 prfl_btrfs(btrfs_snap_flags_v2, args->flags, "BTRFS_SUBVOL_???");
Jeff Mahoney35866792016-05-18 18:09:42 -0400324
325 if (args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
326 printf(", size=%" PRI__u64 ", qgroup_inherit=", args->size);
327 if (args->qgroup_inherit && print_qgroups)
328 btrfs_print_qgroup_inherit(args->qgroup_inherit);
329 else if (args->qgroup_inherit)
330 printf("%p", args->qgroup_inherit);
331 else
332 printf("NULL");
333 }
334 printf(", name=\"%s\"}", args->name);
335}
336
337/*
338 * Consumes argument, returns nothing:
339 * - BTRFS_IOC_SNAP_CREATE
340 * - BTRFS_IOC_SUBVOL_CREATE
341 * - BTRFS_IOC_SNAP_DESTROY
342 * - BTRFS_IOC_DEFAULT_SUBVOL
343 *
344 * Consumes argument, returns u64:
345 * - BTRFS_IOC_SNAP_CREATE_V2
346 * - BTRFS_IOC_SUBVOL_CREATE_V2
347 */
348
349static void
350btrfs_test_subvol_ioctls(void)
351{
352 const char *subvol_name = "subvol-name";
353 char *long_subvol_name;
Elliott Hughesd35df492017-02-15 15:19:05 -0800354 void *bad_pointer = (void *) (unsigned long) 0xdeadbeeffffffeedULL;
355 uint64_t u64val = 0xdeadbeefbadc0dedULL;
Jeff Mahoney35866792016-05-18 18:09:42 -0400356 struct btrfs_ioctl_vol_args vol_args = {};
357 struct btrfs_ioctl_vol_args_v2 vol_args_v2 = {
358 .fd = 2,
359 .flags = max_flags_plus_one(2),
360 };
Jeff Mahoney35866792016-05-18 18:09:42 -0400361
362 long_subvol_name = malloc(BTRFS_PATH_NAME_MAX);
363 if (!long_subvol_name)
364 perror_msg_and_fail("malloc failed");
365 memset(long_subvol_name, 'f', BTRFS_PATH_NAME_MAX);
366 long_subvol_name[BTRFS_PATH_NAME_MAX - 1] = '\0';
367
368 strcpy(vol_args.name, subvol_name);
369
370 ioctl(-1, BTRFS_IOC_SNAP_CREATE, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700371 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
372 ioc(BTRFS_IOC_SNAP_CREATE));
Jeff Mahoney35866792016-05-18 18:09:42 -0400373
374 ioctl(-1, BTRFS_IOC_SNAP_CREATE, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700375 printf("ioctl(-1, %s, {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n",
376 ioc(BTRFS_IOC_SNAP_CREATE), vol_args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400377
378 ioctl(-1, BTRFS_IOC_SUBVOL_CREATE, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700379 printf("ioctl(-1, %s, {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n",
380 ioc(BTRFS_IOC_SUBVOL_CREATE), vol_args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400381
382 ioctl(-1, BTRFS_IOC_SNAP_DESTROY, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700383 printf("ioctl(-1, %s, {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n",
384 ioc(BTRFS_IOC_SNAP_DESTROY), vol_args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400385
386 strncpy(vol_args.name, long_subvol_name, BTRFS_PATH_NAME_MAX);
387 ioctl(-1, BTRFS_IOC_SNAP_CREATE, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700388 printf("ioctl(-1, %s, {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n",
389 ioc(BTRFS_IOC_SNAP_CREATE), vol_args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400390
391 ioctl(-1, BTRFS_IOC_SUBVOL_CREATE, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700392 printf("ioctl(-1, %s, {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n",
393 ioc(BTRFS_IOC_SUBVOL_CREATE), vol_args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400394
395 ioctl(-1, BTRFS_IOC_SNAP_DESTROY, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700396 printf("ioctl(-1, %s, {fd=0, name=\"%s\"}) = -1 EBADF (%m)\n",
397 ioc(BTRFS_IOC_SNAP_DESTROY), vol_args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400398
399 long_subvol_name = realloc(long_subvol_name, BTRFS_SUBVOL_NAME_MAX);
400 if (!long_subvol_name)
401 perror_msg_and_fail("realloc failed");
402
403 ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700404 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
405 ioc(BTRFS_IOC_SNAP_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400406
407 ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700408 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
409 ioc(BTRFS_IOC_SUBVOL_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400410
411 strcpy(vol_args_v2.name, subvol_name);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700412 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SNAP_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400413 btrfs_print_vol_args_v2(&vol_args_v2, 1);
414 ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2);
415 printf(") = -1 EBADF (%m)\n");
416
Elliott Hughes03a418e2018-06-15 13:11:40 -0700417 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SUBVOL_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400418 btrfs_print_vol_args_v2(&vol_args_v2, 1);
419 ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2);
420 printf(") = -1 EBADF (%m)\n");
421
422 strncpy(vol_args_v2.name, long_subvol_name, BTRFS_SUBVOL_NAME_MAX);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700423 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SNAP_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400424 btrfs_print_vol_args_v2(&vol_args_v2, 1);
425 ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2);
426 printf(") = -1 EBADF (%m)\n");
427
Elliott Hughes03a418e2018-06-15 13:11:40 -0700428 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SUBVOL_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400429 btrfs_print_vol_args_v2(&vol_args_v2, 1);
430 ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2);
431 printf(") = -1 EBADF (%m)\n");
432
433 strcpy(vol_args_v2.name, subvol_name);
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000434 vol_args_v2.qgroup_inherit = bad_pointer;
Jeff Mahoney35866792016-05-18 18:09:42 -0400435
Elliott Hughes03a418e2018-06-15 13:11:40 -0700436 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SNAP_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400437 btrfs_print_vol_args_v2(&vol_args_v2, 0);
438 ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2);
439 printf(") = -1 EBADF (%m)\n");
440
Elliott Hughes03a418e2018-06-15 13:11:40 -0700441 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SUBVOL_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400442 btrfs_print_vol_args_v2(&vol_args_v2, 0);
443 ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2);
444 printf(") = -1 EBADF (%m)\n");
445
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000446 const unsigned int n_qgroups = 8;
447 unsigned int i;
448 struct btrfs_qgroup_inherit *inherit;
449 vol_args_v2.size =
450 sizeof(*inherit) + n_qgroups * sizeof(inherit->qgroups[0]);
451 inherit = tail_alloc(vol_args_v2.size);
Jeff Mahoney35866792016-05-18 18:09:42 -0400452
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000453 inherit->flags = 0x3;
454 inherit->num_ref_copies = 0;
455 inherit->num_excl_copies = 0;
456 inherit->num_qgroups = n_qgroups;
457 for (i = 0; i < n_qgroups; i++)
458 inherit->qgroups[i] = 1ULL << i;
459 inherit->lim.flags = 0x7f;
460 inherit->lim.max_rfer = u64val;
461 inherit->lim.max_excl = u64val;
462 inherit->lim.rsv_rfer = u64val;
463 inherit->lim.rsv_excl = u64val;
464 vol_args_v2.qgroup_inherit = inherit;
Jeff Mahoney35866792016-05-18 18:09:42 -0400465
Elliott Hughes03a418e2018-06-15 13:11:40 -0700466 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SNAP_CREATE_V2));
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000467 btrfs_print_vol_args_v2(&vol_args_v2, 1);
468 ioctl(-1, BTRFS_IOC_SNAP_CREATE_V2, &vol_args_v2);
469 printf(") = -1 EBADF (%m)\n");
Jeff Mahoney35866792016-05-18 18:09:42 -0400470
Elliott Hughes03a418e2018-06-15 13:11:40 -0700471 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SUBVOL_CREATE_V2));
Dmitry V. Levin45fcc0c2016-05-24 01:32:27 +0000472 btrfs_print_vol_args_v2(&vol_args_v2, 1);
473 ioctl(-1, BTRFS_IOC_SUBVOL_CREATE_V2, &vol_args_v2);
474 printf(") = -1 EBADF (%m)\n");
Jeff Mahoney35866792016-05-18 18:09:42 -0400475
476 ioctl(-1, BTRFS_IOC_DEFAULT_SUBVOL, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700477 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
478 ioc(BTRFS_IOC_DEFAULT_SUBVOL));
Jeff Mahoney35866792016-05-18 18:09:42 -0400479
480 ioctl(-1, BTRFS_IOC_DEFAULT_SUBVOL, &u64val);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700481 printf("ioctl(-1, %s, [%" PRIu64 "]) = -1 EBADF (%m)\n",
482 ioc(BTRFS_IOC_DEFAULT_SUBVOL), u64val);
Jeff Mahoney35866792016-05-18 18:09:42 -0400483
Elliott Hughes03a418e2018-06-15 13:11:40 -0700484 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_SUBVOL_SETFLAGS));
485 prfl_btrfs(btrfs_snap_flags_v2, vol_args_v2.flags,
Jeff Mahoney35866792016-05-18 18:09:42 -0400486 "BTRFS_SUBVOL_???");
487 ioctl(-1, BTRFS_IOC_SUBVOL_SETFLAGS, &vol_args_v2.flags);
488 printf(") = -1 EBADF (%m)\n");
489
490 if (write_ok) {
491 struct btrfs_ioctl_vol_args_v2 args_passed;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700492 long ret;
Jeff Mahoney35866792016-05-18 18:09:42 -0400493 /*
494 * Returns transid if flags & BTRFS_SUBVOL_CREATE_ASYNC
495 * - BTRFS_IOC_SNAP_CREATE_V2
496 * - BTRFS_IOC_SUBVOL_CREATE_V2
497 */
498 int subvolfd;
499
500 strncpy(vol_args_v2.name, subvol_name,
501 sizeof(vol_args_v2.name));
502 vol_args_v2.flags = BTRFS_SUBVOL_CREATE_ASYNC;
503 vol_args_v2.size = 0;
504 vol_args_v2.qgroup_inherit = NULL;
505 args_passed = vol_args_v2;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700506 printf("ioctl(%d, %s, ",
507 btrfs_test_dir_fd, ioc(BTRFS_IOC_SUBVOL_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400508 btrfs_print_vol_args_v2(&vol_args_v2, 1);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700509 ret = ioctl(btrfs_test_dir_fd, BTRFS_IOC_SUBVOL_CREATE_V2,
Jeff Mahoney35866792016-05-18 18:09:42 -0400510 &args_passed);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700511 if (ret < 0)
512 perror_msg_and_fail("ioctl(BTRFS_IOC_SUBVOL_CREATE_V2) "
513 "failed");
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700514 printf(" => {transid=%" PRI__u64 "}) = 0\n",
Jeff Mahoney35866792016-05-18 18:09:42 -0400515 args_passed.transid);
516
517 subvolfd = openat(btrfs_test_dir_fd, subvol_name,
518 O_RDONLY|O_DIRECTORY);
519 if (subvolfd < 0)
520 perror_msg_and_fail("openat(%s) failed", subvol_name);
521
522 strncpy(vol_args_v2.name, long_subvol_name, BTRFS_NAME_LEN);
523 vol_args_v2.fd = subvolfd;
524 args_passed = vol_args_v2;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700525 printf("ioctl(%d, %s, ",
526 btrfs_test_dir_fd, ioc(BTRFS_IOC_SNAP_CREATE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400527 btrfs_print_vol_args_v2(&args_passed, 1);
528 ioctl(btrfs_test_dir_fd, BTRFS_IOC_SNAP_CREATE_V2,
529 &args_passed);
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700530 printf(" => {transid=%" PRI__u64 "}) = 0\n",
Jeff Mahoney35866792016-05-18 18:09:42 -0400531 args_passed.transid);
532
533 /* This only works when mounted w/ -ouser_subvol_rm_allowed */
534 strncpy(vol_args.name, long_subvol_name, 255);
535 vol_args.name[255] = 0;
536 ioctl(btrfs_test_dir_fd, BTRFS_IOC_SNAP_DESTROY, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700537 printf("ioctl(%d, %s, {fd=%d, name=\"%.*s\"}) = 0\n",
538 btrfs_test_dir_fd, ioc(BTRFS_IOC_SNAP_DESTROY),
539 (int) vol_args.fd, 255, long_subvol_name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400540
541 strcpy(vol_args.name, subvol_name);
542 ioctl(btrfs_test_dir_fd, BTRFS_IOC_SNAP_DESTROY, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700543 printf("ioctl(%d, %s, {fd=%d, name=\"%s\"}) = 0\n",
544 btrfs_test_dir_fd, ioc(BTRFS_IOC_SNAP_DESTROY),
545 (int) vol_args.fd, subvol_name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400546
547 close(subvolfd);
548 }
549 free(long_subvol_name);
550}
551
552static void
553btrfs_print_balance_args(struct btrfs_balance_args *args)
554{
555 printf("{profiles=");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700556 prfl_btrfs(btrfs_space_info_flags, args->profiles,
Jeff Mahoney35866792016-05-18 18:09:42 -0400557 "BTRFS_BLOCK_GROUP_???");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700558 print_uint64(", usage=", args->usage);
559 printf(", devid=makedev(%u, %u)",
560 major(args->devid), minor(args->devid));
561 print_uint64(", pstart=", args->pstart);
562 print_uint64(", pend=", args->pend);
563 print_uint64(", vstart=", args->vstart);
564 print_uint64(", vend=", args->vend);
565 print_uint64(", target=", args->target);
566 printf(", flags=");
567 prfl_btrfs(btrfs_balance_args, args->flags, "BTRFS_BALANCE_ARGS_???");
Jeff Mahoney35866792016-05-18 18:09:42 -0400568 printf("}");
569}
570
571/*
572 * Accepts argument, returns nothing
573 * - BTRFS_IOC_BALANCE
574 * - BTRFS_IOC_BALANCE_CTL
575 *
576 * Accepts argument, returns argument
577 * - BTRFS_IOC_BALANCE_V2
578 */
579static void
580btrfs_test_balance_ioctls(void)
581{
582 struct btrfs_ioctl_balance_args args = {
583 .flags = 0x3f,
584 .data = {
585 .profiles = 0x7,
586 .flags = 0x7,
587 .devid = 1,
588 .pend = -1ULL,
589 .vend = -1ULL,
590 },
591
592 .meta = {
593 .profiles = 0x38,
594 .flags = 0x38,
595 .devid = 1,
596 },
597
598 .sys = {
599 .profiles = 0x1c0 | (1ULL << 48),
600 .flags = 0x4c0,
601 .devid = 1,
602 },
603 };
604 struct btrfs_ioctl_vol_args vol_args = {};
605
606 ioctl(-1, BTRFS_IOC_BALANCE_CTL, 1);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700607 printf("ioctl(-1, %s, %sBTRFS_BALANCE_CTL_PAUSE%s) = -1 EBADF (%m)\n",
608 ioc(BTRFS_IOC_BALANCE_CTL),
609 verbose_xlat ? "0x1 /* " : "",
610 verbose_xlat ? " */" : "");
Jeff Mahoney35866792016-05-18 18:09:42 -0400611
612 ioctl(-1, BTRFS_IOC_BALANCE_CTL, 2);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700613 printf("ioctl(-1, %s, "
614 "%sBTRFS_BALANCE_CTL_CANCEL%s) = -1 EBADF (%m)\n",
615 ioc(BTRFS_IOC_BALANCE_CTL),
616 verbose_xlat ? "0x2 /* " : "",
617 verbose_xlat ? " */" : "");
Jeff Mahoney35866792016-05-18 18:09:42 -0400618
619 ioctl(-1, BTRFS_IOC_BALANCE, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700620 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_BALANCE));
Jeff Mahoney35866792016-05-18 18:09:42 -0400621
622 ioctl(-1, BTRFS_IOC_BALANCE, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700623 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_BALANCE));
Jeff Mahoney35866792016-05-18 18:09:42 -0400624
625 /* struct btrfs_ioctl_balance_args */
626 ioctl(-1, BTRFS_IOC_BALANCE_V2, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700627 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
628 ioc(BTRFS_IOC_BALANCE_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400629
Elliott Hughes03a418e2018-06-15 13:11:40 -0700630 printf("ioctl(-1, %s, {flags=", ioc(BTRFS_IOC_BALANCE_V2));
631 prfl_btrfs(btrfs_balance_flags, args.flags, "BTRFS_BALANCE_???");
Jeff Mahoney35866792016-05-18 18:09:42 -0400632 printf(", data=");
633 btrfs_print_balance_args(&args.data);
634 printf(", meta=");
635 btrfs_print_balance_args(&args.meta);
636 printf(", sys=");
637 btrfs_print_balance_args(&args.sys);
638 ioctl(-1, BTRFS_IOC_BALANCE_V2, &args);
639 printf("}) = -1 EBADF (%m)\n");
640
641 if (write_ok) {
Elliott Hughes03a418e2018-06-15 13:11:40 -0700642 long ret;
643
Jeff Mahoney35866792016-05-18 18:09:42 -0400644 args.flags = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA |
645 BTRFS_BALANCE_SYSTEM;
646 args.data.flags = 0;
647 args.data.profiles = 0;
648 args.meta.flags = 0;
649 args.meta.profiles = 0;
650 args.sys.flags = 0;
651 args.sys.profiles = 0;
Jeff Mahoney35866792016-05-18 18:09:42 -0400652
Elliott Hughes03a418e2018-06-15 13:11:40 -0700653 /*
654 * We should keep args the same for data in meta in case
655 * volume-under-tests uses mixed groups data and metadata.
656 */
657 args.meta.pend = -1ULL;
658 args.meta.vend = -1ULL;
659
660 printf("ioctl(%d, %s, {flags=",
661 btrfs_test_dir_fd, ioc(BTRFS_IOC_BALANCE_V2));
662
663 prfl_btrfs(btrfs_balance_flags, args.flags,
Jeff Mahoney35866792016-05-18 18:09:42 -0400664 "BTRFS_BALANCE_???");
665 printf(", data=");
666 btrfs_print_balance_args(&args.data);
667 printf(", meta=");
668 btrfs_print_balance_args(&args.meta);
669 printf(", sys=");
670 btrfs_print_balance_args(&args.sys);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700671 ret = ioctl(btrfs_test_dir_fd, BTRFS_IOC_BALANCE_V2, &args);
672 if (ret < 0) {
673 printf("}) = %s\n", sprintrc(ret));
674 } else {
675 printf("} => {flags=");
676 prfl_btrfs(btrfs_balance_flags, args.flags,
677 "BTRFS_BALANCE_???");
678 printf(", state=");
679 prfl_btrfs(btrfs_balance_state, args.state,
680 "BTRFS_BALANCE_STATE_???");
681 printf(", data=");
682 btrfs_print_balance_args(&args.data);
683 printf(", meta=");
684 btrfs_print_balance_args(&args.meta);
685 printf(", sys=");
686 btrfs_print_balance_args(&args.sys);
687 printf("}) = %ld\n", ret);
688 }
Jeff Mahoney35866792016-05-18 18:09:42 -0400689 }
690}
691
692/*
693 * Consumes argument, returns nothing:
694 * - BTRFS_IOC_RESIZE
695 *
696 * Requires /dev/btrfs-control, consumes argument, returns nothing:
697 * - BTRFS_IOC_SCAN_DEV
698 * - BTRFS_IOC_DEVICES_READY
699 *
700 */
701static void
702btrfs_test_device_ioctls(void)
703{
704 const char *devid = "1";
705 const char *devname = "/dev/sda1";
706 struct btrfs_ioctl_vol_args args = {
707 .fd = 2,
708 };
709
710 ioctl(-1, BTRFS_IOC_RESIZE, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700711 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_RESIZE));
Jeff Mahoney35866792016-05-18 18:09:42 -0400712
713 strcpy(args.name, devid);
714 ioctl(-1, BTRFS_IOC_RESIZE, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700715 printf("ioctl(-1, %s, {fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
716 ioc(BTRFS_IOC_RESIZE), (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400717
718 ioctl(-1, BTRFS_IOC_SCAN_DEV, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700719 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
720 ioc(BTRFS_IOC_SCAN_DEV));
Jeff Mahoney35866792016-05-18 18:09:42 -0400721
722 strcpy(args.name, devname);
723 ioctl(-1, BTRFS_IOC_SCAN_DEV, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700724 printf("ioctl(-1, %s, {fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
725 ioc(BTRFS_IOC_SCAN_DEV), (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400726
727 ioctl(-1, BTRFS_IOC_ADD_DEV, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700728 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_ADD_DEV));
Jeff Mahoney35866792016-05-18 18:09:42 -0400729
730 ioctl(-1, BTRFS_IOC_ADD_DEV, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700731 printf("ioctl(-1, %s, {fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
732 ioc(BTRFS_IOC_ADD_DEV), (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400733
734 ioctl(-1, BTRFS_IOC_RM_DEV, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700735 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_RM_DEV));
Jeff Mahoney35866792016-05-18 18:09:42 -0400736
737 ioctl(-1, BTRFS_IOC_RM_DEV, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700738 printf("ioctl(-1, %s, {fd=%d, name=\"%s\"}) = -1 EBADF (%m)\n",
739 ioc(BTRFS_IOC_RM_DEV), (int) args.fd, args.name);
Jeff Mahoney35866792016-05-18 18:09:42 -0400740
741}
742
743/*
744 * Consumes argument, returns nothing:
745 * - BTRFS_IOC_CLONE
746 * - BTRFS_IOC_CLONE_RANGE
747 */
748static void
749btrfs_test_clone_ioctls(void)
750{
751 int clone_fd = 4;
752 struct btrfs_ioctl_clone_range_args args = {
753 .src_fd = clone_fd,
754 .src_offset = 4096,
755 .src_length = 16384,
756 .dest_offset = 128 * 1024,
757 };
758
759 ioctl(-1, BTRFS_IOC_CLONE, clone_fd);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700760 printf("ioctl(-1, %s, %x) = -1 EBADF (%m)\n",
761 sprint_xlat_(BTRFS_IOC_CLONE, "BTRFS_IOC_CLONE or FICLONE"),
762 clone_fd);
Jeff Mahoney35866792016-05-18 18:09:42 -0400763
764 ioctl(-1, BTRFS_IOC_CLONE_RANGE, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700765 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
766 sprint_xlat_(BTRFS_IOC_CLONE_RANGE,
767 "BTRFS_IOC_CLONE_RANGE or FICLONERANGE"));
Jeff Mahoney35866792016-05-18 18:09:42 -0400768
769 ioctl(-1, BTRFS_IOC_CLONE_RANGE, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700770 printf("ioctl(-1, %s, "
Dmitry V. Levin3f366112016-05-24 02:17:49 +0000771 "{src_fd=%d, src_offset=%" PRI__u64 ", src_length=%" PRI__u64
772 ", dest_offset=%" PRI__u64 "}) = -1 EBADF (%m)\n",
Elliott Hughes03a418e2018-06-15 13:11:40 -0700773 sprint_xlat_(BTRFS_IOC_CLONE_RANGE,
774 "BTRFS_IOC_CLONE_RANGE or FICLONERANGE"),
Dmitry V. Levin6ce6d3b2016-05-24 01:53:02 +0000775 (int) args.src_fd, args.src_offset, args.src_length,
Jeff Mahoney35866792016-05-18 18:09:42 -0400776 args.dest_offset);
777}
778
779#define BTRFS_COMPRESS_TYPES 2
780#define BTRFS_INVALID_COMPRESS (BTRFS_COMPRESS_TYPES + 1)
781
782static void
Elliott Hughes03a418e2018-06-15 13:11:40 -0700783btrfs_print_defrag_range_args(struct btrfs_ioctl_defrag_range_args *args,
784 bool compress_type_known)
Jeff Mahoney35866792016-05-18 18:09:42 -0400785{
Elliott Hughes03a418e2018-06-15 13:11:40 -0700786 printf("{start=%" PRIu64, (uint64_t) args->start);
787 print_uint64(", len=", args->len);
Jeff Mahoney35866792016-05-18 18:09:42 -0400788
Elliott Hughes03a418e2018-06-15 13:11:40 -0700789 printf(", flags=");
790 prfl_btrfs(btrfs_defrag_flags, args->flags, "BTRFS_DEFRAG_RANGE_???");
Jeff Mahoney35866792016-05-18 18:09:42 -0400791 printf(", extent_thresh=%u, compress_type=", args->extent_thresh);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700792 prxval_btrfs(btrfs_compress_types, args->compress_type,
793 "BTRFS_COMPRESS_???", compress_type_known);
Jeff Mahoney35866792016-05-18 18:09:42 -0400794 printf("}");
795}
796
797/*
798 * Consumes argument, returns nothing:
799 * - BTRFS_IOC_DEFRAG
800 * - BTRFS_DEFRAG_RANGE
801 */
802static void
803btrfs_test_defrag_ioctls(void)
804{
805 struct btrfs_ioctl_vol_args vol_args = {};
806 struct btrfs_ioctl_defrag_range_args args = {
807 .start = 0,
808 .len = -1ULL,
809 .flags = max_flags_plus_one(1),
810 .extent_thresh = 128 * 1024,
811 .compress_type = 2, /* BTRFS_COMPRESS_LZO */
812 };
813
814 /*
815 * These are documented as using vol_args but don't
816 * actually consume it.
817 */
818 ioctl(-1, BTRFS_IOC_DEFRAG, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700819 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_DEFRAG));
Jeff Mahoney35866792016-05-18 18:09:42 -0400820
821 ioctl(-1, BTRFS_IOC_DEFRAG, &vol_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700822 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_DEFRAG));
Jeff Mahoney35866792016-05-18 18:09:42 -0400823
824 /* struct btrfs_ioctl_defrag_range_args */
825 ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700826 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
827 ioc(BTRFS_IOC_DEFRAG_RANGE));
Jeff Mahoney35866792016-05-18 18:09:42 -0400828
Elliott Hughes03a418e2018-06-15 13:11:40 -0700829 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_DEFRAG_RANGE));
830 btrfs_print_defrag_range_args(&args, true);
Jeff Mahoney35866792016-05-18 18:09:42 -0400831 ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, &args);
832 printf(") = -1 EBADF (%m)\n");
833
834 args.compress_type = BTRFS_INVALID_COMPRESS;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700835 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_DEFRAG_RANGE));
836 btrfs_print_defrag_range_args(&args, false);
Jeff Mahoney35866792016-05-18 18:09:42 -0400837 ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, &args);
838 printf(") = -1 EBADF (%m)\n");
839
840 args.len--;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700841 printf("ioctl(-1, %s, ", ioc(BTRFS_IOC_DEFRAG_RANGE));
842 btrfs_print_defrag_range_args(&args, false);
Jeff Mahoney35866792016-05-18 18:09:42 -0400843 ioctl(-1, BTRFS_IOC_DEFRAG_RANGE, &args);
844 printf(") = -1 EBADF (%m)\n");
845}
846
847static const char *
848xlookup(const struct xlat *xlat, const uint64_t val)
849{
850 for (; xlat->str != NULL; xlat++)
851 if (xlat->val == val)
852 return xlat->str;
853 return NULL;
854}
855
856static void
857btrfs_print_objectid(uint64_t objectid)
858{
859 const char *str = xlookup(btrfs_tree_objectids, objectid);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700860 if (str) {
861 if (verbose_xlat)
862 printf("%" PRIu64 " /* %s */", objectid, str);
863 else
864 printf("%s", str);
865 } else {
866 printf("%" PRIu64, objectid);
867 }
Jeff Mahoney35866792016-05-18 18:09:42 -0400868}
869
870static void
871btrfs_print_key_type(uint32_t type)
872{
873 const char *str = xlookup(btrfs_key_types, type);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700874 if (str) {
875 if (verbose_xlat)
876 printf("%u /* %s */", type, str);
877 else
878 printf("%s", str);
879 } else {
880 printf("%u", type);
881 }
Jeff Mahoney35866792016-05-18 18:09:42 -0400882}
883
884static void
885btrfs_print_search_key(struct btrfs_ioctl_search_key *key)
886{
887 printf("key={tree_id=");
888 btrfs_print_objectid(key->tree_id);
889 if (verbose || key->min_objectid != 256) {
890 printf(", min_objectid=");
891 btrfs_print_objectid(key->min_objectid);
892 }
893 if (verbose || key->max_objectid != -256ULL) {
894 printf(", max_objectid=");
895 btrfs_print_objectid(key->max_objectid);
896 }
Elliott Hughes03a418e2018-06-15 13:11:40 -0700897 print_uint64(", min_offset=", key->min_offset);
898 print_uint64(", max_offset=", key->max_offset);
899 print_uint64(", min_transid=", key->min_transid);
900 print_uint64(", max_transid=", key->max_transid);
Jeff Mahoney35866792016-05-18 18:09:42 -0400901 printf(", min_type=");
902 btrfs_print_key_type(key->min_type);
903 printf(", max_type=");
904 btrfs_print_key_type(key->max_type);
905 printf(", nr_items=%u}", key->nr_items);
906}
907
908static void
909btrfs_print_tree_search_buf(struct btrfs_ioctl_search_key *key,
910 void *buf, uint64_t buf_size)
911{
912 if (verbose) {
913 uint64_t i;
914 uint64_t off = 0;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700915 printf("buf=[");
Jeff Mahoney35866792016-05-18 18:09:42 -0400916 for (i = 0; i < key->nr_items; i++) {
917 struct btrfs_ioctl_search_header *sh;
918 sh = (typeof(sh))(buf + off);
919 if (i)
920 printf(", ");
921 printf("{transid=%" PRI__u64 ", objectid=",
922 sh->transid);
923 btrfs_print_objectid(sh->objectid);
924 printf(", offset=%" PRI__u64 ", type=", sh->offset);
925 btrfs_print_key_type(sh->type);
926 printf(", len=%u}", sh->len);
927 off += sizeof(*sh) + sh->len;
928 }
929 printf("]");
930 } else
931 printf("...");
932}
933
934/*
935 * Consumes argument, returns argument:
936 * - BTRFS_IOC_TREE_SEARCH
937 * - BTRFS_IOC_TREE_SEARCH_V2
938 */
939static void
940btrfs_test_search_ioctls(void)
941{
942 struct btrfs_ioctl_search_key key_reference = {
943 .tree_id = 5,
944 .min_objectid = 256,
945 .max_objectid = -1ULL,
946 .min_offset = 0,
947 .max_offset = -1ULL,
948 .min_transid = 0,
949 .max_transid = -1ULL,
950 .min_type = 0,
951 .max_type = -1U,
952 .nr_items = 10,
953 };
954 struct btrfs_ioctl_search_args search_args;
955 struct btrfs_ioctl_search_args_v2 search_args_v2 = {
956 .buf_size = 4096,
957 };
958
959 ioctl(-1, BTRFS_IOC_TREE_SEARCH, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700960 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
961 ioc(BTRFS_IOC_TREE_SEARCH));
Jeff Mahoney35866792016-05-18 18:09:42 -0400962
963 ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700964 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
965 ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400966
Jeff Mahoney563a7582016-05-26 16:09:28 -0400967 search_args.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700968 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH));
Jeff Mahoney35866792016-05-18 18:09:42 -0400969 btrfs_print_search_key(&search_args.key);
970 ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args);
971 printf("}) = -1 EBADF (%m)\n");
972
973 search_args_v2.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700974 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400975 btrfs_print_search_key(&search_args_v2.key);
976 ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2);
977 printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n",
978 (uint64_t)search_args_v2.buf_size);
979
980 key_reference.min_objectid = 6;
981 key_reference.max_objectid = 7;
Jeff Mahoney563a7582016-05-26 16:09:28 -0400982 search_args.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700983 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH));
Jeff Mahoney35866792016-05-18 18:09:42 -0400984 btrfs_print_search_key(&search_args.key);
985 ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args);
986 printf("}) = -1 EBADF (%m)\n");
987
988 search_args_v2.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700989 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -0400990 btrfs_print_search_key(&search_args_v2.key);
991 ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2);
992 printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n",
993 (uint64_t)search_args_v2.buf_size);
994
995 key_reference.min_offset++;
996 key_reference.max_offset--;
Jeff Mahoney563a7582016-05-26 16:09:28 -0400997 search_args.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700998 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH));
Jeff Mahoney35866792016-05-18 18:09:42 -0400999 btrfs_print_search_key(&search_args.key);
1000 ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args);
1001 printf("}) = -1 EBADF (%m)\n");
1002
1003 search_args_v2.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001004 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -04001005 btrfs_print_search_key(&search_args_v2.key);
1006 ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2);
1007 printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n",
1008 (uint64_t)search_args_v2.buf_size);
1009
1010 key_reference.min_transid++;
1011 key_reference.max_transid--;
Jeff Mahoney563a7582016-05-26 16:09:28 -04001012 search_args.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001013 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH));
Jeff Mahoney35866792016-05-18 18:09:42 -04001014 btrfs_print_search_key(&search_args.key);
1015 ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args);
1016 printf("}) = -1 EBADF (%m)\n");
1017
1018 search_args_v2.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001019 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -04001020 btrfs_print_search_key(&search_args_v2.key);
1021 ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2);
1022 printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n",
1023 (uint64_t)search_args_v2.buf_size);
1024
1025 key_reference.min_type = 1;
1026 key_reference.max_type = 12;
Jeff Mahoney563a7582016-05-26 16:09:28 -04001027 search_args.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001028 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH));
Jeff Mahoney35866792016-05-18 18:09:42 -04001029 btrfs_print_search_key(&search_args.key);
1030 ioctl(-1, BTRFS_IOC_TREE_SEARCH, &search_args);
1031 printf("}) = -1 EBADF (%m)\n");
1032
1033 search_args_v2.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001034 printf("ioctl(-1, %s, {", ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -04001035 btrfs_print_search_key(&search_args_v2.key);
1036 ioctl(-1, BTRFS_IOC_TREE_SEARCH_V2, &search_args_v2);
1037 printf(", buf_size=%" PRIu64 "}) = -1 EBADF (%m)\n",
1038 (uint64_t)search_args_v2.buf_size);
1039
1040 if (btrfs_test_root) {
1041 struct btrfs_ioctl_search_args_v2 *args;
1042 int bufsize = 4096;
1043
1044 key_reference.tree_id = 5;
1045 key_reference.min_type = 1;
1046 key_reference.max_type = 1;
1047 key_reference.min_objectid = 256;
1048 key_reference.max_objectid = 357;
1049 key_reference.min_offset = 0;
1050 key_reference.max_offset = -1ULL;
1051
1052 search_args.key = key_reference;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001053 printf("ioctl(%d, %s, {",
1054 btrfs_test_dir_fd, ioc(BTRFS_IOC_TREE_SEARCH));
Jeff Mahoney35866792016-05-18 18:09:42 -04001055 btrfs_print_search_key(&search_args.key);
1056 ioctl(btrfs_test_dir_fd, BTRFS_IOC_TREE_SEARCH, &search_args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001057 printf("} => {key={nr_items=%u}, ",
Jeff Mahoney35866792016-05-18 18:09:42 -04001058 search_args.key.nr_items);
Jeff Mahoneyfd70b502016-05-26 23:33:05 -04001059 btrfs_print_tree_search_buf(&search_args.key, search_args.buf,
1060 sizeof(search_args.buf));
1061 printf("}) = 0\n");
Jeff Mahoney35866792016-05-18 18:09:42 -04001062
1063 args = malloc(sizeof(*args) + bufsize);
1064 if (!args)
1065 perror_msg_and_fail("malloc failed");
1066
1067 args->key = key_reference;
1068 args->buf_size = bufsize;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001069 printf("ioctl(%d, %s, {",
1070 btrfs_test_dir_fd, ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -04001071 btrfs_print_search_key(&key_reference);
1072 printf(", buf_size=%" PRIu64 "}", (uint64_t) args->buf_size);
1073 ioctl(btrfs_test_dir_fd, BTRFS_IOC_TREE_SEARCH_V2, args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001074 printf(" => {key={nr_items=%u}, buf_size=%" PRIu64 ", ",
Jeff Mahoney35866792016-05-18 18:09:42 -04001075 args->key.nr_items, (uint64_t)args->buf_size);
1076 btrfs_print_tree_search_buf(&args->key, args->buf,
1077 args->buf_size);
1078 printf("}) = 0\n");
1079
1080 args->key = key_reference;
1081 args->buf_size = sizeof(struct btrfs_ioctl_search_header);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001082 printf("ioctl(%d, %s, {",
1083 btrfs_test_dir_fd, ioc(BTRFS_IOC_TREE_SEARCH_V2));
Jeff Mahoney35866792016-05-18 18:09:42 -04001084 btrfs_print_search_key(&args->key);
1085 printf(", buf_size=%" PRIu64 "}", (uint64_t)args->buf_size);
1086 ioctl(btrfs_test_dir_fd, BTRFS_IOC_TREE_SEARCH_V2, args);
Jeff Mahoneyfd70b502016-05-26 23:33:05 -04001087 printf(" => {buf_size=%" PRIu64 "}) = -1 EOVERFLOW (%m)\n",
Jeff Mahoney35866792016-05-18 18:09:42 -04001088 (uint64_t)args->buf_size);
1089 free(args);
1090 }
1091}
1092
1093/*
1094 * Consumes argument, returns argument:
1095 * - BTRFS_IOC_INO_LOOKUP
1096 */
1097static void
1098btrfs_test_ino_lookup_ioctl(void)
1099{
1100 struct btrfs_ioctl_ino_lookup_args args = {
1101 .treeid = 5,
1102 .objectid = 256,
1103 };
1104
1105 ioctl(-1, BTRFS_IOC_INO_LOOKUP, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001106 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1107 ioc(BTRFS_IOC_INO_LOOKUP));
Jeff Mahoney35866792016-05-18 18:09:42 -04001108
Elliott Hughes03a418e2018-06-15 13:11:40 -07001109 printf("ioctl(-1, %s, {treeid=", ioc(BTRFS_IOC_INO_LOOKUP));
Jeff Mahoney35866792016-05-18 18:09:42 -04001110 btrfs_print_objectid(args.treeid);
1111 printf(", objectid=");
1112 btrfs_print_objectid(args.objectid);
1113 ioctl(-1, BTRFS_IOC_INO_LOOKUP, &args);
1114 printf("}) = -1 EBADF (%m)\n");
1115
1116 if (btrfs_test_root) {
Elliott Hughes03a418e2018-06-15 13:11:40 -07001117 printf("ioctl(%d, %s, {treeid=",
1118 btrfs_test_dir_fd, ioc(BTRFS_IOC_INO_LOOKUP));
Jeff Mahoney35866792016-05-18 18:09:42 -04001119 btrfs_print_objectid(args.treeid);
1120 printf(", objectid=");
1121 btrfs_print_objectid(args.objectid);
1122 ioctl(btrfs_test_dir_fd, BTRFS_IOC_INO_LOOKUP, &args);
1123 printf("} => {name=\"%s\"}) = 0\n", args.name);
1124 }
1125}
1126
1127/*
1128 * Consumes argument, returns argument:
1129 * - BTRFS_IOC_SPACE_INFO
1130 */
1131static void
1132btrfs_test_space_info_ioctl(void)
1133{
1134 struct btrfs_ioctl_space_args args = {};
1135
1136 ioctl(-1, BTRFS_IOC_SPACE_INFO, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001137 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1138 ioc(BTRFS_IOC_SPACE_INFO));
Jeff Mahoney35866792016-05-18 18:09:42 -04001139
1140 ioctl(-1, BTRFS_IOC_SPACE_INFO, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001141 printf("ioctl(-1, %s, {space_slots=%" PRI__u64 "}) = -1 EBADF (%m)\n",
1142 ioc(BTRFS_IOC_SPACE_INFO), args.space_slots);
Jeff Mahoney35866792016-05-18 18:09:42 -04001143
1144 if (btrfs_test_root) {
1145 struct btrfs_ioctl_space_args args_passed;
1146 struct btrfs_ioctl_space_args *argsp;
1147 args_passed = args;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001148 printf("ioctl(%d, %s, {space_slots=%" PRI__u64 "}",
1149 btrfs_test_dir_fd, ioc(BTRFS_IOC_SPACE_INFO),
1150 args_passed.space_slots);
Jeff Mahoney35866792016-05-18 18:09:42 -04001151 ioctl(btrfs_test_dir_fd, BTRFS_IOC_SPACE_INFO, &args_passed);
1152 printf(" => {total_spaces=%" PRI__u64 "}) = 0\n",
1153 args_passed.total_spaces);
1154
1155 argsp = malloc(sizeof(args) +
1156 args_passed.total_spaces * sizeof(args.spaces[0]));
1157 if (!argsp)
1158 perror_msg_and_fail("malloc failed");
1159
1160 *argsp = args;
1161 argsp->space_slots = args_passed.total_spaces;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001162 printf("ioctl(%d, %s, {space_slots=%" PRI__u64 "}",
1163 btrfs_test_dir_fd, ioc(BTRFS_IOC_SPACE_INFO),
1164 argsp->space_slots);
Jeff Mahoney35866792016-05-18 18:09:42 -04001165 ioctl(btrfs_test_dir_fd, BTRFS_IOC_SPACE_INFO, argsp);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001166 printf(" => {total_spaces=%" PRI__u64 ", ",
Jeff Mahoney35866792016-05-18 18:09:42 -04001167 argsp->total_spaces);
1168 if (verbose) {
1169 unsigned int i;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001170 printf("spaces=[");
Jeff Mahoney35866792016-05-18 18:09:42 -04001171 for (i = 0; i < argsp->total_spaces; i++) {
1172 struct btrfs_ioctl_space_info *info;
1173 info = &argsp->spaces[i];
1174 if (i)
1175 printf(", ");
1176 printf("{flags=");
Elliott Hughes03a418e2018-06-15 13:11:40 -07001177 prfl_btrfs(btrfs_space_info_flags, info->flags,
Jeff Mahoney35866792016-05-18 18:09:42 -04001178 "BTRFS_SPACE_INFO_???");
1179 printf(", total_bytes=%" PRI__u64
1180 ", used_bytes=%" PRI__u64 "}",
1181 info->total_bytes, info->used_bytes);
1182 }
1183
1184 printf("]");
1185 } else
1186 printf("...");
1187 printf("}) = 0\n");
1188 free(argsp);
1189 }
1190}
1191
1192/*
1193 * Consumes no arguments, returns nothing:
1194 * - BTRFS_IOC_SCRUB_CANCEL
1195 * Consumes argument, returns argument:
1196 - * BTRFS_IOC_SCRUB
1197 - * BTRFS_IOC_SCRUB_PROGRESS
1198 */
1199static void
1200btrfs_test_scrub_ioctls(void)
1201{
1202 struct btrfs_ioctl_scrub_args args = {
1203 .devid = 1,
1204 .start = 0,
1205 .end = -1ULL,
1206 .flags = max_flags_plus_one(0),
1207 };
1208
1209 ioctl(-1, BTRFS_IOC_SCRUB, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001210 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_SCRUB));
Jeff Mahoney35866792016-05-18 18:09:42 -04001211
1212 ioctl(-1, BTRFS_IOC_SCRUB_CANCEL, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001213 printf("ioctl(-1, %s) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_SCRUB_CANCEL));
Jeff Mahoney35866792016-05-18 18:09:42 -04001214
Elliott Hughes03a418e2018-06-15 13:11:40 -07001215 printf("ioctl(-1, %s, {devid=makedev(%u, %u)",
1216 ioc(BTRFS_IOC_SCRUB), major(args.devid), minor(args.devid));
1217 print_uint64(", start=", args.start);
1218 print_uint64(", end=", args.end);
1219 printf(", flags=");
1220 prfl_btrfs(btrfs_scrub_flags, args.flags, "BTRFS_SCRUB_???");
Jeff Mahoney35866792016-05-18 18:09:42 -04001221 ioctl(-1, BTRFS_IOC_SCRUB, &args);
1222 printf("}) = -1 EBADF (%m)\n");
1223
1224 ioctl(-1, BTRFS_IOC_SCRUB_PROGRESS, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001225 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1226 ioc(BTRFS_IOC_SCRUB_PROGRESS));
Jeff Mahoney35866792016-05-18 18:09:42 -04001227
1228 ioctl(-1, BTRFS_IOC_SCRUB_PROGRESS, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001229 printf("ioctl(-1, %s, {devid=makedev(%u, %u)}) = -1 EBADF (%m)\n",
1230 ioc(BTRFS_IOC_SCRUB_PROGRESS),
1231 major(args.devid), minor(args.devid));
Jeff Mahoney35866792016-05-18 18:09:42 -04001232}
1233
1234/*
1235 * Consumes argument, returns argument:
1236 * - BTRFS_IOC_DEV_INFO
1237 */
1238static void
1239btrfs_test_dev_info_ioctl(void)
1240{
1241 struct btrfs_ioctl_dev_info_args args = {
1242 .devid = 1,
1243 };
1244 memcpy(&args.uuid, uuid_reference, BTRFS_UUID_SIZE);
1245
1246 ioctl(-1, BTRFS_IOC_DEV_INFO, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001247 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1248 ioc(BTRFS_IOC_DEV_INFO));
Jeff Mahoney35866792016-05-18 18:09:42 -04001249
1250 ioctl(-1, BTRFS_IOC_DEV_INFO, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001251 printf("ioctl(-1, %s, "
1252 "{devid=makedev(%u, %u), uuid=%s}) = -1 EBADF (%m)\n",
1253 ioc(BTRFS_IOC_DEV_INFO), major(args.devid), minor(args.devid),
1254 uuid_reference_string);
Jeff Mahoney35866792016-05-18 18:09:42 -04001255}
1256
1257/*
1258 * Consumes argument, returns argument:
1259 * - BTRFS_IOC_INO_PATHS
1260 * - BTRFS_IOC_LOGICAL_INO
1261 */
1262static void
1263btrfs_test_ino_path_ioctls(void)
1264{
1265 char buf[16384];
1266 struct btrfs_ioctl_ino_path_args args = {
1267 .inum = 256,
1268 .size = sizeof(buf),
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001269 .reserved = {
1270 0xdeadc0defacefeebULL,
1271 0xdeadc0defacefeecULL,
1272 0xdeadc0defacefeedULL,
1273 },
1274 .fspath = 0,
Jeff Mahoney35866792016-05-18 18:09:42 -04001275 };
1276
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001277#ifdef HAVE_BTRFS_IOCTL_LOGICAL_INO_ARGS
1278 args.flags =
1279#else
1280 args.reserved[3] =
1281#endif
1282 0xdeadc0defacefeeeULL;
1283
1284
Jeff Mahoney35866792016-05-18 18:09:42 -04001285 ioctl(-1, BTRFS_IOC_INO_PATHS, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001286 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1287 ioc(BTRFS_IOC_INO_PATHS));
Jeff Mahoney35866792016-05-18 18:09:42 -04001288
1289 ioctl(-1, BTRFS_IOC_LOGICAL_INO, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001290 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1291 ioc(BTRFS_IOC_LOGICAL_INO));
Jeff Mahoney35866792016-05-18 18:09:42 -04001292
1293 ioctl(-1, BTRFS_IOC_INO_PATHS, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001294 printf("ioctl(-1, %s, {inum=%" PRI__u64 ", size=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001295 ", fspath=NULL}) = -1 EBADF (%m)\n",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001296 ioc(BTRFS_IOC_INO_PATHS), args.inum, args.size);
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001297
1298 args.fspath = (uintptr_t) buf;
1299 ioctl(-1, BTRFS_IOC_INO_PATHS, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001300 printf("ioctl(-1, %s, {inum=%" PRI__u64 ", size=%" PRI__u64
Jeff Mahoney35866792016-05-18 18:09:42 -04001301 ", fspath=0x%" PRI__x64 "}) = -1 EBADF (%m)\n",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001302 ioc(BTRFS_IOC_INO_PATHS), args.inum, args.size, args.fspath);
Jeff Mahoney35866792016-05-18 18:09:42 -04001303
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001304 args.fspath = 0;
Jeff Mahoney35866792016-05-18 18:09:42 -04001305 ioctl(-1, BTRFS_IOC_LOGICAL_INO, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001306 printf("ioctl(-1, %s, {logical=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001307 ", size=%" PRI__u64 ", reserved=[0xdeadc0defacefeeb"
1308 ", 0xdeadc0defacefeec, 0xdeadc0defacefeed]"
1309 ", flags=0xdeadc0defacefeee /* BTRFS_LOGICAL_INO_ARGS_??? */"
1310 ", inodes=NULL}) = -1 EBADF (%m)\n",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001311 ioc(BTRFS_IOC_LOGICAL_INO), args.inum, args.size);
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001312
1313 args.fspath = (uintptr_t) buf;
1314 args.reserved[0] = 0;
1315 args.reserved[2] = 0;
1316#ifdef HAVE_BTRFS_IOCTL_LOGICAL_INO_ARGS
1317 args.flags =
1318#else
1319 args.reserved[3] =
1320#endif
1321 1;
1322
1323 ioctl(-1, BTRFS_IOC_LOGICAL_INO, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001324 printf("ioctl(-1, %s, {logical=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001325 ", size=%" PRI__u64 ", reserved=[0, 0xdeadc0defacefeec, 0]"
Elliott Hughes03a418e2018-06-15 13:11:40 -07001326 ", flags=%sBTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET%s"
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001327 ", inodes=0x%" PRI__x64 "}) = -1 EBADF (%m)\n",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001328 ioc(BTRFS_IOC_LOGICAL_INO), args.inum, args.size,
1329 verbose_xlat ? "0x1 /* " : "", verbose_xlat ? " */" : "",
1330 args.fspath);
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001331
1332 args.reserved[1] = 0;
1333#ifdef HAVE_BTRFS_IOCTL_LOGICAL_INO_ARGS
1334 args.flags =
1335#else
1336 args.reserved[3] =
1337#endif
1338 0;
1339
1340 ioctl(-1, BTRFS_IOC_LOGICAL_INO, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001341 printf("ioctl(-1, %s, {logical=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001342 ", size=%" PRI__u64 ", flags=0, inodes=0x%" PRI__x64
Elliott Hughes03a418e2018-06-15 13:11:40 -07001343 "}) = -1 EBADF (%m)\n",
1344 ioc(BTRFS_IOC_LOGICAL_INO), args.inum, args.size, args.fspath);
Jeff Mahoney35866792016-05-18 18:09:42 -04001345
Dmitry V. Levin22129182016-05-24 01:32:09 +00001346#ifdef HAVE_LINUX_FIEMAP_H
Jeff Mahoney35866792016-05-18 18:09:42 -04001347 if (btrfs_test_root) {
1348 int size;
1349 struct stat si;
1350 int ret;
1351 struct btrfs_data_container *data = (void *)buf;
1352 struct fiemap *fiemap;
1353 int fd;
1354
1355 ret = fstat(btrfs_test_dir_fd, &si);
1356 if (ret)
1357 perror_msg_and_fail("fstat failed");
1358
1359 args.inum = si.st_ino;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001360 printf("ioctl(%d, %s, {inum=%" PRI__u64 ", size=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001361 ", fspath=0x%" PRI__x64 "}",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001362 btrfs_test_dir_fd, ioc(BTRFS_IOC_INO_PATHS),
1363 args.inum, args.size, args.fspath);
Jeff Mahoney35866792016-05-18 18:09:42 -04001364 ioctl(btrfs_test_dir_fd, BTRFS_IOC_INO_PATHS, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001365 printf(" => {fspath={bytes_left=%u, bytes_missing=%u, elem_cnt=%u, elem_missed=%u, ",
Jeff Mahoney35866792016-05-18 18:09:42 -04001366 data->bytes_left, data->bytes_missing, data->elem_cnt,
1367 data->elem_missed);
1368 if (verbose) {
Elliott Hughes03a418e2018-06-15 13:11:40 -07001369 printf("val=[\"%s\"]", dir_name);
Jeff Mahoney35866792016-05-18 18:09:42 -04001370 } else
1371 printf("...");
1372 printf("}}) = 0\n");
1373
1374 fd = openat(btrfs_test_dir_fd, "file1", O_RDWR|O_CREAT, 0600);
1375 if (fd < 0)
1376 perror_msg_and_fail("openat(file1) failed");
1377
1378 ret = fstat(fd, &si);
1379 if (ret)
1380 perror_msg_and_fail("fstat failed");
1381
1382 if (write(fd, buf, sizeof(buf)) < 0)
1383 perror_msg_and_fail("write: fd");
1384
1385 /*
1386 * Force delalloc so we can actually
1387 * search for the extent.
1388 */
1389 fsync(fd);
1390 ioctl(fd, BTRFS_IOC_SYNC, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001391 printf("ioctl(%d, %s) = 0\n", fd, ioc(BTRFS_IOC_SYNC));
Jeff Mahoney35866792016-05-18 18:09:42 -04001392
1393 size = sizeof(*fiemap) + 2 * sizeof(fiemap->fm_extents[0]);
1394 fiemap = malloc(size);
1395 if (!fiemap)
1396 perror_msg_and_fail("malloc failed");
1397 memset(fiemap, 0, size);
1398
1399 fiemap->fm_length = sizeof(buf);
1400 fiemap->fm_extent_count = 2;
1401
1402 /* This is also a live test for FIEMAP */
Elliott Hughes03a418e2018-06-15 13:11:40 -07001403 printf("ioctl(%d, %s, {fm_start=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001404 ", fm_length=%" PRI__u64 ", fm_flags=",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001405 fd, ioc(FS_IOC_FIEMAP),
1406 fiemap->fm_start, fiemap->fm_length);
1407 prfl_btrfs(fiemap_flags, fiemap->fm_flags, "FIEMAP_FLAG_???");
Jeff Mahoney35866792016-05-18 18:09:42 -04001408 printf(", fm_extent_count=%u}", fiemap->fm_extent_count);
1409 ioctl(fd, FS_IOC_FIEMAP, fiemap);
1410 printf(" => {fm_flags=");
Elliott Hughes03a418e2018-06-15 13:11:40 -07001411 prfl_btrfs(fiemap_flags, fiemap->fm_flags, "FIEMAP_FLAG_???");
1412 printf(", fm_mapped_extents=%u, ",
Jeff Mahoney35866792016-05-18 18:09:42 -04001413 fiemap->fm_mapped_extents);
1414 if (verbose) {
Elliott Hughes03a418e2018-06-15 13:11:40 -07001415 printf("fm_extents=[");
Jeff Mahoney35866792016-05-18 18:09:42 -04001416 unsigned int i;
1417 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
1418 struct fiemap_extent *fe;
1419 fe = &fiemap->fm_extents[i];
1420 if (i)
1421 printf(", ");
1422 printf("{fe_logical=%" PRI__u64
1423 ", fe_physical=%" PRI__u64
1424 ", fe_length=%" PRI__u64
1425 ", ",
1426 fe->fe_logical, fe->fe_physical,
1427 fe->fe_length);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001428 prfl_btrfs(fiemap_extent_flags, fe->fe_flags,
Jeff Mahoney35866792016-05-18 18:09:42 -04001429 "FIEMAP_EXTENT_???");
1430 printf("}");
1431 }
1432 printf("]");
1433 } else
1434 printf("...");
1435 printf("}) = 0\n");
1436
1437 args.inum = fiemap->fm_extents[0].fe_physical;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001438 printf("ioctl(%d, %s, {logical=%" PRI__u64
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001439 ", size=%" PRI__u64 ", flags=0, inodes=0x%" PRI__x64 "}",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001440 fd, ioc(BTRFS_IOC_LOGICAL_INO),
1441 args.inum, args.size, args.fspath);
Jeff Mahoney35866792016-05-18 18:09:42 -04001442 ioctl(fd, BTRFS_IOC_LOGICAL_INO, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001443 printf(" => {inodes={bytes_left=%u, bytes_missing=%u, elem_cnt=%u, elem_missed=%u, ",
Jeff Mahoney35866792016-05-18 18:09:42 -04001444 data->bytes_left, data->bytes_missing, data->elem_cnt,
1445 data->elem_missed);
1446 if (verbose) {
Elliott Hughes03a418e2018-06-15 13:11:40 -07001447 printf("val=[{inum=%llu, offset=0, root=5}]",
Gleb Fotengauer-Malinovskiye79f0382016-05-24 18:09:46 +03001448 (unsigned long long) si.st_ino);
Jeff Mahoney35866792016-05-18 18:09:42 -04001449 } else
1450 printf("...");
1451 printf("}}) = 0\n");
1452 close(fd);
1453 free(fiemap);
1454 }
Dmitry V. Levin22129182016-05-24 01:32:09 +00001455#endif /* HAVE_LINUX_FIEMAP_H */
Jeff Mahoney35866792016-05-18 18:09:42 -04001456}
1457
1458/*
1459 * Consumes argument, returns argument:
1460 * - BTRFS_IOC_SET_RECEIVED_SUBVOL
1461 */
1462static void
1463btrfs_test_set_received_subvol_ioctl(void)
1464{
1465 struct btrfs_ioctl_received_subvol_args args = {
1466 .stransid = 0x12345,
1467 .stime = {
1468 .sec = 1463193386,
1469 .nsec = 12345,
1470 },
1471 };
Elliott Hughes03a418e2018-06-15 13:11:40 -07001472 int saved_errno;
1473
Jeff Mahoney35866792016-05-18 18:09:42 -04001474 memcpy(&args.uuid, uuid_reference, BTRFS_UUID_SIZE);
1475
1476 ioctl(-1, BTRFS_IOC_SET_RECEIVED_SUBVOL, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001477 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1478 ioc(BTRFS_IOC_SET_RECEIVED_SUBVOL));
Jeff Mahoney35866792016-05-18 18:09:42 -04001479
1480 ioctl(-1, BTRFS_IOC_SET_RECEIVED_SUBVOL, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001481 saved_errno = errno;
1482 printf("ioctl(-1, %s, {uuid=%s, stransid=%" PRI__u64
1483 ", stime={sec=%" PRI__u64 ", nsec=%u}",
1484 ioc(BTRFS_IOC_SET_RECEIVED_SUBVOL), uuid_reference_string,
1485 args.stransid, args.stime.sec, args.stime.nsec);
1486 print_time_t_nsec(args.stime.sec, args.stime.nsec, true);
1487 errno = saved_errno;
1488 printf(", flags=0}) = -1 EBADF (%m)\n");
Jeff Mahoney35866792016-05-18 18:09:42 -04001489}
1490
1491/*
1492 * Consumes argument, returns nothing (output is via send_fd)
1493 * - BTRFS_IOC_SEND
1494 */
1495static void
1496btrfs_test_send_ioctl(void)
1497{
1498 uint64_t u64_array[2] = { 256, 257 };
1499 struct btrfs_ioctl_send_args args = {
1500 .send_fd = 4,
1501 .parent_root = 257,
1502 .flags = max_flags_plus_one(2),
1503 };
1504
1505 ioctl(-1, BTRFS_IOC_SEND, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001506 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_SEND));
Jeff Mahoney35866792016-05-18 18:09:42 -04001507
Elliott Hughes03a418e2018-06-15 13:11:40 -07001508 printf("ioctl(-1, %s, {send_fd=%d, clone_sources_count=%" PRI__u64
1509 ", clone_sources=NULL",
1510 ioc(BTRFS_IOC_SEND),
Dmitry V. Levin6ce6d3b2016-05-24 01:53:02 +00001511 (int) args.send_fd, args.clone_sources_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001512 printf(", parent_root=");
1513 btrfs_print_objectid(args.parent_root);
1514 printf(", flags=");
Elliott Hughes03a418e2018-06-15 13:11:40 -07001515 prfl_btrfs(btrfs_send_flags, args.flags, "BTRFS_SEND_FLAGS_???");
Jeff Mahoney35866792016-05-18 18:09:42 -04001516 ioctl(-1, BTRFS_IOC_SEND, &args);
1517 printf("}) = -1 EBADF (%m)\n");
1518
1519 args.clone_sources_count = 2;
Dmitry V. Levinde5b0092016-07-26 15:59:28 +00001520 args.clone_sources = (__u64 *) (void *) u64_array;
Jeff Mahoney35866792016-05-18 18:09:42 -04001521
Elliott Hughes03a418e2018-06-15 13:11:40 -07001522 printf("ioctl(-1, %s, {send_fd=%d, clone_sources_count=%" PRI__u64
Dmitry V. Levin6ce6d3b2016-05-24 01:53:02 +00001523 ", clone_sources=",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001524 ioc(BTRFS_IOC_SEND),
Dmitry V. Levin6ce6d3b2016-05-24 01:53:02 +00001525 (int) args.send_fd, args.clone_sources_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001526 if (verbose) {
1527 printf("[");
1528 btrfs_print_objectid(u64_array[0]);
1529 printf(", ");
1530 btrfs_print_objectid(u64_array[1]);
1531 printf("]");
1532 } else
Elliott Hughes03a418e2018-06-15 13:11:40 -07001533 printf("%p", args.clone_sources);
Jeff Mahoney35866792016-05-18 18:09:42 -04001534 printf(", parent_root=");
1535 btrfs_print_objectid(args.parent_root);
1536 printf(", flags=");
Elliott Hughes03a418e2018-06-15 13:11:40 -07001537 prfl_btrfs(btrfs_send_flags, args.flags, "BTRFS_SEND_FLAGS_???");
Jeff Mahoney35866792016-05-18 18:09:42 -04001538 ioctl(-1, BTRFS_IOC_SEND, &args);
1539 printf("}) = -1 EBADF (%m)\n");
1540}
1541
1542/*
1543 * Consumes argument, returns nothing:
1544 * - BTRFS_IOC_QUOTA_CTL
1545 */
1546static void
1547btrfs_test_quota_ctl_ioctl(void)
1548{
1549 struct btrfs_ioctl_quota_ctl_args args = {
1550 .cmd = 1,
1551 };
1552
1553 ioctl(-1, BTRFS_IOC_QUOTA_CTL, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001554 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1555 ioc(BTRFS_IOC_QUOTA_CTL));
Jeff Mahoney35866792016-05-18 18:09:42 -04001556
1557 ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001558 printf("ioctl(-1, %s, {cmd=%sBTRFS_QUOTA_CTL_ENABLE%s}) "
1559 "= -1 EBADF (%m)\n",
1560 ioc(BTRFS_IOC_QUOTA_CTL),
1561 verbose_xlat ? "0x1 /* " : "",
1562 verbose_xlat ? " */" : "");
Jeff Mahoney35866792016-05-18 18:09:42 -04001563
1564 args.cmd = 2;
1565 ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001566 printf("ioctl(-1, %s, {cmd=%sBTRFS_QUOTA_CTL_DISABLE%s}) "
1567 "= -1 EBADF (%m)\n",
1568 ioc(BTRFS_IOC_QUOTA_CTL),
1569 verbose_xlat ? "0x2 /* " : "",
1570 verbose_xlat ? " */" : "");
Jeff Mahoney35866792016-05-18 18:09:42 -04001571
1572 args.cmd = 3;
1573 ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001574 printf("ioctl(-1, %s, "
1575 "{cmd=%sBTRFS_QUOTA_CTL_RESCAN__NOTUSED%s}) = -1 EBADF (%m)\n",
1576 ioc(BTRFS_IOC_QUOTA_CTL),
1577 verbose_xlat ? "0x3 /* " : "",
1578 verbose_xlat ? " */" : "");
Jeff Mahoney35866792016-05-18 18:09:42 -04001579
1580 args.cmd = 4;
1581 ioctl(-1, BTRFS_IOC_QUOTA_CTL, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001582 printf("ioctl(-1, %s, "
1583 "{cmd=0x4 /* BTRFS_QUOTA_CTL_??? */}) = -1 EBADF (%m)\n",
1584 ioc(BTRFS_IOC_QUOTA_CTL));
Jeff Mahoney35866792016-05-18 18:09:42 -04001585}
1586
1587/*
1588 * Consumes argument, returns nothing:
1589 * - BTRFS_IOC_QGROUP_ASSIGN
1590 */
1591static void
1592btrfs_test_qgroup_assign_ioctl(void)
1593{
1594 struct btrfs_ioctl_qgroup_assign_args args = {
1595 .assign = 1,
1596 .src = 257,
1597 .dst = 258,
1598 };
1599
1600 ioctl(-1, BTRFS_IOC_QGROUP_ASSIGN, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001601 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1602 ioc(BTRFS_IOC_QGROUP_ASSIGN));
Jeff Mahoney35866792016-05-18 18:09:42 -04001603
1604 ioctl(-1, BTRFS_IOC_QGROUP_ASSIGN, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001605 printf("ioctl(-1, %s, "
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001606 "{assign=%" PRI__u64 ", src=%" PRI__u64 ", dst=%" PRI__u64
Elliott Hughes03a418e2018-06-15 13:11:40 -07001607 "}) = -1 EBADF (%m)\n",
1608 ioc(BTRFS_IOC_QGROUP_ASSIGN), args.assign, args.src, args.dst);
Jeff Mahoney35866792016-05-18 18:09:42 -04001609}
1610
1611/*
1612 * Consumes argument, returns nothing:
1613 * - BTRFS_IOC_QGROUP_CREATE
1614 */
1615static void
1616btrfs_test_qgroup_create_ioctl(void)
1617{
1618 struct btrfs_ioctl_qgroup_create_args args = {
1619 .create = 1,
1620 .qgroupid = 257,
1621 };
1622
1623 ioctl(-1, BTRFS_IOC_QGROUP_CREATE, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001624 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_QGROUP_CREATE));
Jeff Mahoney35866792016-05-18 18:09:42 -04001625
1626 ioctl(-1, BTRFS_IOC_QGROUP_CREATE, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001627 printf("ioctl(-1, %s, "
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001628 "{create=%" PRI__u64 ", qgroupid=%" PRI__u64
Elliott Hughes03a418e2018-06-15 13:11:40 -07001629 "}) = -1 EBADF (%m)\n",
1630 ioc(BTRFS_IOC_QGROUP_CREATE), args.create, args.qgroupid);
Jeff Mahoney35866792016-05-18 18:09:42 -04001631}
1632
1633/*
1634 * Consumes nothing, returns nothing:
1635 * - BTRFS_IOC_QUOTA_RESCAN_WAIT
1636 * Consumes argument, returns nothing:
1637 * - BTRFS_IOC_QUOTA_RESCAN
1638 */
1639static void
1640btrfs_test_quota_rescan_ioctl(void)
1641{
1642 struct btrfs_ioctl_quota_rescan_args args = {
1643 .progress = 1,
1644 };
1645
1646 ioctl(-1, BTRFS_IOC_QUOTA_RESCAN, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001647 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1648 ioc(BTRFS_IOC_QUOTA_RESCAN));
Jeff Mahoney35866792016-05-18 18:09:42 -04001649
1650 ioctl(-1, BTRFS_IOC_QUOTA_RESCAN, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001651 printf("ioctl(-1, %s, {flags=0}) = -1 EBADF (%m)\n",
1652 ioc(BTRFS_IOC_QUOTA_RESCAN));
Jeff Mahoney35866792016-05-18 18:09:42 -04001653 ioctl(-1, BTRFS_IOC_QUOTA_RESCAN_WAIT, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001654 printf("ioctl(-1, %s) = -1 EBADF (%m)\n",
1655 ioc(BTRFS_IOC_QUOTA_RESCAN_WAIT));
Jeff Mahoney35866792016-05-18 18:09:42 -04001656
1657}
1658
1659/*
1660 * Consumes argument, returns nothing:
Elliott Hughesc1873762018-12-19 15:13:36 -08001661 * - FS_IOC_SETFSLABEL
Jeff Mahoney35866792016-05-18 18:09:42 -04001662 *
1663 * Consumes no argument, returns argument:
Elliott Hughesc1873762018-12-19 15:13:36 -08001664 * - FS_IOC_GETFSLABEL
Jeff Mahoney35866792016-05-18 18:09:42 -04001665 */
1666static void
1667btrfs_test_label_ioctls(void)
1668{
1669 char label[BTRFS_LABEL_SIZE] = "btrfs-label";
1670
Elliott Hughesc1873762018-12-19 15:13:36 -08001671 ioctl(-1, FS_IOC_SETFSLABEL, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001672 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
Elliott Hughesc1873762018-12-19 15:13:36 -08001673 ioc(FS_IOC_SETFSLABEL));
Jeff Mahoney35866792016-05-18 18:09:42 -04001674
Elliott Hughesc1873762018-12-19 15:13:36 -08001675 ioctl(-1, FS_IOC_SETFSLABEL, label);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001676 printf("ioctl(-1, %s, \"%s\") = -1 EBADF (%m)\n",
Elliott Hughesc1873762018-12-19 15:13:36 -08001677 ioc(FS_IOC_SETFSLABEL), label);
Jeff Mahoney35866792016-05-18 18:09:42 -04001678
1679 if (write_ok) {
Elliott Hughesc1873762018-12-19 15:13:36 -08001680 ioctl(btrfs_test_dir_fd, FS_IOC_SETFSLABEL, label);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001681 printf("ioctl(%d, %s, \"%s\") = 0\n",
Elliott Hughesc1873762018-12-19 15:13:36 -08001682 btrfs_test_dir_fd, ioc(FS_IOC_SETFSLABEL), label);
Jeff Mahoney35866792016-05-18 18:09:42 -04001683
Elliott Hughesc1873762018-12-19 15:13:36 -08001684 ioctl(btrfs_test_dir_fd, FS_IOC_GETFSLABEL, label);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001685 printf("ioctl(%d, %s, \"%s\") = 0\n",
Elliott Hughesc1873762018-12-19 15:13:36 -08001686 btrfs_test_dir_fd, ioc(FS_IOC_GETFSLABEL), label);
Jeff Mahoney35866792016-05-18 18:09:42 -04001687 }
1688}
1689
1690/*
1691 * Consumes argument, returns argument:
1692 * - BTRFS_IOC_GET_DEV_STATS
1693 */
1694static void
1695btrfs_test_get_dev_stats_ioctl(void)
1696{
1697 struct btrfs_ioctl_get_dev_stats args = {
1698 .devid = 1,
1699 .nr_items = 5,
1700 .flags = max_flags_plus_one(0),
1701 };
1702
1703 ioctl(-1, BTRFS_IOC_GET_DEV_STATS, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001704 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_GET_DEV_STATS));
Jeff Mahoney35866792016-05-18 18:09:42 -04001705
Elliott Hughes03a418e2018-06-15 13:11:40 -07001706 printf("ioctl(-1, %s, {devid=makedev(%u, %u)"
1707 ", nr_items=%" PRI__u64 ", flags=",
1708 ioc(BTRFS_IOC_GET_DEV_STATS),
1709 major(args.devid), minor(args.devid), args.nr_items);
1710 prfl_btrfs(btrfs_dev_stats_flags, args.flags,
Jeff Mahoney35866792016-05-18 18:09:42 -04001711 "BTRFS_DEV_STATS_???");
1712 ioctl(-1, BTRFS_IOC_GET_DEV_STATS, &args);
1713 printf("}) = -1 EBADF (%m)\n");
1714
1715 if (write_ok) {
1716 unsigned int i;
1717 args.flags = BTRFS_DEV_STATS_RESET;
Elliott Hughes03a418e2018-06-15 13:11:40 -07001718 printf("ioctl(%d, %s, {devid=makedev(%u, %u)"
Elliott Hughes28e98bc2018-06-14 16:59:04 -07001719 ", nr_items=%" PRI__u64 ", flags=",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001720 btrfs_test_dir_fd, ioc(BTRFS_IOC_GET_DEV_STATS),
1721 major(args.devid), minor(args.devid), args.nr_items);
1722 prfl_btrfs(btrfs_dev_stats_flags, args.flags,
Jeff Mahoney35866792016-05-18 18:09:42 -04001723 "BTRFS_DEV_STATS_???");
1724 ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_DEV_STATS, &args);
1725 printf("} => {nr_items=%" PRI__u64 ", flags=",
1726 args.nr_items);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001727 prfl_btrfs(btrfs_dev_stats_flags, args.flags,
Jeff Mahoney35866792016-05-18 18:09:42 -04001728 "BTRFS_DEV_STATS_???");
1729 printf(", [");
1730 for (i = 0; i < args.nr_items; i++) {
1731 const char *name = xlookup(btrfs_dev_stats_values, i);
1732 if (i)
1733 printf(", ");
Elliott Hughes03a418e2018-06-15 13:11:40 -07001734
1735 if (name) {
1736 if (verbose_xlat)
1737 printf("[%u /* %s */] = ", i, name);
1738 else
1739 printf("[%s] = ", name);
1740 } else {
1741 printf("[%u] = ", i);
1742 }
1743
Jeff Mahoney35866792016-05-18 18:09:42 -04001744 printf("%" PRI__u64, args.values[i]);
1745 }
1746 printf("]}) = 0\n");
1747 }
1748}
1749
1750/*
1751 * Consumes argument, returns argument:
1752 * - BTRFS_IOC_DEV_REPLACE
1753 *
1754 * Test environment for this is more difficult; It's better to do it by hand.
1755 */
1756static void
1757btrfs_test_dev_replace_ioctl(void)
1758{
1759 struct btrfs_ioctl_dev_replace_args args = {
1760 .cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_START,
Jeff Mahoney35866792016-05-18 18:09:42 -04001761 };
Elliott Hughesd35df492017-02-15 15:19:05 -08001762 args.start.srcdevid = 1;
Jeff Mahoney35866792016-05-18 18:09:42 -04001763 strcpy((char *)args.start.srcdev_name, "/dev/sda1");
1764 strcpy((char *)args.start.tgtdev_name, "/dev/sdb1");
1765
1766 /* struct btrfs_ioctl_dev_replace_args */
1767 ioctl(-1, BTRFS_IOC_DEV_REPLACE, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001768 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", ioc(BTRFS_IOC_DEV_REPLACE));
Jeff Mahoney35866792016-05-18 18:09:42 -04001769
Elliott Hughes03a418e2018-06-15 13:11:40 -07001770 for (unsigned long i = 0; i < 3; i++) {
1771 int saved_errno;
1772
1773 args.start.cont_reading_from_srcdev_mode = i;
1774 ioctl(-1, BTRFS_IOC_DEV_REPLACE, &args);
1775 saved_errno = errno;
1776 printf("ioctl(-1, %s, "
1777 "{cmd=%sBTRFS_IOCTL_DEV_REPLACE_CMD_START%s"
1778 ", start={srcdevid=makedev(%u, %u)"
1779 ", cont_reading_from_srcdev_mode=",
1780 ioc(BTRFS_IOC_DEV_REPLACE),
1781 verbose_xlat ? "0 /* " : "", verbose_xlat ? " */" : "",
1782 major(args.start.srcdevid), minor(args.start.srcdevid));
1783 prxval_btrfs(btrfs_cont_reading_from_srcdev_mode,
1784 args.start.cont_reading_from_srcdev_mode,
1785 "BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV"
1786 "_MODE_???", i < 2);
1787 errno = saved_errno;
1788 printf(", srcdev_name=\"%s\", tgtdev_name=\"%s\"}}) "
1789 "= -1 EBADF (%m)\n",
1790 (char *)args.start.srcdev_name,
1791 (char *)args.start.tgtdev_name);
1792 }
Jeff Mahoney35866792016-05-18 18:09:42 -04001793
1794 args.cmd = 1;
1795 ioctl(-1, BTRFS_IOC_DEV_REPLACE, &args);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001796 printf("ioctl(-1, %s, {cmd=%sBTRFS_IOCTL_DEV_REPLACE_CMD_STATUS%s}) "
1797 "= -1 EBADF (%m)\n",
1798 ioc(BTRFS_IOC_DEV_REPLACE),
1799 verbose_xlat ? "0x1 /* " : "", verbose_xlat ? " */" : "");
Jeff Mahoney35866792016-05-18 18:09:42 -04001800}
1801
1802static void
1803btrfs_test_extent_same_ioctl(void)
1804{
Dmitry V. Levinea516df2016-05-24 02:40:13 +00001805#ifdef BTRFS_IOC_FILE_EXTENT_SAME
Jeff Mahoney35866792016-05-18 18:09:42 -04001806 struct file_dedupe_range args = {
1807 .src_offset = 1024,
1808 .src_length = 10240,
1809 };
1810 struct file_dedupe_range *argsp;
1811
1812 ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001813 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1814 sprint_xlat_(BTRFS_IOC_FILE_EXTENT_SAME,
1815 "BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE"));
Jeff Mahoney35866792016-05-18 18:09:42 -04001816
Elliott Hughes03a418e2018-06-15 13:11:40 -07001817 printf("ioctl(-1, %s, "
Jeff Mahoney35866792016-05-18 18:09:42 -04001818 "{src_offset=%" PRIu64
1819 ", src_length=%" PRIu64
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001820 ", dest_count=%hu, info=[]",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001821 sprint_xlat_(BTRFS_IOC_FILE_EXTENT_SAME,
1822 "BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE"),
Jeff Mahoney35866792016-05-18 18:09:42 -04001823 (uint64_t)args.src_offset,
1824 (uint64_t)args.src_length, args.dest_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001825 ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, &args);
1826 printf("}) = -1 EBADF (%m)\n");
1827
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001828 argsp = malloc(sizeof(*argsp) + sizeof(argsp->info[0]) * 3);
Jeff Mahoney35866792016-05-18 18:09:42 -04001829 if (!argsp)
1830 perror_msg_and_fail("malloc failed");
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001831 memset(argsp, 0, sizeof(*argsp) + sizeof(argsp->info[0]) * 3);
Jeff Mahoney35866792016-05-18 18:09:42 -04001832
1833 *argsp = args;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001834 argsp->dest_count = 3;
Jeff Mahoney35866792016-05-18 18:09:42 -04001835 argsp->info[0].dest_fd = 2;
1836 argsp->info[0].dest_offset = 0;
1837 argsp->info[1].dest_fd = 2;
1838 argsp->info[1].dest_offset = 10240;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001839 argsp->info[2].dest_fd = 2;
1840 argsp->info[2].dest_offset = 20480;
Jeff Mahoney35866792016-05-18 18:09:42 -04001841
Elliott Hughes03a418e2018-06-15 13:11:40 -07001842 printf("ioctl(-1, %s, "
Jeff Mahoney35866792016-05-18 18:09:42 -04001843 "{src_offset=%" PRIu64
1844 ", src_length=%" PRIu64
1845 ", dest_count=%hu, info=",
Elliott Hughes03a418e2018-06-15 13:11:40 -07001846 sprint_xlat_(BTRFS_IOC_FILE_EXTENT_SAME,
1847 "BTRFS_IOC_FILE_EXTENT_SAME or FIDEDUPERANGE"),
Jeff Mahoney35866792016-05-18 18:09:42 -04001848 (int64_t)argsp->src_offset,
1849 (uint64_t)argsp->src_length, argsp->dest_count);
Jeff Mahoney35866792016-05-18 18:09:42 -04001850 printf("[{dest_fd=%" PRId64 ", dest_offset=%" PRIu64
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001851 "}, {dest_fd=%" PRId64 ", dest_offset=%"PRIu64 "}",
Jeff Mahoney35866792016-05-18 18:09:42 -04001852 (int64_t)argsp->info[0].dest_fd,
1853 (uint64_t)argsp->info[0].dest_offset,
1854 (int64_t)argsp->info[1].dest_fd,
1855 (uint64_t)argsp->info[1].dest_offset);
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001856 if (verbose)
1857 printf(", {dest_fd=%" PRId64 ", dest_offset=%" PRIu64 "}",
1858 (int64_t)argsp->info[2].dest_fd,
1859 (uint64_t)argsp->info[2].dest_offset);
Jeff Mahoney35866792016-05-18 18:09:42 -04001860 else
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001861 printf(", ...");
1862 printf("]");
Jeff Mahoney35866792016-05-18 18:09:42 -04001863 ioctl(-1, BTRFS_IOC_FILE_EXTENT_SAME, argsp);
1864 printf("}) = -1 EBADF (%m)\n");
1865
1866 if (write_ok) {
1867 int fd1, fd2;
1868 char buf[16384];
Jeff Mahoney35866792016-05-18 18:09:42 -04001869
1870 memset(buf, 0, sizeof(buf));
1871
1872 fd1 = openat(btrfs_test_dir_fd, "file1", O_RDWR|O_CREAT, 0600);
1873 if (fd1 < 0)
1874 perror_msg_and_fail("open file1 failed");
1875
1876 fd2 = openat(btrfs_test_dir_fd, "file2", O_RDWR|O_CREAT, 0600);
1877 if (fd2 < 0)
1878 perror_msg_and_fail("open file2 failed");
1879
1880 if (write(fd1, buf, sizeof(buf)) < 0)
1881 perror_msg_and_fail("write: fd1");
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001882 if (write(fd1, buf, sizeof(buf)) < 0)
1883 perror_msg_and_fail("write: fd1");
1884 if (write(fd2, buf, sizeof(buf)) < 0)
1885 perror_msg_and_fail("write: fd2");
Jeff Mahoney35866792016-05-18 18:09:42 -04001886 if (write(fd2, buf, sizeof(buf)) < 0)
1887 perror_msg_and_fail("write: fd2");
1888
1889 close(fd2);
1890 fd2 = openat(btrfs_test_dir_fd, "file2", O_RDONLY);
1891 if (fd2 < 0)
1892 perror_msg_and_fail("open file2 failed");
1893
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001894 memset(argsp, 0, sizeof(*argsp) + sizeof(argsp->info[0]) * 3);
Jeff Mahoney35866792016-05-18 18:09:42 -04001895
1896 argsp->src_offset = 0;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001897 argsp->src_length = 4096;
1898 argsp->dest_count = 3;
Jeff Mahoney35866792016-05-18 18:09:42 -04001899 argsp->info[0].dest_fd = fd2;
1900 argsp->info[0].dest_offset = 0;
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001901 argsp->info[1].dest_fd = fd2;
1902 argsp->info[1].dest_offset = 10240;
1903 argsp->info[2].dest_fd = fd2;
1904 argsp->info[2].dest_offset = 20480;
Jeff Mahoney35866792016-05-18 18:09:42 -04001905
Elliott Hughes03a418e2018-06-15 13:11:40 -07001906 printf("ioctl(%d, %s, "
Jeff Mahoney35866792016-05-18 18:09:42 -04001907 "{src_offset=%" PRIu64 ", src_length=%" PRIu64
1908 ", dest_count=%hu, info=", fd1,
Elliott Hughes03a418e2018-06-15 13:11:40 -07001909 sprint_xlat_(BTRFS_IOC_FILE_EXTENT_SAME,
1910 "BTRFS_IOC_FILE_EXTENT_SAME"
1911 " or FIDEDUPERANGE"),
Jeff Mahoney35866792016-05-18 18:09:42 -04001912 (uint64_t)argsp->src_offset,
1913 (uint64_t)argsp->src_length, argsp->dest_count);
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001914 printf("[{dest_fd=%" PRId64 ", dest_offset=%" PRIu64
1915 "}, {dest_fd=%" PRId64 ", dest_offset=%"PRIu64 "}",
1916 (int64_t)argsp->info[0].dest_fd,
1917 (uint64_t)argsp->info[0].dest_offset,
1918 (int64_t)argsp->info[1].dest_fd,
1919 (uint64_t)argsp->info[1].dest_offset);
Jeff Mahoney35866792016-05-18 18:09:42 -04001920 if (verbose)
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001921 printf(", {dest_fd=%" PRId64
1922 ", dest_offset=%" PRIu64 "}",
1923 (int64_t)argsp->info[2].dest_fd,
1924 (uint64_t)argsp->info[2].dest_offset);
Jeff Mahoney35866792016-05-18 18:09:42 -04001925 else
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001926 printf(", ...");
1927
Jeff Mahoney35866792016-05-18 18:09:42 -04001928 ioctl(fd1, BTRFS_IOC_FILE_EXTENT_SAME, argsp);
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001929 printf("]} => {info=");
1930 printf("[{bytes_deduped=%" PRIu64 ", status=%d}, "
1931 "{bytes_deduped=%" PRIu64 ", status=%d}",
1932 (uint64_t)argsp->info[0].bytes_deduped,
1933 argsp->info[0].status,
1934 (uint64_t)argsp->info[1].bytes_deduped,
1935 argsp->info[1].status);
Jeff Mahoney35866792016-05-18 18:09:42 -04001936 if (verbose)
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001937 printf(", {bytes_deduped=%" PRIu64 ", status=%d}",
1938 (uint64_t)argsp->info[2].bytes_deduped,
1939 argsp->info[2].status);
Jeff Mahoney35866792016-05-18 18:09:42 -04001940 else
Jeff Mahoneyeb36e382016-05-27 16:07:22 -04001941 printf(", ...");
1942 printf("]}) = 0\n");
Jeff Mahoney35866792016-05-18 18:09:42 -04001943 close(fd1);
1944 close(fd2);
1945 unlinkat(btrfs_test_dir_fd, "file1", 0);
1946 unlinkat(btrfs_test_dir_fd, "file2", 0);
1947 close(fd1);
1948 close(fd2);
1949 }
1950 free(argsp);
Dmitry V. Levinea516df2016-05-24 02:40:13 +00001951#endif /* BTRFS_IOC_FILE_EXTENT_SAME */
Jeff Mahoney35866792016-05-18 18:09:42 -04001952}
1953
1954static void
1955btrfs_print_features(struct btrfs_ioctl_feature_flags *flags)
1956{
1957 printf("{compat_flags=");
1958 printflags(btrfs_features_compat, flags->compat_flags,
1959 "BTRFS_FEATURE_COMPAT_???");
1960
1961 printf(", compat_ro_flags=");
Elliott Hughes03a418e2018-06-15 13:11:40 -07001962 prfl_btrfs(btrfs_features_compat_ro, flags->compat_ro_flags,
Jeff Mahoney35866792016-05-18 18:09:42 -04001963 "BTRFS_FEATURE_COMPAT_RO_???");
1964
1965 printf(", incompat_flags=");
Elliott Hughes03a418e2018-06-15 13:11:40 -07001966 prfl_btrfs(btrfs_features_incompat, flags->incompat_flags,
Jeff Mahoney35866792016-05-18 18:09:42 -04001967 "BTRFS_FEATURE_INCOMPAT_???");
1968 printf("}");
1969}
1970
1971/*
1972 * Consumes argument, returns nothing:
1973 * - BTRFS_IOC_SET_FEATURES
1974 *
1975 * Consumes nothing, returns argument:
1976 * - BTRFS_IOC_GET_FEATURES
1977 * - BTRFS_IOC_GET_SUPPORTED_FEATURES
1978 */
1979static void
1980btrfs_test_features_ioctls(void)
1981{
1982 struct btrfs_ioctl_feature_flags args[2] = {
1983 {
1984 .compat_flags = max_flags_plus_one(-1),
1985 .incompat_flags = max_flags_plus_one(9),
1986 .compat_ro_flags = max_flags_plus_one(0),
1987 }, {
1988 .compat_flags = max_flags_plus_one(-1),
1989 .incompat_flags = max_flags_plus_one(9),
1990 .compat_ro_flags = max_flags_plus_one(0),
1991 },
1992 };
1993 struct btrfs_ioctl_feature_flags supported_features[3];
1994
1995 ioctl(-1, BTRFS_IOC_SET_FEATURES, NULL);
Elliott Hughes03a418e2018-06-15 13:11:40 -07001996 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
1997 ioc(BTRFS_IOC_SET_FEATURES));
Jeff Mahoney35866792016-05-18 18:09:42 -04001998
Elliott Hughes03a418e2018-06-15 13:11:40 -07001999 printf("ioctl(-1, %s, [", ioc(BTRFS_IOC_SET_FEATURES));
Jeff Mahoney35866792016-05-18 18:09:42 -04002000 btrfs_print_features(&args[0]);
2001 printf(", ");
2002 btrfs_print_features(&args[1]);
2003 ioctl(-1, BTRFS_IOC_SET_FEATURES, &args);
2004 printf("]) = -1 EBADF (%m)\n");
2005
2006 if (btrfs_test_root) {
Elliott Hughes03a418e2018-06-15 13:11:40 -07002007 printf("ioctl(%d, %s, ",
2008 btrfs_test_dir_fd, ioc(BTRFS_IOC_GET_FEATURES));
Jeff Mahoney35866792016-05-18 18:09:42 -04002009 ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_FEATURES,
2010 &supported_features);
2011 btrfs_print_features(&supported_features[0]);
2012 printf(") = 0\n");
2013
2014 ioctl(btrfs_test_dir_fd, BTRFS_IOC_GET_SUPPORTED_FEATURES,
2015 &supported_features);
Elliott Hughes03a418e2018-06-15 13:11:40 -07002016 printf("ioctl(%d, %s, ",
2017 btrfs_test_dir_fd,
2018 ioc(BTRFS_IOC_GET_SUPPORTED_FEATURES));
Elliott Hughes39bac052017-05-25 16:56:11 -07002019 printf("[");
Jeff Mahoney35866792016-05-18 18:09:42 -04002020 btrfs_print_features(&supported_features[0]);
Elliott Hughes39bac052017-05-25 16:56:11 -07002021 printf(" /* supported */, ");
Jeff Mahoney35866792016-05-18 18:09:42 -04002022 btrfs_print_features(&supported_features[1]);
Elliott Hughes39bac052017-05-25 16:56:11 -07002023 printf(" /* safe to set */, ");
Jeff Mahoney35866792016-05-18 18:09:42 -04002024 btrfs_print_features(&supported_features[2]);
Elliott Hughes39bac052017-05-25 16:56:11 -07002025 printf(" /* safe to clear */]) = 0\n");
Jeff Mahoney35866792016-05-18 18:09:42 -04002026 }
2027}
2028
Dmitry V. Levinb356ad82016-05-26 14:26:15 +00002029static void
2030btrfs_test_read_ioctls(void)
2031{
2032 static const struct xlat btrfs_read_cmd[] = {
2033 XLAT(BTRFS_IOC_BALANCE_PROGRESS),
2034 XLAT(BTRFS_IOC_FS_INFO),
2035 XLAT(BTRFS_IOC_GET_FEATURES),
Elliott Hughesc1873762018-12-19 15:13:36 -08002036 XLAT(FS_IOC_GETFSLABEL),
Dmitry V. Levinb356ad82016-05-26 14:26:15 +00002037 XLAT(BTRFS_IOC_GET_SUPPORTED_FEATURES),
2038 XLAT(BTRFS_IOC_QGROUP_LIMIT),
2039 XLAT(BTRFS_IOC_QUOTA_RESCAN_STATUS),
2040 XLAT(BTRFS_IOC_START_SYNC),
2041 XLAT(BTRFS_IOC_SUBVOL_GETFLAGS),
2042 };
2043
2044 unsigned int i;
2045 for (i = 0; i < ARRAY_SIZE(btrfs_read_cmd); ++i) {
2046 ioctl(-1, (unsigned long) btrfs_read_cmd[i].val, 0);
Elliott Hughes03a418e2018-06-15 13:11:40 -07002047 printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n",
2048 sprint_xlat_(btrfs_read_cmd[i].val,
2049 btrfs_read_cmd[i].str));
Dmitry V. Levinb356ad82016-05-26 14:26:15 +00002050 }
2051}
2052
Elliott Hughes03a418e2018-06-15 13:11:40 -07002053static void
2054rm_test_dir(void)
2055{
2056 int rootfd = open(path, O_RDONLY|O_DIRECTORY);
2057
2058 unlinkat(rootfd, dir_name, AT_REMOVEDIR);
2059}
2060
Jeff Mahoney35866792016-05-18 18:09:42 -04002061int
2062main(int argc, char *argv[])
2063{
2064
2065 int opt;
2066 int ret;
Jeff Mahoney35866792016-05-18 18:09:42 -04002067
Elliott Hughes03a418e2018-06-15 13:11:40 -07002068 while ((opt = getopt(argc, argv, "wvX")) != -1) {
Jeff Mahoney35866792016-05-18 18:09:42 -04002069 switch (opt) {
2070 case 'v':
2071 /*
2072 * These tests are incomplete, especially when
2073 * printing arrays of objects are involved.
2074 */
2075 verbose = true;
2076 break;
2077 case 'w':
2078 write_ok = true;
2079 break;
Elliott Hughes03a418e2018-06-15 13:11:40 -07002080 case 'X':
2081 verbose_xlat = true;
2082 break;
Jeff Mahoney35866792016-05-18 18:09:42 -04002083 default:
Elliott Hughes03a418e2018-06-15 13:11:40 -07002084 error_msg_and_fail("usage: btrfs [-vwX] [path]");
Jeff Mahoney35866792016-05-18 18:09:42 -04002085 }
2086 }
2087
Elliott Hughes03a418e2018-06-15 13:11:40 -07002088 ret = snprintf(dir_name, sizeof(dir_name), dir_name_fmt, getpid());
2089 if (ret < 0)
2090 perror_msg_and_fail("snprintf(dir_name)");
2091
Jeff Mahoney35866792016-05-18 18:09:42 -04002092 /*
2093 * This will enable optional tests that require a valid file descriptor
2094 */
2095 if (optind < argc) {
2096 int rootfd;
2097 struct statfs sfi;
2098 path = argv[optind];
2099
2100 ret = statfs(path, &sfi);
2101 if (ret)
2102 perror_msg_and_fail("statfs(%s) failed", path);
2103
2104 if ((unsigned) sfi.f_type != BTRFS_SUPER_MAGIC)
Dmitry V. Levin9298c662016-05-24 01:33:08 +00002105 error_msg_and_fail("%s is not a btrfs file system",
2106 path);
Jeff Mahoney35866792016-05-18 18:09:42 -04002107
2108 btrfs_test_root = path;
2109 rootfd = open(path, O_RDONLY|O_DIRECTORY);
2110 if (rootfd < 0)
2111 perror_msg_and_fail("open(%s) failed", path);
2112
Elliott Hughes03a418e2018-06-15 13:11:40 -07002113 ret = mkdirat(rootfd, dir_name, 0755);
Jeff Mahoney35866792016-05-18 18:09:42 -04002114 if (ret < 0 && errno != EEXIST)
Elliott Hughes03a418e2018-06-15 13:11:40 -07002115 perror_msg_and_fail("mkdirat(%s) failed", dir_name);
Jeff Mahoney35866792016-05-18 18:09:42 -04002116
Elliott Hughes03a418e2018-06-15 13:11:40 -07002117 /* Register removal of the created directory. */
2118 if (ret == 0)
2119 atexit(rm_test_dir);
2120
2121 btrfs_test_dir_fd = openat(rootfd, dir_name,
Jeff Mahoney35866792016-05-18 18:09:42 -04002122 O_RDONLY|O_DIRECTORY);
2123 if (btrfs_test_dir_fd < 0)
Elliott Hughes03a418e2018-06-15 13:11:40 -07002124 perror_msg_and_fail("openat(%s) failed", dir_name);
Jeff Mahoney35866792016-05-18 18:09:42 -04002125 close(rootfd);
2126 } else
2127 write_ok = false;
2128
2129 if (btrfs_test_root) {
2130 fprintf(stderr, "Testing live ioctls on %s (%s)\n",
2131 btrfs_test_root, write_ok ? "read/write" : "read only");
2132 }
2133
Dmitry V. Levinb356ad82016-05-26 14:26:15 +00002134 btrfs_test_read_ioctls();
Jeff Mahoney35866792016-05-18 18:09:42 -04002135 btrfs_test_trans_ioctls();
2136 btrfs_test_sync_ioctls();
2137 btrfs_test_subvol_ioctls();
2138 btrfs_test_balance_ioctls();
2139 btrfs_test_device_ioctls();
2140 btrfs_test_clone_ioctls();
2141 btrfs_test_defrag_ioctls();
2142 btrfs_test_search_ioctls();
2143 btrfs_test_ino_lookup_ioctl();
2144 btrfs_test_space_info_ioctl();
2145 btrfs_test_scrub_ioctls();
2146 btrfs_test_dev_info_ioctl();
2147 btrfs_test_ino_path_ioctls();
2148 btrfs_test_set_received_subvol_ioctl();
2149 btrfs_test_send_ioctl();
2150 btrfs_test_quota_ctl_ioctl();
2151 btrfs_test_qgroup_assign_ioctl();
2152 btrfs_test_qgroup_create_ioctl();
2153 btrfs_test_quota_rescan_ioctl();
2154 btrfs_test_label_ioctls();
2155 btrfs_test_get_dev_stats_ioctl();
2156 btrfs_test_dev_replace_ioctl();
2157 btrfs_test_extent_same_ioctl();
2158 btrfs_test_features_ioctls();
2159
2160 puts("+++ exited with 0 +++");
2161
2162 return 0;
2163}
Dmitry V. Levin22129182016-05-24 01:32:09 +00002164
2165#else
2166
2167SKIP_MAIN_UNDEFINED("HAVE_LINUX_BTRFS_H")
2168
2169#endif