1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2004 Daniel McNeil <daniel@osdl.org>
4 * 2004 Open Source Development Lab
5 *
6 * Copyright (c) 2004 Marty Ridgeway <mridge@us.ibm.com>
7 *
8 * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
9 *
10 * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
11 */
12
13 /*\
14 * [Description]
15 *
16 * Create a sparse file using O_DIRECT while other processes are doing
17 * buffered reads and check if the buffer reads always see zero.
18 */
19
20 #define _GNU_SOURCE
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include "tst_test.h"
27 #include "common.h"
28
29 static int *run_child;
30
31 static char *str_numchildren;
32 static char *str_writesize;
33 static char *str_filesize;
34 static char *str_offset;
35
36 static int numchildren = 16;
37 static long long writesize = 1024;
38 static long long filesize = 100 * 1024 * 1024;
39 static long long offset = 0;
40 static long long alignment;
41
dio_sparse(int fd,int align,long long fs,int ws,long long off)42 static void dio_sparse(int fd, int align, long long fs, int ws, long long off)
43 {
44 void *bufptr = NULL;
45 long long i;
46 int w;
47
48 bufptr = SAFE_MEMALIGN(align, ws);
49
50 memset(bufptr, 0, ws);
51 SAFE_LSEEK(fd, off, SEEK_SET);
52
53 for (i = off; i < fs;) {
54 w = SAFE_WRITE(0, fd, bufptr, ws);
55 i += w;
56 }
57 }
58
setup(void)59 static void setup(void)
60 {
61 struct stat sb;
62
63 if (tst_parse_int(str_numchildren, &numchildren, 1, INT_MAX))
64 tst_brk(TBROK, "Invalid number of children '%s'", str_numchildren);
65
66 if (tst_parse_filesize(str_writesize, &writesize, 1, LLONG_MAX))
67 tst_brk(TBROK, "Invalid write blocks size '%s'", str_writesize);
68
69 if (tst_parse_filesize(str_filesize, &filesize, 1, LLONG_MAX))
70 tst_brk(TBROK, "Invalid file size '%s'", str_filesize);
71
72 if (tst_parse_filesize(str_offset, &offset, 0, LLONG_MAX))
73 tst_brk(TBROK, "Invalid file offset '%s'", str_offset);
74
75 SAFE_STAT(".", &sb);
76 alignment = sb.st_blksize;
77
78 run_child = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
79
80 tst_res(TINFO, "Dirtying free blocks");
81 dirty_freeblocks(100 * 1024 * 1024);
82 }
83
cleanup(void)84 static void cleanup(void)
85 {
86 if (run_child) {
87 *run_child = 0;
88 SAFE_MUNMAP(run_child, sizeof(int));
89 }
90 }
91
run(void)92 static void run(void)
93 {
94 char *filename = "dio_sparse";
95 int status;
96 int fd;
97 int i;
98
99 fd = SAFE_OPEN(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666);
100 SAFE_FTRUNCATE(fd, filesize);
101
102 *run_child = 1;
103
104 for (i = 0; i < numchildren; i++) {
105 if (!SAFE_FORK()) {
106 io_read(filename, filesize, run_child);
107 return;
108 }
109 }
110
111 dio_sparse(fd, alignment, filesize, writesize, offset);
112
113 if (SAFE_WAITPID(-1, &status, WNOHANG))
114 tst_res(TFAIL, "Non zero bytes read");
115 else
116 tst_res(TPASS, "All bytes read were zeroed");
117
118 *run_child = 0;
119 }
120
121 static struct tst_test test = {
122 .test_all = run,
123 .setup = setup,
124 .cleanup = cleanup,
125 .needs_tmpdir = 1,
126 .forks_child = 1,
127 .options = (struct tst_option[]) {
128 {"n:", &str_numchildren, "Number of threads (default 16)"},
129 {"w:", &str_writesize, "Size of writing blocks (default 1K)"},
130 {"s:", &str_filesize, "Size of file (default 100M)"},
131 {"o:", &str_offset, "File offset (default 0)"},
132 {}
133 },
134 .skip_filesystems = (const char *[]) {
135 "tmpfs",
136 NULL
137 },
138 .timeout = 1800,
139 };
140