Reworks on buffer management
[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 #ifndef EXT4_BLOCKDEV_H_
29 #define EXT4_BLOCKDEV_H_
30
31 /** @addtogroup lwext4
32  * @{
33  */
34 /**
35  * @file  ext4_blockdev.h
36  * @brief Block device module.
37  */
38
39 #include "ext4_config.h"
40 #include "ext4_bcache.h"
41 #include "ext4_debug.h"
42
43 #include <stdbool.h>
44 #include <stdint.h>
45
46 /**@brief   Initialization status flag*/
47 #define EXT4_BDEV_INITIALIZED (1 << 0)
48
49 /**@brief   Definition of the simple block device.*/
50 struct ext4_blockdev {
51
52         /**@brief   Open device function
53          * @param   bdev block device.*/
54         int (*open)(struct ext4_blockdev *bdev);
55
56         /**@brief   Block read function.
57          * @param   bdev block device
58          * @param   buf output buffer
59          * @param   blk_id block id
60          * @param   blk_cnt block count*/
61         int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
62                      uint32_t blk_cnt);
63
64         /**@brief   Block write function.
65          * @param   buf input buffer
66          * @param   blk_id block id
67          * @param   blk_cnt block count*/
68         int (*bwrite)(struct ext4_blockdev *bdev, const void *buf,
69                       uint64_t blk_id, uint32_t blk_cnt);
70
71         /**@brief   Close device function.
72          * @param   bdev block device.*/
73         int (*close)(struct ext4_blockdev *bdev);
74
75         /**@brief   Block cache.*/
76         struct ext4_bcache *bc;
77
78         /**@brief   Block size (bytes): physical*/
79         uint32_t ph_bsize;
80
81         /**@brief   Block count: physical*/
82         uint64_t ph_bcnt;
83
84         /**@brief   Block size buffer: physical*/
85         uint8_t *ph_bbuf;
86
87         /**@brief   Block size (bytes) logical*/
88         uint32_t lg_bsize;
89
90         /**@brief   Block count: physical*/
91         uint64_t lg_bcnt;
92
93         /**@brief   Flags of block device*/
94         uint32_t flags;
95
96         /**@brief   Cache write back mode reference counter*/
97         uint32_t cache_write_back;
98
99         /**@brief   Physical read counter*/
100         uint32_t bread_ctr;
101
102         /**@brief   Physical write counter*/
103         uint32_t bwrite_ctr;
104 };
105
106 /**@brief   Static initialization of the block device.*/
107 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open,         \
108                                       __bread, __bwrite, __close)              \
109         static uint8_t __name##_ph_bbuf[(__bsize)];                            \
110         static struct ext4_blockdev __name = {                                 \
111             .open = __open,                                                    \
112             .bread = __bread,                                                  \
113             .bwrite = __bwrite,                                                \
114             .close = __close,                                                  \
115             .ph_bsize = __bsize,                                               \
116             .ph_bcnt = __bcnt,                                                 \
117             .ph_bbuf = __name##_ph_bbuf,                                       \
118         }
119
120 /**@brief   Block device initialization.
121  * @param   bdev block device descriptor
122  * @param   bg_bsize logical block size
123  * @param   bdev block device descriptor
124  * @return  standard error code*/
125 int ext4_block_init(struct ext4_blockdev *bdev);
126
127 /**@brief   Binds a bcache to block device.
128  * @param   bdev block device descriptor
129  * @param   bc block cache descriptor
130  * @return  standard error code*/
131 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
132
133 /**@brief   Close block device
134  * @param   bdev block device descriptor
135  * @return  standard error code*/
136 int ext4_block_fini(struct ext4_blockdev *bdev);
137
138 /**@brief   Set logical block size in block device.
139  * @param   bdev block device descriptor
140  * @param   lb_size logical block size (in bytes)
141  * @return  standard error code*/
142 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
143
144 /**@brief   Block get function (through cache, don't read).
145  * @param   bdev block device descriptor
146  * @param   b block descriptor
147  * @param   lba logical block address
148  * @return  standard error code*/
149 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
150                           uint64_t lba);
151
152 /**@brief   Block get function (through cache).
153  * @param   bdev block device descriptor
154  * @param   b block descriptor
155  * @param   lba logical block address
156  * @return  standard error code*/
157 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
158                    uint64_t lba);
159
160 /**@brief   Block set procedure (through cache).
161  * @param   bdev block device descriptor
162  * @param   b block descriptor
163  * @return  standard error code*/
164 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
165
166 /**@brief   Block read procedure (without cache)
167  * @param   bdev block device descriptor
168  * @param   buf output buffer
169  * @param   lba logical block address
170  * @return  standard error code*/
171 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
172                            uint32_t cnt);
173
174 /**@brief   Block write procedure (without cache)
175  * @param   bdev block device descriptor
176  * @param   buf output buffer
177  * @param   lba logical block address
178  * @return  standard error code*/
179 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
180                            uint64_t lba, uint32_t cnt);
181
182 /**@brief   Write to block device (by direct address).
183  * @param   bdev block device descriptor
184  * @param   off byte offset in block device
185  * @param   buf input buffer
186  * @param   len length of the write buffer
187  * @return  standard error code*/
188 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
189                           const void *buf, uint32_t len);
190
191 /**@brief   Read freom block device (by direct address).
192  * @param   bdev block device descriptor
193  * @param   off byte offset in block device
194  * @param   buf input buffer
195  * @param   len length of the write buffer
196  * @return  standard error code*/
197 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
198                          uint32_t len);
199
200 /**@brief   Enable/disable write back cache mode
201  * @param   bdev block device descriptor
202  * @param   on_off
203  *              !0 - ENABLE
204  *               0 - DISABLE (all delayed cache buffers will be flushed)
205  * @return  standard error code*/
206 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
207
208 #endif /* EXT4_BLOCKDEV_H_ */
209
210 /**
211  * @}
212  */