1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
4 */
5
6 #ifndef AIODIO_COMMON_H__
7 #define AIODIO_COMMON_H__
8
9 #include <stdlib.h>
10 #include "tst_test.h"
11
check_zero(char * buf,int size)12 static inline char *check_zero(char *buf, int size)
13 {
14 char *p;
15
16 p = buf;
17
18 while (size > 0) {
19 if (*buf != 0) {
20 tst_res(TINFO,
21 "non zero buffer at buf[%lu] => 0x%02x,%02x,%02x,%02x",
22 buf - p, (unsigned int)buf[0],
23 size > 1 ? (unsigned int)buf[1] : 0,
24 size > 2 ? (unsigned int)buf[2] : 0,
25 size > 3 ? (unsigned int)buf[3] : 0);
26 tst_res(TINFO, "buf %p, p %p", buf, p);
27 return buf;
28 }
29 buf++;
30 size--;
31 }
32
33 return 0;
34 }
35
io_append(const char * path,char pattern,int flags,size_t bs,size_t bcount)36 static inline void io_append(const char *path, char pattern, int flags, size_t bs, size_t bcount)
37 {
38 int fd;
39 size_t i;
40 char *bufptr;
41
42 bufptr = SAFE_MEMALIGN(getpagesize(), bs);
43 memset(bufptr, pattern, bs);
44
45 fd = SAFE_OPEN(path, flags, 0666);
46
47 for (i = 0; i < bcount; i++) {
48 SAFE_WRITE(SAFE_WRITE_ALL, fd, bufptr, bs);
49
50 if (!tst_remaining_runtime())
51 break;
52 }
53
54 free(bufptr);
55 SAFE_CLOSE(fd);
56 }
57
io_read(const char * filename,int filesize,volatile int * run_child)58 static inline void io_read(const char *filename, int filesize, volatile int *run_child)
59 {
60 char buff[4096];
61 int fd;
62 int i;
63 int r;
64
65 while ((fd = open(filename, O_RDONLY, 0666)) < 0) {
66 if (!*run_child)
67 return;
68
69 usleep(100);
70 }
71
72 tst_res(TINFO, "child %i reading file", getpid());
73
74 for (;;) {
75 off_t offset = 0;
76 char *bufoff;
77
78 SAFE_LSEEK(fd, SEEK_SET, 0);
79
80 for (i = 0; i < filesize + 1; i += sizeof(buff)) {
81 r = SAFE_READ(0, fd, buff, sizeof(buff));
82 if (r > 0) {
83 bufoff = check_zero(buff, r);
84 if (bufoff) {
85 tst_res(TFAIL,
86 "non-zero read at offset %zu",
87 offset + (bufoff - buff));
88 SAFE_CLOSE(fd);
89 exit(1);
90 }
91 offset += r;
92 }
93
94 if (!*run_child || !tst_remaining_runtime())
95 goto exit;
96 }
97 }
98
99 exit:
100 SAFE_CLOSE(fd);
101 }
102
io_read_eof(const char * filename,volatile int * run_child)103 static inline void io_read_eof(const char *filename, volatile int *run_child)
104 {
105 char buff[4096];
106 int fd;
107 int r;
108
109 while ((fd = open(filename, O_RDONLY, 0666)) < 0) {
110 if (!*run_child)
111 return;
112
113 usleep(100);
114 }
115
116 tst_res(TINFO, "child %i reading file", getpid());
117
118 while (*run_child) {
119 off_t offset;
120 char *bufoff;
121
122 offset = SAFE_LSEEK(fd, SEEK_END, 0);
123
124 r = SAFE_READ(0, fd, buff, sizeof(buff));
125 if (r > 0) {
126 bufoff = check_zero(buff, r);
127 if (bufoff) {
128 tst_res(TINFO, "non-zero read at offset %p", offset + bufoff);
129 break;
130 }
131 }
132 }
133
134 SAFE_CLOSE(fd);
135 }
136
137 /*
138 * This code tries to create dirty free blocks on
139 * the HDD so there is a chance that blocks to be allocated
140 * for a file are filled with something else than zeroes.
141 *
142 * The usefulness of this is IMHO questionable.
143 */
dirty_freeblocks(int size)144 static inline void dirty_freeblocks(int size)
145 {
146 char *filename = "dirty_file";
147 int fd;
148 void *p;
149 int pg;
150
151 pg = getpagesize();
152 size = LTP_ALIGN(size, pg);
153
154 fd = SAFE_OPEN(filename, O_CREAT | O_RDWR, 0600);
155 SAFE_FTRUNCATE(fd, size);
156
157 p = SAFE_MMAP(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FILE, fd, 0);
158 memset(p, 0xaa, size);
159 msync(p, size, MS_SYNC);
160 munmap(p, size);
161
162 SAFE_CLOSE(fd);
163 SAFE_UNLINK(filename);
164 }
165
166 #endif /* AIODIO_COMMON_H__ */
167