source: mainline/uspace/lib/ext4/libext4_ialloc.c@ ebcaff4

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

Fixed bugs in directory removal process

  • Property mode set to 100644
File size: 4.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_ialloc.c
35 * @brief Inode (de)allocation operations.
36 */
37
38#include <errno.h>
39#include <time.h>
40#include "libext4.h"
41
42static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
43 uint32_t inode)
44{
45 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
46 return (inode - 1) % inodes_per_group;
47}
48
49static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
50 uint32_t inode)
51{
52 uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
53 return (inode - 1) / inodes_per_group;
54
55}
56
57
58int ext4_ialloc_free_inode(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
59{
60 int rc;
61 uint32_t block_group = ext4_ialloc_get_bgid_of_inode(
62 fs->superblock, inode_ref->index);
63 uint32_t index_in_group = ext4_ialloc_inode2index_in_group(
64 fs->superblock, inode_ref->index);
65
66 ext4_block_group_ref_t *bg_ref;
67 rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
68 if (rc != EOK) {
69 EXT4FS_DBG("error in loading bg_ref \%d", rc);
70 return rc;
71 }
72
73 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
74 bg_ref->block_group, fs->superblock);
75 block_t *bitmap_block;
76 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
77 if (rc != EOK) {
78 EXT4FS_DBG("error in loading bitmap \%d", rc);
79 return rc;
80 }
81
82 ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
83 bitmap_block->dirty = true;
84
85 rc = block_put(bitmap_block);
86 if (rc != EOK) {
87 // Error in saving bitmap
88 ext4_filesystem_put_block_group_ref(bg_ref);
89 EXT4FS_DBG("error in saving bitmap \%d", rc);
90 return rc;
91 }
92
93 time_t now = time(NULL);
94 ext4_inode_set_deletion_time(inode_ref->inode, (uint32_t)now);
95 inode_ref->dirty = true;
96
97 // if inode is directory, decrement directories count
98 if (ext4_inode_is_type(fs->superblock, inode_ref->inode, EXT4_INODE_MODE_DIRECTORY)) {
99 uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
100 bg_ref->block_group, fs->superblock);
101 bg_used_dirs--;
102 ext4_block_group_set_used_dirs_count(
103 bg_ref->block_group, fs->superblock, bg_used_dirs);
104 }
105
106 // Update superblock free inodes count
107 uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(fs->superblock);
108 sb_free_inodes++;
109 ext4_superblock_set_free_inodes_count(fs->superblock, sb_free_inodes);
110
111 // Update block group free inodes count
112 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
113 bg_ref->block_group, fs->superblock);
114 free_inodes++;
115 ext4_block_group_set_free_inodes_count(bg_ref->block_group,
116 fs->superblock, free_inodes);
117 bg_ref->dirty = true;
118
119 rc = ext4_filesystem_put_block_group_ref(bg_ref);
120 if (rc != EOK) {
121 EXT4FS_DBG("error in saving bg_ref \%d", rc);
122 // TODO error
123 return rc;
124 }
125
126 return EOK;
127}
128
129
130/**
131 * @}
132 */
Note: See TracBrowser for help on using the repository browser.