source: mainline/uspace/lib/ext4/libext4_filesystem.c@ 35f48f2

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

succesful writing (only for direct block)

  • Property mode set to 100644
File size: 13.2 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 int rc;
46 ext4_superblock_t *temp_superblock;
47 size_t block_size;
48 uint32_t block_ids_per_block;
49 int i;
50
51 fs->device = service_id;
52
53 // TODO what does constant 2048 mean?
54 rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
55 if (rc != EOK) {
56 return rc;
57 }
58
59 /* Read superblock from device */
60 rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
61 if (rc != EOK) {
62 block_fini(fs->device);
63 return rc;
64 }
65
66 /* Read block size from superblock and check */
67 block_size = ext4_superblock_get_block_size(temp_superblock);
68 if (block_size > EXT4_MAX_BLOCK_SIZE) {
69 block_fini(fs->device);
70 return ENOTSUP;
71 }
72
73 /* Initialize block caching */
74 rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
75 if (rc != EOK) {
76 block_fini(fs->device);
77 return rc;
78 }
79
80 block_ids_per_block = block_size / sizeof(uint32_t);
81 fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
82 fs->inode_blocks_per_level[0] = 1;
83 for (i = 1; i < 4; i++) {
84 fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i-1] *
85 block_ids_per_block;
86 fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
87 fs->inode_blocks_per_level[i];
88 }
89
90 /* Return loaded superblock */
91 fs->superblock = temp_superblock;
92
93 return EOK;
94}
95
96void ext4_filesystem_fini(ext4_filesystem_t *fs)
97{
98 free(fs->superblock);
99 block_fini(fs->device);
100}
101
102int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
103{
104 int rc;
105
106 rc = ext4_superblock_check_sanity(fs->superblock);
107 if (rc != EOK) {
108 return rc;
109 }
110
111 return EOK;
112}
113
114int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
115{
116 /* Feature flags are present in rev 1 and later */
117 if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
118 *o_read_only = false;
119 return EOK;
120 }
121
122 uint32_t incompatible_features;
123 incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
124 incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
125 if (incompatible_features > 0) {
126 *o_read_only = true;
127 return ENOTSUP;
128 }
129
130 uint32_t compatible_read_only;
131 compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
132 compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
133 if (compatible_read_only > 0) {
134 *o_read_only = true;
135 }
136
137 return EOK;
138}
139
140int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
141 ext4_block_group_ref_t **ref)
142{
143 int rc;
144 aoff64_t block_id;
145 uint32_t descriptors_per_block;
146 size_t offset;
147 ext4_block_group_ref_t *newref;
148
149 newref = malloc(sizeof(ext4_block_group_ref_t));
150 if (newref == NULL) {
151 return ENOMEM;
152 }
153
154 descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
155 / ext4_superblock_get_desc_size(fs->superblock);
156
157 /* Block group descriptor table starts at the next block after superblock */
158 block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
159
160 /* Find the block containing the descriptor we are looking for */
161 block_id += bgid / descriptors_per_block;
162 offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
163
164 rc = block_get(&newref->block, fs->device, block_id, 0);
165 if (rc != EOK) {
166 free(newref);
167 return rc;
168 }
169
170 newref->block_group = newref->block->data + offset;
171 newref->dirty = false;
172
173 *ref = newref;
174
175 return EOK;
176}
177
178int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
179{
180 int rc;
181
182 if (ref->dirty) {
183 ref->block->dirty = true;
184 }
185
186 rc = block_put(ref->block);
187 free(ref);
188
189 return rc;
190}
191
192int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
193 ext4_inode_ref_t **ref)
194{
195 int rc;
196 aoff64_t block_id;
197 uint32_t block_group;
198 uint32_t offset_in_group;
199 uint32_t byte_offset_in_group;
200 size_t offset_in_block;
201 uint32_t inodes_per_group;
202 uint32_t inode_table_start;
203 uint16_t inode_size;
204 uint32_t block_size;
205 ext4_block_group_ref_t *bg_ref;
206 ext4_inode_ref_t *newref;
207
208 newref = malloc(sizeof(ext4_inode_ref_t));
209 if (newref == NULL) {
210 return ENOMEM;
211 }
212
213 inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
214
215 /* inode numbers are 1-based, but it is simpler to work with 0-based
216 * when computing indices
217 */
218 index -= 1;
219 block_group = index / inodes_per_group;
220 offset_in_group = index % inodes_per_group;
221
222 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
223 if (rc != EOK) {
224 free(newref);
225 return rc;
226 }
227
228 inode_table_start = ext4_block_group_get_inode_table_first_block(
229 bg_ref->block_group);
230
231 rc = ext4_filesystem_put_block_group_ref(bg_ref);
232 if (rc != EOK) {
233 free(newref);
234 return rc;
235 }
236
237 inode_size = ext4_superblock_get_inode_size(fs->superblock);
238 block_size = ext4_superblock_get_block_size(fs->superblock);
239
240 byte_offset_in_group = offset_in_group * inode_size;
241
242 block_id = inode_table_start + (byte_offset_in_group / block_size);
243 offset_in_block = byte_offset_in_group % block_size;
244
245 rc = block_get(&newref->block, fs->device, block_id, 0);
246 if (rc != EOK) {
247 free(newref);
248 return rc;
249 }
250
251 newref->inode = newref->block->data + offset_in_block;
252 /* we decremented index above, but need to store the original value
253 * in the reference
254 */
255 newref->index = index+1;
256 newref->dirty = false;
257
258 *ref = newref;
259
260 return EOK;
261}
262
263
264int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
265{
266 int rc;
267
268 if (ref->dirty) {
269 ref->block->dirty = true;
270 }
271
272 rc = block_put(ref->block);
273 free(ref);
274
275 return rc;
276}
277
278int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
279 aoff64_t iblock, uint32_t* fblock)
280{
281 int rc;
282 uint32_t offset_in_block;
283 uint32_t current_block;
284 aoff64_t block_offset_in_level;
285 int i;
286 int level;
287 block_t *block;
288
289 /* Handle inode using extents */
290 if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
291 ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
292 current_block = ext4_inode_get_extent_block(inode, iblock, fs->device);
293 *fblock = current_block;
294 return EOK;
295
296 }
297
298 /* Handle simple case when we are dealing with direct reference */
299 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
300 current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
301 *fblock = current_block;
302 return EOK;
303 }
304
305 /* Determine the indirection level needed to get the desired block */
306 level = -1;
307 for (i = 1; i < 4; i++) {
308 if (iblock < fs->inode_block_limits[i]) {
309 level = i;
310 break;
311 }
312 }
313
314 if (level == -1) {
315 return EIO;
316 }
317
318 /* Compute offsets for the topmost level */
319 block_offset_in_level = iblock - fs->inode_block_limits[level-1];
320 current_block = ext4_inode_get_indirect_block(inode, level-1);
321 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
322
323 /* Navigate through other levels, until we find the block number
324 * or find null reference meaning we are dealing with sparse file
325 */
326 while (level > 0) {
327 rc = block_get(&block, fs->device, current_block, 0);
328 if (rc != EOK) {
329 return rc;
330 }
331
332 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
333
334 rc = block_put(block);
335 if (rc != EOK) {
336 return rc;
337 }
338
339 if (current_block == 0) {
340 /* This is a sparse file */
341 *fblock = 0;
342 return EOK;
343 }
344
345 level -= 1;
346
347 /* If we are on the last level, break here as
348 * there is no next level to visit
349 */
350 if (level == 0) {
351 break;
352 }
353
354 /* Visit the next level */
355 block_offset_in_level %= fs->inode_blocks_per_level[level];
356 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
357 }
358
359 *fblock = current_block;
360
361 return EOK;
362}
363
364
365int ext4_filesystem_set_inode_data_block_index(ext4_filesystem_t *fs,
366 ext4_inode_t *inode, aoff64_t iblock, uint32_t fblock)
367{
368
369// int rc;
370// uint32_t offset_in_block;
371// uint32_t current_block;
372// aoff64_t block_offset_in_level;
373// int i;
374// int level;
375// block_t *block;
376
377 /* Handle inode using extents */
378 if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
379 ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
380 // TODO
381 return ENOTSUP;
382
383 }
384
385 /* Handle simple case when we are dealing with direct reference */
386 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
387 ext4_inode_set_direct_block(inode, (uint32_t)iblock, fblock);
388 return EOK;
389 }
390
391// /* Determine the indirection level needed to get the desired block */
392// level = -1;
393// for (i = 1; i < 4; i++) {
394// if (iblock < fs->inode_block_limits[i]) {
395// level = i;
396// break;
397// }
398// }
399//
400// if (level == -1) {
401// return EIO;
402// }
403//
404// /* Compute offsets for the topmost level */
405// block_offset_in_level = iblock - fs->inode_block_limits[level-1];
406// current_block = ext4_inode_get_indirect_block(inode, level-1);
407// offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
408//
409// /* Navigate through other levels, until we find the block number
410// * or find null reference meaning we are dealing with sparse file
411// */
412// while (level > 0) {
413// rc = block_get(&block, fs->device, current_block, 0);
414// if (rc != EOK) {
415// return rc;
416// }
417//
418// current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
419//
420// rc = block_put(block);
421// if (rc != EOK) {
422// return rc;
423// }
424//
425// if (current_block == 0) {
426// /* This is a sparse file */
427// *fblock = 0;
428// return EOK;
429// }
430//
431// level -= 1;
432//
433// /* If we are on the last level, break here as
434// * there is no next level to visit
435// */
436// if (level == 0) {
437// break;
438// }
439//
440// /* Visit the next level */
441// block_offset_in_level %= fs->inode_blocks_per_level[level];
442// offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
443// }
444//
445// *fblock = current_block;
446//
447// return EOK;
448//
449//
450
451 return EOK;
452}
453
454int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs,
455 ext4_inode_ref_t *inode_ref, uint32_t iblock)
456{
457 int rc;
458 uint32_t fblock;
459 int i;
460 int level;
461 aoff64_t block_offset_in_level;
462 uint32_t current_block;
463 uint32_t offset_in_block;
464 block_t *block;
465 ext4_inode_t *inode = inode_ref->inode;
466
467 /* TODO handle extents */
468
469
470 /* Handle simple case when we are dealing with direct reference */
471 if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
472 fblock = ext4_inode_get_direct_block(inode, iblock);
473 // Sparse file
474 if (fblock == 0) {
475 return EOK;
476 }
477
478 ext4_inode_set_direct_block(inode, iblock, 0);
479 return ext4_bitmap_free_block(fs, fblock);
480 }
481
482
483 /* Determine the indirection level needed to get the desired block */
484 level = -1;
485 for (i = 1; i < 4; i++) {
486 if (iblock < fs->inode_block_limits[i]) {
487 level = i;
488 break;
489 }
490 }
491
492 if (level == -1) {
493 return EIO;
494 }
495
496 /* Compute offsets for the topmost level */
497 block_offset_in_level = iblock - fs->inode_block_limits[level-1];
498 current_block = ext4_inode_get_indirect_block(inode, level-1);
499 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
500
501 /* Navigate through other levels, until we find the block number
502 * or find null reference meaning we are dealing with sparse file
503 */
504 while (level > 0) {
505 rc = block_get(&block, fs->device, current_block, 0);
506 if (rc != EOK) {
507 return rc;
508 }
509
510 current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
511
512 // Set zero
513 if (level == 1) {
514 ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
515 block->dirty = true;
516 }
517
518 rc = block_put(block);
519 if (rc != EOK) {
520 return rc;
521 }
522
523 level -= 1;
524
525 /* If we are on the last level, break here as
526 * there is no next level to visit
527 */
528 if (level == 0) {
529 break;
530 }
531
532 /* Visit the next level */
533 block_offset_in_level %= fs->inode_blocks_per_level[level];
534 offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
535 }
536
537 fblock = current_block;
538
539 if (fblock == 0) {
540 return EOK;
541 }
542
543 return ext4_bitmap_free_block(fs, fblock);
544
545}
546
547
548/**
549 * @}
550 */
Note: See TracBrowser for help on using the repository browser.