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 More complex filesystem operations.
|
---|
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 | int rc;
|
---|
46 |
|
---|
47 | fs->device = service_id;
|
---|
48 |
|
---|
49 | rc = block_init(EXCHANGE_SERIALIZE, fs->device, 4096);
|
---|
50 | if (rc != EOK) {
|
---|
51 | return rc;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /* Read superblock from device */
|
---|
55 | ext4_superblock_t *temp_superblock;
|
---|
56 | rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
|
---|
57 | if (rc != EOK) {
|
---|
58 | block_fini(fs->device);
|
---|
59 | return rc;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /* Read block size from superblock and check */
|
---|
63 | uint32_t block_size = ext4_superblock_get_block_size(temp_superblock);
|
---|
64 | if (block_size > EXT4_MAX_BLOCK_SIZE) {
|
---|
65 | block_fini(fs->device);
|
---|
66 | return ENOTSUP;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* Initialize block caching */
|
---|
70 | rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
|
---|
71 | if (rc != EOK) {
|
---|
72 | block_fini(fs->device);
|
---|
73 | return rc;
|
---|
74 | }
|
---|
75 |
|
---|
76 | uint32_t block_ids_per_block = block_size / sizeof(uint32_t);
|
---|
77 | fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
|
---|
78 | fs->inode_blocks_per_level[0] = 1;
|
---|
79 | for (int i = 1; i < 4; i++) {
|
---|
80 | fs->inode_blocks_per_level[i] = fs->inode_blocks_per_level[i-1] *
|
---|
81 | block_ids_per_block;
|
---|
82 | fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
|
---|
83 | fs->inode_blocks_per_level[i];
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* Return loaded superblock */
|
---|
87 | fs->superblock = temp_superblock;
|
---|
88 |
|
---|
89 | return EOK;
|
---|
90 | }
|
---|
91 |
|
---|
92 | int ext4_filesystem_fini(ext4_filesystem_t *fs, bool write_sb)
|
---|
93 | {
|
---|
94 | int rc = EOK;
|
---|
95 |
|
---|
96 | if (write_sb) {
|
---|
97 | rc = ext4_superblock_write_direct(fs->device, fs->superblock);
|
---|
98 | }
|
---|
99 |
|
---|
100 | free(fs->superblock);
|
---|
101 | block_fini(fs->device);
|
---|
102 |
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 | int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
|
---|
107 | {
|
---|
108 | int rc;
|
---|
109 |
|
---|
110 | rc = ext4_superblock_check_sanity(fs->superblock);
|
---|
111 | if (rc != EOK) {
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return EOK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
|
---|
119 | {
|
---|
120 | /* Feature flags are present in rev 1 and later */
|
---|
121 | if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
|
---|
122 | *o_read_only = false;
|
---|
123 | return EOK;
|
---|
124 | }
|
---|
125 |
|
---|
126 | uint32_t incompatible_features;
|
---|
127 | incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
|
---|
128 | incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
|
---|
129 | if (incompatible_features > 0) {
|
---|
130 | *o_read_only = true;
|
---|
131 | return ENOTSUP;
|
---|
132 | }
|
---|
133 |
|
---|
134 | uint32_t compatible_read_only;
|
---|
135 | compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
|
---|
136 | compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
|
---|
137 | if (compatible_read_only > 0) {
|
---|
138 | *o_read_only = true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | return EOK;
|
---|
142 | }
|
---|
143 |
|
---|
144 | int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
|
---|
145 | ext4_block_group_ref_t **ref)
|
---|
146 | {
|
---|
147 | int rc;
|
---|
148 |
|
---|
149 | ext4_block_group_ref_t *newref = malloc(sizeof(ext4_block_group_ref_t));
|
---|
150 | if (newref == NULL) {
|
---|
151 | return ENOMEM;
|
---|
152 | }
|
---|
153 |
|
---|
154 | uint32_t 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 | aoff64_t 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 | uint32_t 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->fs = fs;
|
---|
172 | newref->index = bgid;
|
---|
173 | newref->dirty = false;
|
---|
174 |
|
---|
175 | *ref = newref;
|
---|
176 |
|
---|
177 | return EOK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | static uint16_t ext4_filesystem_bg_checksum(ext4_superblock_t *sb, uint32_t bgid,
|
---|
181 | ext4_block_group_t *bg)
|
---|
182 | {
|
---|
183 | uint16_t crc = 0;
|
---|
184 |
|
---|
185 | if (ext4_superblock_has_feature_read_only(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
|
---|
186 |
|
---|
187 | void *base = bg;
|
---|
188 | void *checksum = &bg->checksum;
|
---|
189 |
|
---|
190 | uint32_t offset = (uint32_t)(checksum - base);
|
---|
191 |
|
---|
192 | uint32_t le_group = host2uint32_t_le(bgid);
|
---|
193 |
|
---|
194 | crc = crc16(~0, sb->uuid, sizeof(sb->uuid));
|
---|
195 | crc = crc16(crc, (uint8_t *)&le_group, sizeof(le_group));
|
---|
196 | crc = crc16(crc, (uint8_t *)bg, offset);
|
---|
197 |
|
---|
198 | offset += sizeof(bg->checksum); /* skip checksum */
|
---|
199 |
|
---|
200 | /* for checksum of struct ext4_group_desc do the rest...*/
|
---|
201 | if ((ext4_superblock_has_feature_incompatible(sb, EXT4_FEATURE_INCOMPAT_64BIT)) &&
|
---|
202 | offset < ext4_superblock_get_desc_size(sb)) {
|
---|
203 |
|
---|
204 | crc = crc16(crc, ((uint8_t *)bg) + offset, ext4_superblock_get_desc_size(sb) - offset);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | return crc;
|
---|
209 |
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
|
---|
214 | {
|
---|
215 | int rc;
|
---|
216 |
|
---|
217 | if (ref->dirty) {
|
---|
218 | uint16_t checksum = ext4_filesystem_bg_checksum(
|
---|
219 | ref->fs->superblock, ref->index, ref->block_group);
|
---|
220 |
|
---|
221 | ext4_block_group_set_checksum(ref->block_group, checksum);
|
---|
222 |
|
---|
223 | ref->block->dirty = true;
|
---|
224 | }
|
---|
225 |
|
---|
226 | rc = block_put(ref->block);
|
---|
227 | free(ref);
|
---|
228 |
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 |
|
---|
232 | int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
|
---|
233 | ext4_inode_ref_t **ref)
|
---|
234 | {
|
---|
235 | int rc;
|
---|
236 |
|
---|
237 | ext4_inode_ref_t *newref = malloc(sizeof(ext4_inode_ref_t));
|
---|
238 | if (newref == NULL) {
|
---|
239 | return ENOMEM;
|
---|
240 | }
|
---|
241 |
|
---|
242 | uint32_t inodes_per_group =
|
---|
243 | ext4_superblock_get_inodes_per_group(fs->superblock);
|
---|
244 |
|
---|
245 | /* inode numbers are 1-based, but it is simpler to work with 0-based
|
---|
246 | * when computing indices
|
---|
247 | */
|
---|
248 | index -= 1;
|
---|
249 | uint32_t block_group = index / inodes_per_group;
|
---|
250 | uint32_t offset_in_group = index % inodes_per_group;
|
---|
251 |
|
---|
252 | ext4_block_group_ref_t *bg_ref;
|
---|
253 | rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
254 | if (rc != EOK) {
|
---|
255 | free(newref);
|
---|
256 | return rc;
|
---|
257 | }
|
---|
258 |
|
---|
259 | uint32_t inode_table_start = ext4_block_group_get_inode_table_first_block(
|
---|
260 | bg_ref->block_group, fs->superblock);
|
---|
261 |
|
---|
262 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
263 | if (rc != EOK) {
|
---|
264 | free(newref);
|
---|
265 | return rc;
|
---|
266 | }
|
---|
267 |
|
---|
268 | uint16_t inode_size = ext4_superblock_get_inode_size(fs->superblock);
|
---|
269 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
270 | uint32_t byte_offset_in_group = offset_in_group * inode_size;
|
---|
271 |
|
---|
272 | aoff64_t block_id = inode_table_start + (byte_offset_in_group / block_size);
|
---|
273 | rc = block_get(&newref->block, fs->device, block_id, 0);
|
---|
274 | if (rc != EOK) {
|
---|
275 | free(newref);
|
---|
276 | return rc;
|
---|
277 | }
|
---|
278 |
|
---|
279 | uint32_t offset_in_block = byte_offset_in_group % block_size;
|
---|
280 | newref->inode = newref->block->data + offset_in_block;
|
---|
281 | /* we decremented index above, but need to store the original value
|
---|
282 | * in the reference
|
---|
283 | */
|
---|
284 | newref->index = index + 1;
|
---|
285 | newref->fs = fs;
|
---|
286 | newref->dirty = false;
|
---|
287 |
|
---|
288 | *ref = newref;
|
---|
289 |
|
---|
290 | return EOK;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
|
---|
295 | {
|
---|
296 | int rc;
|
---|
297 |
|
---|
298 | if (ref->dirty) {
|
---|
299 | ref->block->dirty = true;
|
---|
300 | }
|
---|
301 |
|
---|
302 | rc = block_put(ref->block);
|
---|
303 | free(ref);
|
---|
304 |
|
---|
305 | return rc;
|
---|
306 | }
|
---|
307 |
|
---|
308 | int ext4_filesystem_alloc_inode(ext4_filesystem_t *fs,
|
---|
309 | ext4_inode_ref_t **inode_ref, int flags)
|
---|
310 | {
|
---|
311 | int rc;
|
---|
312 |
|
---|
313 | bool is_dir = false;
|
---|
314 | if (flags & L_DIRECTORY) {
|
---|
315 | is_dir = true;
|
---|
316 | }
|
---|
317 |
|
---|
318 | // allocate inode
|
---|
319 | uint32_t index;
|
---|
320 | rc = ext4_ialloc_alloc_inode(fs, &index, is_dir);
|
---|
321 | if (rc != EOK) {
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 | // TODO dir_index initialization
|
---|
326 |
|
---|
327 | rc = ext4_filesystem_get_inode_ref(fs, index, inode_ref);
|
---|
328 | if (rc != EOK) {
|
---|
329 | ext4_ialloc_free_inode(fs, index, is_dir);
|
---|
330 | return rc;
|
---|
331 | }
|
---|
332 |
|
---|
333 | // init inode
|
---|
334 | ext4_inode_t *inode = (*inode_ref)->inode;
|
---|
335 |
|
---|
336 | if (is_dir) {
|
---|
337 | ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_DIRECTORY);
|
---|
338 | ext4_inode_set_links_count(inode, 1); // '.' entry
|
---|
339 | } else {
|
---|
340 | ext4_inode_set_mode(fs->superblock, inode, EXT4_INODE_MODE_FILE);
|
---|
341 | ext4_inode_set_links_count(inode, 0);
|
---|
342 | }
|
---|
343 |
|
---|
344 | if (ext4_superblock_has_feature_incompatible(
|
---|
345 | fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
|
---|
346 | ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
|
---|
347 | }
|
---|
348 |
|
---|
349 | ext4_inode_set_uid(inode, 0);
|
---|
350 | ext4_inode_set_gid(inode, 0);
|
---|
351 | ext4_inode_set_size(inode, 0);
|
---|
352 | ext4_inode_set_access_time(inode, 0);
|
---|
353 | ext4_inode_set_change_inode_time(inode, 0);
|
---|
354 | ext4_inode_set_modification_time(inode, 0);
|
---|
355 | ext4_inode_set_deletion_time(inode, 0);
|
---|
356 | ext4_inode_set_blocks_count(fs->superblock, inode, 0);
|
---|
357 | ext4_inode_set_flags(inode, 0);
|
---|
358 | ext4_inode_set_generation(inode, 0);
|
---|
359 |
|
---|
360 | for (uint32_t i = 0; i < EXT4_INODE_BLOCKS; i++) {
|
---|
361 | inode->blocks[i] = 0;
|
---|
362 | }
|
---|
363 |
|
---|
364 | (*inode_ref)->dirty = true;
|
---|
365 |
|
---|
366 | return EOK;
|
---|
367 | }
|
---|
368 |
|
---|
369 | int ext4_filesystem_free_inode(ext4_inode_ref_t *inode_ref)
|
---|
370 | {
|
---|
371 | int rc;
|
---|
372 |
|
---|
373 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
374 |
|
---|
375 | if (ext4_superblock_has_feature_incompatible(
|
---|
376 | fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
377 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
378 |
|
---|
379 | // Data structures are released during truncate operation...
|
---|
380 | goto finish;
|
---|
381 | }
|
---|
382 |
|
---|
383 | // release all indirect (no data) blocks
|
---|
384 |
|
---|
385 | // 1) Single indirect
|
---|
386 | uint32_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0);
|
---|
387 | if (fblock != 0) {
|
---|
388 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
389 | if (rc != EOK) {
|
---|
390 | return rc;
|
---|
391 | }
|
---|
392 |
|
---|
393 | ext4_inode_set_indirect_block(inode_ref->inode, 0, 0);
|
---|
394 | }
|
---|
395 |
|
---|
396 | block_t *block;
|
---|
397 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
398 | uint32_t count = block_size / sizeof(uint32_t);
|
---|
399 |
|
---|
400 | // 2) Double indirect
|
---|
401 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1);
|
---|
402 | if (fblock != 0) {
|
---|
403 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
404 | if (rc != EOK) {
|
---|
405 | return rc;
|
---|
406 | }
|
---|
407 |
|
---|
408 | uint32_t ind_block;
|
---|
409 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
410 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
411 |
|
---|
412 | if (ind_block != 0) {
|
---|
413 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
414 | if (rc != EOK) {
|
---|
415 | block_put(block);
|
---|
416 | return rc;
|
---|
417 | }
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | block_put(block);
|
---|
422 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
423 | if (rc != EOK) {
|
---|
424 | return rc;
|
---|
425 | }
|
---|
426 |
|
---|
427 | ext4_inode_set_indirect_block(inode_ref->inode, 1, 0);
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | // 3) Tripple indirect
|
---|
432 | block_t *subblock;
|
---|
433 | fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2);
|
---|
434 | if (fblock != 0) {
|
---|
435 | rc = block_get(&block, fs->device, fblock, BLOCK_FLAGS_NONE);
|
---|
436 | if (rc != EOK) {
|
---|
437 | return rc;
|
---|
438 | }
|
---|
439 |
|
---|
440 | uint32_t ind_block;
|
---|
441 | for (uint32_t offset = 0; offset < count; ++offset) {
|
---|
442 | ind_block = uint32_t_le2host(((uint32_t*)block->data)[offset]);
|
---|
443 |
|
---|
444 | if (ind_block != 0) {
|
---|
445 | rc = block_get(&subblock, fs->device, ind_block, BLOCK_FLAGS_NONE);
|
---|
446 | if (rc != EOK) {
|
---|
447 | block_put(block);
|
---|
448 | return rc;
|
---|
449 | }
|
---|
450 |
|
---|
451 | uint32_t ind_subblock;
|
---|
452 | for (uint32_t suboffset = 0; suboffset < count; ++suboffset) {
|
---|
453 | ind_subblock = uint32_t_le2host(((uint32_t*)subblock->data)[suboffset]);
|
---|
454 |
|
---|
455 | if (ind_subblock != 0) {
|
---|
456 | rc = ext4_balloc_free_block(inode_ref, ind_subblock);
|
---|
457 | if (rc != EOK) {
|
---|
458 | block_put(subblock);
|
---|
459 | block_put(block);
|
---|
460 | return rc;
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | }
|
---|
465 | block_put(subblock);
|
---|
466 |
|
---|
467 | }
|
---|
468 |
|
---|
469 | rc = ext4_balloc_free_block(inode_ref, ind_block);
|
---|
470 | if (rc != EOK) {
|
---|
471 | block_put(block);
|
---|
472 | return rc;
|
---|
473 | }
|
---|
474 |
|
---|
475 |
|
---|
476 | }
|
---|
477 |
|
---|
478 | block_put(block);
|
---|
479 | rc = ext4_balloc_free_block(inode_ref, fblock);
|
---|
480 | if (rc != EOK) {
|
---|
481 | return rc;
|
---|
482 | }
|
---|
483 |
|
---|
484 | ext4_inode_set_indirect_block(inode_ref->inode, 2, 0);
|
---|
485 | }
|
---|
486 |
|
---|
487 | finish:
|
---|
488 | inode_ref->dirty = true;
|
---|
489 |
|
---|
490 | // Free inode
|
---|
491 | if (ext4_inode_is_type(fs->superblock, inode_ref->inode,
|
---|
492 | EXT4_INODE_MODE_DIRECTORY)) {
|
---|
493 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, true);
|
---|
494 | } else {
|
---|
495 | rc = ext4_ialloc_free_inode(fs, inode_ref->index, false);
|
---|
496 | }
|
---|
497 | if (rc != EOK) {
|
---|
498 | return rc;
|
---|
499 | }
|
---|
500 |
|
---|
501 | return EOK;
|
---|
502 | }
|
---|
503 |
|
---|
504 | int ext4_filesystem_truncate_inode(
|
---|
505 | ext4_inode_ref_t *inode_ref, aoff64_t new_size)
|
---|
506 | {
|
---|
507 | int rc;
|
---|
508 |
|
---|
509 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
510 |
|
---|
511 | if (! ext4_inode_can_truncate(sb, inode_ref->inode)) {
|
---|
512 | // Unable to truncate
|
---|
513 | return EINVAL;
|
---|
514 | }
|
---|
515 |
|
---|
516 | aoff64_t old_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
517 | if (old_size == new_size) {
|
---|
518 | // Nothing to do
|
---|
519 | return EOK;
|
---|
520 | }
|
---|
521 |
|
---|
522 | // It's not suppported to make the larger file
|
---|
523 | if (old_size < new_size) {
|
---|
524 | return EINVAL;
|
---|
525 | }
|
---|
526 |
|
---|
527 | aoff64_t size_diff = old_size - new_size;
|
---|
528 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
529 | uint32_t diff_blocks_count = size_diff / block_size;
|
---|
530 | if (size_diff % block_size != 0) {
|
---|
531 | diff_blocks_count++;
|
---|
532 | }
|
---|
533 |
|
---|
534 | uint32_t old_blocks_count = old_size / block_size;
|
---|
535 | if (old_size % block_size != 0) {
|
---|
536 | old_blocks_count++;
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (ext4_superblock_has_feature_incompatible(
|
---|
540 | inode_ref->fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
541 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
542 |
|
---|
543 | rc = ext4_extent_release_blocks_from(inode_ref, old_blocks_count - diff_blocks_count);
|
---|
544 | if (rc != EOK) {
|
---|
545 | return rc;
|
---|
546 | }
|
---|
547 | } else {
|
---|
548 | // starting from 1 because of logical blocks are numbered from 0
|
---|
549 | for (uint32_t i = 1; i <= diff_blocks_count; ++i) {
|
---|
550 | rc = ext4_filesystem_release_inode_block(inode_ref, old_blocks_count - i);
|
---|
551 | if (rc != EOK) {
|
---|
552 | return rc;
|
---|
553 | }
|
---|
554 | }
|
---|
555 | }
|
---|
556 |
|
---|
557 | ext4_inode_set_size(inode_ref->inode, new_size);
|
---|
558 |
|
---|
559 | inode_ref->dirty = true;
|
---|
560 |
|
---|
561 | return EOK;
|
---|
562 | }
|
---|
563 |
|
---|
564 | int ext4_filesystem_get_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
565 | aoff64_t iblock, uint32_t* fblock)
|
---|
566 | {
|
---|
567 | int rc;
|
---|
568 |
|
---|
569 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
570 |
|
---|
571 | uint32_t current_block;
|
---|
572 |
|
---|
573 | /* Handle inode using extents */
|
---|
574 | if (ext4_superblock_has_feature_incompatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
575 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
576 | rc = ext4_extent_find_block(inode_ref, iblock, ¤t_block);
|
---|
577 |
|
---|
578 | if (rc != EOK) {
|
---|
579 | return rc;
|
---|
580 | }
|
---|
581 |
|
---|
582 | *fblock = current_block;
|
---|
583 | return EOK;
|
---|
584 |
|
---|
585 | }
|
---|
586 |
|
---|
587 | ext4_inode_t *inode = inode_ref->inode;
|
---|
588 |
|
---|
589 | /* Handle simple case when we are dealing with direct reference */
|
---|
590 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
591 | current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
|
---|
592 | *fblock = current_block;
|
---|
593 | return EOK;
|
---|
594 | }
|
---|
595 |
|
---|
596 | /* Determine the indirection level needed to get the desired block */
|
---|
597 | int level = -1;
|
---|
598 | for (int i = 1; i < 4; i++) {
|
---|
599 | if (iblock < fs->inode_block_limits[i]) {
|
---|
600 | level = i;
|
---|
601 | break;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | if (level == -1) {
|
---|
606 | return EIO;
|
---|
607 | }
|
---|
608 |
|
---|
609 | /* Compute offsets for the topmost level */
|
---|
610 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
611 | current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
612 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
613 |
|
---|
614 | if (current_block == 0) {
|
---|
615 | *fblock = 0;
|
---|
616 | return EOK;
|
---|
617 | }
|
---|
618 |
|
---|
619 | block_t *block;
|
---|
620 |
|
---|
621 | /* Navigate through other levels, until we find the block number
|
---|
622 | * or find null reference meaning we are dealing with sparse file
|
---|
623 | */
|
---|
624 | while (level > 0) {
|
---|
625 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
626 | if (rc != EOK) {
|
---|
627 | return rc;
|
---|
628 | }
|
---|
629 |
|
---|
630 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
631 |
|
---|
632 | rc = block_put(block);
|
---|
633 | if (rc != EOK) {
|
---|
634 | return rc;
|
---|
635 | }
|
---|
636 |
|
---|
637 | if (current_block == 0) {
|
---|
638 | /* This is a sparse file */
|
---|
639 | *fblock = 0;
|
---|
640 | return EOK;
|
---|
641 | }
|
---|
642 |
|
---|
643 | level -= 1;
|
---|
644 |
|
---|
645 | /* If we are on the last level, break here as
|
---|
646 | * there is no next level to visit
|
---|
647 | */
|
---|
648 | if (level == 0) {
|
---|
649 | break;
|
---|
650 | }
|
---|
651 |
|
---|
652 | /* Visit the next level */
|
---|
653 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
654 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
655 | }
|
---|
656 |
|
---|
657 | *fblock = current_block;
|
---|
658 |
|
---|
659 | return EOK;
|
---|
660 | }
|
---|
661 |
|
---|
662 |
|
---|
663 | int ext4_filesystem_set_inode_data_block_index(ext4_inode_ref_t *inode_ref,
|
---|
664 | aoff64_t iblock, uint32_t fblock)
|
---|
665 | {
|
---|
666 | int rc;
|
---|
667 |
|
---|
668 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
669 |
|
---|
670 | /* Handle inode using extents */
|
---|
671 | if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
672 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
673 | // not reachable !!!
|
---|
674 | return ENOTSUP;
|
---|
675 | }
|
---|
676 |
|
---|
677 | /* Handle simple case when we are dealing with direct reference */
|
---|
678 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
679 | ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, fblock);
|
---|
680 | inode_ref->dirty = true;
|
---|
681 | return EOK;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* Determine the indirection level needed to get the desired block */
|
---|
685 | int level = -1;
|
---|
686 | for (int i = 1; i < 4; i++) {
|
---|
687 | if (iblock < fs->inode_block_limits[i]) {
|
---|
688 | level = i;
|
---|
689 | break;
|
---|
690 | }
|
---|
691 | }
|
---|
692 |
|
---|
693 | if (level == -1) {
|
---|
694 | return EIO;
|
---|
695 | }
|
---|
696 |
|
---|
697 | uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
|
---|
698 |
|
---|
699 | /* Compute offsets for the topmost level */
|
---|
700 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
701 | uint32_t current_block = ext4_inode_get_indirect_block(inode_ref->inode, level-1);
|
---|
702 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
703 |
|
---|
704 | uint32_t new_block_addr;
|
---|
705 | block_t *block, *new_block;
|
---|
706 |
|
---|
707 | if (current_block == 0) {
|
---|
708 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
709 | if (rc != EOK) {
|
---|
710 | return rc;
|
---|
711 | }
|
---|
712 |
|
---|
713 | ext4_inode_set_indirect_block(inode_ref->inode, level - 1, new_block_addr);
|
---|
714 |
|
---|
715 | inode_ref->dirty = true;
|
---|
716 |
|
---|
717 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
718 | if (rc != EOK) {
|
---|
719 | ext4_balloc_free_block(inode_ref, new_block_addr);
|
---|
720 | return rc;
|
---|
721 | }
|
---|
722 |
|
---|
723 | memset(new_block->data, 0, block_size);
|
---|
724 | new_block->dirty = true;
|
---|
725 |
|
---|
726 | rc = block_put(new_block);
|
---|
727 | if (rc != EOK) {
|
---|
728 | return rc;
|
---|
729 | }
|
---|
730 |
|
---|
731 | current_block = new_block_addr;
|
---|
732 | }
|
---|
733 |
|
---|
734 | /* Navigate through other levels, until we find the block number
|
---|
735 | * or find null reference meaning we are dealing with sparse file
|
---|
736 | */
|
---|
737 | while (level > 0) {
|
---|
738 |
|
---|
739 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
740 | if (rc != EOK) {
|
---|
741 | return rc;
|
---|
742 | }
|
---|
743 |
|
---|
744 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
745 |
|
---|
746 | if ((level > 1) && (current_block == 0)) {
|
---|
747 | rc = ext4_balloc_alloc_block(inode_ref, &new_block_addr);
|
---|
748 | if (rc != EOK) {
|
---|
749 | block_put(block);
|
---|
750 | return rc;
|
---|
751 | }
|
---|
752 |
|
---|
753 | rc = block_get(&new_block, fs->device, new_block_addr, BLOCK_FLAGS_NOREAD);
|
---|
754 | if (rc != EOK) {
|
---|
755 | block_put(block);
|
---|
756 | return rc;
|
---|
757 | }
|
---|
758 |
|
---|
759 | memset(new_block->data, 0, block_size);
|
---|
760 | new_block->dirty = true;
|
---|
761 |
|
---|
762 | rc = block_put(new_block);
|
---|
763 | if (rc != EOK) {
|
---|
764 | block_put(block);
|
---|
765 | return rc;
|
---|
766 | }
|
---|
767 |
|
---|
768 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(new_block_addr);
|
---|
769 | block->dirty = true;
|
---|
770 | current_block = new_block_addr;
|
---|
771 | }
|
---|
772 |
|
---|
773 | if (level == 1) {
|
---|
774 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(fblock);
|
---|
775 | block->dirty = true;
|
---|
776 | }
|
---|
777 |
|
---|
778 | rc = block_put(block);
|
---|
779 | if (rc != EOK) {
|
---|
780 | return rc;
|
---|
781 | }
|
---|
782 |
|
---|
783 | level -= 1;
|
---|
784 |
|
---|
785 | /* If we are on the last level, break here as
|
---|
786 | * there is no next level to visit
|
---|
787 | */
|
---|
788 | if (level == 0) {
|
---|
789 | break;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /* Visit the next level */
|
---|
793 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
794 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
795 | }
|
---|
796 |
|
---|
797 | return EOK;
|
---|
798 | }
|
---|
799 |
|
---|
800 | int ext4_filesystem_release_inode_block(
|
---|
801 | ext4_inode_ref_t *inode_ref, uint32_t iblock)
|
---|
802 | {
|
---|
803 | int rc;
|
---|
804 |
|
---|
805 | uint32_t fblock;
|
---|
806 |
|
---|
807 | ext4_filesystem_t *fs = inode_ref->fs;
|
---|
808 |
|
---|
809 | // EXTENTS are handled otherwise
|
---|
810 | assert(! (ext4_superblock_has_feature_incompatible(fs->superblock,
|
---|
811 | EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
812 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)));
|
---|
813 |
|
---|
814 | ext4_inode_t *inode = inode_ref->inode;
|
---|
815 |
|
---|
816 | /* Handle simple case when we are dealing with direct reference */
|
---|
817 | if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
|
---|
818 | fblock = ext4_inode_get_direct_block(inode, iblock);
|
---|
819 | // Sparse file
|
---|
820 | if (fblock == 0) {
|
---|
821 | return EOK;
|
---|
822 | }
|
---|
823 |
|
---|
824 | ext4_inode_set_direct_block(inode, iblock, 0);
|
---|
825 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
826 | }
|
---|
827 |
|
---|
828 |
|
---|
829 | /* Determine the indirection level needed to get the desired block */
|
---|
830 | int level = -1;
|
---|
831 | for (int i = 1; i < 4; i++) {
|
---|
832 | if (iblock < fs->inode_block_limits[i]) {
|
---|
833 | level = i;
|
---|
834 | break;
|
---|
835 | }
|
---|
836 | }
|
---|
837 |
|
---|
838 | if (level == -1) {
|
---|
839 | return EIO;
|
---|
840 | }
|
---|
841 |
|
---|
842 | /* Compute offsets for the topmost level */
|
---|
843 | aoff64_t block_offset_in_level = iblock - fs->inode_block_limits[level-1];
|
---|
844 | uint32_t current_block = ext4_inode_get_indirect_block(inode, level-1);
|
---|
845 | uint32_t offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
846 |
|
---|
847 | /* Navigate through other levels, until we find the block number
|
---|
848 | * or find null reference meaning we are dealing with sparse file
|
---|
849 | */
|
---|
850 | block_t *block;
|
---|
851 | while (level > 0) {
|
---|
852 | rc = block_get(&block, fs->device, current_block, 0);
|
---|
853 | if (rc != EOK) {
|
---|
854 | return rc;
|
---|
855 | }
|
---|
856 |
|
---|
857 | current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
|
---|
858 |
|
---|
859 | // Set zero
|
---|
860 | if (level == 1) {
|
---|
861 | ((uint32_t*)block->data)[offset_in_block] = host2uint32_t_le(0);
|
---|
862 | block->dirty = true;
|
---|
863 | }
|
---|
864 |
|
---|
865 | rc = block_put(block);
|
---|
866 | if (rc != EOK) {
|
---|
867 | return rc;
|
---|
868 | }
|
---|
869 |
|
---|
870 | level -= 1;
|
---|
871 |
|
---|
872 | /* If we are on the last level, break here as
|
---|
873 | * there is no next level to visit
|
---|
874 | */
|
---|
875 | if (level == 0) {
|
---|
876 | break;
|
---|
877 | }
|
---|
878 |
|
---|
879 | /* Visit the next level */
|
---|
880 | block_offset_in_level %= fs->inode_blocks_per_level[level];
|
---|
881 | offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
|
---|
882 | }
|
---|
883 |
|
---|
884 | fblock = current_block;
|
---|
885 |
|
---|
886 | if (fblock == 0) {
|
---|
887 | return EOK;
|
---|
888 | }
|
---|
889 |
|
---|
890 | return ext4_balloc_free_block(inode_ref, fblock);
|
---|
891 |
|
---|
892 | }
|
---|
893 |
|
---|
894 | int ext4_filesystem_append_inode_block(ext4_inode_ref_t *inode_ref,
|
---|
895 | uint32_t *fblock, uint32_t *iblock)
|
---|
896 | {
|
---|
897 | int rc;
|
---|
898 |
|
---|
899 | // Handle extents separately
|
---|
900 | if (ext4_superblock_has_feature_incompatible(
|
---|
901 | inode_ref->fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
|
---|
902 | ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)) {
|
---|
903 |
|
---|
904 | return ext4_extent_append_block(inode_ref, iblock, fblock);
|
---|
905 |
|
---|
906 | }
|
---|
907 |
|
---|
908 | ext4_superblock_t *sb = inode_ref->fs->superblock;
|
---|
909 |
|
---|
910 | // Compute next block index and allocate data block
|
---|
911 | uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
|
---|
912 | uint32_t block_size = ext4_superblock_get_block_size(sb);
|
---|
913 |
|
---|
914 | assert(inode_size % block_size == 0);
|
---|
915 |
|
---|
916 | // Logical blocks are numbered from 0
|
---|
917 | uint32_t new_block_idx = inode_size / block_size;
|
---|
918 |
|
---|
919 | uint32_t phys_block;
|
---|
920 | rc = ext4_balloc_alloc_block(inode_ref, &phys_block);
|
---|
921 | if (rc != EOK) {
|
---|
922 | return rc;
|
---|
923 | }
|
---|
924 |
|
---|
925 | rc = ext4_filesystem_set_inode_data_block_index(inode_ref, new_block_idx, phys_block);
|
---|
926 | if (rc != EOK) {
|
---|
927 | ext4_balloc_free_block(inode_ref, phys_block);
|
---|
928 | return rc;
|
---|
929 | }
|
---|
930 |
|
---|
931 | ext4_inode_set_size(inode_ref->inode, inode_size + block_size);
|
---|
932 |
|
---|
933 | inode_ref->dirty = true;
|
---|
934 |
|
---|
935 | *fblock = phys_block;
|
---|
936 | *iblock = new_block_idx;
|
---|
937 | return EOK;
|
---|
938 | }
|
---|
939 |
|
---|
940 | int ext4_filesystem_add_orphan(ext4_inode_ref_t *inode_ref)
|
---|
941 | {
|
---|
942 | uint32_t next_orphan = ext4_superblock_get_last_orphan(
|
---|
943 | inode_ref->fs->superblock);
|
---|
944 | ext4_inode_set_deletion_time(inode_ref->inode, next_orphan);
|
---|
945 | ext4_superblock_set_last_orphan(
|
---|
946 | inode_ref->fs->superblock, inode_ref->index);
|
---|
947 | inode_ref->dirty = true;
|
---|
948 |
|
---|
949 | return EOK;
|
---|
950 | }
|
---|
951 |
|
---|
952 | int ext4_filesystem_delete_orphan(ext4_inode_ref_t *inode_ref)
|
---|
953 | {
|
---|
954 | int rc;
|
---|
955 |
|
---|
956 | uint32_t last_orphan = ext4_superblock_get_last_orphan(
|
---|
957 | inode_ref->fs->superblock);
|
---|
958 | assert(last_orphan > 0);
|
---|
959 |
|
---|
960 | uint32_t next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
|
---|
961 |
|
---|
962 | if (last_orphan == inode_ref->index) {
|
---|
963 | ext4_superblock_set_last_orphan(inode_ref->fs->superblock, next_orphan);
|
---|
964 | ext4_inode_set_deletion_time(inode_ref->inode, 0);
|
---|
965 | inode_ref->dirty = true;
|
---|
966 | return EOK;
|
---|
967 | }
|
---|
968 |
|
---|
969 | ext4_inode_ref_t *current;
|
---|
970 | rc = ext4_filesystem_get_inode_ref(inode_ref->fs, last_orphan, ¤t);
|
---|
971 | if (rc != EOK) {
|
---|
972 | return rc;
|
---|
973 | }
|
---|
974 | next_orphan = ext4_inode_get_deletion_time(current->inode);
|
---|
975 |
|
---|
976 | bool found;
|
---|
977 |
|
---|
978 | while (next_orphan != 0) {
|
---|
979 | if (next_orphan == inode_ref->index) {
|
---|
980 | next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
|
---|
981 | ext4_inode_set_deletion_time(current->inode, next_orphan);
|
---|
982 | current->dirty = true;
|
---|
983 | found = true;
|
---|
984 | break;
|
---|
985 | }
|
---|
986 |
|
---|
987 | ext4_filesystem_put_inode_ref(current);
|
---|
988 |
|
---|
989 | rc = ext4_filesystem_get_inode_ref(inode_ref->fs, next_orphan, ¤t);
|
---|
990 | if (rc != EOK) {
|
---|
991 | return rc;
|
---|
992 | }
|
---|
993 | next_orphan = ext4_inode_get_deletion_time(current->inode);
|
---|
994 |
|
---|
995 | }
|
---|
996 |
|
---|
997 | ext4_inode_set_deletion_time(inode_ref->inode, 0);
|
---|
998 |
|
---|
999 | rc = ext4_filesystem_put_inode_ref(current);
|
---|
1000 | if (rc != EOK) {
|
---|
1001 | return rc;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | if (!found) {
|
---|
1005 | return ENOENT;
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | return EOK;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /**
|
---|
1012 | * @}
|
---|
1013 | */
|
---|