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 * Clear the uninit block bitmap flag if necessary
32 static void clear_block_uninit(ext2_filsys fs, dgrp_t group)
34 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
35 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
36 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
39 /* uninit block bitmaps are now initialized in read_bitmaps() */
41 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
42 ext2fs_group_desc_csum_set(fs, group);
43 ext2fs_mark_super_dirty(fs);
44 ext2fs_mark_bb_dirty(fs);
48 * Check for uninit inode bitmaps and deal with them appropriately
50 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
55 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
56 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
57 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
60 ino = (group * fs->super->s_inodes_per_group) + 1;
61 for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
62 ext2fs_fast_unmark_inode_bitmap2(map, ino);
64 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
65 /* Mimics what the kernel does */
66 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
67 ext2fs_group_desc_csum_set(fs, group);
68 ext2fs_mark_ib_dirty(fs);
69 ext2fs_mark_super_dirty(fs);
73 * Right now, just search forward from the parent directory's block
74 * group to find the next free inode.
76 * Should have a special policy for directories.
78 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
79 int mode EXT2FS_ATTR((unused)),
80 ext2fs_inode_bitmap map, ext2_ino_t *ret)
82 ext2_ino_t start_inode = 0;
83 ext2_ino_t i, ino_in_group, upto, first_zero;
87 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
92 return EXT2_ET_NO_INODE_BITMAP;
95 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
96 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
98 if (start_inode < EXT2_FIRST_INODE(fs->super))
99 start_inode = EXT2_FIRST_INODE(fs->super);
100 if (start_inode > fs->super->s_inodes_count)
101 return EXT2_ET_INODE_ALLOC_FAIL;
104 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
105 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
107 check_inode_uninit(fs, map, group);
108 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
109 if (i < start_inode && upto >= start_inode)
110 upto = start_inode - 1;
111 if (upto > fs->super->s_inodes_count)
112 upto = fs->super->s_inodes_count;
114 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
120 if (retval != ENOENT)
121 return EXT2_ET_INODE_ALLOC_FAIL;
123 if (i > fs->super->s_inodes_count)
124 i = EXT2_FIRST_INODE(fs->super);
125 } while (i != start_inode);
127 if (ext2fs_test_inode_bitmap2(map, i))
128 return EXT2_ET_INODE_ALLOC_FAIL;
134 * Stupid algorithm --- we now just search forward starting from the
135 * goal. Should put in a smarter one someday....
137 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
138 ext2fs_block_bitmap map, blk64_t *ret)
143 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
148 return EXT2_ET_NO_BLOCK_BITMAP;
149 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
150 goal = fs->super->s_first_data_block;
151 goal &= ~EXT2FS_CLUSTER_MASK(fs);
153 retval = ext2fs_find_first_zero_block_bitmap2(map,
154 goal, ext2fs_blocks_count(fs->super) - 1, &b);
155 if ((retval == ENOENT) && (goal != fs->super->s_first_data_block))
156 retval = ext2fs_find_first_zero_block_bitmap2(map,
157 fs->super->s_first_data_block, goal - 1, &b);
158 if (retval == ENOENT)
159 return EXT2_ET_BLOCK_ALLOC_FAIL;
163 clear_block_uninit(fs, ext2fs_group_of_blk2(fs, b));
168 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
169 ext2fs_block_bitmap map, blk_t *ret)
173 retval = ext2fs_new_block2(fs, goal, map, &val);
180 * This function zeros out the allocated block, and updates all of the
181 * appropriate filesystem records.
183 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
184 char *block_buf, blk64_t *ret)
191 retval = ext2fs_get_mem(fs->blocksize, &buf);
196 memset(block_buf, 0, fs->blocksize);
198 if (fs->get_alloc_block) {
199 retval = (fs->get_alloc_block)(fs, goal, &block);
203 if (!fs->block_map) {
204 retval = ext2fs_read_block_bitmap(fs);
209 retval = ext2fs_new_block2(fs, goal, 0, &block);
214 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
218 ext2fs_block_alloc_stats2(fs, block, +1);
223 ext2fs_free_mem(&buf);
227 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
228 char *block_buf, blk_t *ret)
232 retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
238 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
239 int num, ext2fs_block_bitmap map, blk64_t *ret)
244 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
249 return EXT2_ET_NO_BLOCK_BITMAP;
251 b = fs->super->s_first_data_block;
256 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
258 finish &= ~(c_ratio -1);
260 if (b + num - 1 >= ext2fs_blocks_count(fs->super)) {
262 return EXT2_ET_BLOCK_ALLOC_FAIL;
263 b = fs->super->s_first_data_block;
265 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
270 } while (b != finish);
271 return EXT2_ET_BLOCK_ALLOC_FAIL;
274 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
275 int num, ext2fs_block_bitmap map, blk_t *ret)
279 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
285 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
286 errcode_t (*func)(ext2_filsys fs,
289 errcode_t (**old)(ext2_filsys fs,
293 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
297 *old = fs->get_alloc_block;
299 fs->get_alloc_block = func;