2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 /** @addtogroup lwext4
32 * @file ext4_blockdev.h
33 * @brief Block device module.
36 #ifndef EXT4_BLOCKDEV_H_
37 #define EXT4_BLOCKDEV_H_
43 #include <ext4_config.h>
44 #include <ext4_bcache.h>
49 struct ext4_blockdev_iface {
50 /**@brief Open device function
51 * @param bdev block device.*/
52 int (*open)(struct ext4_blockdev *bdev);
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,
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);
69 /**@brief Close device function.
70 * @param bdev block device.*/
71 int (*close)(struct ext4_blockdev *bdev);
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);
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);
83 /**@brief Block size (bytes): physical*/
86 /**@brief Block count: physical*/
89 /**@brief Block size buffer: physical*/
92 /**@brief Reference counter to block device interface*/
95 /**@brief Physical read counter*/
98 /**@brief Physical write counter*/
101 /**@brief User data pointer*/
105 /**@brief Definition of the simple block device.*/
106 struct ext4_blockdev {
107 /**@brief Block device interface*/
108 struct ext4_blockdev_iface *bdif;
110 /**@brief Offset in bdif. For multi partition mode.*/
111 uint64_t part_offset;
113 /**@brief Part size in bdif. For multi partition mode.*/
116 /**@brief Block cache.*/
117 struct ext4_bcache *bc;
119 /**@brief Block size (bytes) logical*/
122 /**@brief Block count: logical*/
125 /**@brief Cache write back mode reference counter*/
126 uint32_t cache_write_back;
128 /**@brief The filesystem this block device belongs to. */
134 /**@brief Static initialization of the block device.*/
135 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\
136 __bwrite, __close, __lock, __unlock) \
137 static uint8_t __name##_ph_bbuf[(__bsize)]; \
138 static struct ext4_blockdev_iface __name##_iface = { \
141 .bwrite = __bwrite, \
144 .unlock = __unlock, \
145 .ph_bsize = __bsize, \
147 .ph_bbuf = __name##_ph_bbuf, \
149 static struct ext4_blockdev __name = { \
150 .bdif = &__name##_iface, \
152 .part_size = (__bcnt) * (__bsize), \
155 /**@brief Block device initialization.
156 * @param bdev block device descriptor
157 * @param bg_bsize logical block size
158 * @param bdev block device descriptor
159 * @return standard error code*/
160 int ext4_block_init(struct ext4_blockdev *bdev);
162 /**@brief Binds a bcache to block device.
163 * @param bdev block device descriptor
164 * @param bc block cache descriptor
165 * @return standard error code*/
166 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
168 /**@brief Close block device
169 * @param bdev block device descriptor
170 * @return standard error code*/
171 int ext4_block_fini(struct ext4_blockdev *bdev);
173 /**@brief Flush data in given buffer to disk.
174 * @param bdev block device descriptor
176 * @return standard error code*/
177 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
179 /**@brief Flush data in buffer of given lba to disk,
180 * if that buffer exists in block cache.
181 * @param bdev block device descriptor
182 * @param lba logical block address
183 * @return standard error code*/
184 int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba);
186 /**@brief Set logical block size in block device.
187 * @param bdev block device descriptor
188 * @param lb_size logical block size (in bytes)
189 * @return standard error code*/
190 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize);
192 /**@brief Block get function (through cache, don't read).
193 * @param bdev block device descriptor
194 * @param b block descriptor
195 * @param lba logical block address
196 * @return standard error code*/
197 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
200 /**@brief Block get function (through cache).
201 * @param bdev block device descriptor
202 * @param b block descriptor
203 * @param lba logical block address
204 * @return standard error code*/
205 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
208 /**@brief Block set procedure (through cache).
209 * @param bdev block device descriptor
210 * @param b block descriptor
211 * @return standard error code*/
212 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
214 /**@brief Block read procedure (without cache)
215 * @param bdev block device descriptor
216 * @param buf output buffer
217 * @param lba logical block address
218 * @return standard error code*/
219 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
222 /**@brief Block write procedure (without cache)
223 * @param bdev block device descriptor
224 * @param buf output buffer
225 * @param lba logical block address
226 * @return standard error code*/
227 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
228 uint64_t lba, uint32_t cnt);
230 /**@brief Write to block device (by direct address).
231 * @param bdev block device descriptor
232 * @param off byte offset in block device
233 * @param buf input buffer
234 * @param len length of the write buffer
235 * @return standard error code*/
236 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
237 const void *buf, uint32_t len);
239 /**@brief Read freom block device (by direct address).
240 * @param bdev block device descriptor
241 * @param off byte offset in block device
242 * @param buf input buffer
243 * @param len length of the write buffer
244 * @return standard error code*/
245 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
248 /**@brief Flush all dirty buffers to disk
249 * @param bdev block device descriptor
250 * @return standard error code*/
251 int ext4_block_cache_flush(struct ext4_blockdev *bdev);
253 /**@brief Enable/disable write back cache mode
254 * @param bdev block device descriptor
257 * 0 - DISABLE (all delayed cache buffers will be flushed)
258 * @return standard error code*/
259 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
265 #endif /* EXT4_BLOCKDEV_H_ */