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)
252 ext2fs_free_mem(&io);
253 if (data)
254 ext2fs_free_mem(&data);
255 return retval;
256 }
257
test_close(io_channel channel)258 static errcode_t test_close(io_channel channel)
259 {
260 struct test_private_data *data;
261 errcode_t retval = 0;
262
263 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
264 data = (struct test_private_data *) channel->private_data;
265 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
266
267 if (--channel->refcount > 0)
268 return 0;
269
270 if (data->real)
271 retval = io_channel_close(data->real);
272
273 if (data->outfile && data->outfile != stderr)
274 fclose(data->outfile);
275
276 ext2fs_free_mem(&channel->private_data);
277 if (channel->name)
278 ext2fs_free_mem(&channel->name);
279 ext2fs_free_mem(&channel);
280 return retval;
281 }
282
test_set_blksize(io_channel channel,int blksize)283 static errcode_t test_set_blksize(io_channel channel, int blksize)
284 {
285 struct test_private_data *data;
286 errcode_t retval = 0;
287
288 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
289 data = (struct test_private_data *) channel->private_data;
290 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
291
292 if (data->real) {
293 retval = io_channel_set_blksize(data->real, blksize);
294 channel->align = data->real->align;
295 }
296 if (data->set_blksize)
297 data->set_blksize(blksize, retval);
298 if (data->flags & TEST_FLAG_SET_BLKSIZE)
299 fprintf(data->outfile,
300 "Test_io: set_blksize(%d) returned %s\n",
301 blksize, retval ? error_message(retval) : "OK");
302 channel->block_size = blksize;
303 return retval;
304 }
305
306
test_read_blk(io_channel channel,unsigned long block,int count,void * buf)307 static errcode_t test_read_blk(io_channel channel, unsigned long block,
308 int count, void *buf)
309 {
310 struct test_private_data *data;
311 errcode_t retval = 0;
312
313 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
314 data = (struct test_private_data *) channel->private_data;
315 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
316
317 if (data->real)
318 retval = io_channel_read_blk(data->real, block, count, buf);
319 if (data->read_blk)
320 data->read_blk(block, count, retval);
321 if (data->flags & TEST_FLAG_READ)
322 fprintf(data->outfile,
323 "Test_io: read_blk(%lu, %d) returned %s\n",
324 block, count, retval ? error_message(retval) : "OK");
325 if (data->block && data->block == block) {
326 if (data->flags & TEST_FLAG_DUMP)
327 test_dump_block(channel, data, block, buf);
328 if (--data->read_abort_count == 0)
329 test_abort(channel, block);
330 }
331 return retval;
332 }
333
test_write_blk(io_channel channel,unsigned long block,int count,const void * buf)334 static errcode_t test_write_blk(io_channel channel, unsigned long block,
335 int count, const void *buf)
336 {
337 struct test_private_data *data;
338 errcode_t retval = 0;
339
340 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
341 data = (struct test_private_data *) channel->private_data;
342 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
343
344 if (data->real)
345 retval = io_channel_write_blk(data->real, block, count, buf);
346 if (data->write_blk)
347 data->write_blk(block, count, retval);
348 if (data->flags & TEST_FLAG_WRITE)
349 fprintf(data->outfile,
350 "Test_io: write_blk(%lu, %d) returned %s\n",
351 block, count, retval ? error_message(retval) : "OK");
352 if (data->block && data->block == block) {
353 if (data->flags & TEST_FLAG_DUMP)
354 test_dump_block(channel, data, block, buf);
355 if (--data->write_abort_count == 0)
356 test_abort(channel, block);
357 }
358 return retval;
359 }
360
test_read_blk64(io_channel channel,unsigned long long block,int count,void * buf)361 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
362 int count, void *buf)
363 {
364 struct test_private_data *data;
365 errcode_t retval = 0;
366
367 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
368 data = (struct test_private_data *) channel->private_data;
369 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
370
371 if (data->real)
372 retval = io_channel_read_blk64(data->real, block, count, buf);
373 if (data->read_blk64)
374 data->read_blk64(block, count, retval);
375 if (data->flags & TEST_FLAG_READ)
376 fprintf(data->outfile,
377 "Test_io: read_blk64(%llu, %d) returned %s\n",
378 block, count, retval ? error_message(retval) : "OK");
379 if (data->block && data->block == block) {
380 if (data->flags & TEST_FLAG_DUMP)
381 test_dump_block(channel, data, block, buf);
382 if (--data->read_abort_count == 0)
383 test_abort(channel, block);
384 }
385 return retval;
386 }
387
test_write_blk64(io_channel channel,unsigned long long block,int count,const void * buf)388 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
389 int count, const void *buf)
390 {
391 struct test_private_data *data;
392 errcode_t retval = 0;
393
394 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
395 data = (struct test_private_data *) channel->private_data;
396 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
397
398 if (data->real)
399 retval = io_channel_write_blk64(data->real, block, count, buf);
400 if (data->write_blk64)
401 data->write_blk64(block, count, retval);
402 if (data->flags & TEST_FLAG_WRITE)
403 fprintf(data->outfile,
404 "Test_io: write_blk64(%llu, %d) returned %s\n",
405 block, count, retval ? error_message(retval) : "OK");
406 if (data->block && data->block == block) {
407 if (data->flags & TEST_FLAG_DUMP)
408 test_dump_block(channel, data, block, buf);
409 if (--data->write_abort_count == 0)
410 test_abort(channel, block);
411 }
412 return retval;
413 }
414
test_write_byte(io_channel channel,unsigned long offset,int count,const void * buf)415 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
416 int count, const void *buf)
417 {
418 struct test_private_data *data;
419 errcode_t retval = 0;
420
421 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
422 data = (struct test_private_data *) channel->private_data;
423 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
424
425 if (data->real && data->real->manager->write_byte)
426 retval = io_channel_write_byte(data->real, offset, count, buf);
427 if (data->write_byte)
428 data->write_byte(offset, count, retval);
429 if (data->flags & TEST_FLAG_WRITE)
430 fprintf(data->outfile,
431 "Test_io: write_byte(%lu, %d) returned %s\n",
432 offset, count, retval ? error_message(retval) : "OK");
433 return retval;
434 }
435
test_set_option(io_channel channel,const char * option,const char * arg)436 static errcode_t test_set_option(io_channel channel, const char *option,
437 const char *arg)
438 {
439 struct test_private_data *data;
440 errcode_t retval = 0;
441
442 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
443 data = (struct test_private_data *) channel->private_data;
444 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
445
446
447 if (data->flags & TEST_FLAG_SET_OPTION)
448 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
449 option, arg);
450 if (data->real && data->real->manager->set_option) {
451 retval = (data->real->manager->set_option)(data->real,
452 option, arg);
453 if (data->flags & TEST_FLAG_SET_OPTION)
454 fprintf(data->outfile, "returned %s\n",
455 retval ? error_message(retval) : "OK");
456 } else {
457 if (data->flags & TEST_FLAG_SET_OPTION)
458 fprintf(data->outfile, "not implemented\n");
459 }
460 return retval;
461 }
462
test_get_stats(io_channel channel,io_stats * stats)463 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
464 {
465 struct test_private_data *data;
466 errcode_t retval = 0;
467
468 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
469 data = (struct test_private_data *) channel->private_data;
470 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
471
472 if (data->real && data->real->manager->get_stats) {
473 retval = (data->real->manager->get_stats)(data->real, stats);
474 }
475 return retval;
476 }
477
test_discard(io_channel channel,unsigned long long block,unsigned long long count)478 static errcode_t test_discard(io_channel channel, unsigned long long block,
479 unsigned long long count)
480 {
481 struct test_private_data *data;
482 errcode_t retval = 0;
483
484 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
485 data = (struct test_private_data *) channel->private_data;
486 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
487
488 if (data->real)
489 retval = io_channel_discard(data->real, block, count);
490 if (data->flags & TEST_FLAG_DISCARD)
491 fprintf(data->outfile,
492 "Test_io: discard(%llu, %llu) returned %s\n",
493 block, count, retval ? error_message(retval) : "OK");
494 return retval;
495 }
496
test_cache_readahead(io_channel channel,unsigned long long block,unsigned long long count)497 static errcode_t test_cache_readahead(io_channel channel,
498 unsigned long long block,
499 unsigned long long count)
500 {
501 struct test_private_data *data;
502 errcode_t retval = 0;
503
504 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
505 data = (struct test_private_data *) channel->private_data;
506 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
507
508 if (data->real)
509 retval = io_channel_cache_readahead(data->real, block, count);
510 if (data->flags & TEST_FLAG_READAHEAD)
511 fprintf(data->outfile,
512 "Test_io: readahead(%llu, %llu) returned %s\n",
513 block, count, retval ? error_message(retval) : "OK");
514 return retval;
515 }
516
test_zeroout(io_channel channel,unsigned long long block,unsigned long long count)517 static errcode_t test_zeroout(io_channel channel, unsigned long long block,
518 unsigned long long count)
519 {
520 struct test_private_data *data;
521 errcode_t retval = 0;
522
523 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
524 data = (struct test_private_data *) channel->private_data;
525 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
526
527 if (data->real)
528 retval = io_channel_zeroout(data->real, block, count);
529 if (data->flags & TEST_FLAG_ZEROOUT)
530 fprintf(data->outfile,
531 "Test_io: zeroout(%llu, %llu) returned %s\n",
532 block, count, retval ? error_message(retval) : "OK");
533 return retval;
534 }
535
536 static struct struct_io_manager struct_test_manager = {
537 .magic = EXT2_ET_MAGIC_IO_MANAGER,
538 .name = "Test I/O Manager",
539 .open = test_open,
540 .close = test_close,
541 .set_blksize = test_set_blksize,
542 .read_blk = test_read_blk,
543 .write_blk = test_write_blk,
544 .flush = test_flush,
545 .write_byte = test_write_byte,
546 .set_option = test_set_option,
547 .get_stats = test_get_stats,
548 .read_blk64 = test_read_blk64,
549 .write_blk64 = test_write_blk64,
550 .discard = test_discard,
551 .cache_readahead = test_cache_readahead,
552 .zeroout = test_zeroout,
553 };
554
555 io_manager test_io_manager = &struct_test_manager;
556