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_filesystem.c
|
---|
35 | * @brief TODO
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <malloc.h>
|
---|
41 | #include "libext4.h"
|
---|
42 |
|
---|
43 | int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id)
|
---|
44 | {
|
---|
45 |
|
---|
46 | int rc;
|
---|
47 | ext4_superblock_t *temp_superblock;
|
---|
48 | size_t block_size;
|
---|
49 |
|
---|
50 | fs->device = service_id;
|
---|
51 |
|
---|
52 | // TODO what does constant 2048 mean?
|
---|
53 | rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
|
---|
54 | if (rc != EOK) {
|
---|
55 | return rc;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /* Read superblock from device */
|
---|
59 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
---|
60 | if (rc != EOK) {
|
---|
61 | block_fini(fs->device);
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Read block size from superblock and check */
|
---|
66 | block_size = ext4_superblock_get_block_size(temp_superblock);
|
---|
67 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
---|
68 | block_fini(fs->device);
|
---|
69 | return ENOTSUP;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* Initialize block caching */
|
---|
73 | rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
|
---|
74 | if (rc != EOK) {
|
---|
75 | block_fini(fs->device);
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* Return loaded superblock */
|
---|
80 | fs->superblock = temp_superblock;
|
---|
81 |
|
---|
82 | return EOK;
|
---|
83 | }
|
---|
84 |
|
---|
85 | void ext4_filesystem_fini(ext4_filesystem_t *fs)
|
---|
86 | {
|
---|
87 | free(fs->superblock);
|
---|
88 | block_fini(fs->device);
|
---|
89 | }
|
---|
90 |
|
---|
91 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
---|
92 | {
|
---|
93 | int rc;
|
---|
94 |
|
---|
95 | rc = ext4_superblock_check_sanity(fs->superblock);
|
---|
96 | if (rc != EOK) {
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
|
---|
104 | {
|
---|
105 | /* Feature flags are present in rev 1 and later */
|
---|
106 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
---|
107 | *o_read_only = false;
|
---|
108 | return EOK;
|
---|
109 | }
|
---|
110 |
|
---|
111 | uint32_t incompatible_features;
|
---|
112 | incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
|
---|
113 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
---|
114 | if (incompatible_features > 0) {
|
---|
115 | *o_read_only = true;
|
---|
116 | return ENOTSUP;
|
---|
117 | }
|
---|
118 |
|
---|
119 | uint32_t compatible_read_only;
|
---|
120 | compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
|
---|
121 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
---|
122 | if (compatible_read_only > 0) {
|
---|
123 | *o_read_only = true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | return EOK;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Feature checkers
|
---|
130 | bool ext4_filesystem_has_feature_compatible(ext4_filesystem_t *fs, uint32_t feature)
|
---|
131 | {
|
---|
132 | ext4_superblock_t *sb = fs->superblock;
|
---|
133 |
|
---|
134 | if (ext4_superblock_get_features_compatible(sb) & feature) {
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool ext4_filesystem_has_feature_incompatible(ext4_filesystem_t *fs, uint32_t feature)
|
---|
141 | {
|
---|
142 | ext4_superblock_t *sb = fs->superblock;
|
---|
143 |
|
---|
144 | if (ext4_superblock_get_features_incompatible(sb) & feature) {
|
---|
145 | return true;
|
---|
146 | }
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | bool ext4_filesystem_has_feature_read_only(ext4_filesystem_t *fs, uint32_t feature)
|
---|
151 | {
|
---|
152 | ext4_superblock_t *sb = fs->superblock;
|
---|
153 |
|
---|
154 | if (ext4_superblock_get_features_read_only(sb) & feature) {
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 | return false;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
---|
162 | ext4_block_group_ref_t **ref)
|
---|
163 | {
|
---|
164 | int rc;
|
---|
165 | aoff64_t block_id;
|
---|
166 | uint32_t descriptors_per_block;
|
---|
167 | size_t offset;
|
---|
168 | ext4_block_group_ref_t *newref;
|
---|
169 |
|
---|
170 | newref = malloc(sizeof(ext4_block_group_ref_t));
|
---|
171 | if (newref == NULL) {
|
---|
172 | return ENOMEM;
|
---|
173 | }
|
---|
174 |
|
---|
175 | descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
|
---|
176 | / EXT4_BLOCK_GROUP_DESCRIPTOR_SIZE;
|
---|
177 |
|
---|
178 | /* Block group descriptor table starts at the next block after superblock */
|
---|
179 | block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
|
---|
180 |
|
---|
181 | /* Find the block containing the descriptor we are looking for */
|
---|
182 | block_id += bgid / descriptors_per_block;
|
---|
183 | offset = (bgid % descriptors_per_block) * EXT4_BLOCK_GROUP_DESCRIPTOR_SIZE;
|
---|
184 |
|
---|
185 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
186 | if (rc != EOK) {
|
---|
187 | free(newref);
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 | newref->block_group = newref->block->data + offset;
|
---|
192 |
|
---|
193 | *ref = newref;
|
---|
194 |
|
---|
195 | return EOK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
---|
199 | {
|
---|
200 | int rc;
|
---|
201 |
|
---|
202 | rc = block_put(ref->block);
|
---|
203 | free(ref);
|
---|
204 |
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
---|
209 | ext4_inode_ref_t **ref)
|
---|
210 | {
|
---|
211 | int rc;
|
---|
212 | aoff64_t block_id;
|
---|
213 | uint32_t block_group;
|
---|
214 | uint32_t offset_in_group;
|
---|
215 | uint32_t byte_offset_in_group;
|
---|
216 | size_t offset_in_block;
|
---|
217 | uint32_t inodes_per_group;
|
---|
218 | uint32_t inode_table_start;
|
---|
219 | uint16_t inode_size;
|
---|
220 | uint32_t block_size;
|
---|
221 | ext4_block_group_ref_t *bg_ref;
|
---|
222 | ext4_inode_ref_t *newref;
|
---|
223 |
|
---|
224 | newref = malloc(sizeof(ext4_inode_ref_t));
|
---|
225 | if (newref == NULL) {
|
---|
226 | return ENOMEM;
|
---|
227 | }
|
---|
228 |
|
---|
229 | inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
230 |
|
---|
231 | /* inode numbers are 1-based, but it is simpler to work with 0-based
|
---|
232 | * when computing indices
|
---|
233 | */
|
---|
234 | index -= 1;
|
---|
235 | block_group = index / inodes_per_group;
|
---|
236 | offset_in_group = index % inodes_per_group;
|
---|
237 |
|
---|
238 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
239 | if (rc != EOK) {
|
---|
240 | free(newref);
|
---|
241 | return rc;
|
---|
242 | }
|
---|
243 |
|
---|
244 | inode_table_start = ext4_block_group_get_inode_table_first_block(
|
---|
245 | bg_ref->block_group);
|
---|
246 |
|
---|
247 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
248 | if (rc != EOK) {
|
---|
249 | free(newref);
|
---|
250 | return rc;
|
---|
251 | }
|
---|
252 |
|
---|
253 | inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
254 | block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
255 |
|
---|
256 | byte_offset_in_group = offset_in_group * inode_size;
|
---|
257 |
|
---|
258 | block_id = inode_table_start + (byte_offset_in_group / block_size);
|
---|
259 | offset_in_block = byte_offset_in_group % block_size;
|
---|
260 |
|
---|
261 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
262 | if (rc != EOK) {
|
---|
263 | free(newref);
|
---|
264 | return rc;
|
---|
265 | }
|
---|
266 |
|
---|
267 | newref->inode = newref->block->data + offset_in_block;
|
---|
268 | /* we decremented index above, but need to store the original value
|
---|
269 | * in the reference
|
---|
270 | */
|
---|
271 | newref->index = index+1;
|
---|
272 |
|
---|
273 | *ref = newref;
|
---|
274 |
|
---|
275 | return EOK;
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
---|
280 | {
|
---|
281 | int rc;
|
---|
282 |
|
---|
283 | rc = block_put(ref->block);
|
---|
284 | free(ref);
|
---|
285 |
|
---|
286 | return rc;
|
---|
287 | }
|
---|
288 |
|
---|
289 | int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
|
---|
290 | aoff64_t iblock, uint32_t* fblock)
|
---|
291 | {
|
---|
292 | int rc;
|
---|
293 | aoff64_t limits[4];
|
---|
294 | uint32_t block_ids_per_block;
|
---|
295 | aoff64_t blocks_per_level[4];
|
---|
296 | uint32_t offset_in_block;
|
---|
297 | uint32_t current_block;
|
---|
298 | aoff64_t block_offset_in_level;
|
---|
299 | int i;
|
---|
300 | int level;
|
---|
301 | block_t *block;
|
---|
302 |
|
---|
303 | // TODO extents
|
---|
304 | if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
305 | EXT4FS_DBG("Inode contains Extent");
|
---|
306 | // TODO
|
---|
307 | /*
|
---|
308 | current_block = ext4_inode_get_extent_block(inode, iblock);
|
---|
309 | *fblock = current_block;
|
---|
310 | return EOK;
|
---|
311 | */
|
---|
312 |
|
---|
313 | }
|
---|
314 |
|
---|
315 | /* Handle simple case when we are dealing with direct reference */
|
---|
316 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
317 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
---|
318 | *fblock = current_block;
|
---|
319 | return EOK;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /* Compute limits for indirect block levels
|
---|
323 | * TODO: compute this once when loading filesystem and store in ext2_filesystem_t
|
---|
324 | */
|
---|
325 | block_ids_per_block = ext4_superblock_get_block_size(fs->superblock) / sizeof(uint32_t);
|
---|
326 | limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
---|
327 | blocks_per_level[0] = 1;
|
---|
328 | for (i = 1; i < 4; i++) {
|
---|
329 | blocks_per_level[i] = blocks_per_level[i-1] *
|
---|
330 | block_ids_per_block;
|
---|
331 | limits[i] = limits[i-1] + blocks_per_level[i];
|
---|
332 | }
|
---|
333 |
|
---|
334 | /* Determine the indirection level needed to get the desired block */
|
---|
335 | level = -1;
|
---|
336 | for (i = 1; i < 4; i++) {
|
---|
337 | if (iblock < limits[i]) {
|
---|
338 | level = i;
|
---|
339 | break;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | if (level == -1) {
|
---|
344 | return EIO;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /* Compute offsets for the topmost level */
|
---|
348 | block_offset_in_level = iblock - limits[level-1];
|
---|
349 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
350 | offset_in_block = block_offset_in_level / blocks_per_level[level-1];
|
---|
351 |
|
---|
352 | /* Navigate through other levels, until we find the block number
|
---|
353 | * or find null reference meaning we are dealing with sparse file
|
---|
354 | */
|
---|
355 | while (level > 0) {
|
---|
356 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
357 | if (rc != EOK) {
|
---|
358 | return rc;
|
---|
359 | }
|
---|
360 |
|
---|
361 | assert(offset_in_block < block_ids_per_block);
|
---|
362 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
363 |
|
---|
364 | rc = block_put(block);
|
---|
365 | if (rc != EOK) {
|
---|
366 | return rc;
|
---|
367 | }
|
---|
368 |
|
---|
369 | if (current_block == 0) {
|
---|
370 | /* This is a sparse file */
|
---|
371 | *fblock = 0;
|
---|
372 | return EOK;
|
---|
373 | }
|
---|
374 |
|
---|
375 | level -= 1;
|
---|
376 |
|
---|
377 | /* If we are on the last level, break here as
|
---|
378 | * there is no next level to visit
|
---|
379 | */
|
---|
380 | if (level == 0) {
|
---|
381 | break;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* Visit the next level */
|
---|
385 | block_offset_in_level %= blocks_per_level[level];
|
---|
386 | offset_in_block = block_offset_in_level / blocks_per_level[level-1];
|
---|
387 | }
|
---|
388 |
|
---|
389 | *fblock = current_block;
|
---|
390 |
|
---|
391 | return EOK;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * @}
|
---|
396 | */
|
---|