aboutsummaryrefslogtreecommitdiff
path: root/tests/ubi-tests/io_read.c
blob: 3fccf930cb5487c502024fcf477bb3b6b889ccc5 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Artem B. Bityutskiy
 *
 * Test UBI volume read.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "libubi.h"
#define TESTNAME "io_basic"
#include "common.h"

static libubi_t libubi;
static struct ubi_dev_info dev_info;
const char *node;

static int test_static(void);
static int test_read(int type);

int main(int argc, char * const argv[])
{
	if (initial_check(argc, argv))
		return 1;

	node = argv[1];

	libubi = libubi_open();
	if (libubi == NULL) {
		failed("libubi_open");
		return 1;
	}

	if (ubi_get_dev_info(libubi, node, &dev_info)) {
		failed("ubi_get_dev_info");
		goto close;
	}

	if (test_static())
		goto close;
	if (test_read(UBI_DYNAMIC_VOLUME))
		goto close;
	if (test_read(UBI_STATIC_VOLUME))
		goto close;

	libubi_close(libubi);
	return 0;

close:
	libubi_close(libubi);
	return 1;
}

/**
 * test_static - test static volume-specific features.
 *
 * Thus function returns %0 in case of success and %-1 in case of failure.
 */
static int test_static(void)
{
	struct ubi_mkvol_request req;
	const char *name = TESTNAME ":io_basic()";
	char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
	struct ubi_vol_info vol_info;
	int fd, ret;
	char buf[20];

	req.vol_id = UBI_VOL_NUM_AUTO;
	req.alignment = 1;
	req.bytes = dev_info.avail_bytes;
	req.vol_type = UBI_STATIC_VOLUME;
	req.name = name;

	if (ubi_mkvol(libubi, node, &req)) {
		failed("ubi_mkvol");
		return -1;
	}

	sprintf(&vol_node[0], UBI_VOLUME_PATTERN, dev_info.dev_num, req.vol_id);

	fd = open(vol_node, O_RDWR);
	if (fd == -1) {
		failed("open");
		err_msg("cannot open \"%s\"\n", node);
		goto remove;
	}

	if (ubi_get_vol_info(libubi, vol_node, &vol_info)) {
		failed("ubi_get_vol_info");
		goto close;
	}

	/* Make sure new static volume contains no data */
	if (vol_info.data_bytes != 0) {
		err_msg("data_bytes = %lld, not zero", vol_info.data_bytes);
		goto close;
	}

	/* Ensure read returns EOF */
	ret = read(fd, &buf[0], 1);
	if (ret < 0) {
		failed("read");
		goto close;
	}
	if (ret != 0) {
		err_msg("read data from free static volume");
		goto close;
	}

	if (ubi_update_start(libubi, fd, 10)) {
		failed("ubi_update_start");
		goto close;
	}

	ret = write(fd, &buf[0], 10);
	if (ret < 0) {
		failed("write");
		goto close;
	}
	if (ret != 10) {
		err_msg("written %d bytes", ret);
		goto close;
	}

	if (lseek(fd, 0, SEEK_SET) != 0) {
		failed("seek");
		goto close;
	}
	ret = read(fd, &buf[0], 20);
	if (ret < 0) {
		failed("read");
		goto close;
	}
	if (ret != 10) {
		err_msg("read %d bytes", ret);
		goto close;
	}

	close(fd);
	if (ubi_rmvol(libubi, node, req.vol_id)) {
		failed("ubi_rmvol");
		return -1;
	}

	return 0;

close:
	close(fd);
remove:
	ubi_rmvol(libubi, node, req.vol_id);
	return -1;
}

static int test_read1(struct ubi_vol_info *vol_info);

/**
 * test_read - test UBI volume reading from different offsets.
 *
 * @type  volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
 *
 * Thus function returns %0 in case of success and %-1 in case of failure.
 */
static int test_read(int type)
{
	const char *name = TESTNAME ":test_read()";
	int alignments[] = ALIGNMENTS(dev_info.leb_size);
	char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
	struct ubi_mkvol_request req;
	int i;

	for (i = 0; i < sizeof(alignments)/sizeof(int); i++) {
		int leb_size;
		struct ubi_vol_info vol_info;

		req.vol_id = UBI_VOL_NUM_AUTO;
		req.vol_type = type;
		req.name = name;

		req.alignment = alignments[i];
		req.alignment -= req.alignment % dev_info.min_io_size;
		if (req.alignment == 0)
			req.alignment = dev_info.min_io_size;

		leb_size = dev_info.leb_size - dev_info.leb_size % req.alignment;
		req.bytes =  MIN_AVAIL_EBS * leb_size;

		if (ubi_mkvol(libubi, node, &req)) {
			failed("ubi_mkvol");
			return -1;
		}

		sprintf(&vol_node[0], UBI_VOLUME_PATTERN, dev_info.dev_num,
			req.vol_id);

		if (ubi_get_vol_info(libubi, vol_node, &vol_info)) {
			failed("ubi_get_vol_info");
			goto remove;
		}

		if (test_read1(&vol_info)) {
			err_msg("alignment = %d", req.alignment);
			goto remove;
		}

		if (ubi_rmvol(libubi, node, req.vol_id)) {
			failed("ubi_rmvol");
			return -1;
		}
	}

	return 0;

remove:
	ubi_rmvol(libubi, node, req.vol_id);
	return -1;
}

