Whamcloud - gitweb
ext2fs: add readahead method to improve scanning
[tools/e2fsprogs.git] / lib / ext2fs / test_io.c
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
90 static void test_dump_block(io_channel channel,
91                             struct test_private_data *data,
92                             unsigned long block, const void *buf)
93 {
94         const unsigned char *cp;
95         FILE *f = data->outfile;
96         int     i;
97         unsigned long   cksum = 0;
98
99         for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
100                 cksum += *cp;
101         }
102         fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
103         for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
104                 if ((i % 16) == 0)
105                         fprintf(f, "%04x: ", i);
106                 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
107         }
108 }
109
110 /*
111  * Flush data buffers to disk.
112  */
113 static errcode_t test_flush(io_channel channel)
114 {
115         struct test_private_data        *data;
116         errcode_t                       retval = 0;
117
118         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
119         data = (struct test_private_data *)channel->private_data;
120         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
121
122         if (data->real)
123                 retval = io_channel_flush(data->real);
124
125         if (data->flags & TEST_FLAG_FLUSH)
126                 fprintf(data->outfile, "Test_io: flush() returned %s\n",
127                         retval ? error_message(retval) : "OK");
128
129         return retval;
130 }
131
132 static void test_abort(io_channel channel, unsigned long block)
133 {
134         struct test_private_data *data;
135         FILE *f;
136
137         data = (struct test_private_data *) channel->private_data;
138         f = data->outfile;
139         test_flush(channel);
140
141         fprintf(f, "Aborting due to I/O to block %lu\n", block);
142         fflush(f);
143         abort();
144 }
145
146 static char *safe_getenv(const char *arg)
147 {
148         if ((getuid() != geteuid()) || (getgid() != getegid()))
149                 return NULL;
150 #if HAVE_PRCTL
151         if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
152                 return NULL;
153 #else
154 #if (defined(linux) && defined(SYS_prctl))
155         if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
156                 return NULL;
157 #endif
158 #endif
159
160 #if defined(HAVE_SECURE_GETENV)
161         return secure_getenv(arg);
162 #elif defined(HAVE___SECURE_GETENV)
163         return __secure_getenv(arg);
164 #else
165         return getenv(arg);
166 #endif
167 }
168
169 static errcode_t test_open(const char *name, int flags, io_channel *channel)
170 {
171         io_channel      io = NULL;
172         struct test_private_data *data = NULL;
173         errcode_t       retval;
174         char            *value;
175
176         if (name == 0)
177                 return EXT2_ET_BAD_DEVICE_NAME;
178         retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
179         if (retval)
180                 goto cleanup;
181         memset(io, 0, sizeof(struct struct_io_channel));
182         io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
183         retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
184         if (retval)
185                 goto cleanup;
186         io->manager = test_io_manager;
187         retval = ext2fs_get_mem(strlen(name)+1, &io->name);
188         if (retval)
189                 goto cleanup;
190
191         strcpy(io->name, name);
192         io->private_data = data;
193         io->block_size = 1024;
194         io->read_error = 0;
195         io->write_error = 0;
196         io->refcount = 1;
197
198         memset(data, 0, sizeof(struct test_private_data));
199         data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
200         if (test_io_backing_manager) {
201                 retval = test_io_backing_manager->open(name, flags,
202                                                        &data->real);
203                 if (retval)
204                         goto cleanup;
205         } else {
206                 data->real = 0;
207         }
208         data->read_blk =        test_io_cb_read_blk;
209         data->write_blk =       test_io_cb_write_blk;
210         data->set_blksize =     test_io_cb_set_blksize;
211         data->write_byte =      test_io_cb_write_byte;
212         data->read_blk64 =      test_io_cb_read_blk64;
213         data->write_blk64 =     test_io_cb_write_blk64;
214
215         data->outfile = NULL;
216         if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
217                 data->outfile = fopen(value, "w");
218         if (!data->outfile)
219                 data->outfile = stderr;
220
221         data->flags = 0;
222         if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
223                 data->flags = strtoul(value, NULL, 0);
224
225         data->block = 0;
226         if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
227                 data->block = strtoul(value, NULL, 0);
228
229         data->read_abort_count = 0;
230         if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
231                 data->read_abort_count = strtoul(value, NULL, 0);
232
233         data->write_abort_count = 0;
234         if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
235                 data->write_abort_count = strtoul(value, NULL, 0);
236
237         if (data->real)
238                 io->align = data->real->align;
239
240         *channel = io;
241         return 0;
242
243 cleanup:
244         if (io)
245                 ext2fs_free_mem(&io);
246         if (data)
247                 ext2fs_free_mem(&data);
248         return retval;
249 }
250
251 static errcode_t test_close(io_channel channel)
252 {
253         struct test_private_data *data;
254         errcode_t       retval = 0;
255
256         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
257         data = (struct test_private_data *) channel->private_data;
258         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
259
260         if (--channel->refcount > 0)
261                 return 0;
262
263         if (data->real)
264                 retval = io_channel_close(data->real);
265
266         if (data->outfile && data->outfile != stderr)
267                 fclose(data->outfile);
268
269         ext2fs_free_mem(&channel->private_data);
270         if (channel->name)
271                 ext2fs_free_mem(&channel->name);
272         ext2fs_free_mem(&channel);
273         return retval;
274 }
275
276 static errcode_t test_set_blksize(io_channel channel, int blksize)
277 {
278         struct test_private_data *data;
279         errcode_t       retval = 0;
280
281         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
282         data = (struct test_private_data *) channel->private_data;
283         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
284
285         if (data->real) {
286                 retval = io_channel_set_blksize(data->real, blksize);
287                 channel->align = data->real->align;
288         }
289         if (data->set_blksize)
290                 data->set_blksize(blksize, retval);
291         if (data->flags & TEST_FLAG_SET_BLKSIZE)
292                 fprintf(data->outfile,
293                         "Test_io: set_blksize(%d) returned %s\n",
294                         blksize, retval ? error_message(retval) : "OK");
295         channel->block_size = blksize;
296         return retval;
297 }
298
299
300 static errcode_t test_read_blk(io_channel channel, unsigned long block,
301                                int count, void *buf)
302 {
303         struct test_private_data *data;
304         errcode_t       retval = 0;
305
306         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
307         data = (struct test_private_data *) channel->private_data;
308         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
309
310         if (data->real)
311                 retval = io_channel_read_blk(data->real, block, count, buf);
312         if (data->read_blk)
313                 data->read_blk(block, count, retval);
314         if (data->flags & TEST_FLAG_READ)
315                 fprintf(data->outfile,
316                         "Test_io: read_blk(%lu, %d) returned %s\n",
317                         block, count, retval ? error_message(retval) : "OK");
318         if (data->block && data->block == block) {
319                 if (data->flags & TEST_FLAG_DUMP)
320                         test_dump_block(channel, data, block, buf);
321                 if (--data->read_abort_count == 0)
322                         test_abort(channel, block);
323         }
324         return retval;
325 }
326
327 static errcode_t test_write_blk(io_channel channel, unsigned long block,
328                                int count, const void *buf)
329 {
330         struct test_private_data *data;
331         errcode_t       retval = 0;
332
333         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
334         data = (struct test_private_data *) channel->private_data;
335         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
336
337         if (data->real)
338                 retval = io_channel_write_blk(data->real, block, count, buf);
339         if (data->write_blk)
340                 data->write_blk(block, count, retval);
341         if (data->flags & TEST_FLAG_WRITE)
342                 fprintf(data->outfile,
343                         "Test_io: write_blk(%lu, %d) returned %s\n",
344                         block, count, retval ? error_message(retval) : "OK");
345         if (data->block && data->block == block) {
346                 if (data->flags & TEST_FLAG_DUMP)
347                         test_dump_block(channel, data, block, buf);
348                 if (--data->write_abort_count == 0)
349                         test_abort(channel, block);
350         }
351         return retval;
352 }
353
354 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
355                                int count, void *buf)
356 {
357         struct test_private_data *data;
358         errcode_t       retval = 0;
359
360         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
361         data = (struct test_private_data *) channel->private_data;
362         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
363
364         if (data->real)
365                 retval = io_channel_read_blk64(data->real, block, count, buf);
366         if (data->read_blk64)
367                 data->read_blk64(block, count, retval);
368         if (data->flags & TEST_FLAG_READ)
369                 fprintf(data->outfile,
370                         "Test_io: read_blk64(%llu, %d) returned %s\n",
371                         block, count, retval ? error_message(retval) : "OK");
372         if (data->block && data->block == block) {
373                 if (data->flags & TEST_FLAG_DUMP)
374                         test_dump_block(channel, data, block, buf);
375                 if (--data->read_abort_count == 0)
376                         test_abort(channel, block);
377         }
378         return retval;
379 }
380
381 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
382                                int count, const void *buf)
383 {
384         struct test_private_data *data;
385         errcode_t       retval = 0;
386
387         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
388         data = (struct test_private_data *) channel->private_data;
389         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
390
391         if (data->real)
392                 retval = io_channel_write_blk64(data->real, block, count, buf);
393         if (data->write_blk64)
394                 data->write_blk64(block, count, retval);
395         if (data->flags & TEST_FLAG_WRITE)
396                 fprintf(data->outfile,
397                         "Test_io: write_blk64(%llu, %d) returned %s\n",
398                         block, count, retval ? error_message(retval) : "OK");
399         if (data->block && data->block == block) {
400                 if (data->flags & TEST_FLAG_DUMP)
401                         test_dump_block(channel, data, block, buf);
402                 if (--data->write_abort_count == 0)
403                         test_abort(channel, block);
404         }
405         return retval;
406 }
407
408 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
409                                int count, const void *buf)
410 {
411         struct test_private_data *data;
412         errcode_t       retval = 0;
413
414         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
415         data = (struct test_private_data *) channel->private_data;
416         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
417
418         if (data->real && data->real->manager->write_byte)
419                 retval = io_channel_write_byte(data->real, offset, count, buf);
420         if (data->write_byte)
421                 data->write_byte(offset, count, retval);
422         if (data->flags & TEST_FLAG_WRITE)
423                 fprintf(data->outfile,
424                         "Test_io: write_byte(%lu, %d) returned %s\n",
425                         offset, count, retval ? error_message(retval) : "OK");
426         return retval;
427 }
428
429 static errcode_t test_set_option(io_channel channel, const char *option,
430                                  const char *arg)
431 {
432         struct test_private_data *data;
433         errcode_t       retval = 0;
434
435         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
436         data = (struct test_private_data *) channel->private_data;
437         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
438
439
440         if (data->flags & TEST_FLAG_SET_OPTION)
441                 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
442                         option, arg);
443         if (data->real && data->real->manager->set_option) {
444                 retval = (data->real->manager->set_option)(data->real,
445                                                            option, arg);
446                 if (data->flags & TEST_FLAG_SET_OPTION)
447                         fprintf(data->outfile, "returned %s\n",
448                                 retval ? error_message(retval) : "OK");
449         } else {
450                 if (data->flags & TEST_FLAG_SET_OPTION)
451                         fprintf(data->outfile, "not implemented\n");
452         }
453         return retval;
454 }
455
456 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
457 {
458         struct test_private_data *data;
459         errcode_t       retval = 0;
460
461         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
462         data = (struct test_private_data *) channel->private_data;
463         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
464
465         if (data->real && data->real->manager->get_stats) {
466                 retval = (data->real->manager->get_stats)(data->real, stats);
467         }
468         return retval;
469 }
470
471 static errcode_t test_discard(io_channel channel, unsigned long long block,
472                               unsigned long long count)
473 {
474         struct test_private_data *data;
475         errcode_t       retval = 0;
476
477         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
478         data = (struct test_private_data *) channel->private_data;
479         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
480
481         if (data->real)
482                 retval = io_channel_discard(data->real, block, count);
483         if (data->flags & TEST_FLAG_DISCARD)
484                 fprintf(data->outfile,
485                         "Test_io: discard(%llu, %llu) returned %s\n",
486                         block, count, retval ? error_message(retval) : "OK");
487         return retval;
488 }
489
490 static errcode_t test_cache_readahead(io_channel channel,
491                                       unsigned long long block,
492                                       unsigned long long count)
493 {
494         struct test_private_data *data;
495         errcode_t       retval = 0;
496
497         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
498         data = (struct test_private_data *) channel->private_data;
499         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
500
501         if (data->real)
502                 retval = io_channel_cache_readahead(data->real, block, count);
503         if (data->flags & TEST_FLAG_READAHEAD)
504                 fprintf(data->outfile,
505                         "Test_io: readahead(%llu, %llu) returned %s\n",
506                         block, count, retval ? error_message(retval) : "OK");
507         return retval;
508 }
509
510 static struct struct_io_manager struct_test_manager = {
511         .magic          = EXT2_ET_MAGIC_IO_MANAGER,
512         .name           = "Test I/O Manager",
513         .open           = test_open,
514         .close          = test_close,
515         .set_blksize    = test_set_blksize,
516         .read_blk       = test_read_blk,
517         .write_blk      = test_write_blk,
518         .flush          = test_flush,
519         .write_byte     = test_write_byte,
520         .set_option     = test_set_option,
521         .get_stats      = test_get_stats,
522         .read_blk64     = test_read_blk64,
523         .write_blk64    = test_write_blk64,
524         .discard        = test_discard,
525         .cache_readahead        = test_cache_readahead,
526 };
527
528 io_manager test_io_manager = &struct_test_manager;