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