Extra debug info
[lwext4.git] / lwext4 / ext4_super.h
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  *
4  *
5  * HelenOS:
6  * Copyright (c) 2012 Martin Sucha
7  * Copyright (c) 2012 Frantisek Princ
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * - Redistributions of source code must retain the above copyright
15  *   notice, this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright
17  *   notice, this list of conditions and the following disclaimer in the
18  *   documentation and/or other materials provided with the distribution.
19  * - The name of the author may not be used to endorse or promote products
20  *   derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /** @addtogroup lwext4
35  * @{
36  */
37 /**
38  * @file  ext4_super.c
39  * @brief Superblock operations.
40  */
41
42 #ifndef EXT4_SUPER_H_
43 #define EXT4_SUPER_H_
44
45
46 #include <ext4_config.h>
47 #include <ext4_types.h>
48
49
50
51 /**@brief   Blocks count get stored in superblock.
52  * @param   s superblock descriptor
53  * @return  count of blocks*/
54 static inline uint64_t ext4_sb_get_blocks_cnt(struct ext4_sblock *s)
55 {
56     return ((uint64_t) to_le32(s->blocks_count_hi) << 32) |
57             to_le32(s->blocks_count_lo);
58 }
59
60 /**@brief   Free blocks count get stored in superblock.
61  * @param   s superblock descriptor
62  * @return  free blocks*/
63 static inline uint64_t ext4_sb_get_free_blocks_cnt(struct ext4_sblock *s)
64 {
65     return ((uint64_t) to_le32(s->free_blocks_count_hi) << 32) |
66             to_le32(s->free_blocks_count_lo);
67 }
68
69 /**@brief   Free blocks count set.
70  * @param   s superblock descriptor
71  * @param   cnt new value of free blocks*/
72 static inline void ext4_sb_set_free_blocks_cnt(struct ext4_sblock *s,
73     uint64_t cnt)
74 {
75     s->free_blocks_count_lo = to_le32((cnt << 32) >> 32);
76     s->free_blocks_count_hi = to_le32(cnt >> 32);
77 }
78
79 /**@brief   Block size get from superblock.
80  * @param   s superblock descriptor
81  * @return  block size in bytes*/
82 static inline uint32_t ext4_sb_get_block_size(struct ext4_sblock *s)
83 {
84     return 1024 << to_le32(s->log_block_size);
85 }
86
87 /**@brief   Block group descriptor size.
88  * @param   s superblock descriptor
89  * @return  block group descriptor size in bytes*/
90 static inline uint16_t ext4_sb_get_desc_size(struct ext4_sblock *s)
91 {
92     uint16_t size = to_le16(s->desc_size);
93
94     return size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE ?
95             EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE : size;
96 }
97
98 /*************************Flags and features*********************************/
99
100 /**@brief   Support check of flag.
101  * @param   s superblock descriptor
102  * @param   v flag to check
103  * @return  true if flag is supported*/
104 static inline bool ext4_sb_check_flag(struct ext4_sblock *s, uint32_t v)
105 {
106     return to_le32(s->flags) & v;
107 }
108
109 /**@brief   Support check of feature compatible.
110  * @param   s superblock descriptor
111  * @param   v feature to check
112  * @return  true if feature is supported*/
113 static inline bool ext4_sb_has_feature_compatible(struct ext4_sblock *s,
114     uint32_t v)
115 {
116     return to_le32(s->features_compatible) & v;
117 }
118
119
120 /**@brief   Support check of feature incompatible.
121  * @param   s superblock descriptor
122  * @param   v feature to check
123  * @return  true if feature is supported*/
124 static inline bool ext4_sb_has_feature_incompatible(struct ext4_sblock *s,
125     uint32_t v)
126 {
127     return to_le32(s->features_incompatible) & v;
128 }
129
130
131 /**@brief   Support check of read only flag.
132  * @param   s superblock descriptor
133  * @param   v flag to check
134  * @return  true if flag is supported*/
135 static inline bool ext4_sb_has_feature_read_only(struct ext4_sblock *s,
136         uint32_t v)
137 {
138     return to_le32(s->features_read_only) & v;
139 }
140
141 /**@brief   Block group to flex group.
142  * @param   s superblock descriptor
143  * @param   block_group block group
144  * @return  flex group id*/
145 static inline uint32_t ext4_sb_bg_to_flex(struct ext4_sblock *s,
146                          uint32_t block_group)
147 {
148     return block_group >> to_le32(s->log_groups_per_flex);
149 }
150
151 /**@brief   Flex block group size.
152  * @param   s superblock descriptor
153  * @return  flex bg size*/
154 static inline uint32_t ext4_sb_flex_bg_size(struct ext4_sblock *s)
155 {
156     return 1 << to_le32(s->log_groups_per_flex);
157 }
158
159 /**@brief   Return first meta block group id.
160  * @param   s superblock descriptor
161  * @return  first meta_bg id */
162 static inline uint32_t ext4_sb_first_meta_bg(struct ext4_sblock *s)
163 {
164     return to_le32(s->first_meta_bg);
165 }
166
167 /**************************More complex functions****************************/
168
169 /**@brief   Returns a block group count.
170  * @param   s superblock descriptor
171  * @return  count of block groups*/
172 uint32_t ext4_block_group_cnt(struct ext4_sblock *s);
173
174 /**@brief   Returns block count in block group
175  *          (last block group may have less blocks)
176  * @param   s superblock descriptor
177  * @param   bgid block group id
178  * @return  blocks count*/
179 uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid);
180
181 /**@brief   Returns inodes count in block group
182  *          (last block group may have less inodes)
183  * @param   s superblock descriptor
184  * @param   bgid block group id
185  * @return  inodes count*/
186 uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid);
187
188 /***************************Read/write/check superblock**********************/
189
190 /**@brief   Superblock write.
191  * @param   bdev block device descriptor.
192  * @param   s superblock descruptor
193  * @return  Standard error code */
194 int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s);
195
196 /**@brief   Superblock read.
197  * @param   bdev block device descriptor.
198  * @param   s superblock descruptor
199  * @return  Standard error code */
200 int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s);
201
202 /**@brief   Superblock simple validation.
203  * @param   s superblock dsecriptor
204  * @return  true if OK*/
205 bool ext4_sb_check(struct ext4_sblock *s);
206
207 /**@brief   Superblock presence in block group.
208  * @param   s superblock dsecriptor
209  * @param   block_group block group id
210  * @return  true if block group has superblock*/
211 bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t block_group);
212
213
214 /**@brief   TODO:*/
215 uint32_t ext4_bg_num_gdb(struct ext4_sblock *s, uint32_t group);
216
217 /**@brief   TODO:*/
218 uint32_t ext4_num_base_meta_clusters(struct ext4_sblock *s,
219         uint32_t block_group);
220
221 #endif /* EXT4_SUPER_H_ */
222
223 /**
224  * @}
225  */
226