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) 2024 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Test for splicing from /dev/zero and /dev/full.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * The support for splicing from /dev/zero and /dev/full was removed in:
12*49cdfc7eSAndroid Build Coastguard Worker * c6585011bc1d ("splice: Remove generic_file_splice_read()")
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * And added back in:
15*49cdfc7eSAndroid Build Coastguard Worker * 1b057bd800c3 ("drivers/char/mem: implement splice() for /dev/zero, /dev/full")
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker static int fd_zero;
22*49cdfc7eSAndroid Build Coastguard Worker static int fd_full;
23*49cdfc7eSAndroid Build Coastguard Worker
test_splice(unsigned int bytes,int dev_fd)24*49cdfc7eSAndroid Build Coastguard Worker static void test_splice(unsigned int bytes, int dev_fd)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker int pipefd[2];
27*49cdfc7eSAndroid Build Coastguard Worker char buf[bytes];
28*49cdfc7eSAndroid Build Coastguard Worker size_t i;
29*49cdfc7eSAndroid Build Coastguard Worker int fail = 0;
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker memset(buf, 0xff, sizeof(buf));
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(pipefd);
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_POSITIVE(splice(dev_fd, NULL, pipefd[1], NULL, sizeof(buf), 0));
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker if (!TST_PASS)
38*49cdfc7eSAndroid Build Coastguard Worker goto ret;
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker if ((size_t)TST_RET != sizeof(buf)) {
41*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Only part %lu bytes of %u were spliced",
42*49cdfc7eSAndroid Build Coastguard Worker TST_RET, bytes);
43*49cdfc7eSAndroid Build Coastguard Worker goto ret;
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker SAFE_READ(1, pipefd[0], buf, sizeof(buf));
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < sizeof(buf); i++) {
49*49cdfc7eSAndroid Build Coastguard Worker if (buf[i])
50*49cdfc7eSAndroid Build Coastguard Worker fail++;
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker if (fail)
54*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%i non-zero bytes spliced from /dev/zero", fail);
55*49cdfc7eSAndroid Build Coastguard Worker else
56*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "All bytes spliced from /dev/zero are zeroed");
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker ret:
59*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pipefd[0]);
60*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pipefd[1]);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
verify_splice(unsigned int n)63*49cdfc7eSAndroid Build Coastguard Worker static void verify_splice(unsigned int n)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker unsigned int bytes = 1009 * n;
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Splicing %u bytes from /dev/zero", bytes);
68*49cdfc7eSAndroid Build Coastguard Worker test_splice(bytes, fd_zero);
69*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Splicing %u bytes from /dev/full", bytes);
70*49cdfc7eSAndroid Build Coastguard Worker test_splice(bytes, fd_full);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker
setup(void)73*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker fd_zero = SAFE_OPEN("/dev/zero", O_RDONLY);
76*49cdfc7eSAndroid Build Coastguard Worker fd_full = SAFE_OPEN("/dev/full", O_RDONLY);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)79*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker if (fd_zero > 0)
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_zero);
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker if (fd_full > 0)
85*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd_full);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
89*49cdfc7eSAndroid Build Coastguard Worker .test = verify_splice,
90*49cdfc7eSAndroid Build Coastguard Worker .tcnt = 9,
91*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
92*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
93*49cdfc7eSAndroid Build Coastguard Worker .min_kver = "6.7",
94*49cdfc7eSAndroid Build Coastguard Worker };
95