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_debug.h"
50 /**@brief Initialization status flag*/
51 #define EXT4_BDEV_INITIALIZED (1 << 0)
53 /**@brief Definition of the simple block device.*/
54 struct ext4_blockdev {
56 /**@brief Open device function
57 * @param bdev block device.*/
58 int (*open)(struct ext4_blockdev *bdev);
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,
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);
75 /**@brief Close device function.
76 * @param bdev block device.*/
77 int (*close)(struct ext4_blockdev *bdev);
79 /**@brief Block cache.*/
80 struct ext4_bcache *bc;
82 /**@brief Block size (bytes): physical*/
85 /**@brief Block count: physical*/
88 /**@brief Block size buffer: physical*/
91 /**@brief Block size (bytes) logical*/
94 /**@brief Block count: physical*/
97 /**@brief Flags of block device*/
100 /**@brief Cache write back mode reference counter*/
101 uint32_t cache_write_back;
103 /**@brief Physical read counter*/
106 /**@brief Physical write counter*/
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 = { \
117 .bwrite = __bwrite, \
119 .ph_bsize = __bsize, \
121 .ph_bbuf = __name##_ph_bbuf, \
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);
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);
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);
142 /**@brief Flush data in given buffer to disk.
143 * @param bdev block device descriptor
145 * @return standard error code*/
146 int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
148 /**@brief Set logical block size in block device.
149 * @param bdev block device descriptor
150 * @param lb_size logical block size (in bytes)
151 * @return standard error code*/
152 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
154 /**@brief Block get function (through cache, don't read).
155 * @param bdev block device descriptor
156 * @param b block descriptor
157 * @param lba logical block address
158 * @return standard error code*/
159 int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
162 /**@brief Block get function (through cache).
163 * @param bdev block device descriptor
164 * @param b block descriptor
165 * @param lba logical block address
166 * @return standard error code*/
167 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
170 /**@brief Block set procedure (through cache).
171 * @param bdev block device descriptor
172 * @param b block descriptor
173 * @return standard error code*/
174 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
176 /**@brief Block read procedure (without cache)
177 * @param bdev block device descriptor
178 * @param buf output buffer
179 * @param lba logical block address
180 * @return standard error code*/
181 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
184 /**@brief Block write procedure (without cache)
185 * @param bdev block device descriptor
186 * @param buf output buffer
187 * @param lba logical block address
188 * @return standard error code*/
189 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
190 uint64_t lba, uint32_t cnt);
192 /**@brief Write to block device (by direct address).
193 * @param bdev block device descriptor
194 * @param off byte offset in block device
195 * @param buf input buffer
196 * @param len length of the write buffer
197 * @return standard error code*/
198 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
199 const void *buf, uint32_t len);
201 /**@brief Read freom block device (by direct address).
202 * @param bdev block device descriptor
203 * @param off byte offset in block device
204 * @param buf input buffer
205 * @param len length of the write buffer
206 * @return standard error code*/
207 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
210 /**@brief Enable/disable write back cache mode
211 * @param bdev block device descriptor
214 * 0 - DISABLE (all delayed cache buffers will be flushed)
215 * @return standard error code*/
216 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
222 #endif /* EXT4_BLOCKDEV_H_ */