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