1 | /*
|
---|
2 | * Copyright (c) 2012 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 | /** Get number of i-nodes in the whole filesystem.
|
---|
45 | *
|
---|
46 | * @param sb superblock
|
---|
47 | * @return number of i-nodes
|
---|
48 | */
|
---|
49 | uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *sb)
|
---|
50 | {
|
---|
51 | return uint32_t_le2host(sb->inodes_count);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** Set number of i-nodes in the whole filesystem.
|
---|
55 | *
|
---|
56 | * @param sb superblock
|
---|
57 | * @param count number of i-nodes
|
---|
58 | */
|
---|
59 | void ext4_superblock_set_inodes_count(ext4_superblock_t *sb, uint32_t count)
|
---|
60 | {
|
---|
61 | sb->inodes_count = host2uint32_t_le(count);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Get number of data blocks in the whole filesystem.
|
---|
65 | *
|
---|
66 | * @param sb superblock
|
---|
67 | * @return number of data blocks
|
---|
68 | */
|
---|
69 | uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *sb)
|
---|
70 | {
|
---|
71 | return ((uint64_t)uint32_t_le2host(sb->blocks_count_hi) << 32) |
|
---|
72 | uint32_t_le2host(sb->blocks_count_lo);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Set number of data blocks in the whole filesystem.
|
---|
76 | *
|
---|
77 | * @param sb superblock
|
---|
78 | * @param count number of data blocks
|
---|
79 | */
|
---|
80 | void ext4_superblock_set_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
---|
81 | {
|
---|
82 | sb->blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
---|
83 | sb->blocks_count_hi = host2uint32_t_le(count >> 32);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Get number of reserved data blocks in the whole filesystem.
|
---|
87 | *
|
---|
88 | * @param sb superblock
|
---|
89 | * @return number of reserved data blocks
|
---|
90 | */
|
---|
91 | uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *sb)
|
---|
92 | {
|
---|
93 | return ((uint64_t)uint32_t_le2host(sb->reserved_blocks_count_hi) << 32) |
|
---|
94 | uint32_t_le2host(sb->reserved_blocks_count_lo);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Set number of reserved data blocks in the whole filesystem.
|
---|
98 | *
|
---|
99 | * @param sb superblock
|
---|
100 | * @param count number of reserved data blocks
|
---|
101 | */
|
---|
102 | void ext4_superblock_set_reserved_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
---|
103 | {
|
---|
104 | sb->reserved_blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
---|
105 | sb->reserved_blocks_count_hi = host2uint32_t_le(count >> 32);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Get number of free data blocks in the whole filesystem.
|
---|
109 | *
|
---|
110 | * @param sb superblock
|
---|
111 | * @return number of free data blocks
|
---|
112 | */
|
---|
113 | uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *sb)
|
---|
114 | {
|
---|
115 | return ((uint64_t)uint32_t_le2host(sb->free_blocks_count_hi) << 32) |
|
---|
116 | uint32_t_le2host(sb->free_blocks_count_lo);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Set number of free data blocks in the whole filesystem.
|
---|
120 | *
|
---|
121 | * @param sb superblock
|
---|
122 | * @param count number of free data blocks
|
---|
123 | */
|
---|
124 | void ext4_superblock_set_free_blocks_count(ext4_superblock_t *sb, uint64_t count)
|
---|
125 | {
|
---|
126 | sb->free_blocks_count_lo = host2uint32_t_le((count << 32) >> 32);
|
---|
127 | sb->free_blocks_count_hi = host2uint32_t_le(count >> 32);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Get number of free i-nodes in the whole filesystem.
|
---|
131 | *
|
---|
132 | * @param sb superblock
|
---|
133 | * @return number of free i-nodes
|
---|
134 | */
|
---|
135 | uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *sb)
|
---|
136 | {
|
---|
137 | return uint32_t_le2host(sb->free_inodes_count);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Set number of free i-nodes in the whole filesystem.
|
---|
141 | *
|
---|
142 | * @param sb superblock
|
---|
143 | * @param count number of free i-nodes
|
---|
144 | */
|
---|
145 | void ext4_superblock_set_free_inodes_count(ext4_superblock_t *sb, uint32_t count)
|
---|
146 | {
|
---|
147 | sb->free_inodes_count = host2uint32_t_le(count);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Get index of first data block (block, where is located superblock)
|
---|
151 | *
|
---|
152 | * @param sb superblock
|
---|
153 | * @return index of the first data block
|
---|
154 | */
|
---|
155 | uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
|
---|
156 | {
|
---|
157 | return uint32_t_le2host(sb->first_data_block);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Set index of first data block (block, where is located superblock)
|
---|
161 | *
|
---|
162 | * @param sb superblock
|
---|
163 | * @param first index of the first data block
|
---|
164 | */
|
---|
165 | void ext4_superblock_set_first_data_block(ext4_superblock_t *sb, uint32_t first)
|
---|
166 | {
|
---|
167 | sb->first_data_block = host2uint32_t_le(first);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** Get logarithmic block size (1024 << size == block_size)
|
---|
171 | *
|
---|
172 | * @param sb superblock
|
---|
173 | * @return logarithmic block size
|
---|
174 | */
|
---|
175 | uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
|
---|
176 | {
|
---|
177 | return uint32_t_le2host(sb->log_block_size);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /** Set logarithmic block size (1024 << size == block_size)
|
---|
181 | *
|
---|
182 | * @param sb superblock
|
---|
183 | * @return logarithmic block size
|
---|
184 | */
|
---|
185 | void ext4_superblock_set_log_block_size(ext4_superblock_t *sb, uint32_t log_size)
|
---|
186 | {
|
---|
187 | sb->log_block_size = host2uint32_t_le(log_size);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** Get size of data block (in bytes).
|
---|
191 | *
|
---|
192 | * @param sb superblock
|
---|
193 | * @return size of data block
|
---|
194 | */
|
---|
195 | uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
|
---|
196 | {
|
---|
197 | return 1024 << ext4_superblock_get_log_block_size(sb);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** Set size of data block (in bytes).
|
---|
201 | *
|
---|
202 | * @param sb superblock
|
---|
203 | * @param size size of data block (must be power of 2, at least 1024)
|
---|
204 | */
|
---|
205 | void ext4_superblock_set_block_size(ext4_superblock_t *sb, uint32_t size)
|
---|
206 | {
|
---|
207 | uint32_t log = 0;
|
---|
208 | uint32_t tmp = size / EXT4_MIN_BLOCK_SIZE;
|
---|
209 |
|
---|
210 | tmp >>= 1;
|
---|
211 | while (tmp) {
|
---|
212 | log++;
|
---|
213 | tmp >>= 1;
|
---|
214 | }
|
---|
215 |
|
---|
216 | ext4_superblock_set_log_block_size(sb, log);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Get number of data blocks per block group (except last BG)
|
---|
220 | *
|
---|
221 | * @param sb superblock
|
---|
222 | * @return data blocks per block group
|
---|
223 | */
|
---|
224 | uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
|
---|
225 | {
|
---|
226 | return uint32_t_le2host(sb->blocks_per_group);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Set number of data blocks per block group (except last BG)
|
---|
230 | *
|
---|
231 | * @param sb superblock
|
---|
232 | * @param blocks data blocks per block group
|
---|
233 | */
|
---|
234 | void ext4_superblock_set_blocks_per_group(ext4_superblock_t *sb, uint32_t blocks)
|
---|
235 | {
|
---|
236 | sb->blocks_per_group = host2uint32_t_le(blocks);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Get number of i-nodes per block group (except last BG)
|
---|
240 | *
|
---|
241 | * @param sb superblock
|
---|
242 | * @return i-nodes per block group
|
---|
243 | */
|
---|
244 | uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
|
---|
245 | {
|
---|
246 | return uint32_t_le2host(sb->inodes_per_group);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** Set number of i-nodes per block group (except last BG)
|
---|
250 | *
|
---|
251 | * @param sb superblock
|
---|
252 | * @param inodes i-nodes per block group
|
---|
253 | */
|
---|
254 | void ext4_superblock_set_inodes_per_group(ext4_superblock_t *sb, uint32_t inodes)
|
---|
255 | {
|
---|
256 | sb->inodes_per_group = host2uint32_t_le(inodes);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /** Get time when filesystem was mounted (POSIX time).
|
---|
260 | *
|
---|
261 | * @param sb superblock
|
---|
262 | * @return mount time
|
---|
263 | */
|
---|
264 | uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
|
---|
265 | {
|
---|
266 | return uint32_t_le2host(sb->mount_time);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Set time when filesystem was mounted (POSIX time).
|
---|
270 | *
|
---|
271 | * @param sb superblock
|
---|
272 | * @param time mount time
|
---|
273 | */
|
---|
274 | void ext4_superblock_set_mount_time(ext4_superblock_t *sb, uint32_t time)
|
---|
275 | {
|
---|
276 | sb->mount_time = host2uint32_t_le(time);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /** Get time when filesystem was last accesed by write operation (POSIX time).
|
---|
280 | *
|
---|
281 | * @param sb superblock
|
---|
282 | * @return write time
|
---|
283 | */
|
---|
284 | uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
|
---|
285 | {
|
---|
286 | return uint32_t_le2host(sb->write_time);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** Set time when filesystem was last accesed by write operation (POSIX time).
|
---|
290 | *
|
---|
291 | * @param sb superblock
|
---|
292 | * @param time write time
|
---|
293 | */
|
---|
294 | void ext4_superblock_set_write_time(ext4_superblock_t *sb, uint32_t time)
|
---|
295 | {
|
---|
296 | sb->write_time = host2uint32_t_le(time);
|
---|
297 | }
|
---|
298 |
|
---|
299 | /** Get number of mount from last filesystem check.
|
---|
300 | *
|
---|
301 | * @param sb superblock
|
---|
302 | * @return number of mounts
|
---|
303 | */
|
---|
304 | uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
|
---|
305 | {
|
---|
306 | return uint16_t_le2host(sb->mount_count);
|
---|
307 | }
|
---|
308 |
|
---|
309 | /** Set number of mount from last filesystem check.
|
---|
310 | *
|
---|
311 | * @param sb superblock
|
---|
312 | * @param count number of mounts
|
---|
313 | */
|
---|
314 | void ext4_superblock_set_mount_count(ext4_superblock_t *sb, uint16_t count)
|
---|
315 | {
|
---|
316 | sb->mount_count = host2uint16_t_le(count);
|
---|
317 | }
|
---|
318 |
|
---|
319 | /** Get maximum number of mount from last filesystem check.
|
---|
320 | *
|
---|
321 | * @param sb superblock
|
---|
322 | * @return maximum number of mounts
|
---|
323 | */
|
---|
324 | uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
|
---|
325 | {
|
---|
326 | return uint16_t_le2host(sb->max_mount_count);
|
---|
327 | }
|
---|
328 |
|
---|
329 | /** Set maximum number of mount from last filesystem check.
|
---|
330 | *
|
---|
331 | * @param sb superblock
|
---|
332 | * @param count maximum number of mounts
|
---|
333 | */
|
---|
334 | void ext4_superblock_set_max_mount_count(ext4_superblock_t *sb, uint16_t count)
|
---|
335 | {
|
---|
336 | sb->max_mount_count = host2uint16_t_le(count);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** Get superblock magic value.
|
---|
340 | *
|
---|
341 | * @param sb superblock
|
---|
342 | * @return magic value
|
---|
343 | */
|
---|
344 | uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
|
---|
345 | {
|
---|
346 | return uint16_t_le2host(sb->magic);
|
---|
347 | }
|
---|
348 |
|
---|
349 | /** Set superblock magic value.
|
---|
350 | *
|
---|
351 | * @param sb superblock
|
---|
352 | * @param magic value
|
---|
353 | */
|
---|
354 | void ext4_superblock_set_magic(ext4_superblock_t *sb, uint16_t magic)
|
---|
355 | {
|
---|
356 | sb->magic = host2uint16_t_le(magic);
|
---|
357 | }
|
---|
358 |
|
---|
359 | /** TODO comment
|
---|
360 | *
|
---|
361 | */
|
---|
362 | uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
|
---|
363 | {
|
---|
364 | return uint16_t_le2host(sb->state);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /** TODO comment
|
---|
368 | *
|
---|
369 | */
|
---|
370 | void ext4_superblock_set_state(ext4_superblock_t *sb, uint16_t state)
|
---|
371 | {
|
---|
372 | sb->state = host2uint16_t_le(state);
|
---|
373 | }
|
---|
374 |
|
---|
375 | /** TODO comment
|
---|
376 | *
|
---|
377 | */
|
---|
378 | uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
|
---|
379 | {
|
---|
380 | return uint16_t_le2host(sb->errors);
|
---|
381 | }
|
---|
382 |
|
---|
383 | /** TODO comment
|
---|
384 | *
|
---|
385 | */
|
---|
386 | void ext4_superblock_set_errors(ext4_superblock_t *sb, uint16_t errors)
|
---|
387 | {
|
---|
388 | sb->errors = host2uint16_t_le(errors);
|
---|
389 | }
|
---|
390 |
|
---|
391 | /** TODO comment
|
---|
392 | *
|
---|
393 | */
|
---|
394 | uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
|
---|
395 | {
|
---|
396 | return uint16_t_le2host(sb->minor_rev_level);
|
---|
397 | }
|
---|
398 |
|
---|
399 | /** TODO comment
|
---|
400 | *
|
---|
401 | */
|
---|
402 | void ext4_superblock_set_minor_rev_level(ext4_superblock_t *sb, uint16_t level)
|
---|
403 | {
|
---|
404 | sb->minor_rev_level = host2uint16_t_le(level);
|
---|
405 | }
|
---|
406 |
|
---|
407 | /** TODO comment
|
---|
408 | *
|
---|
409 | */
|
---|
410 | uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
|
---|
411 | {
|
---|
412 | return uint32_t_le2host(sb->last_check_time);
|
---|
413 | }
|
---|
414 |
|
---|
415 | /** TODO comment
|
---|
416 | *
|
---|
417 | */
|
---|
418 | void ext4_superblock_set_last_check_time(ext4_superblock_t *sb, uint32_t time)
|
---|
419 | {
|
---|
420 | sb->state = host2uint32_t_le(time);
|
---|
421 | }
|
---|
422 |
|
---|
423 | /** TODO comment
|
---|
424 | *
|
---|
425 | */
|
---|
426 | uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb){
|
---|
427 | return uint32_t_le2host(sb->check_interval);
|
---|
428 | }
|
---|
429 |
|
---|
430 | /** TODO comment
|
---|
431 | *
|
---|
432 | */
|
---|
433 | void ext4_superblock_set_check_interval(ext4_superblock_t *sb, uint32_t interval)
|
---|
434 | {
|
---|
435 | sb->check_interval = host2uint32_t_le(interval);
|
---|
436 | }
|
---|
437 |
|
---|
438 | /** TODO comment
|
---|
439 | *
|
---|
440 | */
|
---|
441 | uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
|
---|
442 | {
|
---|
443 | return uint32_t_le2host(sb->creator_os);
|
---|
444 | }
|
---|
445 |
|
---|
446 | /** TODO comment
|
---|
447 | *
|
---|
448 | */
|
---|
449 | void ext4_superblock_set_creator_os(ext4_superblock_t *sb, uint32_t os)
|
---|
450 | {
|
---|
451 | sb->creator_os = host2uint32_t_le(os);
|
---|
452 | }
|
---|
453 |
|
---|
454 | /** TODO comment
|
---|
455 | *
|
---|
456 | */
|
---|
457 | uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
|
---|
458 | {
|
---|
459 | return uint32_t_le2host(sb->rev_level);
|
---|
460 | }
|
---|
461 |
|
---|
462 | /** TODO comment
|
---|
463 | *
|
---|
464 | */
|
---|
465 | void ext4_superblock_set_rev_level(ext4_superblock_t *sb, uint32_t level)
|
---|
466 | {
|
---|
467 | sb->rev_level = host2uint32_t_le(level);
|
---|
468 | }
|
---|
469 |
|
---|
470 | /** TODO comment
|
---|
471 | *
|
---|
472 | */
|
---|
473 | uint16_t ext4_superblock_get_def_resuid(ext4_superblock_t *sb)
|
---|
474 | {
|
---|
475 | return uint16_t_le2host(sb->def_resuid);
|
---|
476 | }
|
---|
477 |
|
---|
478 | /** TODO comment
|
---|
479 | *
|
---|
480 | */
|
---|
481 | void ext4_superblock_set_def_resuid(ext4_superblock_t *sb, uint16_t uid)
|
---|
482 | {
|
---|
483 | sb->def_resuid = host2uint16_t_le(uid);
|
---|
484 | }
|
---|
485 |
|
---|
486 | /** TODO comment
|
---|
487 | *
|
---|
488 | */
|
---|
489 | uint16_t ext4_superblock_get_def_resgid(ext4_superblock_t *sb)
|
---|
490 | {
|
---|
491 | return uint16_t_le2host(sb->def_resgid);
|
---|
492 | }
|
---|
493 |
|
---|
494 | /** TODO comment
|
---|
495 | *
|
---|
496 | */
|
---|
497 | void ext4_superblock_set_def_resgid(ext4_superblock_t *sb, uint16_t gid)
|
---|
498 | {
|
---|
499 | sb->def_resgid = host2uint16_t_le(gid);
|
---|
500 | }
|
---|
501 |
|
---|
502 | /** TODO comment
|
---|
503 | *
|
---|
504 | */
|
---|
505 | uint32_t ext4_superblock_get_first_inode(ext4_superblock_t *sb)
|
---|
506 | {
|
---|
507 | return uint32_t_le2host(sb->first_inode);
|
---|
508 | }
|
---|
509 |
|
---|
510 | /** TODO comment
|
---|
511 | *
|
---|
512 | */
|
---|
513 | void ext4_superblock_set_first_inode(ext4_superblock_t *sb, uint32_t first_inode)
|
---|
514 | {
|
---|
515 | sb->first_inode = host2uint32_t_le(first_inode);
|
---|
516 | }
|
---|
517 |
|
---|
518 | /** TODO comment
|
---|
519 | *
|
---|
520 | */
|
---|
521 | uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
|
---|
522 | {
|
---|
523 | if (ext4_superblock_get_rev_level(sb) == 0) {
|
---|
524 | return EXT4_REV0_INODE_SIZE;
|
---|
525 | }
|
---|
526 | return uint16_t_le2host(sb->inode_size);
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** TODO comment
|
---|
530 | *
|
---|
531 | */
|
---|
532 | void ext4_superblock_set_inode_size(ext4_superblock_t *sb, uint16_t size)
|
---|
533 | {
|
---|
534 | sb->inode_size = host2uint16_t_le(size);
|
---|
535 | }
|
---|
536 |
|
---|
537 | /** TODO comment
|
---|
538 | *
|
---|
539 | */
|
---|
540 | uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *sb)
|
---|
541 | {
|
---|
542 | return uint16_t_le2host(sb->block_group_number);
|
---|
543 | }
|
---|
544 |
|
---|
545 | /** TODO comment
|
---|
546 | *
|
---|
547 | */
|
---|
548 | void ext4_superblock_set_block_group_number(ext4_superblock_t *sb, uint16_t bg)
|
---|
549 | {
|
---|
550 | sb->block_group_number = host2uint16_t_le(bg);
|
---|
551 | }
|
---|
552 |
|
---|
553 | /** TODO comment
|
---|
554 | *
|
---|
555 | */
|
---|
556 | uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
|
---|
557 | {
|
---|
558 | return uint32_t_le2host(sb->features_compatible);
|
---|
559 | }
|
---|
560 |
|
---|
561 | /** TODO comment
|
---|
562 | *
|
---|
563 | */
|
---|
564 | void ext4_superblock_set_features_compatible(ext4_superblock_t *sb, uint32_t features)
|
---|
565 | {
|
---|
566 | sb->features_compatible = host2uint32_t_le(features);
|
---|
567 | }
|
---|
568 |
|
---|
569 | /** TODO comment
|
---|
570 | *
|
---|
571 | */
|
---|
572 | uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
|
---|
573 | {
|
---|
574 | return uint32_t_le2host(sb->features_incompatible);
|
---|
575 | }
|
---|
576 |
|
---|
577 | /** TODO comment
|
---|
578 | *
|
---|
579 | */
|
---|
580 | void ext4_superblock_set_features_incompatible(ext4_superblock_t *sb, uint32_t features)
|
---|
581 | {
|
---|
582 | sb->features_incompatible = host2uint32_t_le(features);
|
---|
583 | }
|
---|
584 |
|
---|
585 | /** TODO comment
|
---|
586 | *
|
---|
587 | */
|
---|
588 | uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
|
---|
589 | {
|
---|
590 | return uint32_t_le2host(sb->features_read_only);
|
---|
591 | }
|
---|
592 |
|
---|
593 | /** TODO comment
|
---|
594 | *
|
---|
595 | */
|
---|
596 | void ext4_superblock_set_features_read_only(ext4_superblock_t *sb, uint32_t features)
|
---|
597 | {
|
---|
598 | sb->features_read_only = host2uint32_t_le(features);
|
---|
599 | }
|
---|
600 |
|
---|
601 | /** TODO comment
|
---|
602 | *
|
---|
603 | */
|
---|
604 | const uint8_t * ext4_superblock_get_uuid(ext4_superblock_t *sb)
|
---|
605 | {
|
---|
606 | return sb->uuid;
|
---|
607 | }
|
---|
608 |
|
---|
609 | /** TODO comment
|
---|
610 | *
|
---|
611 | */
|
---|
612 | void ext4_superblock_set_uuid(ext4_superblock_t *sb, const uint8_t *uuid)
|
---|
613 | {
|
---|
614 | memcpy(sb->uuid, uuid, sizeof(sb->uuid));
|
---|
615 | }
|
---|
616 |
|
---|
617 | const char * ext4_superblock_get_volume_name(ext4_superblock_t *sb)
|
---|
618 | {
|
---|
619 | return sb->volume_name;
|
---|
620 | }
|
---|
621 |
|
---|
622 | /** TODO comment
|
---|
623 | *
|
---|
624 | */
|
---|
625 | void ext4_superblock_set_volume_name(ext4_superblock_t *sb, const char *name)
|
---|
626 | {
|
---|
627 | memcpy(sb->volume_name, name, sizeof(sb->volume_name));
|
---|
628 | }
|
---|
629 |
|
---|
630 | /** TODO comment
|
---|
631 | *
|
---|
632 | */
|
---|
633 | const char * ext4_superblock_get_last_mounted(ext4_superblock_t *sb)
|
---|
634 | {
|
---|
635 | return sb->last_mounted;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /** TODO comment
|
---|
639 | *
|
---|
640 | */
|
---|
641 | void ext4_superblock_set_last_mounted(ext4_superblock_t *sb, const char *last)
|
---|
642 | {
|
---|
643 | memcpy(sb->last_mounted, last, sizeof(sb->last_mounted));
|
---|
644 | }
|
---|
645 |
|
---|
646 | /** TODO comment
|
---|
647 | *
|
---|
648 | */
|
---|
649 | uint32_t ext4_superblock_get_last_orphan(ext4_superblock_t *sb)
|
---|
650 | {
|
---|
651 | return uint32_t_le2host(sb->last_orphan);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /** TODO comment
|
---|
655 | *
|
---|
656 | */
|
---|
657 | void ext4_superblock_set_last_orphan(ext4_superblock_t *sb, uint32_t last_orphan)
|
---|
658 | {
|
---|
659 | sb->last_orphan = host2uint32_t_le(last_orphan);
|
---|
660 | }
|
---|
661 |
|
---|
662 | /** TODO comment
|
---|
663 | *
|
---|
664 | */
|
---|
665 | uint32_t* ext4_superblock_get_hash_seed(ext4_superblock_t *sb)
|
---|
666 | {
|
---|
667 | return sb->hash_seed;
|
---|
668 | }
|
---|
669 |
|
---|
670 | /** TODO comment
|
---|
671 | *
|
---|
672 | */
|
---|
673 | uint8_t ext4_superblock_get_default_hash_version(ext4_superblock_t *sb)
|
---|
674 | {
|
---|
675 | return sb->default_hash_version;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /** TODO comment
|
---|
679 | *
|
---|
680 | */
|
---|
681 | void ext4_superblock_set_default_hash_version(ext4_superblock_t *sb, uint8_t version)
|
---|
682 | {
|
---|
683 | sb->default_hash_version = version;
|
---|
684 | }
|
---|
685 |
|
---|
686 | /** TODO comment
|
---|
687 | *
|
---|
688 | */
|
---|
689 | uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *sb)
|
---|
690 | {
|
---|
691 | uint16_t size = uint16_t_le2host(sb->desc_size);
|
---|
692 |
|
---|
693 | if (size < EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
|
---|
694 | size = EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE;
|
---|
695 | }
|
---|
696 |
|
---|
697 | return size;
|
---|
698 | }
|
---|
699 |
|
---|
700 | /** TODO comment
|
---|
701 | *
|
---|
702 | */
|
---|
703 | void ext4_superblock_set_desc_size(ext4_superblock_t *sb, uint16_t size)
|
---|
704 | {
|
---|
705 | sb->desc_size = host2uint16_t_le(size);
|
---|
706 | }
|
---|
707 |
|
---|
708 | /** TODO comment
|
---|
709 | *
|
---|
710 | */
|
---|
711 | uint32_t ext4_superblock_get_flags(ext4_superblock_t *sb)
|
---|
712 | {
|
---|
713 | return uint32_t_le2host(sb->flags);
|
---|
714 | }
|
---|
715 |
|
---|
716 | /** TODO comment
|
---|
717 | *
|
---|
718 | */
|
---|
719 | void ext4_superblock_set_flags(ext4_superblock_t *sb, uint32_t flags)
|
---|
720 | {
|
---|
721 | sb->flags = host2uint32_t_le(flags);
|
---|
722 | }
|
---|
723 |
|
---|
724 |
|
---|
725 | /*
|
---|
726 | * More complex superblock operations
|
---|
727 | */
|
---|
728 |
|
---|
729 | /** TODO comment
|
---|
730 | *
|
---|
731 | */
|
---|
732 | bool ext4_superblock_has_flag(ext4_superblock_t *sb, uint32_t flag)
|
---|
733 | {
|
---|
734 | if (ext4_superblock_get_flags(sb) & flag) {
|
---|
735 | return true;
|
---|
736 | }
|
---|
737 | return false;
|
---|
738 | }
|
---|
739 |
|
---|
740 | /** TODO comment
|
---|
741 | *
|
---|
742 | */
|
---|
743 | bool ext4_superblock_has_feature_compatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
744 | {
|
---|
745 | if (ext4_superblock_get_features_compatible(sb) & feature) {
|
---|
746 | return true;
|
---|
747 | }
|
---|
748 | return false;
|
---|
749 | }
|
---|
750 |
|
---|
751 | /** TODO comment
|
---|
752 | *
|
---|
753 | */
|
---|
754 | bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *sb, uint32_t feature)
|
---|
755 | {
|
---|
756 | if (ext4_superblock_get_features_incompatible(sb) & feature) {
|
---|
757 | return true;
|
---|
758 | }
|
---|
759 | return false;
|
---|
760 | }
|
---|
761 |
|
---|
762 | /** TODO comment
|
---|
763 | *
|
---|
764 | */
|
---|
765 | bool ext4_superblock_has_feature_read_only(ext4_superblock_t *sb, uint32_t feature)
|
---|
766 | {
|
---|
767 | if (ext4_superblock_get_features_read_only(sb) & feature) {
|
---|
768 | return true;
|
---|
769 | }
|
---|
770 | return false;
|
---|
771 | }
|
---|
772 |
|
---|
773 | /** TODO comment
|
---|
774 | *
|
---|
775 | */
|
---|
776 | int ext4_superblock_read_direct(service_id_t service_id,
|
---|
777 | ext4_superblock_t **superblock)
|
---|
778 | {
|
---|
779 | int rc;
|
---|
780 |
|
---|
781 | void *data = malloc(EXT4_SUPERBLOCK_SIZE);
|
---|
782 | if (data == NULL) {
|
---|
783 | return ENOMEM;
|
---|
784 | }
|
---|
785 |
|
---|
786 | rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
|
---|
787 | EXT4_SUPERBLOCK_SIZE, data);
|
---|
788 |
|
---|
789 | if (rc != EOK) {
|
---|
790 | free(data);
|
---|
791 | return rc;
|
---|
792 | }
|
---|
793 |
|
---|
794 | (*superblock) = data;
|
---|
795 |
|
---|
796 | return EOK;
|
---|
797 | }
|
---|
798 |
|
---|
799 | /** TODO comment
|
---|
800 | *
|
---|
801 | */
|
---|
802 | int ext4_superblock_write_direct(service_id_t service_id,
|
---|
803 | ext4_superblock_t *sb)
|
---|
804 | {
|
---|
805 | int rc;
|
---|
806 | uint32_t phys_block_size;
|
---|
807 |
|
---|
808 | rc = block_get_bsize(service_id, &phys_block_size);
|
---|
809 | if (rc != EOK) {
|
---|
810 | // TODO error
|
---|
811 | return rc;
|
---|
812 | }
|
---|
813 |
|
---|
814 | uint64_t first_block = EXT4_SUPERBLOCK_OFFSET / phys_block_size;
|
---|
815 | uint32_t block_count = EXT4_SUPERBLOCK_SIZE / phys_block_size;
|
---|
816 |
|
---|
817 | if (EXT4_SUPERBLOCK_SIZE % phys_block_size) {
|
---|
818 | block_count++;
|
---|
819 | }
|
---|
820 |
|
---|
821 | return block_write_direct(service_id, first_block, block_count, sb);
|
---|
822 |
|
---|
823 | }
|
---|
824 |
|
---|
825 | /** TODO comment
|
---|
826 | *
|
---|
827 | */
|
---|
828 | int ext4_superblock_check_sanity(ext4_superblock_t *sb)
|
---|
829 | {
|
---|
830 | if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC) {
|
---|
831 | return ENOTSUP;
|
---|
832 | }
|
---|
833 |
|
---|
834 | // block size
|
---|
835 | // desc size
|
---|
836 |
|
---|
837 |
|
---|
838 | // TODO more checks !!!
|
---|
839 |
|
---|
840 | return EOK;
|
---|
841 | }
|
---|
842 |
|
---|
843 | /** TODO comment
|
---|
844 | *
|
---|
845 | */
|
---|
846 | uint32_t ext4_superblock_get_block_group_count(ext4_superblock_t *sb)
|
---|
847 | {
|
---|
848 | uint64_t blocks_count = ext4_superblock_get_blocks_count(sb);
|
---|
849 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
850 |
|
---|
851 | uint32_t block_groups_count = blocks_count / blocks_per_group;
|
---|
852 |
|
---|
853 | if (blocks_count % blocks_per_group) {
|
---|
854 | block_groups_count++;
|
---|
855 | }
|
---|
856 |
|
---|
857 | return block_groups_count;
|
---|
858 |
|
---|
859 | }
|
---|
860 |
|
---|
861 | /** TODO comment
|
---|
862 | *
|
---|
863 | */
|
---|
864 | uint32_t ext4_superblock_get_blocks_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
865 | {
|
---|
866 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
867 | uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(sb);
|
---|
868 | uint64_t total_blocks = ext4_superblock_get_blocks_count(sb);
|
---|
869 |
|
---|
870 | if (bgid < block_group_count - 1) {
|
---|
871 | return blocks_per_group;
|
---|
872 | } else {
|
---|
873 | return (total_blocks - ((block_group_count - 1) * blocks_per_group));
|
---|
874 | }
|
---|
875 |
|
---|
876 | }
|
---|
877 |
|
---|
878 | /** TODO comment
|
---|
879 | *
|
---|
880 | */
|
---|
881 | uint32_t ext4_superblock_get_inodes_in_group(ext4_superblock_t *sb, uint32_t bgid)
|
---|
882 | {
|
---|
883 | uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
|
---|
884 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
885 | uint32_t total_inodes = ext4_superblock_get_inodes_count(sb);
|
---|
886 |
|
---|
887 | if (bgid < block_group_count - 1) {
|
---|
888 | return inodes_per_group;
|
---|
889 | } else {
|
---|
890 | return (total_inodes - ((block_group_count - 1) * inodes_per_group));
|
---|
891 | }
|
---|
892 |
|
---|
893 | }
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * @}
|
---|
897 | */
|
---|