summaryrefslogtreecommitdiffstats
path: root/tests/bugs/quick-read/bz1523599/test_bz1523599.c
blob: 843408680a70d6faccde7e4475c4f1152c4fe12f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * ./test_bz1523599 0 vm140-111 gv0 test211 log
 * ./test_bz1523599 1 vm140-111 gv0 test211 log
 * Open - Discard - Read - Then check read information to see if the initial TEST_STR_LEN/2 bytes read zero
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>
#include <errno.h>
#include <sys/uio.h>

#define TEST_STR_LEN 2048

enum fallocate_flag {
	TEST_WRITE,
	TEST_DISCARD,
	TEST_ZEROFILL,
};

void print_str(char *str, int len)
{
	int i, addr;

	printf("%07x\t", 0);
	for (i = 0; i < len; i++) {
		printf("%02x", str[i]);
		if (i) {
			if ((i + 1) % 16 == 0)
				printf("\n%07x\t", i+1);
			else if ((i + 1) % 4 == 0)
				printf(" ");
		}
	}
	printf("\n");
}

int
test_read(char *str, int total_length, int len_zero)
{
	int i;
	int ret = 0;

	for (i = 0; i < len_zero; i++) {
		if (str[i]) {
			fprintf(stderr, "char at position %d not zeroed out\n",
				i);
			ret = -EIO;
			goto out;
		}
	}

	for (i = len_zero; i < total_length; i++) {
		if (str[i] != 0x11) {
			fprintf(stderr,
				"char at position %d does not contain pattern\n",
				i);
			ret = -EIO;
			goto out;
		}
	}
out:
	return ret;
}

int main(int argc, char *argv[])
{
	int opcode;
	char *host_name, *volume_name, *file_path, *glfs_log_path;
	glfs_t *fs = NULL;
	glfs_fd_t *fd = NULL;
	off_t offset = 0;
	size_t len_zero = TEST_STR_LEN / 2;
	char writestr[TEST_STR_LEN];
	char readstr[TEST_STR_LEN];
	struct iovec iov = {&readstr, TEST_STR_LEN};
	int i;
	int ret = 1;

	for (i = 0; i < TEST_STR_LEN; i++)
		writestr[i] = 0x11;
	for (i = 0; i < TEST_STR_LEN; i++)
		readstr[i] = 0x22;

	if (argc != 6) {
		fprintf(stderr,
			"Syntax: %s <test type> <host> <volname> <file-path> <log-file>\n",
			argv[0]);
		return 1;
	}

	opcode = atoi(argv[1]);
	host_name = argv[2];
	volume_name = argv[3];
	file_path = argv[4];
	glfs_log_path = argv[5];

	fs = glfs_new(volume_name);
	if (!fs) {
		perror("glfs_new");
		return 1;
	}

	ret = glfs_set_volfile_server(fs, "tcp", host_name, 24007);
	if (ret != 0) {
		perror("glfs_set_volfile_server");
		goto out;
	}

	ret = glfs_set_logging(fs, glfs_log_path, 7);
	if (ret != 0) {
		perror("glfs_set_logging");
		goto out;
	}

	ret = glfs_init(fs);
	if (ret != 0) {
		perror("glfs_init");
		goto out;
	}

	fd = glfs_creat(fs, file_path, O_RDWR, 0777);
	if (fd == NULL) {
		perror("glfs_creat");
		ret = -1;
		goto out;
	}

	switch (opcode) {
		case TEST_WRITE:
			fprintf(stderr, "Test Write\n");
			ret = glfs_write(fd, writestr, TEST_STR_LEN, 0);
			if (ret < 0) {
				perror("glfs_write");
				goto out;
			} else if (ret != TEST_STR_LEN) {
				fprintf(stderr, "insufficient data written %d \n", ret);
				ret = -EIO;
				goto out;
			}
			ret = 0;
			goto out;
		case TEST_DISCARD:
			fprintf(stderr, "Test Discard\n");
			ret = glfs_discard(fd, offset, len_zero);
			if (ret < 0) {
				if (errno == EOPNOTSUPP) {
					fprintf(stderr, "Operation not supported\n");
					ret = 0;
					goto out;
				}
				perror("glfs_discard");
				goto out;
			}
			goto test_read;
		case TEST_ZEROFILL:
			fprintf(stderr, "Test Zerofill\n");
			ret = glfs_zerofill(fd, offset, len_zero);
			if (ret < 0) {
				if (errno == EOPNOTSUPP) {
					fprintf(stderr, "Operation not supported\n");
					ret = 0;
					goto out;
				}
				perror("glfs_zerofill");
				goto out;
			}
			goto test_read;
		default:
			ret = -1;
			fprintf(stderr, "Incorrect test code %d\n", opcode);
			goto out;
	}

test_read:
	ret = glfs_readv(fd, &iov, 1, 0);
	if (ret < 0) {
		perror("glfs_readv");
		goto out;
	}

	/* printf("Read str\n"); print_str(readstr, TEST_STR_LEN); printf("\n"); */
	ret = test_read(readstr, TEST_STR_LEN, len_zero);

out:
	if (fd)
		glfs_close(fd);
	glfs_fini(fs);

	if (ret)
		return -1;

	return 0;
}