xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sendfile/sendfile09.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) International Business Machines  Corp., 2014
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2013-2023
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  * Testcase copied from sendfile02.c to test the basic functionality of
11*49cdfc7eSAndroid Build Coastguard Worker  * the sendfile() system call on large file. There is a kernel bug which
12*49cdfc7eSAndroid Build Coastguard Worker  * introduced by commit 8f9c0119d7ba9 and fixed by commit 5d73320a96fcc.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * Only supports 64bit systems.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * [Algorithm]
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * 1. Call sendfile() with offset at 0.
19*49cdfc7eSAndroid Build Coastguard Worker  * 2. Call sendfile() with offset at 3GB.
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/sendfile.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #define ONE_GB		(INT64_C(1) << 30)
27*49cdfc7eSAndroid Build Coastguard Worker #define IN_FILE		"in_file"
28*49cdfc7eSAndroid Build Coastguard Worker #define OUT_FILE	"out_file"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
31*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
32*49cdfc7eSAndroid Build Coastguard Worker 	off_t offset;
33*49cdfc7eSAndroid Build Coastguard Worker 	int64_t count;
34*49cdfc7eSAndroid Build Coastguard Worker 	int64_t exp_retval;
35*49cdfc7eSAndroid Build Coastguard Worker 	int64_t exp_updated_offset;
36*49cdfc7eSAndroid Build Coastguard Worker } tc[] = {
37*49cdfc7eSAndroid Build Coastguard Worker 	{ "offset at 0", 0, ONE_GB, ONE_GB, ONE_GB },
38*49cdfc7eSAndroid Build Coastguard Worker 	{ "offset at 3GB", 3 * ONE_GB, ONE_GB, ONE_GB, 4 * ONE_GB }
39*49cdfc7eSAndroid Build Coastguard Worker };
40*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)41*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	int i, fd;
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	if (!tst_fs_has_free(".", 5, TST_GB))
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Test on large file needs 5G free space");
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(IN_FILE, 00700);
49*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 1; i <= (4 * 1024); ++i) {
50*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_LSEEK(fd, 1024 * 1024 - 1, SEEK_CUR);
51*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WRITE(SAFE_WRITE_ALL, fd, "C", 1);
52*49cdfc7eSAndroid Build Coastguard Worker 	}
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(OUT_FILE, 00700);
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)59*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	int in_fd = SAFE_OPEN(IN_FILE, O_RDONLY);
62*49cdfc7eSAndroid Build Coastguard Worker 	int out_fd = SAFE_OPEN(OUT_FILE, O_WRONLY);
63*49cdfc7eSAndroid Build Coastguard Worker 	off_t offset = tc[i].offset;
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	off_t before_pos, after_pos;
66*49cdfc7eSAndroid Build Coastguard Worker 	before_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	TEST(sendfile(out_fd, in_fd, &offset, tc[i].count));
69*49cdfc7eSAndroid Build Coastguard Worker 	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != tc[i].exp_retval)
72*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sendfile() failed to return expected value, "
73*49cdfc7eSAndroid Build Coastguard Worker 			       "expected: %" PRId64 ", got: %ld",
74*49cdfc7eSAndroid Build Coastguard Worker 			tc[i].exp_retval, TST_RET);
75*49cdfc7eSAndroid Build Coastguard Worker 	else if (offset != tc[i].exp_updated_offset)
76*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sendfile() failed to update OFFSET parameter to "
77*49cdfc7eSAndroid Build Coastguard Worker 			       "expected value, expected: %" PRId64 ", got: %" PRId64,
78*49cdfc7eSAndroid Build Coastguard Worker 			tc[i].exp_updated_offset, (int64_t)(offset));
79*49cdfc7eSAndroid Build Coastguard Worker 	else if (before_pos != after_pos)
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sendfile() updated the file position of in_fd "
81*49cdfc7eSAndroid Build Coastguard Worker 			       "unexpectedly, expected file position: %" PRId64
82*49cdfc7eSAndroid Build Coastguard Worker 			       ", actual file position %" PRId64,
83*49cdfc7eSAndroid Build Coastguard Worker 			(int64_t)(before_pos), (int64_t)(after_pos));
84*49cdfc7eSAndroid Build Coastguard Worker 	else
85*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "sendfile() with %s", tc[i].desc);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(in_fd);
88*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(out_fd);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
92*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
93*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
94*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
95*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tc),
96*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = 120,
97*49cdfc7eSAndroid Build Coastguard Worker 	.skip_in_compat = 1,
98*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
99*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "5d73320a96fcc"},
100*49cdfc7eSAndroid Build Coastguard Worker 		{}
101*49cdfc7eSAndroid Build Coastguard Worker 	}
102*49cdfc7eSAndroid Build Coastguard Worker };
103