1 | /*
|
---|
2 | * Copyright (c) 2011 Frantisek Princ
|
---|
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 |
|
---|
29 | /** @addtogroup libext4
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file libext4_superblock.c
|
---|
35 | * @brief Ext4 superblock operations.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <libblock.h>
|
---|
41 | #include <malloc.h>
|
---|
42 | #include "libext4.h"
|
---|
43 |
|
---|
44 | uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *sb)
|
---|
45 | {
|
---|
46 | return uint32_t_le2host(sb->inodes_count);
|
---|
47 | }
|
---|
48 |
|
---|
49 | uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *sb)
|
---|
50 | {
|
---|
51 | return ((uint64_t)uint32_t_le2host(sb->blocks_count_hi) << 32) |
|
---|
52 | uint32_t_le2host(sb->blocks_count_lo);
|
---|
53 | }
|
---|
54 |
|
---|
55 | uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *sb)
|
---|
56 | {
|
---|
57 | return ((uint64_t)uint32_t_le2host(sb->reserved_blocks_count_hi) << 32) |
|
---|
58 | uint32_t_le2host(sb->reserved_blocks_count_lo);
|
---|
59 | }
|
---|
60 |
|
---|
61 | uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *sb)
|
---|
62 | {
|
---|
63 | return ((uint64_t)uint32_t_le2host(sb->free_blocks_count_hi) << 32) |
|
---|
64 | uint32_t_le2host(sb->free_blocks_count_lo);
|
---|
65 | }
|
---|
66 |
|
---|
67 | void ext4_superblock_set_free_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
---|
68 | {
|
---|
69 | sb->free_blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
---|
70 | sb->free_blocks_count_hi = host2uint32_t_le(count >> 32);
|
---|
71 | }
|
---|
72 |
|
---|
73 | uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *sb)
|
---|
74 | {
|
---|
75 | return uint32_t_le2host(sb->free_inodes_count);
|
---|
76 | }
|
---|
77 |
|
---|
78 | uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
|
---|
79 | {
|
---|
80 | return uint32_t_le2host(sb->first_data_block);
|
---|
81 | }
|
---|
82 |
|
---|
83 | uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
|
---|
84 | {
|
---|
85 | return uint32_t_le2host(sb->log_block_size);
|
---|
86 | }
|
---|
87 |
|
---|
88 | uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
|
---|
89 | {
|
---|
90 | return 1024 << ext4_superblock_get_log_block_size(sb);
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
|
---|
95 | {
|
---|
96 | return uint32_t_le2host(sb->blocks_per_group);
|
---|
97 | }
|
---|
98 |
|
---|
99 | uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
|
---|
100 | {
|
---|
101 | return uint32_t_le2host(sb->inodes_per_group);
|
---|
102 | }
|
---|
103 |
|
---|
104 | uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
|
---|
105 | {
|
---|
106 | return uint32_t_le2host(sb->mount_time);
|
---|
107 | }
|
---|
108 |
|
---|
109 | uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
|
---|
110 | {
|
---|
111 | return uint32_t_le2host(sb->write_time);
|
---|
112 | }
|
---|
113 |
|
---|
114 | uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
|
---|
115 | {
|
---|
116 | return uint16_t_le2host(sb->mount_count);
|
---|
117 | }
|
---|
118 |
|
---|
119 | uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
|
---|
120 | {
|
---|
121 | return uint16_t_le2host(sb->max_mount_count);
|
---|
122 | }
|
---|
123 |
|
---|
124 | uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
|
---|
125 | {
|
---|
126 | return uint16_t_le2host(sb->magic);
|
---|
127 | }
|
---|
128 |
|
---|
129 | uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
|
---|
130 | {
|
---|
131 | return uint16_t_le2host(sb->state);
|
---|
132 | }
|
---|
133 |
|
---|
134 | uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
|
---|
135 | {
|
---|
136 | return uint16_t_le2host(sb->errors);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
|
---|
141 | {
|
---|
142 | return uint16_t_le2host(sb->minor_rev_level);
|
---|
143 | }
|
---|
144 |
|
---|
145 | uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
|
---|
146 | {
|
---|
147 | return uint32_t_le2host(sb->last_check_time);
|
---|
148 | }
|
---|
149 |
|
---|
150 | uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb){
|
---|
151 | return uint32_t_le2host(sb->check_interval);
|
---|
152 | }
|
---|
153 |
|
---|
154 | uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
|
---|
155 | {
|
---|
156 | return uint32_t_le2host(sb->creator_os);
|
---|
157 | }
|
---|
158 |
|
---|
159 | uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
|
---|
160 | {
|
---|
161 | return uint32_t_le2host(sb->rev_level);
|
---|
162 | }
|
---|
163 |
|
---|
164 | uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
|
---|
165 | {
|
---|
166 | if (ext4_superblock_get_rev_level(sb) == 0) {
|
---|
167 | return EXT4_REV0_INODE_SIZE;
|
---|
168 | }
|
---|
169 | return uint16_t_le2host(sb->inode_size);
|
---|
170 | }
|
---|
171 |
|
---|
172 | uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *sb)
|
---|
173 | {
|
---|
174 | return uint16_t_le2host(sb->block_group_number);
|
---|
175 | }
|
---|
176 |
|
---|
177 | uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
|
---|
178 | {
|
---|
179 | return uint32_t_le2host(sb->features_compatible);
|
---|
180 | }
|
---|
181 |
|
---|
182 | uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
|
---|
183 | {
|
---|
184 | return uint32_t_le2host(sb->features_incompatible);
|
---|
185 | }
|
---|
186 |
|
---|
187 | uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
|
---|
188 | {
|
---|
189 | return uint32_t_le2host(sb->features_read_only);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | uint32_t* ext4_superblock_get_hash_seed(ext4_superblock_t *sb)
|
---|
194 | {
|
---|
195 | return sb->hash_seed;
|
---|
196 | }
|
---|
197 |
|
---|
198 | uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *sb)
|
---|
199 | {
|
---|
200 | uint16_t size = uint16_t_le2host(sb->desc_size);
|
---|
201 |
|
---|
202 | if (size < EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
203 | size = EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE;
|
---|
204 | }
|
---|
205 |
|
---|
206 | return size;
|
---|
207 | }
|
---|
208 |
|
---|
209 | uint32_t ext4_superblock_get_flags(ext4_superblock_t *sb)
|
---|
210 | {
|
---|
211 | return uint32_t_le2host(sb->flags);
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * More complex superblock operations
|
---|
217 | */
|
---|
218 |
|
---|
219 | bool ext4_superblock_has_flag(ext4_superblock_t *sb, uint32_t flag)
|
---|
220 | {
|
---|
221 | if (ext4_superblock_get_flags(sb) & flag) {
|
---|
222 | return true;
|
---|
223 | }
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 |
|
---|
227 | // Feature checkers
|
---|
228 | bool ext4_superblock_has_feature_compatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
229 | {
|
---|
230 | if (ext4_superblock_get_features_compatible(sb) & feature) {
|
---|
231 | return true;
|
---|
232 | }
|
---|
233 | return false;
|
---|
234 | }
|
---|
235 |
|
---|
236 | bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
237 | {
|
---|
238 | if (ext4_superblock_get_features_incompatible(sb) & feature) {
|
---|
239 | return true;
|
---|
240 | }
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | bool ext4_superblock_has_feature_read_only(ext4_superblock_t *sb, uint32_t feature)
|
---|
245 | {
|
---|
246 | if (ext4_superblock_get_features_read_only(sb) & feature) {
|
---|
247 | return true;
|
---|
248 | }
|
---|
249 | return false;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | int ext4_superblock_read_direct(service_id_t service_id,
|
---|
254 | ext4_superblock_t **superblock)
|
---|
255 | {
|
---|
256 | void *data;
|
---|
257 | int rc;
|
---|
258 |
|
---|
259 | data = malloc(EXT4_SUPERBLOCK_SIZE);
|
---|
260 | if (data == NULL) {
|
---|
261 | return ENOMEM;
|
---|
262 | }
|
---|
263 |
|
---|
264 | rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
|
---|
265 | EXT4_SUPERBLOCK_SIZE, data);
|
---|
266 |
|
---|
267 | if (rc != EOK) {
|
---|
268 | free(data);
|
---|
269 | return rc;
|
---|
270 | }
|
---|
271 |
|
---|
272 | (*superblock) = data;
|
---|
273 |
|
---|
274 | return EOK;
|
---|
275 | }
|
---|
276 |
|
---|
277 | int ext4_superblock_write_direct(service_id_t service_id,
|
---|
278 | ext4_superblock_t *sb)
|
---|
279 | {
|
---|
280 | int rc;
|
---|
281 | uint32_t phys_block_size;
|
---|
282 | uint64_t first_block;
|
---|
283 | uint32_t block_count;
|
---|
284 |
|
---|
285 | rc = block_get_bsize(service_id, &phys_block_size);
|
---|
286 | if (rc != EOK) {
|
---|
287 | // TODO error
|
---|
288 | return rc;
|
---|
289 | }
|
---|
290 |
|
---|
291 | first_block = EXT4_SUPERBLOCK_OFFSET / phys_block_size;
|
---|
292 | block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
|
---|
293 |
|
---|
294 | if (EXT4_SUPERBLOCK_SIZE % phys_block_size) {
|
---|
295 | block_count++;
|
---|
296 | }
|
---|
297 |
|
---|
298 | return block_write_direct(service_id, first_block, block_count, sb);
|
---|
299 |
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | int ext4_superblock_check_sanity(ext4_superblock_t *sb)
|
---|
304 | {
|
---|
305 | if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC) {
|
---|
306 | return ENOTSUP;
|
---|
307 | }
|
---|
308 |
|
---|
309 | // TODO more checks !!!
|
---|
310 |
|
---|
311 | return EOK;
|
---|
312 | }
|
---|
313 |
|
---|
314 | uint32_t ext4_superblock_get_block_group_count(ext4_superblock_t *sb)
|
---|
315 | {
|
---|
316 | uint64_t blocks_count = ext4_superblock_get_blocks_count(sb);
|
---|
317 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
318 |
|
---|
319 | uint32_t block_groups_count = blocks_count / blocks_per_group;
|
---|
320 |
|
---|
321 | if (blocks_count % blocks_per_group) {
|
---|
322 | block_groups_count++;
|
---|
323 | }
|
---|
324 |
|
---|
325 | return block_groups_count;
|
---|
326 |
|
---|
327 | }
|
---|
328 |
|
---|
329 | uint32_t ext4_superblock_get_blocks_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
330 | {
|
---|
331 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
332 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
333 | uint64_t total_blocks = ext4_superblock_get_blocks_count(sb);
|
---|
334 |
|
---|
335 | if (bgid < block_group_count - 1) {
|
---|
336 | return blocks_per_group;
|
---|
337 | } else {
|
---|
338 | return (total_blocks - ((block_group_count - 1) * blocks_per_group));
|
---|
339 | }
|
---|
340 |
|
---|
341 | }
|
---|
342 |
|
---|
343 | uint32_t ext4_superblock_get_inodes_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
344 | {
|
---|
345 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
346 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
347 | uint32_t total_inodes = ext4_superblock_get_inodes_count(sb);
|
---|
348 |
|
---|
349 | if (bgid < block_group_count - 1) {
|
---|
350 | return inodes_per_group;
|
---|
351 | } else {
|
---|
352 | return (total_inodes - ((block_group_count - 1) * inodes_per_group));
|
---|
353 | }
|
---|
354 |
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * @}
|
---|
359 | */
|
---|