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