2 * alloc.c --- allocate new inodes, blocks for ext2fs
4 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
13 #include <sys/types.h>
18 #include <linux/ext2_fs.h>
23 * Right now, just search forward from the parent directory's block
24 * group to find the next free inode.
26 * Should have a special policy for directories.
28 errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
29 ext2fs_inode_bitmap map, ino_t *ret)
35 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
40 return EXT2_ET_NO_INODE_BITMAP;
43 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
45 start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
46 if (start_inode < EXT2_FIRST_INODE(fs->super))
47 start_inode = EXT2_FIRST_INODE(fs->super);
51 if (!ext2fs_test_inode_bitmap(map, i))
54 if (i > fs->super->s_inodes_count)
55 i = EXT2_FIRST_INODE(fs->super);
56 } while (i != start_inode);
58 if (ext2fs_test_inode_bitmap(map, i))
65 * Stupid algorithm --- we now just search forward starting from the
66 * goal. Should put in a smarter one someday....
68 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
69 ext2fs_block_bitmap map, blk_t *ret)
73 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
78 return EXT2_ET_NO_BLOCK_BITMAP;
80 i = fs->super->s_first_data_block;
82 if (!ext2fs_test_block_bitmap(map, i)) {
87 if (i > fs->super->s_blocks_count)
88 i = fs->super->s_first_data_block;
93 static int check_blocks_free(ext2_filsys fs, ext2fs_block_bitmap map,
98 for (i=0; i < num; i++) {
99 if ((blk+i) > fs->super->s_blocks_count)
101 if (ext2fs_test_block_bitmap(map, blk+i))
107 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
108 int num, ext2fs_block_bitmap map, blk_t *ret)
112 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
117 return EXT2_ET_NO_BLOCK_BITMAP;
119 b = fs->super->s_first_data_block;
125 if (check_blocks_free(fs, map, b, num)) {
130 if (b > fs->super->s_blocks_count)
131 b = fs->super->s_first_data_block;
132 } while (b != finish);