289a879e082f016b4582edf3c3d83c5005773bf6
[lwext4.git] / lwext4 / ext4_blockdev.h
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
16  *
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.
27  */
28 #ifndef EXT4_BLOCKDEV_H_
29 #define EXT4_BLOCKDEV_H_
30
31 /** @addtogroup lwext4
32  * @{
33  */
34 /**
35  * @file  ext4_blockdev.h
36  * @brief Block device module.
37  */
38
39 #include <ext4_config.h>
40 #include <ext4_bcache.h>
41 #include <ext4_debug.h>
42
43
44 #include <stdbool.h>
45 #include <stdint.h>
46
47 /**@brief       Initialization status flag*/
48 #define EXT4_BDEV_INITIALIZED                   (1 << 0)
49
50
51 /**@brief       Definiton of the simple block device.*/
52 struct  ext4_blockdev  {
53
54     /**@brief   Open device function
55      * @param   bdev block device.*/
56     int                 (*open)(struct  ext4_blockdev *bdev);
57
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);
65
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);
72
73     /**@brief   Close device function.
74      * @param   bdev block device.*/
75     int                 (*close)(struct ext4_blockdev *bdev);
76
77     /**@brief   Block size (bytes): physical*/
78     uint32_t    ph_bsize;
79
80     /**@brief   Block count: physical.*/
81     uint64_t    ph_bcnt;
82
83     /**@brief   Block size buffer: physical.*/
84     uint8_t             *ph_bbuf;
85
86     /**@brief   Block size (bytes) logical*/
87     uint32_t    lg_bsize;
88
89     /**@brief   Block count: phisical.*/
90     uint64_t    lg_bcnt;
91
92     /**@brief   Flags of te block device.*/
93     uint8_t             flags;
94
95     /**@brief   Block cache.*/
96     struct      ext4_bcache *bc;
97
98     uint8_t             cache_flush_delay;
99
100     uint32_t    bread_ctr;
101     uint32_t    bwrite_ctr;
102 };
103
104
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 = {                        \
110                 .open   = __open,                                       \
111                 .bread  = __bread,                                      \
112                 .bwrite = __bwrite,                                     \
113                 .close  = __close,                                      \
114                 .ph_bsize  = __bsize,                                   \
115                 .ph_bcnt   = __bcnt,                                    \
116                 .ph_bbuf   = __name##_ph_bbuf,                          \
117         }
118
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);
125
126
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);
133
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);
138
139
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,
145     uint64_t lb_bsize);
146
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,
153     uint64_t lba);
154
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);
160
161
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,
168     uint64_t lba);
169
170
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,
177     uint64_t lba);
178
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);
187
188
189
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);
198
199 /**@brief       Enable/disable delayed cache flush mode.
200  * @param       bdev block device descriptor
201  * @param       on_off
202  *                              !0      - ENABLE
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);
206
207 #endif /* EXT4_BLOCKDEV_H_ */
208
209 /**
210  * @}
211  */