• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * test_io.c --- This is the Test I/O interface.
3  *
4  * Copyright (C) 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11 
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #ifdef HAVE_SYS_PRCTL_H
27 #include <sys/prctl.h>
28 #else
29 #define PR_GET_DUMPABLE 3
30 #endif
31 #if (!defined(HAVE_PRCTL) && defined(linux))
32 #include <sys/syscall.h>
33 #endif
34 
35 #include "ext2_fs.h"
36 #include "ext2fs.h"
37 
38 /*
39  * For checking structure magic numbers...
40  */
41 
42 #define EXT2_CHECK_MAGIC(struct, code) \
43 	  if ((struct)->magic != (code)) return (code)
44 
45 struct test_private_data {
46 	int	magic;
47 	io_channel real;
48 	int flags;
49 	FILE *outfile;
50 	unsigned long block;
51 	int read_abort_count, write_abort_count;
52 	void (*read_blk)(unsigned long block, int count, errcode_t err);
53 	void (*write_blk)(unsigned long block, int count, errcode_t err);
54 	void (*set_blksize)(int blksize, errcode_t err);
55 	void (*write_byte)(unsigned long block, int count, errcode_t err);
56 	void (*read_blk64)(unsigned long long block, int count, errcode_t err);
57 	void (*write_blk64)(unsigned long long block, int count, errcode_t err);
58 };
59 
60 /*
61  * These global variable can be set by the test program as
62  * necessary *before* calling test_open
63  */
64 io_manager test_io_backing_manager = 0;
65 void (*test_io_cb_read_blk)
66 	(unsigned long block, int count, errcode_t err) = 0;
67 void (*test_io_cb_write_blk)
68 	(unsigned long block, int count, errcode_t err) = 0;
69 void (*test_io_cb_read_blk64)
70 	(unsigned long long block, int count, errcode_t err) = 0;
71 void (*test_io_cb_write_blk64)
72 	(unsigned long long block, int count, errcode_t err) = 0;
73 void (*test_io_cb_set_blksize)
74 	(int blksize, errcode_t err) = 0;
75 void (*test_io_cb_write_byte)
76 	(unsigned long block, int count, errcode_t err) = 0;
77 
78 /*
79  * Test flags
80  */
81 #define TEST_FLAG_READ			0x01
82 #define TEST_FLAG_WRITE			0x02
83 #define TEST_FLAG_SET_BLKSIZE		0x04
84 #define TEST_FLAG_FLUSH			0x08
85 #define TEST_FLAG_DUMP			0x10
86 #define TEST_FLAG_SET_OPTION		0x20
87 #define TEST_FLAG_DISCARD		0x40
88 #define TEST_FLAG_READAHEAD		0x80
89 #define TEST_FLAG_ZEROOUT		0x100
90 
test_dump_block(io_channel channel,struct test_private_data * data,unsigned long block,const void * buf)91 static void test_dump_block(io_channel channel,
92 			    struct test_private_data *data,
93 			    unsigned long block, const void *buf)
94 {
95 	const unsigned char *cp;
96 	FILE *f = data->outfile;
97 	int	i;
98 	unsigned long	cksum = 0;
99 
100 	for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
101 		cksum += *cp;
102 	}
103 	fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
104 	for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
105 		if ((i % 16) == 0)
106 			fprintf(f, "%04x: ", i);
107 		fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
108 	}
109 }
110 
111 /*
112  * Flush data buffers to disk.
113  */
test_flush(io_channel channel)114 static errcode_t test_flush(io_channel channel)
115 {
116 	struct test_private_data	*data;
117 	errcode_t			retval = 0;
118 
119 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
120 	data = (struct test_private_data *)channel->private_data;
121 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
122 
123 	if (data->real)
124 		retval = io_channel_flush(data->real);
125 
126 	if (data->flags & TEST_FLAG_FLUSH)
127 		fprintf(data->outfile, "Test_io: flush() returned %s\n",
128 			retval ? error_message(retval) : "OK");
129 
130 	return retval;
131 }
132 
test_abort(io_channel channel,unsigned long block)133 static void test_abort(io_channel channel, unsigned long block)
134 {
135 	struct test_private_data *data;
136 	FILE *f;
137 
138 	data = (struct test_private_data *) channel->private_data;
139 	f = data->outfile;
140 	test_flush(channel);
141 
142 	fprintf(f, "Aborting due to I/O to block %lu\n", block);
143 	fflush(f);
144 	abort();
145 }
146 
safe_getenv(const char * arg)147 static char *safe_getenv(const char *arg)
148 {
149 #if !defined(_WIN32)
150 	if ((getuid() != geteuid()) || (getgid() != getegid()))
151 		return NULL;
152 #endif
153 #if HAVE_PRCTL
154 	if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
155 		return NULL;
156 #else
157 #if (defined(linux) && defined(SYS_prctl))
158 	if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
159 		return NULL;
160 #endif
161 #endif
162 
163 #if defined(HAVE_SECURE_GETENV)
164 	return secure_getenv(arg);
165 #elif defined(HAVE___SECURE_GETENV)
166 	return __secure_getenv(arg);
167 #else
168 	return getenv(arg);
169 #endif
170 }
171 
test_open(const char * name,int flags,io_channel * channel)172 static errcode_t test_open(const char *name, int flags, io_channel *channel)
173 {
174 	io_channel	io = NULL;
175 	struct test_private_data *data = NULL;
176 	errcode_t	retval;
177 	char		*value;
178 
179 	if (name == 0)
180 		return EXT2_ET_BAD_DEVICE_NAME;
181 	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
182 	if (retval)
183 		goto cleanup;
184 	memset(io, 0, sizeof(struct struct_io_channel));
185 	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
186 	retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
187 	if (retval)
188 		goto cleanup;
189 	io->manager = test_io_manager;
190 	retval = ext2fs_get_mem(strlen(name)+1, &io->name);
191 	if (retval)
192 		goto cleanup;
193 
194 	strcpy(io->name, name);
195 	io->private_data = data;
196 	io->block_size = 1024;
197 	io->read_error = 0;
198 	io->write_error = 0;
199 	io->refcount = 1;
200 	io->flags = 0;
201 
202 	memset(data, 0, sizeof(struct test_private_data));
203 	data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
204 	if (test_io_backing_manager) {
205 		retval = test_io_backing_manager->open(name, flags,
206 						       &data->real);
207 		if (retval)
208 			goto cleanup;
209 	} else {
210 		data->real = 0;
211 	}
212 	data->read_blk =	test_io_cb_read_blk;
213 	data->write_blk =	test_io_cb_write_blk;
214 	data->set_blksize =	test_io_cb_set_blksize;
215 	data->write_byte =	test_io_cb_write_byte;
216 	data->read_blk64 =	test_io_cb_read_blk64;
217 	data->write_blk64 =	test_io_cb_write_blk64;
218 
219 	data->outfile = NULL;
220 	if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
221 		data->outfile = fopen(value, "w");
222 	if (!data->outfile)
223 		data->outfile = stderr;
224 
225 	data->flags = 0;
226 	if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
227 		data->flags = strtoul(value, NULL, 0);
228 
229 	data->block = 0;
230 	if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
231 		data->block = strtoul(value, NULL, 0);
232 
233 	data->read_abort_count = 0;
234 	if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
235 		data->read_abort_count = strtoul(value, NULL, 0);
236 
237 	data->write_abort_count = 0;
238 	if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
239 		data->write_abort_count = strtoul(value, NULL, 0);
240 
241 	if (data->real) {
242 		io->align = data->real->align;
243 		if (data->real->flags & CHANNEL_FLAGS_THREADS)
244 			io->flags |= CHANNEL_FLAGS_THREADS;
245 	}
246 
247 	*channel = io;
248 	return 0;
249 
250 cleanup:
251 	if (io && io->name)
252 		ext2fs_free_mem(&io->name);
253 	if (io)
254 		ext2fs_free_mem(&io);
255 	if (data)
256 		ext2fs_free_mem(&data);
257 	return retval;
258 }
259 
test_close(io_channel channel)260 static errcode_t test_close(io_channel channel)
261 {
262 	struct test_private_data *data;
263 	errcode_t	retval = 0;
264 
265 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
266 	data = (struct test_private_data *) channel->private_data;
267 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
268 
269 	if (--channel->refcount > 0)
270 		return 0;
271 
272 	if (data->real)
273 		retval = io_channel_close(data->real);
274 
275 	if (data->outfile && data->outfile != stderr)
276 		fclose(data->outfile);
277 
278 	ext2fs_free_mem(&channel->private_data);
279 	if (channel->name)
280 		ext2fs_free_mem(&channel->name);
281 	ext2fs_free_mem(&channel);
282 	return retval;
283 }
284 
test_set_blksize(io_channel channel,int blksize)285 static errcode_t test_set_blksize(io_channel channel, int blksize)
286 {
287 	struct test_private_data *data;
288 	errcode_t	retval = 0;
289 
290 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
291 	data = (struct test_private_data *) channel->private_data;
292 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
293 
294 	if (data->real) {
295 		retval = io_channel_set_blksize(data->real, blksize);
296 		channel->align = data->real->align;
297 	}
298 	if (data->set_blksize)
299 		data->set_blksize(blksize, retval);
300 	if (data->flags & TEST_FLAG_SET_BLKSIZE)
301 		fprintf(data->outfile,
302 			"Test_io: set_blksize(%d) returned %s\n",
303 			blksize, retval ? error_message(retval) : "OK");
304 	channel->block_size = blksize;
305 	return retval;
306 }
307 
308 
test_read_blk(io_channel channel,unsigned long block,int count,void * buf)309 static errcode_t test_read_blk(io_channel channel, unsigned long block,
310 			       int count, void *buf)
311 {
312 	struct test_private_data *data;
313 	errcode_t	retval = 0;
314 
315 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
316 	data = (struct test_private_data *) channel->private_data;
317 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
318 
319 	if (data->real)
320 		retval = io_channel_read_blk(data->real, block, count, buf);
321 	if (data->read_blk)
322 		data->read_blk(block, count, retval);
323 	if (data->flags & TEST_FLAG_READ)
324 		fprintf(data->outfile,
325 			"Test_io: read_blk(%lu, %d) returned %s\n",
326 			block, count, retval ? error_message(retval) : "OK");
327 	if (data->block && data->block == block) {
328 		if (data->flags & TEST_FLAG_DUMP)
329 			test_dump_block(channel, data, block, buf);
330 		if (--data->read_abort_count == 0)
331 			test_abort(channel, block);
332 	}
333 	return retval;
334 }
335 
test_write_blk(io_channel channel,unsigned long block,int count,const void * buf)336 static errcode_t test_write_blk(io_channel channel, unsigned long block,
337 			       int count, const void *buf)
338 {
339 	struct test_private_data *data;
340 	errcode_t	retval = 0;
341 
342 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
343 	data = (struct test_private_data *) channel->private_data;
344 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
345 
346 	if (data->real)
347 		retval = io_channel_write_blk(data->real, block, count, buf);
348 	if (data->write_blk)
349 		data->write_blk(block, count, retval);
350 	if (data->flags & TEST_FLAG_WRITE)
351 		fprintf(data->outfile,
352 			"Test_io: write_blk(%lu, %d) returned %s\n",
353 			block, count, retval ? error_message(retval) : "OK");
354 	if (data->block && data->block == block) {
355 		if (data->flags & TEST_FLAG_DUMP)
356 			test_dump_block(channel, data, block, buf);
357 		if (--data->write_abort_count == 0)
358 			test_abort(channel, block);
359 	}
360 	return retval;
361 }
362 
test_read_blk64(io_channel channel,unsigned long long block,int count,void * buf)363 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
364 			       int count, void *buf)
365 {
366 	struct test_private_data *data;
367 	errcode_t	retval = 0;
368 
369 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
370 	data = (struct test_private_data *) channel->private_data;
371 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
372 
373 	if (data->real)
374 		retval = io_channel_read_blk64(data->real, block, count, buf);
375 	if (data->read_blk64)
376 		data->read_blk64(block, count, retval);
377 	if (data->flags & TEST_FLAG_READ)
378 		fprintf(data->outfile,
379 			"Test_io: read_blk64(%llu, %d) returned %s\n",
380 			block, count, retval ? error_message(retval) : "OK");
381 	if (data->block && data->block == block) {
382 		if (data->flags & TEST_FLAG_DUMP)
383 			test_dump_block(channel, data, block, buf);
384 		if (--data->read_abort_count == 0)
385 			test_abort(channel, block);
386 	}
387 	return retval;
388 }
389 
test_write_blk64(io_channel channel,unsigned long long block,int count,const void * buf)390 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
391 			       int count, const void *buf)
392 {
393 	struct test_private_data *data;
394 	errcode_t	retval = 0;
395 
396 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
397 	data = (struct test_private_data *) channel->private_data;
398 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
399 
400 	if (data->real)
401 		retval = io_channel_write_blk64(data->real, block, count, buf);
402 	if (data->write_blk64)
403 		data->write_blk64(block, count, retval);
404 	if (data->flags & TEST_FLAG_WRITE)
405 		fprintf(data->outfile,
406 			"Test_io: write_blk64(%llu, %d) returned %s\n",
407 			block, count, retval ? error_message(retval) : "OK");
408 	if (data->block && data->block == block) {
409 		if (data->flags & TEST_FLAG_DUMP)
410 			test_dump_block(channel, data, block, buf);
411 		if (--data->write_abort_count == 0)
412 			test_abort(channel, block);
413 	}
414 	return retval;
415 }
416 
test_write_byte(io_channel channel,unsigned long offset,int count,const void * buf)417 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
418 			       int count, const void *buf)
419 {
420 	struct test_private_data *data;
421 	errcode_t	retval = 0;
422 
423 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
424 	data = (struct test_private_data *) channel->private_data;
425 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
426 
427 	if (data->real && data->real->manager->write_byte)
428 		retval = io_channel_write_byte(data->real, offset, count, buf);
429 	if (data->write_byte)
430 		data->write_byte(offset, count, retval);
431 	if (data->flags & TEST_FLAG_WRITE)
432 		fprintf(data->outfile,
433 			"Test_io: write_byte(%lu, %d) returned %s\n",
434 			offset, count, retval ? error_message(retval) : "OK");
435 	return retval;
436 }
437 
test_set_option(io_channel channel,const char * option,const char * arg)438 static errcode_t test_set_option(io_channel channel, const char *option,
439 				 const char *arg)
440 {
441 	struct test_private_data *data;
442 	errcode_t	retval = 0;
443 
444 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
445 	data = (struct test_private_data *) channel->private_data;
446 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
447 
448 
449 	if (data->flags & TEST_FLAG_SET_OPTION)
450 		fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
451 			option, arg);
452 	if (data->real && data->real->manager->set_option) {
453 		retval = (data->real->manager->set_option)(data->real,
454 							   option, arg);
455 		if (data->flags & TEST_FLAG_SET_OPTION)
456 			fprintf(data->outfile, "returned %s\n",
457 				retval ? error_message(retval) : "OK");
458 	} else {
459 		if (data->flags & TEST_FLAG_SET_OPTION)
460 			fprintf(data->outfile, "not implemented\n");
461 	}
462 	return retval;
463 }
464 
test_get_stats(io_channel channel,io_stats * stats)465 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
466 {
467 	struct test_private_data *data;
468 	errcode_t	retval = 0;
469 
470 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
471 	data = (struct test_private_data *) channel->private_data;
472 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
473 
474 	if (data->real && data->real->manager->get_stats) {
475 		retval = (data->real->manager->get_stats)(data->real, stats);
476 	}
477 	return retval;
478 }
479 
test_discard(io_channel channel,unsigned long long block,unsigned long long count)480 static errcode_t test_discard(io_channel channel, unsigned long long block,
481 			      unsigned long long count)
482 {
483 	struct test_private_data *data;
484 	errcode_t	retval = 0;
485 
486 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
487 	data = (struct test_private_data *) channel->private_data;
488 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
489 
490 	if (data->real)
491 		retval = io_channel_discard(data->real, block, count);
492 	if (data->flags & TEST_FLAG_DISCARD)
493 		fprintf(data->outfile,
494 			"Test_io: discard(%llu, %llu) returned %s\n",
495 			block, count, retval ? error_message(retval) : "OK");
496 	return retval;
497 }
498 
test_cache_readahead(io_channel channel,unsigned long long block,unsigned long long count)499 static errcode_t test_cache_readahead(io_channel channel,
500 				      unsigned long long block,
501 				      unsigned long long count)
502 {
503 	struct test_private_data *data;
504 	errcode_t	retval = 0;
505 
506 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
507 	data = (struct test_private_data *) channel->private_data;
508 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
509 
510 	if (data->real)
511 		retval = io_channel_cache_readahead(data->real, block, count);
512 	if (data->flags & TEST_FLAG_READAHEAD)
513 		fprintf(data->outfile,
514 			"Test_io: readahead(%llu, %llu) returned %s\n",
515 			block, count, retval ? error_message(retval) : "OK");
516 	return retval;
517 }
518 
test_zeroout(io_channel channel,unsigned long long block,unsigned long long count)519 static errcode_t test_zeroout(io_channel channel, unsigned long long block,
520 			      unsigned long long count)
521 {
522 	struct test_private_data *data;
523 	errcode_t	retval = 0;
524 
525 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
526 	data = (struct test_private_data *) channel->private_data;
527 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
528 
529 	if (data->real)
530 		retval = io_channel_zeroout(data->real, block, count);
531 	if (data->flags & TEST_FLAG_ZEROOUT)
532 		fprintf(data->outfile,
533 			"Test_io: zeroout(%llu, %llu) returned %s\n",
534 			block, count, retval ? error_message(retval) : "OK");
535 	return retval;
536 }
537 
538 static struct struct_io_manager struct_test_manager = {
539 	.magic		= EXT2_ET_MAGIC_IO_MANAGER,
540 	.name		= "Test I/O Manager",
541 	.open		= test_open,
542 	.close		= test_close,
543 	.set_blksize	= test_set_blksize,
544 	.read_blk	= test_read_blk,
545 	.write_blk	= test_write_blk,
546 	.flush		= test_flush,
547 	.write_byte	= test_write_byte,
548 	.set_option	= test_set_option,
549 	.get_stats	= test_get_stats,
550 	.read_blk64	= test_read_blk64,
551 	.write_blk64	= test_write_blk64,
552 	.discard	= test_discard,
553 	.cache_readahead	= test_cache_readahead,
554 	.zeroout	= test_zeroout,
555 };
556 
557 io_manager test_io_manager = &struct_test_manager;
558