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 | * @file libext4_ialloc.c
|
---|
34 | * @brief I-node (de)allocation operations.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <time.h>
|
---|
39 | #include "libext4.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /** Convert i-node number to relative index in block group.
|
---|
43 | *
|
---|
44 | * @param sb Superblock
|
---|
45 | * @param inode I-node number to be converted
|
---|
46 | *
|
---|
47 | * @return Index of the i-node in the block group
|
---|
48 | *
|
---|
49 | */
|
---|
50 | static uint32_t ext4_ialloc_inode2index_in_group(ext4_superblock_t *sb,
|
---|
51 | uint32_t inode)
|
---|
52 | {
|
---|
53 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
54 | return (inode - 1) % inodes_per_group;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Convert relative index of i-node to absolute i-node number.
|
---|
58 | *
|
---|
59 | * @param sb Superblock
|
---|
60 | * @param inode Index to be converted
|
---|
61 | *
|
---|
62 | * @return Absolute number of the i-node
|
---|
63 | *
|
---|
64 | */
|
---|
65 | static uint32_t ext4_ialloc_index_in_group2inode(ext4_superblock_t *sb,
|
---|
66 | uint32_t index, uint32_t bgid)
|
---|
67 | {
|
---|
68 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
69 | return bgid * inodes_per_group + (index + 1);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Compute block group number from the i-node number.
|
---|
73 | *
|
---|
74 | * @param sb Superblock
|
---|
75 | * @param inode I-node number to be found the block group for
|
---|
76 | *
|
---|
77 | * @return Block group number computed from i-node number
|
---|
78 | *
|
---|
79 | */
|
---|
80 | static uint32_t ext4_ialloc_get_bgid_of_inode(ext4_superblock_t *sb,
|
---|
81 | uint32_t inode)
|
---|
82 | {
|
---|
83 | uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
|
---|
84 | return (inode - 1) / inodes_per_group;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | /** Free i-node number and modify filesystem data structers.
|
---|
89 | *
|
---|
90 | * @param fs Filesystem, where the i-node is located
|
---|
91 | * @param index Index of i-node to be release
|
---|
92 | * @param is_dir Flag us for information whether i-node is directory or not
|
---|
93 | *
|
---|
94 | */
|
---|
95 | int ext4_ialloc_free_inode(ext4_filesystem_t *fs, uint32_t index, bool is_dir)
|
---|
96 | {
|
---|
97 | ext4_superblock_t *sb = fs->superblock;
|
---|
98 |
|
---|
99 | /* Compute index of block group and load it */
|
---|
100 | uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
|
---|
101 |
|
---|
102 | ext4_block_group_ref_t *bg_ref;
|
---|
103 | int rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
|
---|
104 | if (rc != EOK)
|
---|
105 | return rc;
|
---|
106 |
|
---|
107 | /* Load i-node bitmap */
|
---|
108 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
109 | bg_ref->block_group, sb);
|
---|
110 | block_t *bitmap_block;
|
---|
111 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
|
---|
112 | BLOCK_FLAGS_NONE);
|
---|
113 | if (rc != EOK)
|
---|
114 | return rc;
|
---|
115 |
|
---|
116 | /* Free i-node in the bitmap */
|
---|
117 | uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, index);
|
---|
118 | ext4_bitmap_free_bit(bitmap_block->data, index_in_group);
|
---|
119 | bitmap_block->dirty = true;
|
---|
120 |
|
---|
121 | /* Put back the block with bitmap */
|
---|
122 | rc = block_put(bitmap_block);
|
---|
123 | if (rc != EOK) {
|
---|
124 | /* Error in saving bitmap */
|
---|
125 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* If released i-node is a directory, decrement used directories count */
|
---|
130 | if (is_dir) {
|
---|
131 | uint32_t bg_used_dirs = ext4_block_group_get_used_dirs_count(
|
---|
132 | bg_ref->block_group, sb);
|
---|
133 | bg_used_dirs--;
|
---|
134 | ext4_block_group_set_used_dirs_count(bg_ref->block_group, sb,
|
---|
135 | bg_used_dirs);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* Update block group free inodes count */
|
---|
139 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(
|
---|
140 | bg_ref->block_group, sb);
|
---|
141 | free_inodes++;
|
---|
142 | ext4_block_group_set_free_inodes_count(bg_ref->block_group, sb,
|
---|
143 | free_inodes);
|
---|
144 |
|
---|
145 | bg_ref->dirty = true;
|
---|
146 |
|
---|
147 | /* Put back the modified block group */
|
---|
148 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
149 | if (rc != EOK)
|
---|
150 | return rc;
|
---|
151 |
|
---|
152 | /* Update superblock free inodes count */
|
---|
153 | uint32_t sb_free_inodes =
|
---|
154 | ext4_superblock_get_free_inodes_count(sb);
|
---|
155 | sb_free_inodes++;
|
---|
156 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
157 |
|
---|
158 | return EOK;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** I-node allocation algorithm.
|
---|
162 | *
|
---|
163 | * This is more simple algorithm, than Orlov allocator used
|
---|
164 | * in the Linux kernel.
|
---|
165 | *
|
---|
166 | * @param fs Filesystem to allocate i-node on
|
---|
167 | * @param index Output value - allocated i-node number
|
---|
168 | * @param is_dir Flag if allocated i-node will be file or directory
|
---|
169 | *
|
---|
170 | * @return Error code
|
---|
171 | *
|
---|
172 | */
|
---|
173 | int ext4_ialloc_alloc_inode(ext4_filesystem_t *fs, uint32_t *index, bool is_dir)
|
---|
174 | {
|
---|
175 | ext4_superblock_t *sb = fs->superblock;
|
---|
176 |
|
---|
177 | uint32_t bgid = 0;
|
---|
178 | uint32_t bg_count = ext4_superblock_get_block_group_count(sb);
|
---|
179 | uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb);
|
---|
180 | uint32_t avg_free_inodes = sb_free_inodes / bg_count;
|
---|
181 |
|
---|
182 | /* Try to find free i-node in all block groups */
|
---|
183 | while (bgid < bg_count) {
|
---|
184 | /* Load block group to check */
|
---|
185 | ext4_block_group_ref_t *bg_ref;
|
---|
186 | int rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
|
---|
187 | if (rc != EOK)
|
---|
188 | return rc;
|
---|
189 |
|
---|
190 | ext4_block_group_t *bg = bg_ref->block_group;
|
---|
191 |
|
---|
192 | /* Read necessary values for algorithm */
|
---|
193 | uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg, sb);
|
---|
194 | uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb);
|
---|
195 | uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb);
|
---|
196 |
|
---|
197 | /* Check if this block group is good candidate for allocation */
|
---|
198 | if ((free_inodes >= avg_free_inodes) && (free_blocks > 0)) {
|
---|
199 | /* Load block with bitmap */
|
---|
200 | uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap(
|
---|
201 | bg_ref->block_group, sb);
|
---|
202 |
|
---|
203 | block_t *bitmap_block;
|
---|
204 | rc = block_get(&bitmap_block, fs->device, bitmap_block_addr,
|
---|
205 | BLOCK_FLAGS_NONE);
|
---|
206 | if (rc != EOK)
|
---|
207 | return rc;
|
---|
208 |
|
---|
209 | /* Try to allocate i-node in the bitmap */
|
---|
210 | uint32_t inodes_in_group = ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
211 | uint32_t index_in_group;
|
---|
212 | rc = ext4_bitmap_find_free_bit_and_set(bitmap_block->data,
|
---|
213 | 0, &index_in_group, inodes_in_group);
|
---|
214 |
|
---|
215 | /* Block group has not any free i-node */
|
---|
216 | if (rc == ENOSPC) {
|
---|
217 | rc = block_put(bitmap_block);
|
---|
218 | if (rc != EOK) {
|
---|
219 | ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
224 | if (rc != EOK)
|
---|
225 | return rc;
|
---|
226 |
|
---|
227 | continue;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Free i-node found, save the bitmap */
|
---|
231 | bitmap_block->dirty = true;
|
---|
232 |
|
---|
233 | rc = block_put(bitmap_block);
|
---|
234 | if (rc != EOK)
|
---|
235 | return rc;
|
---|
236 |
|
---|
237 | /* Modify filesystem counters */
|
---|
238 | free_inodes--;
|
---|
239 | ext4_block_group_set_free_inodes_count(bg, sb, free_inodes);
|
---|
240 |
|
---|
241 | /* Increment used directories counter */
|
---|
242 | if (is_dir) {
|
---|
243 | used_dirs++;
|
---|
244 | ext4_block_group_set_used_dirs_count(bg, sb, used_dirs);
|
---|
245 | }
|
---|
246 |
|
---|
247 | /* Decrease unused inodes count */
|
---|
248 | if (ext4_block_group_has_flag(bg,
|
---|
249 | EXT4_BLOCK_GROUP_ITABLE_ZEROED)) {
|
---|
250 | uint32_t unused =
|
---|
251 | ext4_block_group_get_itable_unused(bg, sb);
|
---|
252 |
|
---|
253 | uint32_t inodes_in_group =
|
---|
254 | ext4_superblock_get_inodes_in_group(sb, bgid);
|
---|
255 |
|
---|
256 | uint32_t free = inodes_in_group - unused;
|
---|
257 |
|
---|
258 | if (index_in_group >= free) {
|
---|
259 | unused = inodes_in_group - (index_in_group + 1);
|
---|
260 | ext4_block_group_set_itable_unused(bg, sb, unused);
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* Save modified block group */
|
---|
265 | bg_ref->dirty = true;
|
---|
266 |
|
---|
267 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
268 | if (rc != EOK)
|
---|
269 | return rc;
|
---|
270 |
|
---|
271 | /* Update superblock */
|
---|
272 | sb_free_inodes--;
|
---|
273 | ext4_superblock_set_free_inodes_count(sb, sb_free_inodes);
|
---|
274 |
|
---|
275 | /* Compute the absolute i-nodex number */
|
---|
276 | *index = ext4_ialloc_index_in_group2inode(sb, index_in_group, bgid);
|
---|
277 |
|
---|
278 | return EOK;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* Block group not modified, put it and jump to the next block group */
|
---|
282 | rc = ext4_filesystem_put_block_group_ref(bg_ref);
|
---|
283 | if (rc != EOK)
|
---|
284 | return rc;
|
---|
285 |
|
---|
286 | ++bgid;
|
---|
287 | }
|
---|
288 |
|
---|
289 | return ENOSPC;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * @}
|
---|
294 | */
|
---|