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 (!ext2fs_has_group_desc_csum(fs) ||
35 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
38 /* uninit block bitmaps are now initialized in read_bitmaps() */
40 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
41 ext2fs_group_desc_csum_set(fs, group);
42 ext2fs_mark_super_dirty(fs);
43 ext2fs_mark_bb_dirty(fs);
47 * Check for uninit inode bitmaps and deal with them appropriately
49 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
54 if (!ext2fs_has_group_desc_csum(fs) ||
55 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
58 ino = (group * fs->super->s_inodes_per_group) + 1;
59 for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
60 ext2fs_fast_unmark_inode_bitmap2(map, ino);
62 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
63 /* Mimics what the kernel does */
64 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
65 ext2fs_group_desc_csum_set(fs, group);
66 ext2fs_mark_ib_dirty(fs);
67 ext2fs_mark_super_dirty(fs);
71 * Right now, just search forward from the parent directory's block
72 * group to find the next free inode.
74 * Should have a special policy for directories.
76 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
77 int mode EXT2FS_ATTR((unused)),
78 ext2fs_inode_bitmap map, ext2_ino_t *ret)
80 ext2_ino_t start_inode = 0;
81 ext2_ino_t i, ino_in_group, upto, first_zero;
85 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
90 return EXT2_ET_NO_INODE_BITMAP;
93 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
94 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
96 if (start_inode < EXT2_FIRST_INODE(fs->super))
97 start_inode = EXT2_FIRST_INODE(fs->super);
98 if (start_inode > fs->super->s_inodes_count)
99 return EXT2_ET_INODE_ALLOC_FAIL;
102 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
103 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
105 check_inode_uninit(fs, map, group);
106 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
107 if (i < start_inode && upto >= start_inode)
108 upto = start_inode - 1;
109 if (upto > fs->super->s_inodes_count)
110 upto = fs->super->s_inodes_count;
112 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
118 if (retval != ENOENT)
119 return EXT2_ET_INODE_ALLOC_FAIL;
121 if (i > fs->super->s_inodes_count)
122 i = EXT2_FIRST_INODE(fs->super);
123 } while (i != start_inode);
125 if (ext2fs_test_inode_bitmap2(map, i))
126 return EXT2_ET_INODE_ALLOC_FAIL;
132 * Stupid algorithm --- we now just search forward starting from the
133 * goal. Should put in a smarter one someday....
135 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
136 ext2fs_block_bitmap map, blk64_t *ret)
141 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
146 return EXT2_ET_NO_BLOCK_BITMAP;
147 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
148 goal = fs->super->s_first_data_block;
149 goal &= ~EXT2FS_CLUSTER_MASK(fs);
151 retval = ext2fs_find_first_zero_block_bitmap2(map,
152 goal, ext2fs_blocks_count(fs->super) - 1, &b);
153 if ((retval == ENOENT) && (goal != fs->super->s_first_data_block))
154 retval = ext2fs_find_first_zero_block_bitmap2(map,
155 fs->super->s_first_data_block, goal - 1, &b);
156 if (retval == ENOENT)
157 return EXT2_ET_BLOCK_ALLOC_FAIL;
161 clear_block_uninit(fs, ext2fs_group_of_blk2(fs, b));
166 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
167 ext2fs_block_bitmap map, blk_t *ret)
171 retval = ext2fs_new_block2(fs, goal, map, &val);
178 * This function zeros out the allocated block, and updates all of the
179 * appropriate filesystem records.
181 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
182 char *block_buf, blk64_t *ret)
189 retval = ext2fs_get_mem(fs->blocksize, &buf);
194 memset(block_buf, 0, fs->blocksize);
196 if (fs->get_alloc_block) {
197 retval = (fs->get_alloc_block)(fs, goal, &block);
201 if (!fs->block_map) {
202 retval = ext2fs_read_block_bitmap(fs);
207 retval = ext2fs_new_block2(fs, goal, 0, &block);
212 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
216 ext2fs_block_alloc_stats2(fs, block, +1);
221 ext2fs_free_mem(&buf);
225 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
226 char *block_buf, blk_t *ret)
230 retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
236 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
237 int num, ext2fs_block_bitmap map, blk64_t *ret)
242 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
247 return EXT2_ET_NO_BLOCK_BITMAP;
249 b = fs->super->s_first_data_block;
254 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
256 finish &= ~(c_ratio -1);
258 if (b + num - 1 >= ext2fs_blocks_count(fs->super)) {
260 return EXT2_ET_BLOCK_ALLOC_FAIL;
261 b = fs->super->s_first_data_block;
263 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
268 } while (b != finish);
269 return EXT2_ET_BLOCK_ALLOC_FAIL;
272 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
273 int num, ext2fs_block_bitmap map, blk_t *ret)
277 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
283 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
284 errcode_t (*func)(ext2_filsys fs,
287 errcode_t (**old)(ext2_filsys fs,
291 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
295 *old = fs->get_alloc_block;
297 fs->get_alloc_block = func;