ext4_journal: two 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   Set logical block size in block device.
143  * @param   bdev block device descriptor
144  * @param   lb_size logical block size (in bytes)
145  * @return  standard error code*/
146 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
147
148 /**@brief   Block get function (through cache, don't read).
149  * @param   bdev block device descriptor
150  * @param   b block descriptor
151  * @param   lba logical block address
152  * @return  standard error code*/
153 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
154                           uint64_t lba);
155
156 /**@brief   Block get function (through cache).
157  * @param   bdev block device descriptor
158  * @param   b block descriptor
159  * @param   lba logical block address
160  * @return  standard error code*/
161 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
162                    uint64_t lba);
163
164 /**@brief   Block set procedure (through cache).
165  * @param   bdev block device descriptor
166  * @param   b block descriptor
167  * @return  standard error code*/
168 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
169
170 /**@brief   Block read procedure (without cache)
171  * @param   bdev block device descriptor
172  * @param   buf output buffer
173  * @param   lba logical block address
174  * @return  standard error code*/
175 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
176                            uint32_t cnt);
177
178 /**@brief   Block write procedure (without cache)
179  * @param   bdev block device descriptor
180  * @param   buf output buffer
181  * @param   lba logical block address
182  * @return  standard error code*/
183 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
184                            uint64_t lba, uint32_t cnt);
185
186 /**@brief   Write to block device (by direct address).
187  * @param   bdev block device descriptor
188  * @param   off byte offset in block device
189  * @param   buf input buffer
190  * @param   len length of the write buffer
191  * @return  standard error code*/
192 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
193                           const void *buf, uint32_t len);
194
195 /**@brief   Read freom block device (by direct address).
196  * @param   bdev block device descriptor
197  * @param   off byte offset in block device
198  * @param   buf input buffer
199  * @param   len length of the write buffer
200  * @return  standard error code*/
201 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
202                          uint32_t len);
203
204 /**@brief   Enable/disable write back cache mode
205  * @param   bdev block device descriptor
206  * @param   on_off
207  *              !0 - ENABLE
208  *               0 - DISABLE (all delayed cache buffers will be flushed)
209  * @return  standard error code*/
210 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
211
212 #ifdef __cplusplus
213 }
214 #endif
215
216 #endif /* EXT4_BLOCKDEV_H_ */
217
218 /**
219  * @}
220  */