static int test_read2(const struct ubi_vol_info *vol_info, int len);

static int fd;

/* Data lengthes to test, @io - minimal I/O unit size, @s - eraseblock size */
#define LENGTHES(io, s)                                                        \
	{1, (io), (io)+1, 2*(io), 3*(io)-1, 3*(io),                            \
	 PAGE_SIZE-1, PAGE_SIZE-(io), 2*PAGE_SIZE, 2*PAGE_SIZE-(io),           \
	 (s)/2-1, (s)/2, (s)/2+1, (s)-1, (s), (s)+1, 2*(s)-(io), 2*(s),        \
	 2*(s)+(io), 3*(s), 3*(s)+(io)};

/*
 * A helper function for test_read().
 */
static int test_read1(struct ubi_vol_info *vol_info)
{
	int i, written = 0;
	char vol_node[strlen(UBI_VOLUME_PATTERN) + 100];
	int lengthes[] = LENGTHES(dev_info.min_io_size, vol_info->leb_size);

	sprintf(&vol_node[0], UBI_VOLUME_PATTERN, dev_info.dev_num,
		vol_info->vol_id);

	fd = open(vol_node, O_RDWR);
	if (fd == -1) {
		failed("open");
		err_msg("cannot open \"%s\"\n", node);
		return -1;
	}

	/* Write some pattern to the volume */
	if (ubi_update_start(libubi, fd, vol_info->rsvd_bytes)) {
		failed("ubi_update_start");
		err_msg("bytes = %lld", vol_info->rsvd_bytes);
		goto close;
	}

	while (written < vol_info->rsvd_bytes) {
		int i, ret;
		unsigned char buf[512];

		for (i = 0; i < 512; i++)
			buf[i] = (unsigned char)(written + i);

		ret = write(fd, &buf[0], 512);
		if (ret == -1) {
			failed("write");
			err_msg("written = %d, ret = %d", written, ret);
			goto close;
		}
		written += ret;
	}

	close(fd);

	if (ubi_get_vol_info(libubi, vol_node, vol_info)) {
		failed("ubi_get_vol_info");
		return -1;
	}

	fd = open(vol_node, O_RDONLY);
	if (fd == -1) {
		failed("open");
		err_msg("cannot open \"%s\"\n", node);
		return -1;
	}

	for (i = 0; i < sizeof(lengthes)/sizeof(int); i++) {
		if (test_read2(vol_info, lengthes[i])) {
			err_msg("length = %d", lengthes[i]);
			goto close;
		}
	}

	close(fd);
	return 0;

close:
	close(fd);
	return -1;
}

static int test_read3(const struct ubi_vol_info *vol_info, int len, off_t off);

/*
 * Offsets to test, @io - minimal I/O unit size, @s - eraseblock size, @sz -
 * volume size.
 */
#define OFFSETS(io, s, sz)                                                     \
	{0, (io)-1, (io), (io)+1, 2*(io)-1, 2*(io), 3*(io)-1, 3*(io),          \
	 PAGE_SIZE-1, PAGE_SIZE-(io), 2*PAGE_SIZE, 2*PAGE_SIZE-(io),           \
	 (s)/2-1, (s)/2, (s)/2+1, (s)-1, (s), (s)+1, 2*(s)-(io), 2*(s),        \
	 2*(s)+(io), 3*(s), (sz)-(s)-1, (sz)-(io)-1, (sz)-PAGE_SIZE-1};

/*
 * A helper function for test_read1().
 */
static int test_read2(const struct ubi_vol_info *vol_info, int len)
{
	int i;
	off_t offsets[] = OFFSETS(dev_info.min_io_size, vol_info->leb_size,
				  vol_info->data_bytes);

	for (i = 0; i < sizeof(offsets)/sizeof(off_t); i++) {
		if (test_read3(vol_info, len, offsets[i])) {
			err_msg("offset = %d", offsets[i]);
			return -1;
		}
	}

	return 0;
}

/*
 * A helper function for test_read2().
 */
static int test_read3(const struct ubi_vol_info *vol_info, int len, off_t off)
{
	int i, len1;
	unsigned char ck_buf[len], buf[len];
	off_t new_off;

	if (off + len > vol_info->data_bytes)
		len1 = vol_info->data_bytes - off;
	else
		len1 = len;

	if (lseek(fd, off, SEEK_SET) != off) {
		failed("seek");
		err_msg("len = %d", len);
		return -1;
	}
	if (read(fd, &buf[0], len) != len1) {
		failed("read");
		err_msg("len = %d", len);
		return -1;
	}

	new_off = lseek(fd, 0, SEEK_CUR);
	if (new_off != off + len1) {
		if (new_off == -1)
			failed("lseek");
		else
			err_msg("read %d bytes from %lld, but resulting "
				"offset is %lld", len1, (long long) off, (long long) new_off);
		return -1;
	}

	for (i = 0; i < len1; i++)
		ck_buf[i] = (unsigned char)(off + i);

	if (memcmp(&buf[0], &ck_buf[0], len1)) {
		err_msg("incorrect data read from offset %lld",
			(long long)off);
		err_msg("len = %d", len);
		return -1;
	}

	return 0;
}