1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Jens Axboe <axboe@kernel.dk>, 2009
4 * Copyright (c) 2021 Petr Vorel <pvorel@suse.cz>
5 */
6
7 /*\
8 * [Description]
9 * Original reproducer for kernel fix
10 * bf40d3435caf NFS: add support for splice writes
11 * from v2.6.31-rc1.
12 *
13 * http://lkml.org/lkml/2009/4/2/55
14 *
15 * [ALGORITHM]
16 * - create pipe
17 * - fork(), child replace stdin with pipe
18 * - parent write to pipe
19 * - child slice from pipe
20 * - check resulted file size and content
21 */
22
23 #define _GNU_SOURCE
24
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include "tst_test.h"
32 #include "lapi/mmap.h"
33 #include "lapi/splice.h"
34
35 #define BUFSIZE 512
36 #define SPLICE_SIZE 1024
37
38 #define TEST_FILENAME "splice02-temp"
39
40 static char *sarg;
41 static int file_size;
42 static int pipe_fd[2];
43
setup(void)44 static void setup(void)
45 {
46 if (tst_parse_int(sarg, &file_size, 1, INT_MAX))
47 tst_brk(TBROK, "invalid number of writes '%s', use <1,%d>", sarg, INT_MAX);
48 }
49
get_letter(int n)50 static inline int get_letter(int n)
51 {
52 return n % ('z' - 'a' + 1) + 'a';
53 }
54
do_child(void)55 static void do_child(void)
56 {
57 int fd;
58 size_t page_size, to_check, block, blocks, i, fail = 0;
59 struct stat st;
60 char *map;
61
62 SAFE_CLOSE(pipe_fd[1]);
63 SAFE_DUP2(pipe_fd[0], STDIN_FILENO);
64
65 fd = SAFE_OPEN(TEST_FILENAME, O_WRONLY | O_CREAT | O_TRUNC, 0644);
66
67 do {
68 TEST(splice(STDIN_FILENO, NULL, fd, NULL, SPLICE_SIZE, 0));
69 if (TST_RET < 0) {
70 tst_res(TFAIL | TTERRNO, "splice failed");
71 goto cleanup;
72 }
73 } while (TST_RET > 0);
74
75 stat(TEST_FILENAME, &st);
76 if (st.st_size != file_size) {
77 tst_res(TFAIL, "file size is different from expected: %ld (%d)",
78 st.st_size, file_size);
79 goto cleanup;
80 }
81
82 SAFE_CLOSE(fd);
83 fd = SAFE_OPEN(TEST_FILENAME, O_RDONLY);
84
85 page_size = sysconf(_SC_PAGESIZE);
86
87 tst_res(TINFO, "checking file content");
88
89 blocks = LTP_ALIGN(st.st_size, page_size) / page_size;
90
91 for (block = 0; block < blocks; block++) {
92 map = SAFE_MMAP(NULL, page_size, PROT_READ, MAP_PRIVATE,
93 fd,block * page_size);
94
95 to_check = (block+1) * page_size < (unsigned long)st.st_size ?
96 page_size : st.st_size % page_size;
97
98 for (i = 0; i < to_check; i++) {
99 if (map[i] != get_letter(block * page_size + i))
100 fail++;
101 }
102
103 SAFE_MUNMAP(map, page_size);
104 }
105
106 if (fail) {
107 tst_res(TFAIL, "%ld unexpected bytes found", fail);
108 goto cleanup;
109 }
110
111 tst_res(TPASS, "splice() system call passed");
112
113 cleanup:
114 SAFE_CLOSE(fd);
115 exit(0);
116 }
117
run(void)118 static void run(void)
119 {
120 size_t i, size, written, max_pipe_size, to_write;
121 char buf[BUFSIZE];
122
123 SAFE_PIPE(pipe_fd);
124
125 if (!file_size) {
126 max_pipe_size = SAFE_FCNTL(pipe_fd[1], F_GETPIPE_SZ);
127 file_size = max_pipe_size << 4;
128 }
129
130 to_write = file_size;
131
132 if (!SAFE_FORK())
133 do_child();
134
135 tst_res(TINFO, "writting %d bytes", file_size);
136
137 while (to_write > 0) {
138 size = to_write > BUFSIZE ? BUFSIZE : to_write;
139
140 for (i = 0; i < size; i++)
141 buf[i] = get_letter(file_size - to_write + i);
142
143 written = SAFE_WRITE(SAFE_WRITE_ALL, pipe_fd[1], &buf, size);
144 to_write -= written;
145 }
146
147 SAFE_CLOSE(pipe_fd[0]);
148 SAFE_CLOSE(pipe_fd[1]);
149
150 tst_reap_children();
151 }
152
153 static struct tst_test test = {
154 .test_all = run,
155 .setup = setup,
156 .needs_tmpdir = 1,
157 .forks_child = 1,
158 .options = (struct tst_option[]) {
159 {"s:", &sarg, "Size of output file in bytes (default: 16x max pipe size, i.e. 1M on intel)"},
160 {}
161 },
162 };
163