blob: cac67219b81be214c692e541e4503b6b1f518285 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
2 * test_io.c --- This is the Test I/O interface.
3 *
4 * Copyright (C) 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 * %End-Header%
10 */
11
Theodore Ts'od1154eb2011-09-18 17:34:37 -040012#include "config.h"
Theodore Ts'o1ad31742013-06-16 14:34:59 -040013#if HAVE_SECURE_GETENV
14#define _GNU_SOURCE
15#endif
16#if HAVE_SECURE_GETENV
17#define _GNU_SOURCE
18#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000019#include <stdio.h>
20#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000021#if HAVE_UNISTD_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000022#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000023#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000024#include <fcntl.h>
25#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000026#if HAVE_SYS_STAT_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000027#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000028#endif
29#if HAVE_SYS_TYPES_H
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000030#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000031#endif
Theodore Ts'o762c7c62005-04-06 14:44:16 -040032#ifdef HAVE_SYS_PRCTL_H
33#include <sys/prctl.h>
34#else
35#define PR_GET_DUMPABLE 3
36#endif
37#if (!defined(HAVE_PRCTL) && defined(linux))
38#include <sys/syscall.h>
39#endif
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000040
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000041#include "ext2_fs.h"
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000042#include "ext2fs.h"
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000043
44/*
45 * For checking structure magic numbers...
46 */
47
48#define EXT2_CHECK_MAGIC(struct, code) \
49 if ((struct)->magic != (code)) return (code)
Theodore Ts'oefc6f622008-08-27 23:07:54 -040050
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000051struct test_private_data {
52 int magic;
53 io_channel real;
Theodore Ts'o2a29f132003-05-05 12:08:47 -040054 int flags;
55 FILE *outfile;
56 unsigned long block;
Theodore Ts'oa002e7e2004-05-26 21:04:07 -040057 int read_abort_count, write_abort_count;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000058 void (*read_blk)(unsigned long block, int count, errcode_t err);
59 void (*write_blk)(unsigned long block, int count, errcode_t err);
60 void (*set_blksize)(int blksize, errcode_t err);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000061 void (*write_byte)(unsigned long block, int count, errcode_t err);
Theodore Ts'oc7015e42008-05-24 19:17:01 -040062 void (*read_blk64)(unsigned long long block, int count, errcode_t err);
63 void (*write_blk64)(unsigned long long block, int count, errcode_t err);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000064};
65
66static errcode_t test_open(const char *name, int flags, io_channel *channel);
67static errcode_t test_close(io_channel channel);
68static errcode_t test_set_blksize(io_channel channel, int blksize);
69static errcode_t test_read_blk(io_channel channel, unsigned long block,
70 int count, void *data);
71static errcode_t test_write_blk(io_channel channel, unsigned long block,
72 int count, const void *data);
Theodore Ts'oc7015e42008-05-24 19:17:01 -040073static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
74 int count, void *data);
75static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
76 int count, const void *data);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000077static errcode_t test_flush(io_channel channel);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000078static errcode_t test_write_byte(io_channel channel, unsigned long offset,
79 int count, const void *buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040080static errcode_t test_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050081 const char *arg);
Theodore Ts'o6d96b002007-08-03 20:07:09 -040082static errcode_t test_get_stats(io_channel channel, io_stats *stats);
Lukas Czernere90a59e2010-11-18 03:38:36 +000083static errcode_t test_discard(io_channel channel, unsigned long long block,
84 unsigned long long count);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000085
86static struct struct_io_manager struct_test_manager = {
87 EXT2_ET_MAGIC_IO_MANAGER,
88 "Test I/O Manager",
89 test_open,
90 test_close,
91 test_set_blksize,
92 test_read_blk,
93 test_write_blk,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +000094 test_flush,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050095 test_write_byte,
Theodore Ts'o6d96b002007-08-03 20:07:09 -040096 test_set_option,
97 test_get_stats,
Theodore Ts'oc7015e42008-05-24 19:17:01 -040098 test_read_blk64,
99 test_write_blk64,
Lukas Czernere90a59e2010-11-18 03:38:36 +0000100 test_discard,
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000101};
102
103io_manager test_io_manager = &struct_test_manager;
104
105/*
106 * These global variable can be set by the test program as
107 * necessary *before* calling test_open
108 */
109io_manager test_io_backing_manager = 0;
110void (*test_io_cb_read_blk)
111 (unsigned long block, int count, errcode_t err) = 0;
112void (*test_io_cb_write_blk)
113 (unsigned long block, int count, errcode_t err) = 0;
Theodore Ts'oc7015e42008-05-24 19:17:01 -0400114void (*test_io_cb_read_blk64)
115 (unsigned long long block, int count, errcode_t err) = 0;
116void (*test_io_cb_write_blk64)
117 (unsigned long long block, int count, errcode_t err) = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000118void (*test_io_cb_set_blksize)
119 (int blksize, errcode_t err) = 0;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000120void (*test_io_cb_write_byte)
121 (unsigned long block, int count, errcode_t err) = 0;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000122
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400123/*
124 * Test flags
125 */
126#define TEST_FLAG_READ 0x01
127#define TEST_FLAG_WRITE 0x02
128#define TEST_FLAG_SET_BLKSIZE 0x04
129#define TEST_FLAG_FLUSH 0x08
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400130#define TEST_FLAG_DUMP 0x10
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500131#define TEST_FLAG_SET_OPTION 0x20
Lukas Czernere90a59e2010-11-18 03:38:36 +0000132#define TEST_FLAG_DISCARD 0x40
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400133
134static void test_dump_block(io_channel channel,
135 struct test_private_data *data,
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400136 unsigned long block, const void *buf)
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400137{
138 const unsigned char *cp;
139 FILE *f = data->outfile;
140 int i;
141 unsigned long cksum = 0;
142
143 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
144 cksum += *cp;
145 }
Theodore Ts'o48e6e812003-07-06 00:36:48 -0400146 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400147 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
148 if ((i % 16) == 0)
149 fprintf(f, "%04x: ", i);
150 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
151 }
152}
153
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400154static void test_abort(io_channel channel, unsigned long block)
155{
156 struct test_private_data *data;
157 FILE *f;
158
159 data = (struct test_private_data *) channel->private_data;
160 f = data->outfile;
161 test_flush(channel);
162
163 fprintf(f, "Aborting due to I/O to block %lu\n", block);
164 fflush(f);
165 abort();
166}
167
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400168static char *safe_getenv(const char *arg)
169{
Theodore Ts'o09f3eba2005-04-16 13:56:03 -0400170 if ((getuid() != geteuid()) || (getgid() != getegid()))
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400171 return NULL;
172#if HAVE_PRCTL
Theodore Ts'o583d1f82005-07-25 11:36:43 -0500173 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400174 return NULL;
175#else
176#if (defined(linux) && defined(SYS_prctl))
Theodore Ts'o583d1f82005-07-25 11:36:43 -0500177 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400178 return NULL;
179#endif
180#endif
181
Theodore Ts'o1ad31742013-06-16 14:34:59 -0400182#if defined(HAVE_SECURE_GETENV)
183 return secure_getenv(arg);
184#elif defined(HAVE___SECURE_GETENV)
Theodore Ts'o3af0a452005-05-08 02:15:02 -0400185 return __secure_getenv(arg);
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400186#else
Theodore Ts'o3af0a452005-05-08 02:15:02 -0400187 return getenv(arg);
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400188#endif
189}
190
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000191static errcode_t test_open(const char *name, int flags, io_channel *channel)
192{
193 io_channel io = NULL;
194 struct test_private_data *data = NULL;
195 errcode_t retval;
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400196 char *value;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000197
198 if (name == 0)
199 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400200 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000201 if (retval)
Eric Sandeen624e8eb2011-09-16 15:49:28 -0500202 goto cleanup;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000203 memset(io, 0, sizeof(struct struct_io_channel));
204 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400205 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
Eric Sandeen624e8eb2011-09-16 15:49:28 -0500206 if (retval)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000207 goto cleanup;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000208 io->manager = test_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400209 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000210 if (retval)
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000211 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000212
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000213 strcpy(io->name, name);
214 io->private_data = data;
215 io->block_size = 1024;
216 io->read_error = 0;
217 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000218 io->refcount = 1;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000219
220 memset(data, 0, sizeof(struct test_private_data));
221 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
222 if (test_io_backing_manager) {
223 retval = test_io_backing_manager->open(name, flags,
224 &data->real);
225 if (retval)
226 goto cleanup;
227 } else
228 data->real = 0;
229 data->read_blk = test_io_cb_read_blk;
230 data->write_blk = test_io_cb_write_blk;
231 data->set_blksize = test_io_cb_set_blksize;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000232 data->write_byte = test_io_cb_write_byte;
Theodore Ts'oc7015e42008-05-24 19:17:01 -0400233 data->read_blk64 = test_io_cb_read_blk64;
234 data->write_blk64 = test_io_cb_write_blk64;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400235
236 data->outfile = NULL;
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400237 if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400238 data->outfile = fopen(value, "w");
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400239 if (!data->outfile)
240 data->outfile = stderr;
241
242 data->flags = 0;
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400243 if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400244 data->flags = strtoul(value, NULL, 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400245
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400246 data->block = 0;
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400247 if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400248 data->block = strtoul(value, NULL, 0);
249
250 data->read_abort_count = 0;
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400251 if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400252 data->read_abort_count = strtoul(value, NULL, 0);
253
254 data->write_abort_count = 0;
Theodore Ts'o762c7c62005-04-06 14:44:16 -0400255 if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400256 data->write_abort_count = strtoul(value, NULL, 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400257
Theodore Ts'o0a05b902012-05-07 12:56:07 -0400258 if (data->real)
259 io->align = data->real->align;
260
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000261 *channel = io;
262 return 0;
263
264cleanup:
265 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400266 ext2fs_free_mem(&io);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000267 if (data)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400268 ext2fs_free_mem(&data);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000269 return retval;
270}
271
272static errcode_t test_close(io_channel channel)
273{
274 struct test_private_data *data;
275 errcode_t retval = 0;
276
277 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
278 data = (struct test_private_data *) channel->private_data;
279 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
280
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000281 if (--channel->refcount > 0)
282 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400283
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000284 if (data->real)
285 retval = io_channel_close(data->real);
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400286
Theodore Ts'oc62dbdb2003-05-05 19:50:49 -0400287 if (data->outfile && data->outfile != stderr)
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400288 fclose(data->outfile);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400289
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400290 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000291 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400292 ext2fs_free_mem(&channel->name);
293 ext2fs_free_mem(&channel);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000294 return retval;
295}
296
297static errcode_t test_set_blksize(io_channel channel, int blksize)
298{
299 struct test_private_data *data;
300 errcode_t retval = 0;
301
302 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
303 data = (struct test_private_data *) channel->private_data;
304 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
305
Theodore Ts'o0a05b902012-05-07 12:56:07 -0400306 if (data->real) {
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000307 retval = io_channel_set_blksize(data->real, blksize);
Theodore Ts'o0a05b902012-05-07 12:56:07 -0400308 channel->align = data->real->align;
309 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000310 if (data->set_blksize)
311 data->set_blksize(blksize, retval);
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400312 if (data->flags & TEST_FLAG_SET_BLKSIZE)
313 fprintf(data->outfile,
314 "Test_io: set_blksize(%d) returned %s\n",
315 blksize, retval ? error_message(retval) : "OK");
316 channel->block_size = blksize;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000317 return retval;
318}
319
320
321static errcode_t test_read_blk(io_channel channel, unsigned long block,
322 int count, void *buf)
323{
324 struct test_private_data *data;
325 errcode_t retval = 0;
326
327 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
328 data = (struct test_private_data *) channel->private_data;
329 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
330
331 if (data->real)
332 retval = io_channel_read_blk(data->real, block, count, buf);
333 if (data->read_blk)
334 data->read_blk(block, count, retval);
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400335 if (data->flags & TEST_FLAG_READ)
336 fprintf(data->outfile,
337 "Test_io: read_blk(%lu, %d) returned %s\n",
338 block, count, retval ? error_message(retval) : "OK");
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400339 if (data->block && data->block == block) {
340 if (data->flags & TEST_FLAG_DUMP)
341 test_dump_block(channel, data, block, buf);
342 if (--data->read_abort_count == 0)
343 test_abort(channel, block);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400344 }
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000345 return retval;
346}
347
348static errcode_t test_write_blk(io_channel channel, unsigned long block,
349 int count, const void *buf)
350{
351 struct test_private_data *data;
352 errcode_t retval = 0;
353
354 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
355 data = (struct test_private_data *) channel->private_data;
356 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
357
358 if (data->real)
359 retval = io_channel_write_blk(data->real, block, count, buf);
Theodore Ts'of20d0d52000-12-05 03:53:41 +0000360 if (data->write_blk)
361 data->write_blk(block, count, retval);
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400362 if (data->flags & TEST_FLAG_WRITE)
363 fprintf(data->outfile,
364 "Test_io: write_blk(%lu, %d) returned %s\n",
365 block, count, retval ? error_message(retval) : "OK");
Theodore Ts'oa002e7e2004-05-26 21:04:07 -0400366 if (data->block && data->block == block) {
367 if (data->flags & TEST_FLAG_DUMP)
368 test_dump_block(channel, data, block, buf);
369 if (--data->write_abort_count == 0)
370 test_abort(channel, block);
371 }
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000372 return retval;
373}
374
Theodore Ts'oc7015e42008-05-24 19:17:01 -0400375static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
376 int count, void *buf)
377{
378 struct test_private_data *data;
379 errcode_t retval = 0;
380
381 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
382 data = (struct test_private_data *) channel->private_data;
383 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
384
385 if (data->real)
386 retval = io_channel_read_blk64(data->real, block, count, buf);
387 if (data->read_blk64)
388 data->read_blk64(block, count, retval);
389 if (data->flags & TEST_FLAG_READ)
390 fprintf(data->outfile,
391 "Test_io: read_blk64(%llu, %d) returned %s\n",
392 block, count, retval ? error_message(retval) : "OK");
393 if (data->block && data->block == block) {
394 if (data->flags & TEST_FLAG_DUMP)
395 test_dump_block(channel, data, block, buf);
396 if (--data->read_abort_count == 0)
397 test_abort(channel, block);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400398 }
Theodore Ts'oc7015e42008-05-24 19:17:01 -0400399 return retval;
400}
401
402static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
403 int count, const void *buf)
404{
405 struct test_private_data *data;
406 errcode_t retval = 0;
407
408 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
409 data = (struct test_private_data *) channel->private_data;
410 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
411
412 if (data->real)
413 retval = io_channel_write_blk64(data->real, block, count, buf);
414 if (data->write_blk64)
415 data->write_blk64(block, count, retval);
416 if (data->flags & TEST_FLAG_WRITE)
417 fprintf(data->outfile,
418 "Test_io: write_blk64(%llu, %d) returned %s\n",
419 block, count, retval ? error_message(retval) : "OK");
420 if (data->block && data->block == block) {
421 if (data->flags & TEST_FLAG_DUMP)
422 test_dump_block(channel, data, block, buf);
423 if (--data->write_abort_count == 0)
424 test_abort(channel, block);
425 }
426 return retval;
427}
428
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000429static errcode_t test_write_byte(io_channel channel, unsigned long offset,
430 int count, const void *buf)
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 if (data->real && data->real->manager->write_byte)
440 retval = io_channel_write_byte(data->real, offset, count, buf);
441 if (data->write_byte)
442 data->write_byte(offset, count, retval);
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400443 if (data->flags & TEST_FLAG_WRITE)
444 fprintf(data->outfile,
445 "Test_io: write_byte(%lu, %d) returned %s\n",
446 offset, count, retval ? error_message(retval) : "OK");
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000447 return retval;
448}
449
450/*
451 * Flush data buffers to disk.
452 */
453static errcode_t test_flush(io_channel channel)
454{
455 struct test_private_data *data;
Theodore Ts'o9abd2ce1998-02-16 22:00:37 +0000456 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400457
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000458 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
459 data = (struct test_private_data *) channel->private_data;
460 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
461
462 if (data->real)
Theodore Ts'o9abd2ce1998-02-16 22:00:37 +0000463 retval = io_channel_flush(data->real);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400464
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400465 if (data->flags & TEST_FLAG_FLUSH)
466 fprintf(data->outfile, "Test_io: flush() returned %s\n",
467 retval ? error_message(retval) : "OK");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400468
Theodore Ts'o9abd2ce1998-02-16 22:00:37 +0000469 return retval;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000470}
471
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400472static errcode_t test_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500473 const char *arg)
474{
475 struct test_private_data *data;
476 errcode_t retval = 0;
477
478 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
479 data = (struct test_private_data *) channel->private_data;
480 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
481
482
483 if (data->flags & TEST_FLAG_SET_OPTION)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400484 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500485 option, arg);
486 if (data->real && data->real->manager->set_option) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400487 retval = (data->real->manager->set_option)(data->real,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500488 option, arg);
489 if (data->flags & TEST_FLAG_SET_OPTION)
490 fprintf(data->outfile, "returned %s\n",
491 retval ? error_message(retval) : "OK");
492 } else {
493 if (data->flags & TEST_FLAG_SET_OPTION)
494 fprintf(data->outfile, "not implemented\n");
495 }
496 return retval;
497}
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400498
499static errcode_t test_get_stats(io_channel channel, io_stats *stats)
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 && data->real->manager->get_stats) {
509 retval = (data->real->manager->get_stats)(data->real, stats);
510 }
511 return retval;
512}
Lukas Czernere90a59e2010-11-18 03:38:36 +0000513
514static errcode_t test_discard(io_channel channel, unsigned long long block,
515 unsigned long long count)
516{
517 struct test_private_data *data;
518 errcode_t retval = 0;
519
520 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
521 data = (struct test_private_data *) channel->private_data;
522 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
523
Lukas Czernerefa1a352010-11-18 03:38:38 +0000524 if (data->real)
525 retval = io_channel_discard(data->real, block, count);
Lukas Czernere90a59e2010-11-18 03:38:36 +0000526 if (data->flags & TEST_FLAG_DISCARD)
527 fprintf(data->outfile,
528 "Test_io: discard(%llu, %llu) returned %s\n",
529 block, count, retval ? error_message(retval) : "OK");
530 return retval;
531}