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"
45 #include "ext4_trans.h"
46 #include "ext4_debug.h"
51 struct ext4_blockdev_iface {
52 /**@brief Open device function
53 * @param bdev block device.*/
54 int (*open)(struct ext4_blockdev *bdev);
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,
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);
71 /**@brief Close device function.
72 * @param bdev block device.*/
73 int (*close)(struct ext4_blockdev *bdev);
75 /**@brief Lock block device. Required in multi partition mode
76 * operations. Not mandatory field.
77 * @param bdev block device.*/
78 int (*lock)(struct ext4_blockdev *bdev);
80 /**@brief Unlock block device. Required in multi partition mode
81 * operations. Not mandatory field.
82 * @param bdev block device.*/
83 int (*unlock)(struct ext4_blockdev *bdev);
85 /**@brief Block size (bytes): physical*/
88 /**@brief Block count: physical*/
91 /**@brief Block size buffer: physical*/
94 /**@brief Reference counter to block device interface*/
97 /**@brief Physical read counter*/
100 /**@brief Physical write counter*/
104 /**@brief Definition of the simple block device.*/
105 struct ext4_blockdev {
106 /**@brief Block device interface*/
107 struct ext4_blockdev_iface *bdif;
109 /**@brief Offset in bdif. For multi partition mode.*/
110 uint64_t part_offset;
112 /**@brief Part size in bdif. For multi partition mode.*/
115 /**@brief Block cache.*/
116 struct ext4_bcache *bc;
118 /**@brief Block size (bytes) logical*/
121 /**@brief Block count: logical*/
124 /**@brief Cache write back mode reference counter*/
125 uint32_t cache_write_back;
127 /**@brief The filesystem this block device belongs to. */
131 /**@brief Static initialization of the block device.*/
132 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\
133 __bwrite, __close, __lock, __unlock) \
134 static uint8_t __name##_ph_bbuf[(__bsize)]; \
135 static struct ext4_blockdev_iface __name##_iface = { \
138 .bwrite = __bwrite, \
141 .unlock = __unlock, \
142 .ph_bsize = __bsize, \
144 .ph_bbuf = __name##_ph_bbuf, \
146 static struct ext4_blockdev __name = { \
147 .bdif = &__name##_iface, \
149 .part_size = (__bcnt) * (__bsize), \
152 /**@brief Block device initialization.
153 * @param bdev block device descriptor
154 * @param bg_bsize logical block size
155 * @param bdev block device descriptor
156 * @return standard error code*/
157 int ext4_block_init(struct ext4_blockdev *bdev);
159 /**@brief Binds a bcache to block device.
160 * @param bdev block device descriptor
161 * @param bc block cache descriptor
162 * @return standard error code*/
163 int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
165 /**@brief Close block device
166 * @param bdev block device descriptor
167 * @return standard error code*/
168 int ext4_block_fini(struct ext4_blockdev *bdev);
170 /**@brief Flush data in given buffer to disk.
171 * @param bdev block device descriptor
173 * @return standard error code*/
174 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
176 /**@brief Flush data in buffer of given lba to disk,
177 * if that buffer exists in block cache.
178 * @param bdev block device descriptor
179 * @param lba logical block address
180 * @return standard error code*/
181 int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba);
183 /**@brief Set logical block size in block device.
184 * @param bdev block device descriptor
185 * @param lb_size logical block size (in bytes)
186 * @return standard error code*/
187 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
189 /**@brief Block get function (through cache, don't read).
190 * @param bdev block device descriptor
191 * @param b block descriptor
192 * @param lba logical block address
193 * @return standard error code*/
194 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
197 /**@brief Block get function (through cache).
198 * @param bdev block device descriptor
199 * @param b block descriptor
200 * @param lba logical block address
201 * @return standard error code*/
202 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
205 /**@brief Block set procedure (through cache).
206 * @param bdev block device descriptor
207 * @param b block descriptor
208 * @return standard error code*/
209 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
211 /**@brief Block read procedure (without cache)
212 * @param bdev block device descriptor
213 * @param buf output buffer
214 * @param lba logical block address
215 * @return standard error code*/
216 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
219 /**@brief Block write procedure (without cache)
220 * @param bdev block device descriptor
221 * @param buf output buffer
222 * @param lba logical block address
223 * @return standard error code*/
224 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
225 uint64_t lba, uint32_t cnt);
227 /**@brief Write to block device (by direct address).
228 * @param bdev block device descriptor
229 * @param off byte offset in block device
230 * @param buf input buffer
231 * @param len length of the write buffer
232 * @return standard error code*/
233 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
234 const void *buf, uint32_t len);
236 /**@brief Read freom block device (by direct address).
237 * @param bdev block device descriptor
238 * @param off byte offset in block device
239 * @param buf input buffer
240 * @param len length of the write buffer
241 * @return standard error code*/
242 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
245 /**@brief Flush all dirty buffers to disk
246 * @param bdev block device descriptor
247 * @return standard error code*/
248 int ext4_block_cache_flush(struct ext4_blockdev *bdev);
250 /**@brief Enable/disable write back cache mode
251 * @param bdev block device descriptor
254 * 0 - DISABLE (all delayed cache buffers will be flushed)
255 * @return standard error code*/
256 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
262 #endif /* EXT4_BLOCKDEV_H_ */