source: mainline/uspace/lib/ext4/libext4_filesystem.c@ c25e39b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c25e39b was c25e39b, checked in by Frantisek Princ <frantisek.princ@…>, 14 years ago

bugfix, TODO resolving

  • Property mode set to 100644
File size: 9.1 KB
Line 
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
43int 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
85void ext4_filesystem_fini(ext4_filesystem_t *fs)
86{
87 free(fs->superblock);
88 block_fini(fs->device);
89}
90
91int 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
103int 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
129int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
130 ext4_block_group_ref_t **ref)
131{
132 int rc;
133 aoff64_t block_id;
134 uint32_t descriptors_per_block;
135 size_t offset;
136 ext4_block_group_ref_t *newref;
137
138 newref = malloc(sizeof(ext4_block_group_ref_t));
139 if (newref == NULL) {
140 return ENOMEM;
141 }
142
143 //EXT4FS_DBG("desc size = \%u", (uint32_t)ext4_superblock_get_desc_size(fs->superblock));
144
145 descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
146 / ext4_superblock_get_desc_size(fs->superblock);
147
148 /* Block group descriptor table starts at the next block after superblock */
149 block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
150
151 /* Find the block containing the descriptor we are looking for */
152 block_id += bgid / descriptors_per_block;
153 offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
154
155 rc = block_get(&newref->block, fs->device, block_id, 0);
156 if (rc != EOK) {
157 free(newref);
158 return rc;
159 }
160
161 newref->block_group = newref->block->data + offset;
162
163 *ref = newref;
164
165 return EOK;
166}
167
168int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
169{
170 int rc;
171
172 rc = block_put(ref->block);
173 free(ref);
174
175 return rc;
176}
177
178int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
179 ext4_inode_ref_t **ref)
180{
181 int rc;
182 aoff64_t block_id;
183 uint32_t block_group;
184 uint32_t offset_in_group;
185 uint32_t byte_offset_in_group;
186 size_t offset_in_block;
187 uint32_t inodes_per_group;
188 uint32_t inode_table_start;
189 uint16_t inode_size;
190 uint32_t block_size;
191 ext4_block_group_ref_t *bg_ref;
192 ext4_inode_ref_t *newref;
193
194 newref = malloc(sizeof(ext4_inode_ref_t));
195 if (newref == NULL) {
196 return ENOMEM;
197 }
198
199 inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
200
201 /* inode numbers are 1-based, but it is simpler to work with 0-based
202 * when computing indices
203 */
204 index -= 1;
205 block_group = index / inodes_per_group;
206 offset_in_group = index % inodes_per_group;
207
208 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
209 if (rc != EOK) {
210 free(newref);
211 return rc;
212 }
213
214 inode_table_start = ext4_block_group_get_inode_table_first_block(
215 bg_ref->block_group);
216
217 rc = ext4_filesystem_put_block_group_ref(bg_ref);
218 if (rc != EOK) {
219 free(newref);
220 return rc;
221 }
222
223 inode_size = ext4_superblock_get_inode_size(fs->superblock);
224 block_size = ext4_superblock_get_block_size(fs->superblock);
225
226 byte_offset_in_group = offset_in_group * inode_size;
227
228 block_id = inode_table_start + (byte_offset_in_group / block_size);
229 offset_in_block = byte_offset_in_group % block_size;
230
231 rc = block_get(&newref->block, fs->device, block_id, 0);
232 if (rc != EOK) {
233 free(newref);
234 return rc;
235 }
236
237 newref->inode = newref->block->data + offset_in_block;
238 /* we decremented index above, but need to store the original value
239 * in the reference
240 */
241 newref->index = index+1;
242
243 *ref = newref;
244
245 return EOK;
246}
247
248
249int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
250{
251 int rc;
252
253 rc = block_put(ref->block);
254 free(ref);
255
256 return rc;
257}
258
259int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
260 aoff64_t iblock, uint32_t* fblock)
261{
262 int rc;
263 aoff64_t limits[4];
264 uint32_t block_ids_per_block;
265 aoff64_t blocks_per_level[4];
266 uint32_t offset_in_block;
267 uint32_t current_block;
268 aoff64_t block_offset_in_level;
269 int i;
270 int level;
271 block_t *block;
272
273 /* Handle inode using extents */
274 if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
275 ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
276 current_block = ext4_inode_get_extent_block(inode, iblock, fs->device);
277 *fblock = current_block;
278 return EOK;
279
280 }
281
282 /* Handle simple case when we are dealing with direct reference */
283 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
284 current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
285 *fblock = current_block;
286 return EOK;
287 }
288
289 /* Compute limits for indirect block levels
290 * TODO: compute this once when loading filesystem and store in ext2_filesystem_t
291 */
292 block_ids_per_block = ext4_superblock_get_block_size(fs->superblock) / sizeof(uint32_t);
293 limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
294 blocks_per_level[0] = 1;
295 for (i = 1; i < 4; i++) {
296 blocks_per_level[i] = blocks_per_level[i-1] *
297 block_ids_per_block;
298 limits[i] = limits[i-1] + blocks_per_level[i];
299 }
300
301 /* Determine the indirection level needed to get the desired block */
302 level = -1;
303 for (i = 1; i < 4; i++) {
304 if (iblock < limits[i]) {
305 level = i;
306 break;
307 }
308 }
309
310 if (level == -1) {
311 return EIO;
312 }
313
314 /* Compute offsets for the topmost level */
315 block_offset_in_level = iblock - limits[level-1];
316 current_block = ext4_inode_get_indirect_block(inode, level-1);
317 offset_in_block = block_offset_in_level / blocks_per_level[level-1];
318
319 /* Navigate through other levels, until we find the block number
320 * or find null reference meaning we are dealing with sparse file
321 */
322 while (level > 0) {
323 rc = block_get(&block, fs->device, current_block, 0);
324 if (rc != EOK) {
325 return rc;
326 }
327
328 assert(offset_in_block < block_ids_per_block);
329 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
330
331 rc = block_put(block);
332 if (rc != EOK) {
333 return rc;
334 }
335
336 if (current_block == 0) {
337 /* This is a sparse file */
338 *fblock = 0;
339 return EOK;
340 }
341
342 level -= 1;
343
344 /* If we are on the last level, break here as
345 * there is no next level to visit
346 */
347 if (level == 0) {
348 break;
349 }
350
351 /* Visit the next level */
352 block_offset_in_level %= blocks_per_level[level];
353 offset_in_block = block_offset_in_level / blocks_per_level[level-1];
354 }
355
356 *fblock = current_block;
357
358 return EOK;
359}
360
361/**
362 * @}
363 */
Note: See TracBrowser for help on using the repository browser.