1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 SUSE LLC
4 * Author: Christian Amann <[email protected]>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Tests the ioctl functionality to deduplicate fileranges using
11 * btrfs filesystem.
12 *
13 * 1. Sets the same contents for two files and deduplicates it.
14 * Deduplicates 3 bytes and set the status to
15 * FILE_DEDUPE_RANGE_SAME.
16 * 2. Sets different content for two files and tries to
17 * deduplicate it. 0 bytes get deduplicated and status is
18 * set to FILE_DEDUPE_RANGE_DIFFERS.
19 * 3. Sets same content for two files but sets the length to
20 * deduplicate to -1. ioctl(FIDEDUPERANGE) fails with EINVAL.
21 */
22
23 #include "config.h"
24 #include <stdlib.h>
25 #include <sys/ioctl.h>
26 #include <errno.h>
27 #include "tst_test.h"
28
29 #ifdef HAVE_STRUCT_FILE_DEDUPE_RANGE
30 #include <linux/fs.h>
31
32 #define SUCCESS 0
33
34 #define MNTPOINT "mnt_point"
35 #define FILE_SRC_PATH MNTPOINT"/file_src"
36 #define FILE_DEST_PATH MNTPOINT"/file_dest"
37
38 static int fd_src;
39 static int fd_dest;
40 static struct file_dedupe_range *fdr;
41
42 static struct tcase {
43 uint64_t src_length;
44 char *src_fcontents;
45 char *dest_fcontents;
46 int exp_err;
47 uint64_t bytes_deduped;
48 int status;
49 } tcases[] = {
50 {3, "AAA", "AAA", SUCCESS, 3, FILE_DEDUPE_RANGE_SAME},
51 {3, "AAA", "AAB", SUCCESS, 0, FILE_DEDUPE_RANGE_DIFFERS},
52 {-1, "AAA", "AAA", EINVAL, 0, 0},
53 };
54
verify_ioctl(unsigned int n)55 static void verify_ioctl(unsigned int n)
56 {
57 struct tcase *tc = &tcases[n];
58
59 fd_src = SAFE_OPEN(FILE_SRC_PATH, O_RDWR | O_CREAT, 0664);
60 fd_dest = SAFE_OPEN(FILE_DEST_PATH, O_RDWR | O_CREAT, 0664);
61
62 SAFE_WRITE(SAFE_WRITE_ALL, fd_src, tc->src_fcontents, strlen(tc->src_fcontents));
63 SAFE_WRITE(SAFE_WRITE_ALL, fd_dest, tc->dest_fcontents, strlen(tc->dest_fcontents));
64
65 memset(fdr, 0, sizeof(struct file_dedupe_range) +
66 sizeof(struct file_dedupe_range_info));
67
68 fdr->src_length = tc->src_length;
69 fdr->dest_count = 1;
70 fdr->info[0].dest_fd = fd_dest;
71
72 TEST(ioctl(fd_src, FIDEDUPERANGE, fdr));
73
74 if (tc->exp_err != TST_ERR) {
75 tst_res(TFAIL,
76 "ioctl(FIDEDUPERANGE) ended with %s, expected %s",
77 tst_strerrno(TST_ERR), tst_strerrno(tc->exp_err));
78 return;
79 }
80
81 if (fdr->info[0].bytes_deduped != tc->bytes_deduped) {
82 tst_res(TFAIL,
83 "ioctl(FIDEDUPERANGE) deduplicated %lld bytes, expected %ld. Status: %d",
84 fdr->info[0].bytes_deduped, tc->bytes_deduped,
85 fdr->info[0].status);
86 return;
87 }
88
89 if (fdr->info[0].status != tc->status) {
90 tst_res(TFAIL,
91 "ioctl(FIDEDUPERANGE) status set to %d, expected %d",
92 fdr->info[0].status, tc->status);
93 return;
94 }
95
96 tst_res(TPASS, "ioctl(FIDEDUPERANGE) ended with %s as expected",
97 tst_strerrno(tc->exp_err));
98
99 SAFE_CLOSE(fd_dest);
100 SAFE_CLOSE(fd_src);
101 }
102
cleanup(void)103 static void cleanup(void)
104 {
105 if (fd_dest > 0)
106 SAFE_CLOSE(fd_dest);
107 if (fd_src > 0)
108 SAFE_CLOSE(fd_src);
109 if (fdr)
110 free(fdr);
111 }
112
setup(void)113 static void setup(void)
114 {
115 fdr = SAFE_MALLOC(sizeof(struct file_dedupe_range) +
116 sizeof(struct file_dedupe_range_info));
117 }
118
119
120 static struct tst_test test = {
121 .test = verify_ioctl,
122 .tcnt = ARRAY_SIZE(tcases),
123 .setup = setup,
124 .cleanup = cleanup,
125 .min_kver = "4.5",
126 .needs_root = 1,
127 .mount_device = 1,
128 .mntpoint = MNTPOINT,
129 .dev_fs_type = "btrfs",
130 .needs_drivers = (const char *const[]) {
131 "btrfs",
132 NULL,
133 },
134 };
135 #else
136 TST_TEST_TCONF(
137 "This system does not provide support for ioctl(FIDEDUPERANGE)");
138 #endif
139