Pedanitic warning check fixes. Pointer arithmetic, anonymus unions etc
[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 cache.*/
78     struct  ext4_bcache *bc;
79
80     /**@brief   Block size (bytes): physical*/
81     uint32_t    ph_bsize;
82
83     /**@brief   Block count: physical.*/
84     uint64_t    ph_bcnt;
85
86     /**@brief   Block size buffer: physical.*/
87     uint8_t     *ph_bbuf;
88
89     /**@brief   Block size (bytes) logical*/
90     uint32_t    lg_bsize;
91
92     /**@brief   Block count: phisical.*/
93     uint64_t    lg_bcnt;
94
95     /**@brief   Flags of te block device.*/
96     uint32_t    flags;
97
98     /**@brief   Cache write back mode refference counter.*/
99     uint32_t    cache_write_back;
100
101     /**@brief   Physical read counter*/
102     uint32_t    bread_ctr;
103
104     /**@brief   Physical write counter*/
105     uint32_t    bwrite_ctr;
106 };
107
108
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 = {                        \
114                 .open   = __open,                                       \
115                 .bread  = __bread,                                      \
116                 .bwrite = __bwrite,                                     \
117                 .close  = __close,                                      \
118                 .ph_bsize  = __bsize,                                   \
119                 .ph_bcnt   = __bcnt,                                    \
120                 .ph_bbuf   = __name##_ph_bbuf,                          \
121         }
122
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);
129
130
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);
137
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);
142
143
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,
149     uint64_t lb_bsize);
150
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,
157     uint64_t lba);
158
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);
164
165
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);
173
174
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);
182
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);
191
192
193
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);
202
203 /**@brief   Enable/disable write back cache mode
204  * @param   bdev block device descriptor
205  * @param   on_off
206  *              !0 - ENABLE
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);
210
211 #endif /* EXT4_BLOCKDEV_H_ */
212
213 /**
214  * @}
215  */