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 |
|
---|
42 | static 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 |
|
---|
49 | static 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 |
|
---|
58 | int ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
|
---|
59 | {
|
---|
60 | int rc;
|
---|
61 |
|
---|
62 | ext4_superblock_t *sb = fs->superblock;
|
---|
63 |
|
---|
64 | uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, 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 | return rc;
|
---|
70 | }
|
---|
71 |
|
---|
72 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
73 | bg_ref->block_group, sb);
|
---|
74 | block_t *bitmap_block;
|
---|
75 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
76 | if (rc != EOK) {
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
|
---|
81 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
82 | bitmap_block->dirty = true;
|
---|
83 |
|
---|
84 | rc = block_put(bitmap_block);
|
---|
85 | if (rc != EOK) {
|
---|
86 | // Error in saving bitmap
|
---|
87 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // if inode is directory, decrement used directories count
|
---|
92 | if (is_dir) {
|
---|
93 | uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
|
---|
94 | bg_ref->block_group, sb);
|
---|
95 | bg_used_dirs--;
|
---|
96 | ext4_block_group_set_used_dirs_count(
|
---|
97 | bg_ref->block_group, sb, bg_used_dirs);
|
---|
98 | }
|
---|
99 |
|
---|
100 | // Update block group free inodes count
|
---|
101 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
|
---|
102 | bg_ref->block_group, sb);
|
---|
103 | free_inodes++;
|
---|
104 | ext4_block_group_set_free_inodes_count(bg_ref->block_group,
|
---|
105 | sb, free_inodes);
|
---|
106 | bg_ref->dirty = true;
|
---|
107 |
|
---|
108 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
109 | if (rc != EOK) {
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // Update superblock free inodes count
|
---|
114 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
115 | sb_free_inodes++;
|
---|
116 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
117 |
|
---|
118 | return EOK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | int ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
|
---|
122 | {
|
---|
123 | int rc;
|
---|
124 |
|
---|
125 | ext4_superblock_t *sb = fs->superblock;
|
---|
126 |
|
---|
127 | uint32_t bgid = 0;
|
---|
128 | uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
|
---|
129 |
|
---|
130 | while (bgid < bg_count) {
|
---|
131 |
|
---|
132 | ext4_block_group_ref_t *bg_ref;
|
---|
133 | rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
|
---|
134 | if (rc != EOK) {
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 | ext4_block_group_t *bg = bg_ref->block_group;
|
---|
138 |
|
---|
139 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
|
---|
140 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
|
---|
141 | uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
|
---|
142 |
|
---|
143 | if ((free_inodes > 0) && (free_blocks > 0)) {
|
---|
144 |
|
---|
145 | uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
|
---|
146 | bg_ref->block_group, sb);
|
---|
147 |
|
---|
148 | block_t *bitmap_block;
|
---|
149 | rc = block_get(&bitmap_block, fs->device,
|
---|
150 | bitmap_block_addr, BLOCK_FLAGS_NONE);
|
---|
151 | if (rc != EOK) {
|
---|
152 | return rc;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // Alloc bit
|
---|
156 | uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
157 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data, 0, index, inodes_in_group);
|
---|
158 | if (rc == ENOSPC) {
|
---|
159 | block_put(bitmap_block);
|
---|
160 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
161 | continue;
|
---|
162 | }
|
---|
163 |
|
---|
164 | bitmap_block->dirty = true;
|
---|
165 |
|
---|
166 | rc = block_put(bitmap_block);
|
---|
167 | if (rc != EOK) {
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|
171 | // Modify filesystem counters
|
---|
172 | free_inodes--;
|
---|
173 | ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
|
---|
174 |
|
---|
175 | if (is_dir) {
|
---|
176 | used_dirs--;
|
---|
177 | ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
|
---|
178 | }
|
---|
179 |
|
---|
180 | bg_ref->dirty = true;
|
---|
181 |
|
---|
182 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
183 | if (rc != EOK) {
|
---|
184 | // TODO
|
---|
185 | }
|
---|
186 |
|
---|
187 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
188 | sb_free_inodes--;
|
---|
189 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
190 |
|
---|
191 | return EOK;
|
---|
192 |
|
---|
193 | } else {
|
---|
194 | // Not modified
|
---|
195 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
196 | }
|
---|
197 |
|
---|
198 | }
|
---|
199 |
|
---|
200 | return ENOSPC;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * @}
|
---|
205 | */
|
---|