ext4_blockdev: a number of changes below
[lwext4.git] / lwext4 / ext4_blockdev.h
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 /** @addtogroup lwext4
29  * @{
30  */
31 /**
32  * @file  ext4_blockdev.h
33  * @brief Block device module.
34  */
35
36 #ifndef EXT4_BLOCKDEV_H_
37 #define EXT4_BLOCKDEV_H_
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 #include "ext4_config.h"
44 #include "ext4_bcache.h"
45 #include "ext4_debug.h"
46
47 #include <stdbool.h>
48 #include <stdint.h>
49
50 /**@brief   Initialization status flag*/
51 #define EXT4_BDEV_INITIALIZED (1 << 0)
52
53 /**@brief   Definition of the simple block device.*/
54 struct ext4_blockdev {
55
56         /**@brief   Open device function
57          * @param   bdev block device.*/
58         int (*open)(struct ext4_blockdev *bdev);
59
60         /**@brief   Block read function.
61          * @param   bdev block device
62          * @param   buf output buffer
63          * @param   blk_id block id
64          * @param   blk_cnt block count*/
65         int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
66                      uint32_t blk_cnt);
67
68         /**@brief   Block write function.
69          * @param   buf input buffer
70          * @param   blk_id block id
71          * @param   blk_cnt block count*/
72         int (*bwrite)(struct ext4_blockdev *bdev, const void *buf,
73                       uint64_t blk_id, uint32_t blk_cnt);
74
75         /**@brief   Close device function.
76          * @param   bdev block device.*/
77         int (*close)(struct ext4_blockdev *bdev);
78
79         /**@brief   Block cache.*/
80         struct ext4_bcache *bc;
81
82         /**@brief   Block size (bytes): physical*/
83         uint32_t ph_bsize;
84
85         /**@brief   Block count: physical*/
86         uint64_t ph_bcnt;
87
88         /**@brief   Block size buffer: physical*/
89         uint8_t *ph_bbuf;
90
91         /**@brief   Block size (bytes) logical*/
92         uint32_t lg_bsize;
93
94         /**@brief   Block count: physical*/
95         uint64_t lg_bcnt;
96
97         /**@brief   Flags of block device*/
98         uint32_t flags;
99
100         /**@brief   Cache write back mode reference counter*/
101         uint32_t cache_write_back;
102
103         /**@brief   Physical read counter*/
104         uint32_t bread_ctr;
105
106         /**@brief   Physical write counter*/
107         uint32_t bwrite_ctr;
108 };
109
110 /**@brief   Static initialization of the block device.*/
111 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open,         \
112                                       __bread, __bwrite, __close)              \
113         static uint8_t __name##_ph_bbuf[(__bsize)];                            \
114         static struct ext4_blockdev __name = {                                 \
115             .open = __open,                                                    \
116             .bread = __bread,                                                  \
117             .bwrite = __bwrite,                                                \
118             .close = __close,                                                  \
119             .ph_bsize = __bsize,                                               \
120             .ph_bcnt = __bcnt,                                                 \
121             .ph_bbuf = __name##_ph_bbuf,                                       \
122         }
123
124 /**@brief   Block device initialization.
125  * @param   bdev block device descriptor
126  * @param   bg_bsize logical block size
127  * @param   bdev block device descriptor
128  * @return  standard error code*/
129 int ext4_block_init(struct ext4_blockdev *bdev);
130
131 /**@brief   Binds a bcache to block device.
132  * @param   bdev block device descriptor
133  * @param   bc block cache descriptor
134  * @return  standard error code*/
135 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
136
137 /**@brief   Close block device
138  * @param   bdev block device descriptor
139  * @return  standard error code*/
140 int ext4_block_fini(struct ext4_blockdev *bdev);
141
142 /**@brief   Flush data in given buffer to disk.
143  * @param   bdev block device descriptor
144  * @param   buf buffer
145  * @return  standard error code*/
146 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
147
148 /**@brief   Set logical block size in block device.
149  * @param   bdev block device descriptor
150  * @param   lb_size logical block size (in bytes)
151  * @return  standard error code*/
152 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
153
154 /**@brief   Block get function (through cache, don't read).
155  * @param   bdev block device descriptor
156  * @param   b block descriptor
157  * @param   lba logical block address
158  * @return  standard error code*/
159 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
160                           uint64_t lba);
161
162 /**@brief   Block get function (through cache).
163  * @param   bdev block device descriptor
164  * @param   b block descriptor
165  * @param   lba logical block address
166  * @return  standard error code*/
167 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
168                    uint64_t lba);
169
170 /**@brief   Block set procedure (through cache).
171  * @param   bdev block device descriptor
172  * @param   b block descriptor
173  * @return  standard error code*/
174 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
175
176 /**@brief   Block read procedure (without cache)
177  * @param   bdev block device descriptor
178  * @param   buf output buffer
179  * @param   lba logical block address
180  * @return  standard error code*/
181 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
182                            uint32_t cnt);
183
184 /**@brief   Block write procedure (without cache)
185  * @param   bdev block device descriptor
186  * @param   buf output buffer
187  * @param   lba logical block address
188  * @return  standard error code*/
189 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
190                            uint64_t lba, uint32_t cnt);
191
192 /**@brief   Write to block device (by direct address).
193  * @param   bdev block device descriptor
194  * @param   off byte offset in block device
195  * @param   buf input buffer
196  * @param   len length of the write buffer
197  * @return  standard error code*/
198 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
199                           const void *buf, uint32_t len);
200
201 /**@brief   Read freom block device (by direct address).
202  * @param   bdev block device descriptor
203  * @param   off byte offset in block device
204  * @param   buf input buffer
205  * @param   len length of the write buffer
206  * @return  standard error code*/
207 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
208                          uint32_t len);
209
210 /**@brief   Enable/disable write back cache mode
211  * @param   bdev block device descriptor
212  * @param   on_off
213  *              !0 - ENABLE
214  *               0 - DISABLE (all delayed cache buffers will be flushed)
215  * @return  standard error code*/
216 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif /* EXT4_BLOCKDEV_H_ */
223
224 /**
225  * @}
226  */