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 Public
23 #include <sys/types.h>
30 * Right now, just search forward from the parent directory's block
31 * group to find the next free inode.
33 * Should have a special policy for directories.
35 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
36 int mode EXT2FS_ATTR((unused)),
37 ext2fs_inode_bitmap map, ext2_ino_t *ret)
39 ext2_ino_t dir_group = 0;
41 ext2_ino_t start_inode;
43 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
48 return EXT2_ET_NO_INODE_BITMAP;
51 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
53 start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
54 if (start_inode < EXT2_FIRST_INODE(fs->super))
55 start_inode = EXT2_FIRST_INODE(fs->super);
59 if (!ext2fs_fast_test_inode_bitmap(map, i))
62 if (i > fs->super->s_inodes_count)
63 i = EXT2_FIRST_INODE(fs->super);
64 } while (i != start_inode);
66 if (ext2fs_test_inode_bitmap(map, i))
67 return EXT2_ET_INODE_ALLOC_FAIL;
73 * Stupid algorithm --- we now just search forward starting from the
74 * goal. Should put in a smarter one someday....
76 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
77 ext2fs_block_bitmap map, blk_t *ret)
81 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
86 return EXT2_ET_NO_BLOCK_BITMAP;
87 if (!goal || (goal >= fs->super->s_blocks_count))
88 goal = fs->super->s_first_data_block;
91 if (!ext2fs_fast_test_block_bitmap(map, i)) {
96 if (i >= fs->super->s_blocks_count)
97 i = fs->super->s_first_data_block;
99 return EXT2_ET_BLOCK_ALLOC_FAIL;
103 * This function zeros out the allocated block, and updates all of the
104 * appropriate filesystem records.
106 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
107 char *block_buf, blk_t *ret)
114 retval = ext2fs_get_mem(fs->blocksize, &buf);
119 memset(block_buf, 0, fs->blocksize);
121 if (fs->get_alloc_block) {
124 retval = (fs->get_alloc_block)(fs, (blk64_t) goal, &new);
129 if (!fs->block_map) {
130 retval = ext2fs_read_block_bitmap(fs);
135 retval = ext2fs_new_block(fs, goal, 0, &block);
140 retval = io_channel_write_blk(fs->io, block, 1, block_buf);
144 ext2fs_block_alloc_stats(fs, block, +1);
149 ext2fs_free_mem(&buf);
153 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
154 int num, ext2fs_block_bitmap map, blk_t *ret)
158 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
163 return EXT2_ET_NO_BLOCK_BITMAP;
165 b = fs->super->s_first_data_block;
171 if (b+num-1 > fs->super->s_blocks_count)
172 b = fs->super->s_first_data_block;
173 if (ext2fs_fast_test_block_bitmap_range(map, b, num)) {
178 } while (b != finish);
179 return EXT2_ET_BLOCK_ALLOC_FAIL;
182 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
183 errcode_t (*func)(ext2_filsys fs,
186 errcode_t (**old)(ext2_filsys fs,
190 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
194 *old = fs->get_alloc_block;
196 fs->get_alloc_block = func;