Whamcloud - gitweb
e2fsprogs: Add discard function into struct_io_manager
[tools/e2fsprogs.git] / lib / ext2fs / ext2_io.h
1 /*
2  * io.h --- the I/O manager abstraction
3  *
4  * Copyright (C) 1993, 1994, 1995, 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 #ifndef _EXT2FS_EXT2_IO_H
13 #define _EXT2FS_EXT2_IO_H
14
15 /*
16  * ext2_loff_t is defined here since unix_io.c needs it.
17  */
18 #if defined(__GNUC__) || defined(HAS_LONG_LONG)
19 typedef long long       ext2_loff_t;
20 #else
21 typedef long            ext2_loff_t;
22 #endif
23
24 /* llseek.c */
25 ext2_loff_t ext2fs_llseek (int, ext2_loff_t, int);
26
27 typedef struct struct_io_manager *io_manager;
28 typedef struct struct_io_channel *io_channel;
29 typedef struct struct_io_stats *io_stats;
30
31 #define CHANNEL_FLAGS_WRITETHROUGH      0x01
32
33 struct struct_io_channel {
34         errcode_t       magic;
35         io_manager      manager;
36         char            *name;
37         int             block_size;
38         errcode_t       (*read_error)(io_channel channel,
39                                       unsigned long block,
40                                       int count,
41                                       void *data,
42                                       size_t size,
43                                       int actual_bytes_read,
44                                       errcode_t error);
45         errcode_t       (*write_error)(io_channel channel,
46                                        unsigned long block,
47                                        int count,
48                                        const void *data,
49                                        size_t size,
50                                        int actual_bytes_written,
51                                        errcode_t error);
52         int             refcount;
53         int             flags;
54         long            reserved[14];
55         void            *private_data;
56         void            *app_data;
57 };
58
59 struct struct_io_stats {
60         int                     num_fields;
61         int                     reserved;
62         unsigned long long      bytes_read;
63         unsigned long long      bytes_written;
64 };
65
66 struct struct_io_manager {
67         errcode_t magic;
68         const char *name;
69         errcode_t (*open)(const char *name, int flags, io_channel *channel);
70         errcode_t (*close)(io_channel channel);
71         errcode_t (*set_blksize)(io_channel channel, int blksize);
72         errcode_t (*read_blk)(io_channel channel, unsigned long block,
73                               int count, void *data);
74         errcode_t (*write_blk)(io_channel channel, unsigned long block,
75                                int count, const void *data);
76         errcode_t (*flush)(io_channel channel);
77         errcode_t (*write_byte)(io_channel channel, unsigned long offset,
78                                 int count, const void *data);
79         errcode_t (*set_option)(io_channel channel, const char *option,
80                                 const char *arg);
81         errcode_t (*get_stats)(io_channel channel, io_stats *io_stats);
82         errcode_t (*read_blk64)(io_channel channel, unsigned long long block,
83                                         int count, void *data);
84         errcode_t (*write_blk64)(io_channel channel, unsigned long long block,
85                                         int count, const void *data);
86         errcode_t (*discard)(io_channel channel, unsigned long long block,
87                              unsigned long long count);
88         long    reserved[16];
89 };
90
91 #define IO_FLAG_RW              0x0001
92 #define IO_FLAG_EXCLUSIVE       0x0002
93 #define IO_FLAG_DIRECT_IO       0x0004
94
95 /*
96  * Convenience functions....
97  */
98 #define io_channel_close(c)             ((c)->manager->close((c)))
99 #define io_channel_set_blksize(c,s)     ((c)->manager->set_blksize((c),s))
100 #define io_channel_read_blk(c,b,n,d)    ((c)->manager->read_blk((c),b,n,d))
101 #define io_channel_write_blk(c,b,n,d)   ((c)->manager->write_blk((c),b,n,d))
102 #define io_channel_flush(c)             ((c)->manager->flush((c)))
103 #define io_channel_bumpcount(c)         ((c)->refcount++)
104
105 /* io_manager.c */
106 extern errcode_t io_channel_set_options(io_channel channel,
107                                         const char *options);
108 extern errcode_t io_channel_write_byte(io_channel channel,
109                                        unsigned long offset,
110                                        int count, const void *data);
111 extern errcode_t io_channel_read_blk64(io_channel channel,
112                                        unsigned long long block,
113                                        int count, void *data);
114 extern errcode_t io_channel_write_blk64(io_channel channel,
115                                         unsigned long long block,
116                                         int count, const void *data);
117
118 /* unix_io.c */
119 extern io_manager unix_io_manager;
120
121 /* undo_io.c */
122 extern io_manager undo_io_manager;
123 extern errcode_t set_undo_io_backing_manager(io_manager manager);
124 extern errcode_t set_undo_io_backup_file(char *file_name);
125
126 /* test_io.c */
127 extern io_manager test_io_manager, test_io_backing_manager;
128 extern void (*test_io_cb_read_blk)
129         (unsigned long block, int count, errcode_t err);
130 extern void (*test_io_cb_write_blk)
131         (unsigned long block, int count, errcode_t err);
132 extern void (*test_io_cb_read_blk64)
133         (unsigned long long block, int count, errcode_t err);
134 extern void (*test_io_cb_write_blk64)
135         (unsigned long long block, int count, errcode_t err);
136 extern void (*test_io_cb_set_blksize)
137         (int blksize, errcode_t err);
138
139 #endif /* _EXT2FS_EXT2_IO_H */
140