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 #ifndef EXT4_BLOCKDEV_H_
29 #define EXT4_BLOCKDEV_H_
31 /** @addtogroup lwext4
35 * @file ext4_blockdev.h
36 * @brief Block device module.
39 #include <ext4_config.h>
40 #include <ext4_bcache.h>
41 #include <ext4_debug.h>
47 /**@brief Initialization status flag*/
48 #define EXT4_BDEV_INITIALIZED (1 << 0)
51 /**@brief Definiton of the simple block device.*/
52 struct ext4_blockdev {
54 /**@brief Open device function
55 * @param bdev block device.*/
56 int (*open)(struct ext4_blockdev *bdev);
58 /**@brief Block read function.
59 * @param bdev block device
60 * @param buf output buffer
61 * @param blk_id block id
62 * @param blk_cnt block count*/
63 int (*bread)(struct ext4_blockdev *bdev, void *buf,
64 uint64_t blk_id, uint32_t blk_cnt);
66 /**@brief Block write function.
67 * @param buf input buffer
68 * @param blk_id block id
69 * @param blk_cnt block count*/
70 int (*bwrite)(struct ext4_blockdev *bdev, const void *buf,
71 uint64_t blk_id, uint32_t blk_cnt);
73 /**@brief Close device function.
74 * @param bdev block device.*/
75 int (*close)(struct ext4_blockdev *bdev);
77 /**@brief Block size (bytes): physical*/
80 /**@brief Block count: physical.*/
83 /**@brief Block size buffer: physical.*/
86 /**@brief Block size (bytes) logical*/
89 /**@brief Block count: phisical.*/
92 /**@brief Flags of te block device.*/
95 /**@brief Block cache.*/
96 struct ext4_bcache *bc;
98 uint8_t cache_flush_delay;
105 /**@brief Static initialization fo the block device.*/
106 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, \
107 __bread, __bwrite, __close) \
108 static uint8_t __name##_ph_bbuf[(__bsize)]; \
109 static struct ext4_blockdev __name = { \
112 .bwrite = __bwrite, \
114 .ph_bsize = __bsize, \
116 .ph_bbuf = __name##_ph_bbuf, \
119 /**@brief Block device initialization.
120 * @param bdev block device descriptor
121 * @param bg_bsize logical block size
122 * @param bdev block device descriptor
123 * @return standard error code*/
124 int ext4_block_init(struct ext4_blockdev *bdev);
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,
132 struct ext4_bcache *bc);
134 /**@brief Close block device
135 * @param bdev block device descriptor
136 * @return standard error code*/
137 int ext4_block_fini(struct ext4_blockdev *bdev);
140 /**@brief Set logical block size in block device.
141 * @param bdev block device descriptor
142 * @param lb_size ligical block size (in bytes)
143 * @return standard error code*/
144 void ext4_block_set_lb_size(struct ext4_blockdev *bdev,
147 /**@brief Block get function (through cache).
148 * @param bdev block device descriptor
149 * @param b block descriptor
150 * @param lba logical block address
151 * @return standard error code*/
152 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
155 /**@brief Block set procedure (through cache).
156 * @param bdev block device descriptor
157 * @param b block descriptor
158 * @return standard error code*/
159 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
162 /**@brief Block read procedure (without cache)
163 * @param bdev block device descriptor
164 * @param buf output buffer
165 * @param lba logical block adderss
166 * @return standard error code*/
167 int ext4_block_get_direct(struct ext4_blockdev *bdev, void *buf,
171 /**@brief Block write procedure (without cache)
172 * @param bdev block device descriptor
173 * @param buf output buffer
174 * @param lba logical block address
175 * @return standard error code*/
176 int ext4_block_set_direct(struct ext4_blockdev *bdev, const void *buf,
179 /**@brief Write to block device (by direct adress).
180 * @param bdev block device descriptor
181 * @param off byte offset in block device
182 * @param buf input buffer
183 * @param len length of the write nuffer
184 * @return EOK when sucess*/
185 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
186 const void *buf, uint32_t len);
190 /**@brief Read freom block device (by direct adress).
191 * @param bdev block device descriptor
192 * @param off byte offset in block device
193 * @param buf input buffer
194 * @param len length of the write nuffer
195 * @return EOK when sucess*/
196 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off,
197 void *buf, uint32_t len);
199 /**@brief Enable/disable delayed cache flush mode.
200 * @param bdev block device descriptor
203 * 0 - DISABLE (all delayed cache buffers will be flushed)
204 * @return standard error code*/
205 int ext4_block_delay_cache_flush(struct ext4_blockdev *bdev, uint8_t on_off);
207 #endif /* EXT4_BLOCKDEV_H_ */