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.
23 #include <sys/types.h>
30 * Check for uninit block bitmaps and deal with them appropriately
32 static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
36 blk64_t blk, super_blk, old_desc_blk, new_desc_blk;
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)))
44 blk = (group * fs->super->s_blocks_per_group) +
45 fs->super->s_first_data_block;
47 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
48 &old_desc_blk, &new_desc_blk, 0);
50 if (fs->super->s_feature_incompat &
51 EXT2_FEATURE_INCOMPAT_META_BG)
52 old_desc_blocks = fs->super->s_first_meta_bg;
54 old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
56 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++)
57 ext2fs_fast_unmark_block_bitmap2(map, blk);
59 blk = (group * fs->super->s_blocks_per_group) +
60 fs->super->s_first_data_block;
61 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
62 if ((blk == super_blk) ||
63 (old_desc_blk && old_desc_blocks &&
64 (blk >= old_desc_blk) &&
65 (blk < old_desc_blk + old_desc_blocks)) ||
66 (new_desc_blk && (blk == new_desc_blk)) ||
67 (blk == ext2fs_block_bitmap_loc(fs, group)) ||
68 (blk == ext2fs_inode_bitmap_loc(fs, group)) ||
69 (blk >= ext2fs_inode_table_loc(fs, group) &&
70 (blk < ext2fs_inode_table_loc(fs, group)
71 + fs->inode_blocks_per_group)))
72 ext2fs_fast_mark_block_bitmap2(map, blk);
74 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
75 ext2fs_group_desc_csum_set(fs, group);
79 * Check for uninit inode bitmaps and deal with them appropriately
81 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
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)))
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);
95 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
96 check_block_uninit(fs, fs->block_map, group);
100 * Right now, just search forward from the parent directory's block
101 * group to find the next free inode.
103 * Should have a special policy for directories.
105 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
106 int mode EXT2FS_ATTR((unused)),
107 ext2fs_inode_bitmap map, ext2_ino_t *ret)
109 ext2_ino_t start_inode = 0;
110 ext2_ino_t i, ino_in_group, upto, first_zero;
114 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
119 return EXT2_ET_NO_INODE_BITMAP;
122 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
123 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
125 if (start_inode < EXT2_FIRST_INODE(fs->super))
126 start_inode = EXT2_FIRST_INODE(fs->super);
127 if (start_inode > fs->super->s_inodes_count)
128 return EXT2_ET_INODE_ALLOC_FAIL;
131 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
132 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
134 check_inode_uninit(fs, map, group);
135 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
136 if (i < start_inode && upto >= start_inode)
137 upto = start_inode - 1;
138 if (upto > fs->super->s_inodes_count)
139 upto = fs->super->s_inodes_count;
141 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
147 if (retval != ENOENT)
148 return EXT2_ET_INODE_ALLOC_FAIL;
150 if (i > fs->super->s_inodes_count)
151 i = EXT2_FIRST_INODE(fs->super);
152 } while (i != start_inode);
154 if (ext2fs_test_inode_bitmap2(map, i))
155 return EXT2_ET_INODE_ALLOC_FAIL;
161 * Stupid algorithm --- we now just search forward starting from the
162 * goal. Should put in a smarter one someday....
164 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
165 ext2fs_block_bitmap map, blk64_t *ret)
170 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
175 return EXT2_ET_NO_BLOCK_BITMAP;
176 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
177 goal = fs->super->s_first_data_block;
179 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
181 goal &= ~EXT2FS_CLUSTER_MASK(fs);
182 check_block_uninit(fs, map,
183 (i - fs->super->s_first_data_block) /
184 EXT2_BLOCKS_PER_GROUP(fs->super));
186 if (((i - fs->super->s_first_data_block) %
187 EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
188 check_block_uninit(fs, map,
189 (i - fs->super->s_first_data_block) /
190 EXT2_BLOCKS_PER_GROUP(fs->super));
192 if (!ext2fs_fast_test_block_bitmap2(map, i)) {
196 i = (i + c_ratio) & ~(c_ratio - 1);
197 if (i >= ext2fs_blocks_count(fs->super))
198 i = fs->super->s_first_data_block;
200 return EXT2_ET_BLOCK_ALLOC_FAIL;
203 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
204 ext2fs_block_bitmap map, blk_t *ret)
208 retval = ext2fs_new_block2(fs, goal, map, &val);
215 * This function zeros out the allocated block, and updates all of the
216 * appropriate filesystem records.
218 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
219 char *block_buf, blk64_t *ret)
226 retval = ext2fs_get_mem(fs->blocksize, &buf);
231 memset(block_buf, 0, fs->blocksize);
233 if (fs->get_alloc_block) {
234 retval = (fs->get_alloc_block)(fs, goal, &block);
238 if (!fs->block_map) {
239 retval = ext2fs_read_block_bitmap(fs);
244 retval = ext2fs_new_block2(fs, goal, 0, &block);
249 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
253 ext2fs_block_alloc_stats2(fs, block, +1);
258 ext2fs_free_mem(&buf);
262 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
263 char *block_buf, blk_t *ret)
267 retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
273 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
274 int num, ext2fs_block_bitmap map, blk64_t *ret)
279 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
284 return EXT2_ET_NO_BLOCK_BITMAP;
286 b = fs->super->s_first_data_block;
291 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
293 finish &= ~(c_ratio -1);
295 if (b+num-1 > ext2fs_blocks_count(fs->super))
296 b = fs->super->s_first_data_block;
297 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
302 } while (b != finish);
303 return EXT2_ET_BLOCK_ALLOC_FAIL;
306 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
307 int num, ext2fs_block_bitmap map, blk_t *ret)
311 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
317 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
318 errcode_t (*func)(ext2_filsys fs,
321 errcode_t (**old)(ext2_filsys fs,
325 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
329 *old = fs->get_alloc_block;
331 fs->get_alloc_block = func;