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