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 cache.*/
78 struct ext4_bcache *bc;
80 /**@brief Block size (bytes): physical*/
83 /**@brief Block count: physical.*/
86 /**@brief Block size buffer: physical.*/
89 /**@brief Block size (bytes) logical*/
92 /**@brief Block count: phisical.*/
95 /**@brief Flags of te block device.*/
98 /**@brief Cache write back mode refference counter.*/
99 uint32_t cache_write_back;
101 /**@brief Physical read counter*/
104 /**@brief Physical write counter*/
109 /**@brief Static initialization fo the block device.*/
110 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, \
111 __bread, __bwrite, __close) \
112 static uint8_t __name##_ph_bbuf[(__bsize)]; \
113 static struct ext4_blockdev __name = { \
116 .bwrite = __bwrite, \
118 .ph_bsize = __bsize, \
120 .ph_bbuf = __name##_ph_bbuf, \
123 /**@brief Block device initialization.
124 * @param bdev block device descriptor
125 * @param bg_bsize logical block size
126 * @param bdev block device descriptor
127 * @return standard error code*/
128 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,
136 struct ext4_bcache *bc);
138 /**@brief Close block device
139 * @param bdev block device descriptor
140 * @return standard error code*/
141 int ext4_block_fini(struct ext4_blockdev *bdev);
144 /**@brief Set logical block size in block device.
145 * @param bdev block device descriptor
146 * @param lb_size ligical block size (in bytes)
147 * @return standard error code*/
148 void ext4_block_set_lb_size(struct ext4_blockdev *bdev,
151 /**@brief Block get function (through cache).
152 * @param bdev block device descriptor
153 * @param b block descriptor
154 * @param lba logical block address
155 * @return standard error code*/
156 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
159 /**@brief Block set procedure (through cache).
160 * @param bdev block device descriptor
161 * @param b block descriptor
162 * @return standard error code*/
163 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
166 /**@brief Block read procedure (without cache)
167 * @param bdev block device descriptor
168 * @param buf output buffer
169 * @param lba logical block adderss
170 * @return standard error code*/
171 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf,
172 uint64_t lba, uint32_t cnt);
175 /**@brief Block write procedure (without cache)
176 * @param bdev block device descriptor
177 * @param buf output buffer
178 * @param lba logical block address
179 * @return standard error code*/
180 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
181 uint64_t lba, uint32_t cnt);
183 /**@brief Write to block device (by direct adress).
184 * @param bdev block device descriptor
185 * @param off byte offset in block device
186 * @param buf input buffer
187 * @param len length of the write nuffer
188 * @return EOK when sucess*/
189 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
190 const void *buf, uint32_t len);
194 /**@brief Read freom block device (by direct adress).
195 * @param bdev block device descriptor
196 * @param off byte offset in block device
197 * @param buf input buffer
198 * @param len length of the write nuffer
199 * @return EOK when sucess*/
200 int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off,
201 void *buf, uint32_t len);
203 /**@brief Enable/disable write back cache mode
204 * @param bdev block device descriptor
207 * 0 - DISABLE (all delayed cache buffers will be flushed)
208 * @return standard error code*/
209 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
211 #endif /* EXT4_BLOCKDEV_H_ */