1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2006 4f08c3bdfSopenharmony_ci * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> 5f08c3bdfSopenharmony_ci * Author: Yi Yang <yyangcdl@cn.ibm.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci#define _GNU_SOURCE 9f08c3bdfSopenharmony_ci 10f08c3bdfSopenharmony_ci#include <errno.h> 11f08c3bdfSopenharmony_ci#include <string.h> 12f08c3bdfSopenharmony_ci#include <signal.h> 13f08c3bdfSopenharmony_ci#include <sys/types.h> 14f08c3bdfSopenharmony_ci#include <sys/poll.h> 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include "tst_test.h" 17f08c3bdfSopenharmony_ci#include "lapi/fcntl.h" 18f08c3bdfSopenharmony_ci#include "lapi/splice.h" 19f08c3bdfSopenharmony_ci#include "lapi/vmsplice.h" 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#define TEST_BLOCK_SIZE (1<<17) /* 128K */ 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_ci#define TESTFILE "vmsplice_test_file" 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic int fd_out; 26f08c3bdfSopenharmony_cistatic char buffer[TEST_BLOCK_SIZE]; 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic void check_file(void) 29f08c3bdfSopenharmony_ci{ 30f08c3bdfSopenharmony_ci int i; 31f08c3bdfSopenharmony_ci char vmsplicebuffer[TEST_BLOCK_SIZE]; 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci fd_out = SAFE_OPEN(TESTFILE, O_RDONLY); 34f08c3bdfSopenharmony_ci SAFE_READ(1, fd_out, vmsplicebuffer, TEST_BLOCK_SIZE); 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci for (i = 0; i < TEST_BLOCK_SIZE; i++) { 37f08c3bdfSopenharmony_ci if (buffer[i] != vmsplicebuffer[i]) 38f08c3bdfSopenharmony_ci break; 39f08c3bdfSopenharmony_ci } 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci if (i < TEST_BLOCK_SIZE) 42f08c3bdfSopenharmony_ci tst_res(TFAIL, "Wrong data read from the buffer at %i", i); 43f08c3bdfSopenharmony_ci else 44f08c3bdfSopenharmony_ci tst_res(TPASS, "Written data has been read back correctly"); 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_out); 47f08c3bdfSopenharmony_ci} 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic void vmsplice_test(void) 50f08c3bdfSopenharmony_ci{ 51f08c3bdfSopenharmony_ci int pipes[2]; 52f08c3bdfSopenharmony_ci long written; 53f08c3bdfSopenharmony_ci int ret; 54f08c3bdfSopenharmony_ci int fd_out; 55f08c3bdfSopenharmony_ci struct iovec v; 56f08c3bdfSopenharmony_ci loff_t offset; 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci v.iov_base = buffer; 59f08c3bdfSopenharmony_ci v.iov_len = TEST_BLOCK_SIZE; 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci fd_out = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT | O_TRUNC, 0644); 62f08c3bdfSopenharmony_ci SAFE_PIPE(pipes); 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci struct pollfd pfd = {.fd = pipes[1], .events = POLLOUT}; 65f08c3bdfSopenharmony_ci offset = 0; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci while (v.iov_len) { 68f08c3bdfSopenharmony_ci /* 69f08c3bdfSopenharmony_ci * in a real app you'd be more clever with poll of course, 70f08c3bdfSopenharmony_ci * here we are basically just blocking on output room and 71f08c3bdfSopenharmony_ci * not using the free time for anything interesting. 72f08c3bdfSopenharmony_ci */ 73f08c3bdfSopenharmony_ci if (poll(&pfd, 1, -1) < 0) 74f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "poll() failed"); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci written = vmsplice(pipes[1], &v, 1, 0); 77f08c3bdfSopenharmony_ci if (written < 0) { 78f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "vmsplice() failed"); 79f08c3bdfSopenharmony_ci } else { 80f08c3bdfSopenharmony_ci if (written == 0) { 81f08c3bdfSopenharmony_ci break; 82f08c3bdfSopenharmony_ci } else { 83f08c3bdfSopenharmony_ci v.iov_base += written; 84f08c3bdfSopenharmony_ci v.iov_len -= written; 85f08c3bdfSopenharmony_ci } 86f08c3bdfSopenharmony_ci } 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ci ret = splice(pipes[0], NULL, fd_out, &offset, written, 0); 89f08c3bdfSopenharmony_ci if (ret < 0) 90f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "splice() failed"); 91f08c3bdfSopenharmony_ci //printf("offset = %lld\n", (long long)offset); 92f08c3bdfSopenharmony_ci } 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci SAFE_CLOSE(pipes[0]); 95f08c3bdfSopenharmony_ci SAFE_CLOSE(pipes[1]); 96f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_out); 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci check_file(); 99f08c3bdfSopenharmony_ci} 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_cistatic void setup(void) 102f08c3bdfSopenharmony_ci{ 103f08c3bdfSopenharmony_ci int i; 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci for (i = 0; i < TEST_BLOCK_SIZE; i++) 106f08c3bdfSopenharmony_ci buffer[i] = i & 0xff; 107f08c3bdfSopenharmony_ci} 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_cistatic void cleanup(void) 110f08c3bdfSopenharmony_ci{ 111f08c3bdfSopenharmony_ci if (fd_out > 0) 112f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_out); 113f08c3bdfSopenharmony_ci} 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_cistatic struct tst_test test = { 116f08c3bdfSopenharmony_ci .setup = setup, 117f08c3bdfSopenharmony_ci .cleanup = cleanup, 118f08c3bdfSopenharmony_ci .test_all = vmsplice_test, 119f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 120f08c3bdfSopenharmony_ci .skip_filesystems = (const char *const []) { 121f08c3bdfSopenharmony_ci "nfs", 122f08c3bdfSopenharmony_ci NULL 123f08c3bdfSopenharmony_ci }, 124f08c3bdfSopenharmony_ci}; 125