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"
46 /**@brief Initialization status flag*/
47 #define EXT4_BDEV_INITIALIZED (1 << 0)
49 /**@brief Definiton of the simple block device.*/
50 struct ext4_blockdev {
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, uint64_t blk_id,
71 /**@brief Close device function.
72 * @param bdev block device.*/
73 int (*close)(struct ext4_blockdev *bdev);
75 /**@brief Block cache.*/
76 struct ext4_bcache *bc;
78 /**@brief Block size (bytes): physical*/
81 /**@brief Block count: physical.*/
84 /**@brief Block size buffer: physical.*/
87 /**@brief Block size (bytes) logical*/
90 /**@brief Block count: phisical.*/
93 /**@brief Flags of te block device.*/
96 /**@brief Cache write back mode refference counter.*/
97 uint32_t cache_write_back;
99 /**@brief Physical read counter*/
102 /**@brief Physical write counter*/
106 /**@brief Static initialization fo the block device.*/
107 #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, \
108 __bread, __bwrite, __close) \
109 static uint8_t __name##_ph_bbuf[(__bsize)]; \
110 static struct ext4_blockdev __name = { \
113 .bwrite = __bwrite, \
115 .ph_bsize = __bsize, \
117 .ph_bbuf = __name##_ph_bbuf, \
120 /**@brief Block device initialization.
121 * @param bdev block device descriptor
122 * @param bg_bsize logical block size
123 * @param bdev block device descriptor
124 * @return standard error code*/
125 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, struct ext4_bcache *bc);
133 /**@brief Close block device
134 * @param bdev block device descriptor
135 * @return standard error code*/
136 int ext4_block_fini(struct ext4_blockdev *bdev);
138 /**@brief Set logical block size in block device.
139 * @param bdev block device descriptor
140 * @param lb_size ligical block size (in bytes)
141 * @return standard error code*/
142 void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint64_t lb_bsize);
144 /**@brief Block get function (through cache).
145 * @param bdev block device descriptor
146 * @param b block descriptor
147 * @param lba logical block address
148 * @return standard error code*/
149 int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
152 /**@brief Block set procedure (through cache).
153 * @param bdev block device descriptor
154 * @param b block descriptor
155 * @return standard error code*/
156 int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
158 /**@brief Block read procedure (without cache)
159 * @param bdev block device descriptor
160 * @param buf output buffer
161 * @param lba logical block adderss
162 * @return standard error code*/
163 int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
166 /**@brief Block write procedure (without cache)
167 * @param bdev block device descriptor
168 * @param buf output buffer
169 * @param lba logical block address
170 * @return standard error code*/
171 int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
172 uint64_t lba, uint32_t cnt);
174 /**@brief Write to block device (by direct adress).
175 * @param bdev block device descriptor
176 * @param off byte offset in block device
177 * @param buf input buffer
178 * @param len length of the write nuffer
179 * @return EOK when sucess*/
180 int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
181 const void *buf, uint32_t len);
183 /**@brief Read freom 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_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
192 /**@brief Enable/disable write back cache mode
193 * @param bdev block device descriptor
196 * 0 - DISABLE (all delayed cache buffers will be flushed)
197 * @return standard error code*/
198 int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
200 #endif /* EXT4_BLOCKDEV_H_ */