blob: 0c829edb02688132c2ab5a934cb5a4cc86230fb9 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * alloc.c --- allocate new inodes, blocks for ext2fs
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12#include "config.h"
13#include <stdio.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#include <time.h>
18#include <string.h>
19#if HAVE_SYS_STAT_H
20#include <sys/stat.h>
21#endif
22#if HAVE_SYS_TYPES_H
23#include <sys/types.h>
24#endif
25
26#include "ext2_fs.h"
27#include "ext2fs.h"
28
29/*
30 * Check for uninit block bitmaps and deal with them appropriately
31 */
32static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
33 dgrp_t group)
34{
35 blk_t i;
36 blk64_t blk, super_blk, old_desc_blk, new_desc_blk;
37 int old_desc_blocks;
38
39 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
40 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
41 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
42 return;
43
44 blk = ext2fs_group_first_block2(fs, group);
45
46 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
47 &old_desc_blk, &new_desc_blk, 0);
48
49 if (fs->super->s_feature_incompat &
50 EXT2_FEATURE_INCOMPAT_META_BG)
51 old_desc_blocks = fs->super->s_first_meta_bg;
52 else
53 old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
54
55 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++)
56 ext2fs_fast_unmark_block_bitmap2(map, blk);
57
58 blk = ext2fs_group_first_block2(fs, group);
59 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
60 if ((blk == super_blk) ||
61 (old_desc_blk && old_desc_blocks &&
62 (blk >= old_desc_blk) &&
63 (blk < old_desc_blk + old_desc_blocks)) ||
64 (new_desc_blk && (blk == new_desc_blk)) ||
65 (blk == ext2fs_block_bitmap_loc(fs, group)) ||
66 (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
67 (blk >= ext2fs_inode_table_loc(fs, group) &&
68 (blk < ext2fs_inode_table_loc(fs, group)
69 + fs->inode_blocks_per_group)))
70 ext2fs_fast_mark_block_bitmap2(map, blk);
71 }
72 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
73 ext2fs_group_desc_csum_set(fs, group);
74 ext2fs_mark_super_dirty(fs);
75 ext2fs_mark_bb_dirty(fs);
76}
77
78/*
79 * Check for uninit inode bitmaps and deal with them appropriately
80 */
81static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
82 dgrp_t group)
83{
84 ext2_ino_t i, ino;
85
86 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
87 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
88 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
89 return;
90
91 ino = (group * fs->super->s_inodes_per_group) + 1;
92 for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
93 ext2fs_fast_unmark_inode_bitmap2(map, ino);
94
95 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
96 ext2fs_group_desc_csum_set(fs, group);
97 ext2fs_mark_ib_dirty(fs);
98 ext2fs_mark_super_dirty(fs);
99 check_block_uninit(fs, fs->block_map, group);
100}
101
102/*
103 * Right now, just search forward from the parent directory's block
104 * group to find the next free inode.
105 *
106 * Should have a special policy for directories.
107 */
108errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
109 int mode EXT2FS_ATTR((unused)),
110 ext2fs_inode_bitmap map, ext2_ino_t *ret)
111{
112 ext2_ino_t start_inode = 0;
113 ext2_ino_t i, ino_in_group, upto, first_zero;
114 errcode_t retval;
115 dgrp_t group;
116
117 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
118
119 if (!map)
120 map = fs->inode_map;
121 if (!map)
122 return EXT2_ET_NO_INODE_BITMAP;
123
124 if (dir > 0) {
125 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
126 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
127 }
128 if (start_inode < EXT2_FIRST_INODE(fs->super))
129 start_inode = EXT2_FIRST_INODE(fs->super);
130 if (start_inode > fs->super->s_inodes_count)
131 return EXT2_ET_INODE_ALLOC_FAIL;
132 i = start_inode;
133 do {
134 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
135 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
136
137 check_inode_uninit(fs, map, group);
138 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
139 if (i < start_inode && upto >= start_inode)
140 upto = start_inode - 1;
141 if (upto > fs->super->s_inodes_count)
142 upto = fs->super->s_inodes_count;
143
144 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
145 &first_zero);
146 if (retval == 0) {
147 i = first_zero;
148 break;
149 }
150 if (retval != ENOENT)
151 return EXT2_ET_INODE_ALLOC_FAIL;
152 i = upto + 1;
153 if (i > fs->super->s_inodes_count)
154 i = EXT2_FIRST_INODE(fs->super);
155 } while (i != start_inode);
156
157 if (ext2fs_test_inode_bitmap2(map, i))
158 return EXT2_ET_INODE_ALLOC_FAIL;
159 *ret = i;
160 return 0;
161}
162
163/*
164 * Stupid algorithm --- we now just search forward starting from the
165 * goal. Should put in a smarter one someday....
166 */
167errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
168 ext2fs_block_bitmap map, blk64_t *ret)
169{
170 blk64_t i;
171 int c_ratio;
172
173 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
174
175 if (!map)
176 map = fs->block_map;
177 if (!map)
178 return EXT2_ET_NO_BLOCK_BITMAP;
179 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
180 goal = fs->super->s_first_data_block;
181 i = goal;
182 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
183 if (c_ratio > 1)
184 goal &= ~EXT2FS_CLUSTER_MASK(fs);
185 check_block_uninit(fs, map,
186 (i - fs->super->s_first_data_block) /
187 EXT2_BLOCKS_PER_GROUP(fs->super));
188 do {
189 if (((i - fs->super->s_first_data_block) %
190 EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
191 check_block_uninit(fs, map,
192 (i - fs->super->s_first_data_block) /
193 EXT2_BLOCKS_PER_GROUP(fs->super));
194
195 if (!ext2fs_fast_test_block_bitmap2(map, i)) {
196 *ret = i;
197 return 0;
198 }
199 i = (i + c_ratio) & ~(c_ratio - 1);
200 if (i >= ext2fs_blocks_count(fs->super))
201 i = fs->super->s_first_data_block;
202 } while (i != goal);
203 return EXT2_ET_BLOCK_ALLOC_FAIL;
204}
205
206errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
207 ext2fs_block_bitmap map, blk_t *ret)
208{
209 errcode_t retval;
210 blk64_t val;
211 retval = ext2fs_new_block2(fs, goal, map, &val);
212 if (!retval)
213 *ret = (blk_t) val;
214 return retval;
215}
216
217/*
218 * This function zeros out the allocated block, and updates all of the
219 * appropriate filesystem records.
220 */
221errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
222 char *block_buf, blk64_t *ret)
223{
224 errcode_t retval;
225 blk64_t block;
226 char *buf = 0;
227
228 if (!block_buf) {
229 retval = ext2fs_get_mem(fs->blocksize, &buf);
230 if (retval)
231 return retval;
232 block_buf = buf;
233 }
234 memset(block_buf, 0, fs->blocksize);
235
236 if (fs->get_alloc_block) {
237 retval = (fs->get_alloc_block)(fs, goal, &block);
238 if (retval)
239 goto fail;
240 } else {
241 if (!fs->block_map) {
242 retval = ext2fs_read_block_bitmap(fs);
243 if (retval)
244 goto fail;
245 }
246
247 retval = ext2fs_new_block2(fs, goal, 0, &block);
248 if (retval)
249 goto fail;
250 }
251
252 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
253 if (retval)
254 goto fail;
255
256 ext2fs_block_alloc_stats2(fs, block, +1);
257 *ret = block;
258
259fail:
260 if (buf)
261 ext2fs_free_mem(&buf);
262 return retval;
263}
264
265errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
266 char *block_buf, blk_t *ret)
267{
268 errcode_t retval;
269 blk64_t val;
270 retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
271 if (!retval)
272 *ret = (blk_t) val;
273 return retval;
274}
275
276errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
277 int num, ext2fs_block_bitmap map, blk64_t *ret)
278{
279 blk64_t b = start;
280 int c_ratio;
281
282 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
283
284 if (!map)
285 map = fs->block_map;
286 if (!map)
287 return EXT2_ET_NO_BLOCK_BITMAP;
288 if (!b)
289 b = fs->super->s_first_data_block;
290 if (!finish)
291 finish = start;
292 if (!num)
293 num = 1;
294 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
295 b &= ~(c_ratio - 1);
296 finish &= ~(c_ratio -1);
297 do {
298 if (b+num-1 > ext2fs_blocks_count(fs->super))
299 b = fs->super->s_first_data_block;
300 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
301 *ret = b;
302 return 0;
303 }
304 b += c_ratio;
305 } while (b != finish);
306 return EXT2_ET_BLOCK_ALLOC_FAIL;
307}
308
309errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
310 int num, ext2fs_block_bitmap map, blk_t *ret)
311{
312 errcode_t retval;
313 blk64_t val;
314 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
315 if(!retval)
316 *ret = (blk_t) val;
317 return retval;
318}
319
320void ext2fs_set_alloc_block_callback(ext2_filsys fs,
321 errcode_t (*func)(ext2_filsys fs,
322 blk64_t goal,
323 blk64_t *ret),
324 errcode_t (**old)(ext2_filsys fs,
325 blk64_t goal,
326 blk64_t *ret))
327{
328 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
329 return;
330
331 if (old)
332 *old = fs->get_alloc_block;
333
334 fs->get_alloc_block = func;
335}