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.h |
39 | * @brief Superblock operations. |
40 | */ |
41 | |
42 | #include <ext4_config.h> |
43 | #include <ext4_types.h> |
44 | #include <ext4_misc.h> |
45 | #include <ext4_errno.h> |
46 | #include <ext4_debug.h> |
47 | |
48 | #include <ext4_super.h> |
49 | #include <ext4_crc32.h> |
50 | |
51 | uint32_t ext4_block_group_cnt(struct ext4_sblock *s) |
52 | { |
53 | uint64_t blocks_count = ext4_sb_get_blocks_cnt(s); |
54 | uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); |
55 | |
56 | uint32_t block_groups_count = (uint32_t)(blocks_count / blocks_per_group); |
57 | |
58 | if (blocks_count % blocks_per_group) |
59 | block_groups_count++; |
60 | |
61 | return block_groups_count; |
62 | } |
63 | |
64 | uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid) |
65 | { |
66 | uint32_t block_group_count = ext4_block_group_cnt(s); |
67 | uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); |
68 | uint64_t total_blocks = ext4_sb_get_blocks_cnt(s); |
69 | |
70 | if (bgid < block_group_count - 1) |
71 | return blocks_per_group; |
72 | |
73 | return (uint32_t)(total_blocks - ((block_group_count - 1) * blocks_per_group)); |
74 | } |
75 | |
76 | uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid) |
77 | { |
78 | uint32_t block_group_count = ext4_block_group_cnt(s); |
79 | uint32_t inodes_per_group = ext4_get32(s, inodes_per_group); |
80 | uint32_t total_inodes = ext4_get32(s, inodes_count); |
81 | |
82 | if (bgid < block_group_count - 1) |
83 | return inodes_per_group; |
84 | |
85 | return (total_inodes - ((block_group_count - 1) * inodes_per_group)); |
86 | } |
87 | |
88 | #if CONFIG_META_CSUM_ENABLE |
89 | static uint32_t ext4_sb_csum(struct ext4_sblock *s) |
90 | { |
91 | |
92 | return ext4_crc32c(EXT4_CRC32_INIT, buf: s, |
93 | offsetof(struct ext4_sblock, checksum)); |
94 | } |
95 | #else |
96 | #define ext4_sb_csum(...) 0 |
97 | #endif |
98 | |
99 | static bool ext4_sb_verify_csum(struct ext4_sblock *s) |
100 | { |
101 | if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM)) |
102 | return true; |
103 | |
104 | if (s->checksum_type != to_le32(EXT4_CHECKSUM_CRC32C)) |
105 | return false; |
106 | |
107 | return s->checksum == to_le32(ext4_sb_csum(s)); |
108 | } |
109 | |
110 | static void ext4_sb_set_csum(struct ext4_sblock *s) |
111 | { |
112 | if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM)) |
113 | return; |
114 | |
115 | s->checksum = to_le32(ext4_sb_csum(s)); |
116 | } |
117 | |
118 | int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s) |
119 | { |
120 | ext4_sb_set_csum(s); |
121 | return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET, buf: s, |
122 | EXT4_SUPERBLOCK_SIZE); |
123 | } |
124 | |
125 | int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s) |
126 | { |
127 | return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET, buf: s, |
128 | EXT4_SUPERBLOCK_SIZE); |
129 | } |
130 | |
131 | bool ext4_sb_check(struct ext4_sblock *s) |
132 | { |
133 | if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC) |
134 | return false; |
135 | |
136 | if (ext4_get32(s, inodes_count) == 0) |
137 | return false; |
138 | |
139 | if (ext4_sb_get_blocks_cnt(s) == 0) |
140 | return false; |
141 | |
142 | if (ext4_get32(s, blocks_per_group) == 0) |
143 | return false; |
144 | |
145 | if (ext4_get32(s, inodes_per_group) == 0) |
146 | return false; |
147 | |
148 | if (ext4_get16(s, inode_size) < 128) |
149 | return false; |
150 | |
151 | if (ext4_get32(s, first_inode) < 11) |
152 | return false; |
153 | |
154 | if (ext4_sb_get_desc_size(s) < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) |
155 | return false; |
156 | |
157 | if (ext4_sb_get_desc_size(s) > EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) |
158 | return false; |
159 | |
160 | if (!ext4_sb_verify_csum(s)) |
161 | return false; |
162 | |
163 | return true; |
164 | } |
165 | |
166 | static inline int is_power_of(uint32_t a, uint32_t b) |
167 | { |
168 | while (1) { |
169 | if (a < b) |
170 | return 0; |
171 | if (a == b) |
172 | return 1; |
173 | if ((a % b) != 0) |
174 | return 0; |
175 | a = a / b; |
176 | } |
177 | } |
178 | |
179 | bool ext4_sb_sparse(uint32_t group) |
180 | { |
181 | if (group <= 1) |
182 | return 1; |
183 | |
184 | if (!(group & 1)) |
185 | return 0; |
186 | |
187 | return (is_power_of(a: group, b: 7) || is_power_of(a: group, b: 5) || |
188 | is_power_of(a: group, b: 3)); |
189 | } |
190 | |
191 | bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t group) |
192 | { |
193 | if (ext4_sb_feature_ro_com(s, EXT4_FRO_COM_SPARSE_SUPER) && |
194 | !ext4_sb_sparse(group)) |
195 | return false; |
196 | return true; |
197 | } |
198 | |
199 | static uint32_t ext4_bg_num_gdb_meta(struct ext4_sblock *s, uint32_t group) |
200 | { |
201 | uint32_t dsc_per_block = |
202 | ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); |
203 | |
204 | uint32_t metagroup = group / dsc_per_block; |
205 | uint32_t first = metagroup * dsc_per_block; |
206 | uint32_t last = first + dsc_per_block - 1; |
207 | |
208 | if (group == first || group == first + 1 || group == last) |
209 | return 1; |
210 | return 0; |
211 | } |
212 | |
213 | static uint32_t ext4_bg_num_gdb_nometa(struct ext4_sblock *s, uint32_t group) |
214 | { |
215 | if (!ext4_sb_is_super_in_bg(s, group)) |
216 | return 0; |
217 | uint32_t dsc_per_block = |
218 | ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); |
219 | |
220 | uint32_t db_count = |
221 | (ext4_block_group_cnt(s) + dsc_per_block - 1) / dsc_per_block; |
222 | |
223 | if (ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG)) |
224 | return ext4_sb_first_meta_bg(s); |
225 | |
226 | return db_count; |
227 | } |
228 | |
229 | uint32_t ext4_bg_num_gdb(struct ext4_sblock *s, uint32_t group) |
230 | { |
231 | uint32_t dsc_per_block = |
232 | ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); |
233 | uint32_t first_meta_bg = ext4_sb_first_meta_bg(s); |
234 | uint32_t metagroup = group / dsc_per_block; |
235 | |
236 | if (!ext4_sb_feature_incom(s,EXT4_FINCOM_META_BG) || |
237 | metagroup < first_meta_bg) |
238 | return ext4_bg_num_gdb_nometa(s, group); |
239 | |
240 | return ext4_bg_num_gdb_meta(s, group); |
241 | } |
242 | |
243 | uint32_t ext4_num_base_meta_clusters(struct ext4_sblock *s, |
244 | uint32_t block_group) |
245 | { |
246 | uint32_t num; |
247 | uint32_t dsc_per_block = |
248 | ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); |
249 | |
250 | num = ext4_sb_is_super_in_bg(s, group: block_group); |
251 | |
252 | if (!ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG) || |
253 | block_group < ext4_sb_first_meta_bg(s) * dsc_per_block) { |
254 | if (num) { |
255 | num += ext4_bg_num_gdb(s, group: block_group); |
256 | num += ext4_get16(s, s_reserved_gdt_blocks); |
257 | } |
258 | } else { |
259 | num += ext4_bg_num_gdb(s, group: block_group); |
260 | } |
261 | |
262 | uint32_t clustersize = 1024 << ext4_get32(s, log_cluster_size); |
263 | uint32_t cluster_ratio = clustersize / ext4_sb_get_block_size(s); |
264 | uint32_t v = |
265 | (num + cluster_ratio - 1) >> ext4_get32(s, log_cluster_size); |
266 | |
267 | return v; |
268 | } |
269 | |
270 | /** |
271 | * @} |
272 | */ |
273 | |