• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(1, fd, bufptr, bs);
49 
50 	free(bufptr);
51 	SAFE_CLOSE(fd);
52 }
53 
io_read(const char * filename,int filesize,volatile int * run_child)54 static inline void io_read(const char *filename, int filesize, volatile int *run_child)
55 {
56 	char buff[4096];
57 	int fd;
58 	int i;
59 	int r;
60 
61 	while ((fd = open(filename, O_RDONLY, 0666)) < 0)
62 		usleep(100);
63 
64 	tst_res(TINFO, "child %i reading file", getpid());
65 
66 	while (*run_child) {
67 		off_t offset = 0;
68 		char *bufoff;
69 
70 		SAFE_LSEEK(fd, SEEK_SET, 0);
71 
72 		for (i = 0; i < filesize + 1; i += sizeof(buff)) {
73 			r = SAFE_READ(0, fd, buff, sizeof(buff));
74 			if (r > 0) {
75 				bufoff = check_zero(buff, r);
76 				if (bufoff) {
77 					tst_res(TINFO, "non-zero read at offset %zu",
78 						offset + (bufoff - buff));
79 					break;
80 				}
81 				offset += r;
82 			}
83 		}
84 	}
85 
86 	SAFE_CLOSE(fd);
87 }
88 
io_read_eof(const char * filename,volatile int * run_child)89 static inline void io_read_eof(const char *filename, volatile int *run_child)
90 {
91 	char buff[4096];
92 	int fd;
93 	int r;
94 
95 	while ((fd = open(filename, O_RDONLY, 0666)) < 0)
96 		usleep(100);
97 
98 	tst_res(TINFO, "child %i reading file", getpid());
99 
100 	while (*run_child) {
101 		off_t offset;
102 		char *bufoff;
103 
104 		offset = SAFE_LSEEK(fd, SEEK_END, 0);
105 
106 		r = SAFE_READ(0, fd, buff, sizeof(buff));
107 		if (r > 0) {
108 			bufoff = check_zero(buff, r);
109 			if (bufoff) {
110 				tst_res(TINFO, "non-zero read at offset %p", offset + bufoff);
111 				break;
112 			}
113 		}
114 	}
115 
116 	SAFE_CLOSE(fd);
117 }
118 
119 /*
120  * This code tries to create dirty free blocks on
121  * the HDD so there is a chance that blocks to be allocated
122  * for a file are filled with something else than zeroes.
123  *
124  * The usefulness of this is IMHO questionable.
125  */
dirty_freeblocks(int size)126 static inline void dirty_freeblocks(int size)
127 {
128 	char *filename = "dirty_file";
129 	int fd;
130 	void *p;
131 	int pg;
132 
133 	pg = getpagesize();
134 	size = LTP_ALIGN(size, pg);
135 
136 	fd = SAFE_OPEN(filename, O_CREAT | O_RDWR, 0600);
137 	SAFE_FTRUNCATE(fd, size);
138 
139 	p = SAFE_MMAP(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FILE, fd, 0);
140 	memset(p, 0xaa, size);
141 	msync(p, size, MS_SYNC);
142 	munmap(p, size);
143 
144 	SAFE_CLOSE(fd);
145 	SAFE_UNLINK(filename);
146 }
147 
148 #endif /* AIODIO_COMMON_H__ */
149