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