2 * alloc.c --- allocate new inodes, blocks for ext2fs
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
22 #include <sys/types.h>
29 * Check for uninit block bitmaps and deal with them appropriately
31 static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
35 blk64_t blk, super_blk, old_desc_blk, new_desc_blk;
38 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
39 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
40 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
43 blk = (group * fs->super->s_blocks_per_group) +
44 fs->super->s_first_data_block;
46 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
47 &old_desc_blk, &new_desc_blk, 0);
49 if (fs->super->s_feature_incompat &
50 EXT2_FEATURE_INCOMPAT_META_BG)
51 old_desc_blocks = fs->super->s_first_meta_bg;
53 old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
55 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
56 if ((blk == super_blk) ||
57 (old_desc_blk && old_desc_blocks &&
58 (blk >= old_desc_blk) &&
59 (blk < old_desc_blk + old_desc_blocks)) ||
60 (new_desc_blk && (blk == new_desc_blk)) ||
61 (blk == ext2fs_block_bitmap_loc(fs, group)) ||
62 (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
63 (blk >= ext2fs_inode_table_loc(fs, group) &&
64 (blk < ext2fs_inode_table_loc(fs, group)
65 + fs->inode_blocks_per_group)))
66 ext2fs_fast_mark_block_bitmap2(map, blk);
68 ext2fs_fast_unmark_block_bitmap2(map, blk);
70 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
71 ext2fs_group_desc_csum_set(fs, group);
75 * Check for uninit inode bitmaps and deal with them appropriately
77 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
82 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
83 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
84 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
87 ino = (group * fs->super->s_inodes_per_group) + 1;
88 for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
89 ext2fs_fast_unmark_inode_bitmap2(map, ino);
91 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
92 check_block_uninit(fs, fs->block_map, group);
96 * Right now, just search forward from the parent directory's block
97 * group to find the next free inode.
99 * Should have a special policy for directories.
101 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
102 int mode EXT2FS_ATTR((unused)),
103 ext2fs_inode_bitmap map, ext2_ino_t *ret)
105 ext2_ino_t dir_group = 0;
107 ext2_ino_t start_inode;
109 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
114 return EXT2_ET_NO_INODE_BITMAP;
117 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
119 start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
120 if (start_inode < EXT2_FIRST_INODE(fs->super))
121 start_inode = EXT2_FIRST_INODE(fs->super);
122 if (start_inode > fs->super->s_inodes_count)
123 return EXT2_ET_INODE_ALLOC_FAIL;
127 if (((i - 1) % EXT2_INODES_PER_GROUP(fs->super)) == 0)
128 check_inode_uninit(fs, map, (i - 1) /
129 EXT2_INODES_PER_GROUP(fs->super));
131 if (!ext2fs_fast_test_inode_bitmap2(map, i))
134 if (i > fs->super->s_inodes_count)
135 i = EXT2_FIRST_INODE(fs->super);
136 } while (i != start_inode);
138 if (ext2fs_test_inode_bitmap2(map, i))
139 return EXT2_ET_INODE_ALLOC_FAIL;
145 * Stupid algorithm --- we now just search forward starting from the
146 * goal. Should put in a smarter one someday....
148 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
149 ext2fs_block_bitmap map, blk64_t *ret)
153 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
158 return EXT2_ET_NO_BLOCK_BITMAP;
159 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
160 goal = fs->super->s_first_data_block;
162 check_block_uninit(fs, map,
163 (i - fs->super->s_first_data_block) /
164 EXT2_BLOCKS_PER_GROUP(fs->super));
166 if (((i - fs->super->s_first_data_block) %
167 EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
168 check_block_uninit(fs, map,
169 (i - fs->super->s_first_data_block) /
170 EXT2_BLOCKS_PER_GROUP(fs->super));
172 if (!ext2fs_fast_test_block_bitmap2(map, i)) {
177 if (i >= ext2fs_blocks_count(fs->super))
178 i = fs->super->s_first_data_block;
180 return EXT2_ET_BLOCK_ALLOC_FAIL;
183 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
184 ext2fs_block_bitmap map, blk_t *ret)
188 retval = ext2fs_new_block2(fs, goal, map, &val);
195 * This function zeros out the allocated block, and updates all of the
196 * appropriate filesystem records.
198 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
199 char *block_buf, blk64_t *ret)
206 retval = ext2fs_get_mem(fs->blocksize, &buf);
211 memset(block_buf, 0, fs->blocksize);
213 if (fs->get_alloc_block) {
214 retval = (fs->get_alloc_block)(fs, goal, &block);
218 if (!fs->block_map) {
219 retval = ext2fs_read_block_bitmap(fs);
224 retval = ext2fs_new_block2(fs, goal, 0, &block);
229 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
233 ext2fs_block_alloc_stats2(fs, block, +1);
238 ext2fs_free_mem(&buf);
242 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
243 char *block_buf, blk_t *ret)
247 retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
253 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
254 int num, ext2fs_block_bitmap map, blk64_t *ret)
258 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
263 return EXT2_ET_NO_BLOCK_BITMAP;
265 b = fs->super->s_first_data_block;
271 if (b+num-1 > ext2fs_blocks_count(fs->super))
272 b = fs->super->s_first_data_block;
273 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
278 } while (b != finish);
279 return EXT2_ET_BLOCK_ALLOC_FAIL;
282 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
283 int num, ext2fs_block_bitmap map, blk_t *ret)
287 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
293 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
294 errcode_t (*func)(ext2_filsys fs,
297 errcode_t (**old)(ext2_filsys fs,
301 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
305 *old = fs->get_alloc_block;
307 fs->get_alloc_block = func;