Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * alloc.c --- allocate new inodes, blocks for ext2fs |
| 3 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame^] | 4 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <unistd.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <time.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/types.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 19 | #if HAVE_ERRNO_H |
| 20 | #include <errno.h> |
| 21 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 22 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | #include <linux/ext2_fs.h> |
| 24 | |
| 25 | #include "ext2fs.h" |
| 26 | |
| 27 | /* |
| 28 | * Right now, just search forward from the parent directory's block |
| 29 | * group to find the next free inode. |
| 30 | * |
| 31 | * Should have a special policy for directories. |
| 32 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 33 | errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode, |
| 34 | ext2fs_inode_bitmap map, ino_t *ret) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | { |
| 36 | int dir_group = 0; |
| 37 | ino_t i; |
| 38 | ino_t start_inode; |
| 39 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 40 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 41 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | if (!map) |
| 43 | map = fs->inode_map; |
| 44 | if (!map) |
| 45 | return EXT2_ET_NO_INODE_BITMAP; |
| 46 | |
| 47 | if (dir > 0) |
| 48 | dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super); |
| 49 | |
| 50 | start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1; |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 51 | if (start_inode < EXT2_FIRST_INODE(fs->super)) |
| 52 | start_inode = EXT2_FIRST_INODE(fs->super); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 53 | i = start_inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 54 | |
| 55 | do { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 56 | if (!ext2fs_test_inode_bitmap(map, i)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | break; |
| 58 | i++; |
| 59 | if (i > fs->super->s_inodes_count) |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 60 | i = EXT2_FIRST_INODE(fs->super); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 61 | } while (i != start_inode); |
| 62 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 63 | if (ext2fs_test_inode_bitmap(map, i)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 64 | return ENOSPC; |
| 65 | *ret = i; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Stupid algorithm --- we now just search forward starting from the |
| 71 | * goal. Should put in a smarter one someday.... |
| 72 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 73 | errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal, |
| 74 | ext2fs_block_bitmap map, blk_t *ret) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 75 | { |
| 76 | blk_t i = goal; |
| 77 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 78 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 79 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 80 | if (!map) |
| 81 | map = fs->block_map; |
| 82 | if (!map) |
| 83 | return EXT2_ET_NO_BLOCK_BITMAP; |
| 84 | if (!i) |
| 85 | i = fs->super->s_first_data_block; |
| 86 | do { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 87 | if (!ext2fs_test_block_bitmap(map, i)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 88 | *ret = i; |
| 89 | return 0; |
| 90 | } |
| 91 | i++; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame^] | 92 | if (i >= fs->super->s_blocks_count) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 93 | i = fs->super->s_first_data_block; |
| 94 | } while (i != goal); |
| 95 | return ENOSPC; |
| 96 | } |
| 97 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 98 | errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 99 | int num, ext2fs_block_bitmap map, blk_t *ret) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 100 | { |
| 101 | blk_t b = start; |
| 102 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 103 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 104 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | if (!map) |
| 106 | map = fs->block_map; |
| 107 | if (!map) |
| 108 | return EXT2_ET_NO_BLOCK_BITMAP; |
| 109 | if (!b) |
| 110 | b = fs->super->s_first_data_block; |
| 111 | if (!finish) |
| 112 | finish = start; |
| 113 | if (!num) |
| 114 | num = 1; |
| 115 | do { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame^] | 116 | if (b+num-1 > fs->super->s_blocks_count) |
| 117 | b = fs->super->s_first_data_block; |
| 118 | if (ext2fs_fast_test_block_bitmap_range(map, b, num)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 119 | *ret = b; |
| 120 | return 0; |
| 121 | } |
| 122 | b++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 123 | } while (b != finish); |
| 124 | return ENOSPC; |
| 125 | } |
| 126 | |