1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
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 usleep(100);
67
68 tst_res(TINFO, "child %i reading file", getpid());
69
70 for (;;) {
71 off_t offset = 0;
72 char *bufoff;
73
74 SAFE_LSEEK(fd, SEEK_SET, 0);
75
76 for (i = 0; i < filesize + 1; i += sizeof(buff)) {
77 r = SAFE_READ(0, fd, buff, sizeof(buff));
78 if (r > 0) {
79 bufoff = check_zero(buff, r);
80 if (bufoff) {
81 tst_res(TFAIL,
82 "non-zero read at offset %zu",
83 offset + (bufoff - buff));
84 SAFE_CLOSE(fd);
85 exit(1);
86 }
87 offset += r;
88 }
89
90 if (!*run_child || !tst_remaining_runtime())
91 goto exit;
92 }
93 }
94
95 exit:
96 SAFE_CLOSE(fd);
97 }
98
io_read_eof(const char * filename,volatile int * run_child)99 static inline void io_read_eof(const char *filename, volatile int *run_child)
100 {
101 char buff[4096];
102 int fd;
103 int r;
104
105 while ((fd = open(filename, O_RDONLY, 0666)) < 0)
106 usleep(100);
107
108 tst_res(TINFO, "child %i reading file", getpid());
109
110 while (*run_child) {
111 off_t offset;
112 char *bufoff;
113
114 offset = SAFE_LSEEK(fd, SEEK_END, 0);
115
116 r = SAFE_READ(0, fd, buff, sizeof(buff));
117 if (r > 0) {
118 bufoff = check_zero(buff, r);
119 if (bufoff) {
120 tst_res(TINFO, "non-zero read at offset %p", offset + bufoff);
121 break;
122 }
123 }
124 }
125
126 SAFE_CLOSE(fd);
127 }
128
129 /*
130 * This code tries to create dirty free blocks on
131 * the HDD so there is a chance that blocks to be allocated
132 * for a file are filled with something else than zeroes.
133 *
134 * The usefulness of this is IMHO questionable.
135 */
dirty_freeblocks(int size)136 static inline void dirty_freeblocks(int size)
137 {
138 char *filename = "dirty_file";
139 int fd;
140 void *p;
141 int pg;
142
143 pg = getpagesize();
144 size = LTP_ALIGN(size, pg);
145
146 fd = SAFE_OPEN(filename, O_CREAT | O_RDWR, 0600);
147 SAFE_FTRUNCATE(fd, size);
148
149 p = SAFE_MMAP(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FILE, fd, 0);
150 memset(p, 0xaa, size);
151 msync(p, size, MS_SYNC);
152 munmap(p, size);
153
154 SAFE_CLOSE(fd);
155 SAFE_UNLINK(filename);
156 }
157
158 #endif /* AIODIO_COMMON_H__ */
159