752647e1e7994cb52cee3895fe1d450f24f7a66b
[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 struct ext4_blockdev_iface {
51         /**@brief   Open device function
52          * @param   bdev block device.*/
53         int (*open)(struct ext4_blockdev *bdev);
54
55         /**@brief   Block read function.
56          * @param   bdev block device
57          * @param   buf output buffer
58          * @param   blk_id block id
59          * @param   blk_cnt block count*/
60         int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
61                      uint32_t blk_cnt);
62
63         /**@brief   Block write function.
64          * @param   buf input buffer
65          * @param   blk_id block id
66          * @param   blk_cnt block count*/
67         int (*bwrite)(struct ext4_blockdev *bdev, const void *buf,
68                       uint64_t blk_id, uint32_t blk_cnt);
69
70         /**@brief   Close device function.
71          * @param   bdev block device.*/
72         int (*close)(struct ext4_blockdev *bdev);
73
74         /**@brief   Lock block device. Required in multi partition mode
75          *          operations. Not mandatory field.
76          * @param   bdev block device.*/
77         int (*lock)(struct ext4_blockdev *bdev);
78
79         /**@brief   Unlock block device. Required in multi partition mode
80          *          operations. Not mandatory field.
81          * @param   bdev block device.*/
82         int (*unlock)(struct ext4_blockdev *bdev);
83
84         /**@brief   Block size (bytes): physical*/
85         uint32_t ph_bsize;
86
87         /**@brief   Block count: physical*/
88         uint64_t ph_bcnt;
89
90         /**@brief   Block size buffer: physical*/
91         uint8_t *ph_bbuf;
92
93         /**@brief   Reference counter to block device interface*/
94         uint32_t ph_refctr;
95 };
96
97 /**@brief   Definition of the simple block device.*/
98 struct ext4_blockdev {
99         /**@brief Block device interface*/
100         struct ext4_blockdev_iface *bdif;
101
102         /**@brief Offset in bdif. For multi partition mode.*/
103         uint64_t ph_blk_offset;
104
105         /**@brief   Block cache.*/
106         struct ext4_bcache *bc;
107
108         /**@brief   Block size (bytes) logical*/
109         uint32_t lg_bsize;
110
111         /**@brief   Block count: logical*/
112         uint64_t lg_bcnt;
113
114         /**@brief   Cache write back mode reference counter*/
115         uint32_t cache_write_back;
116
117         /**@brief   Physical read counter*/
118         uint32_t bread_ctr;
119
120         /**@brief   Physical write counter*/
121         uint32_t bwrite_ctr;
122 };
123
124 /**@brief   Static initialization of the block device.*/
125 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\
126                                       __bwrite, __close, __lock, __unlock)     \
127         static uint8_t __name##_ph_bbuf[(__bsize)];                            \
128         static struct ext4_blockdev_iface __name##_iface = {                   \
129                 .open = __open,                                                \
130                 .bread = __bread,                                              \
131                 .bwrite = __bwrite,                                            \
132                 .close = __close,                                              \
133                 .lock = __lock,                                                \
134                 .unlock = __unlock,                                            \
135                 .ph_bsize = __bsize,                                           \
136                 .ph_bcnt = __bcnt,                                             \
137                 .ph_bbuf = __name##_ph_bbuf,                                   \
138         };                                                                     \
139         static struct ext4_blockdev __name = {                                 \
140                 .bdif = &__name##_iface,                                       \
141         }
142
143 /**@brief   Block device initialization.
144  * @param   bdev block device descriptor
145  * @param   bg_bsize logical block size
146  * @param   bdev block device descriptor
147  * @return  standard error code*/
148 int ext4_block_init(struct ext4_blockdev *bdev);
149
150 /**@brief   Binds a bcache to block device.
151  * @param   bdev block device descriptor
152  * @param   bc block cache descriptor
153  * @return  standard error code*/
154 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
155
156 /**@brief   Close block device
157  * @param   bdev block device descriptor
158  * @return  standard error code*/
159 int ext4_block_fini(struct ext4_blockdev *bdev);
160
161 /**@brief   Flush data in given buffer to disk.
162  * @param   bdev block device descriptor
163  * @param   buf buffer
164  * @return  standard error code*/
165 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
166
167 /**@brief   Set logical block size in block device.
168  * @param   bdev block device descriptor
169  * @param   lb_size logical block size (in bytes)
170  * @return  standard error code*/
171 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
172
173 /**@brief   Block get function (through cache, don't read).
174  * @param   bdev block device descriptor
175  * @param   b block descriptor
176  * @param   lba logical block address
177  * @return  standard error code*/
178 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
179                           uint64_t lba);
180
181 /**@brief   Block get function (through cache).
182  * @param   bdev block device descriptor
183  * @param   b block descriptor
184  * @param   lba logical block address
185  * @return  standard error code*/
186 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
187                    uint64_t lba);
188
189 /**@brief   Block set procedure (through cache).
190  * @param   bdev block device descriptor
191  * @param   b block descriptor
192  * @return  standard error code*/
193 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
194
195 /**@brief   Block read procedure (without cache)
196  * @param   bdev block device descriptor
197  * @param   buf output buffer
198  * @param   lba logical block address
199  * @return  standard error code*/
200 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
201                            uint32_t cnt);
202
203 /**@brief   Block write procedure (without cache)
204  * @param   bdev block device descriptor
205  * @param   buf output buffer
206  * @param   lba logical block address
207  * @return  standard error code*/
208 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
209                            uint64_t lba, uint32_t cnt);
210
211 /**@brief   Write to block device (by direct address).
212  * @param   bdev block device descriptor
213  * @param   off byte offset in block device
214  * @param   buf input buffer
215  * @param   len length of the write buffer
216  * @return  standard error code*/
217 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
218                           const void *buf, uint32_t len);
219
220 /**@brief   Read freom block device (by direct address).
221  * @param   bdev block device descriptor
222  * @param   off byte offset in block device
223  * @param   buf input buffer
224  * @param   len length of the write buffer
225  * @return  standard error code*/
226 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
227                          uint32_t len);
228
229 /**@brief   Enable/disable write back cache mode
230  * @param   bdev block device descriptor
231  * @param   on_off
232  *              !0 - ENABLE
233  *               0 - DISABLE (all delayed cache buffers will be flushed)
234  * @return  standard error code*/
235 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
236
237 #ifdef __cplusplus
238 }
239 #endif
240
241 #endif /* EXT4_BLOCKDEV_H_ */
242
243 /**
244  * @}
245  */