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>
16 #include <linux/ext2_fs.h>
21 * Right now, just search forward from the parent directory's block
22 * group to find the next free inode.
24 * Should have a special policy for directories.
26 errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode, char *map,
36 return EXT2_ET_NO_INODE_BITMAP;
39 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
41 start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
43 if (i < EXT2_FIRST_INO)
47 if (!ext2fs_test_inode_bitmap(fs, map, i))
50 if (i > fs->super->s_inodes_count)
52 } while (i != start_inode);
54 if (ext2fs_test_inode_bitmap(fs, map, i))
61 * Stupid algorithm --- we now just search forward starting from the
62 * goal. Should put in a smarter one someday....
64 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal, char *map, blk_t *ret)
71 return EXT2_ET_NO_BLOCK_BITMAP;
73 i = fs->super->s_first_data_block;
75 if (!ext2fs_test_block_bitmap(fs, map, i)) {
80 if (i > fs->super->s_blocks_count)
81 i = fs->super->s_first_data_block;
86 static int check_blocks_free(ext2_filsys fs, char *map, blk_t blk, int num)
90 for (i=0; i < num; i++) {
91 if ((blk+i) > fs->super->s_blocks_count)
93 if (ext2fs_test_block_bitmap(fs, map, blk+i))
99 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
100 int num, char *map, blk_t *ret)
107 return EXT2_ET_NO_BLOCK_BITMAP;
109 b = fs->super->s_first_data_block;
115 if (check_blocks_free(fs, map, b, num)) {
120 if (b > fs->super->s_blocks_count)
121 b = fs->super->s_first_data_block;
122 } while (b != finish);