1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*
* Copyright (c) 2011 Martin Sucha
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup libext2
* @{
*/
/**
* @file
*/
#include "libext2.h"
#include <errno.h>
#include <malloc.h>
#include <libblock.h>
#include <byteorder.h>
/**
* Return a magic number from ext2 superblock, this should be equal to
* EXT_SUPERBLOCK_MAGIC for valid ext2 superblock
*
* @param sb pointer to superblock
*/
uint16_t ext2_superblock_get_magic(ext2_superblock_t *sb)
{
return uint16_t_le2host(sb->magic);
}
/**
* Get the position of first ext2 data block (i.e. the block number
* containing main superblock)
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_first_block(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->first_block);
}
/**
* Get the number of bits to shift a value of 1024 to the left necessary
* to get the size of a block
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_block_size_log2(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->block_size_log2);
}
/**
* Get the size of a block, in bytes
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_block_size(ext2_superblock_t *sb)
{
return 1024 << ext2_superblock_get_block_size_log2(sb);
}
/**
* Get the number of bits to shift a value of 1024 to the left necessary
* to get the size of a fragment (note that this is a signed integer and
* if negative, the value should be shifted to the right instead)
*
* @param sb pointer to superblock
*/
int32_t ext2_superblock_get_fragment_size_log2(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->fragment_size_log2);
}
/**
* Get the size of a fragment, in bytes
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_fragment_size(ext2_superblock_t *sb)
{
int32_t log = ext2_superblock_get_fragment_size_log2(sb);
if (log >= 0) {
return 1024 << log;
}
else {
return 1024 >> -log;
}
}
/**
* Get number of blocks per block group
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_blocks_per_group(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->blocks_per_group);
}
/**
* Get number of fragments per block group
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_fragments_per_group(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->fragments_per_group);
}
/**
* Get filesystem state
*
* @param sb pointer to superblock
*/
uint16_t ext2_superblock_get_state(ext2_superblock_t *sb)
{
return uint16_t_le2host(sb->state);
}
/**
* Get minor revision number
*
* @param sb pointer to superblock
*/
uint16_t ext2_superblock_get_rev_minor(ext2_superblock_t *sb)
{
return uint16_t_le2host(sb->rev_minor);
}
/**
* Get major revision number
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_rev_major(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->rev_major);
}
/**
* Get index of first regular inode
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_first_inode(ext2_superblock_t *sb)
{
if (ext2_superblock_get_rev_major(sb) == 0) {
return EXT2_REV0_FIRST_INODE;
}
return uint32_t_le2host(sb->first_inode);
}
/**
* Get size of inode
*
* @param sb pointer to superblock
*/
uint16_t ext2_superblock_get_inode_size(ext2_superblock_t *sb)
{
if (ext2_superblock_get_rev_major(sb) == 0) {
return EXT2_REV0_INODE_SIZE;
}
return uint32_t_le2host(sb->inode_size);
}
/**
* Get total inode count
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_total_inode_count(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->total_inode_count);
}
/**
* Get total block count
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_total_block_count(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->total_block_count);
}
/**
* Get amount of blocks reserved for the superuser
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_reserved_block_count(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->reserved_block_count);
}
/**
* Get amount of free blocks
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_free_block_count(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->free_block_count);
}
/**
* Get amount of free inodes
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_free_inode_count(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->free_inode_count);
}
/**
* Get id of operating system that created the filesystem
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_os(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->os);
}
/**
* Get count of inodes per block group
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_inodes_per_group(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->inodes_per_group);
}
/**
* Get compatible features flags
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_features_compatible(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->features_compatible);
}
/**
* Get incompatible features flags
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_features_incompatible(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->features_incompatible);
}
/**
* Get read-only compatible features flags
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_features_read_only(ext2_superblock_t *sb)
{
return uint32_t_le2host(sb->features_read_only);
}
/**
* Compute count of block groups present in the filesystem
*
* Note: This function works only for correct filesystem,
* i.e. it assumes that total block count > 0 and
* blocks per group > 0
*
* Example:
* If there are 3 blocks per group, the result should be as follows:
* Total blocks Result
* 1 1
* 2 1
* 3 1
* 4 2
*
*
* @param sb pointer to superblock
*/
uint32_t ext2_superblock_get_block_group_count(ext2_superblock_t *sb)
{
/* We add one to the result because e.g. 2/3 = 0, while to store
* 2 blocks in 3-block group we need one (1) block group
*
* We subtract one first because of special case that to store e.g.
* 3 blocks in a 3-block group we need only one group
* (and 3/3 yields one - this is one more that we want as we
* already add one at the end)
*/
return ((ext2_superblock_get_total_block_count(sb)-1) /
ext2_superblock_get_blocks_per_group(sb))+1;
}
/** Read a superblock directly from device (i.e. no libblock cache)
*
* @param devmap_handle Device handle of the block device.
* @param superblock Pointer where to store pointer to new superblock
*
* @return EOK on success or negative error code on failure.
*/
int ext2_superblock_read_direct(devmap_handle_t devmap_handle,
ext2_superblock_t **superblock)
{
void *data;
int rc;
data = malloc(EXT2_SUPERBLOCK_SIZE);
if (data == NULL) {
return ENOMEM;
}
rc = block_read_bytes_direct(devmap_handle, EXT2_SUPERBLOCK_OFFSET,
EXT2_SUPERBLOCK_SIZE, data);
if (rc != EOK) {
free(data);
return rc;
}
(*superblock) = data;
return EOK;
}
/** Check a superblock for sanity
*
* @param sb Pointer to superblock
*
* @return EOK on success or negative error code on failure.
*/
int ext2_superblock_check_sanity(ext2_superblock_t *sb)
{
if (ext2_superblock_get_magic(sb) != EXT2_SUPERBLOCK_MAGIC) {
return ENOTSUP;
}
if (ext2_superblock_get_rev_major(sb) > 1) {
return ENOTSUP;
}
if (ext2_superblock_get_total_inode_count(sb) == 0) {
return ENOTSUP;
}
if (ext2_superblock_get_total_block_count(sb) == 0) {
return ENOTSUP;
}
if (ext2_superblock_get_blocks_per_group(sb) == 0) {
return ENOTSUP;
}
if (ext2_superblock_get_fragments_per_group(sb) == 0) {
return ENOTSUP;
}
/* We don't support fragments smaller than block */
if (ext2_superblock_get_block_size(sb) !=
ext2_superblock_get_fragment_size(sb)) {
return ENOTSUP;
}
if (ext2_superblock_get_blocks_per_group(sb) !=
ext2_superblock_get_fragments_per_group(sb)) {
return ENOTSUP;
}
if (ext2_superblock_get_inodes_per_group(sb) == 0) {
return ENOTSUP;
}
if (ext2_superblock_get_inode_size(sb) < 128) {
return ENOTSUP;
}
if (ext2_superblock_get_first_inode(sb) < 11) {
return ENOTSUP;
}
return EOK;
}
/** @}
*/
|