[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * alloc.c --- allocate new inodes, blocks for ext2fs
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
|
---|
| 6 | *
|
---|
| 7 | * %Begin-Header%
|
---|
| 8 | * This file may be redistributed under the terms of the GNU Public
|
---|
| 9 | * License.
|
---|
| 10 | * %End-Header%
|
---|
| 11 | *
|
---|
| 12 | */
|
---|
| 13 |
|
---|
| 14 | #include <stdio.h>
|
---|
| 15 | #if HAVE_UNISTD_H
|
---|
| 16 | #include <unistd.h>
|
---|
| 17 | #endif
|
---|
| 18 | #include <time.h>
|
---|
| 19 | #include <string.h>
|
---|
| 20 | #if HAVE_SYS_STAT_H
|
---|
| 21 | #include <sys/stat.h>
|
---|
| 22 | #endif
|
---|
| 23 | #if HAVE_SYS_TYPES_H
|
---|
| 24 | #include <sys/types.h>
|
---|
| 25 | #endif
|
---|
| 26 |
|
---|
| 27 | #include "ext2_fs.h"
|
---|
| 28 | #include "ext2fs.h"
|
---|
| 29 |
|
---|
| 30 | /*
|
---|
| 31 | * Right now, just search forward from the parent directory's block
|
---|
| 32 | * group to find the next free inode.
|
---|
| 33 | *
|
---|
| 34 | * Should have a special policy for directories.
|
---|
| 35 | */
|
---|
| 36 | errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
|
---|
| 37 | int mode EXT2FS_ATTR((unused)),
|
---|
| 38 | ext2fs_inode_bitmap map, ext2_ino_t *ret)
|
---|
| 39 | {
|
---|
| 40 | ext2_ino_t dir_group = 0;
|
---|
| 41 | ext2_ino_t i;
|
---|
| 42 | ext2_ino_t start_inode;
|
---|
| 43 |
|
---|
| 44 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
|
---|
| 45 |
|
---|
| 46 | if (!map)
|
---|
| 47 | map = fs->inode_map;
|
---|
| 48 | if (!map)
|
---|
| 49 | return EXT2_ET_NO_INODE_BITMAP;
|
---|
| 50 |
|
---|
| 51 | if (dir > 0)
|
---|
| 52 | dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
|
---|
| 53 |
|
---|
| 54 | start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
|
---|
| 55 | if (start_inode < EXT2_FIRST_INODE(fs->super))
|
---|
| 56 | start_inode = EXT2_FIRST_INODE(fs->super);
|
---|
| 57 | i = start_inode;
|
---|
| 58 |
|
---|
| 59 | do {
|
---|
| 60 | if (!ext2fs_fast_test_inode_bitmap(map, i))
|
---|
| 61 | break;
|
---|
| 62 | i++;
|
---|
| 63 | if (i > fs->super->s_inodes_count)
|
---|
| 64 | i = EXT2_FIRST_INODE(fs->super);
|
---|
| 65 | } while (i != start_inode);
|
---|
| 66 |
|
---|
| 67 | if (ext2fs_test_inode_bitmap(map, i))
|
---|
| 68 | return EXT2_ET_INODE_ALLOC_FAIL;
|
---|
| 69 | *ret = i;
|
---|
| 70 | return 0;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /*
|
---|
| 74 | * Stupid algorithm --- we now just search forward starting from the
|
---|
| 75 | * goal. Should put in a smarter one someday....
|
---|
| 76 | */
|
---|
| 77 | errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
|
---|
| 78 | ext2fs_block_bitmap map, blk_t *ret)
|
---|
| 79 | {
|
---|
| 80 | blk_t i;
|
---|
| 81 |
|
---|
| 82 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
|
---|
| 83 |
|
---|
| 84 | if (!map)
|
---|
| 85 | map = fs->block_map;
|
---|
| 86 | if (!map)
|
---|
| 87 | return EXT2_ET_NO_BLOCK_BITMAP;
|
---|
| 88 | if (!goal || (goal >= fs->super->s_blocks_count))
|
---|
| 89 | goal = fs->super->s_first_data_block;
|
---|
| 90 | i = goal;
|
---|
| 91 | do {
|
---|
| 92 | if (!ext2fs_fast_test_block_bitmap(map, i)) {
|
---|
| 93 | *ret = i;
|
---|
| 94 | return 0;
|
---|
| 95 | }
|
---|
| 96 | i++;
|
---|
| 97 | if (i >= fs->super->s_blocks_count)
|
---|
| 98 | i = fs->super->s_first_data_block;
|
---|
| 99 | } while (i != goal);
|
---|
| 100 | return EXT2_ET_BLOCK_ALLOC_FAIL;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /*
|
---|
| 104 | * This function zeros out the allocated block, and updates all of the
|
---|
| 105 | * appropriate filesystem records.
|
---|
| 106 | */
|
---|
| 107 | errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
|
---|
| 108 | char *block_buf, blk_t *ret)
|
---|
| 109 | {
|
---|
| 110 | errcode_t retval;
|
---|
| 111 | blk_t block;
|
---|
| 112 | char *buf = 0;
|
---|
| 113 |
|
---|
| 114 | if (!block_buf) {
|
---|
| 115 | retval = ext2fs_get_mem(fs->blocksize, &buf);
|
---|
| 116 | if (retval)
|
---|
| 117 | return retval;
|
---|
| 118 | block_buf = buf;
|
---|
| 119 | }
|
---|
| 120 | memset(block_buf, 0, fs->blocksize);
|
---|
| 121 |
|
---|
| 122 | if (!fs->block_map) {
|
---|
| 123 | retval = ext2fs_read_block_bitmap(fs);
|
---|
| 124 | if (retval)
|
---|
| 125 | goto fail;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | retval = ext2fs_new_block(fs, goal, 0, &block);
|
---|
| 129 | if (retval)
|
---|
| 130 | goto fail;
|
---|
| 131 |
|
---|
| 132 | retval = io_channel_write_blk(fs->io, block, 1, block_buf);
|
---|
| 133 | if (retval)
|
---|
| 134 | goto fail;
|
---|
| 135 |
|
---|
| 136 | ext2fs_block_alloc_stats(fs, block, +1);
|
---|
| 137 | *ret = block;
|
---|
| 138 | return 0;
|
---|
| 139 |
|
---|
| 140 | fail:
|
---|
| 141 | if (buf)
|
---|
| 142 | ext2fs_free_mem(&buf);
|
---|
| 143 | return retval;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
|
---|
| 147 | int num, ext2fs_block_bitmap map, blk_t *ret)
|
---|
| 148 | {
|
---|
| 149 | blk_t b = start;
|
---|
| 150 |
|
---|
| 151 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
|
---|
| 152 |
|
---|
| 153 | if (!map)
|
---|
| 154 | map = fs->block_map;
|
---|
| 155 | if (!map)
|
---|
| 156 | return EXT2_ET_NO_BLOCK_BITMAP;
|
---|
| 157 | if (!b)
|
---|
| 158 | b = fs->super->s_first_data_block;
|
---|
| 159 | if (!finish)
|
---|
| 160 | finish = start;
|
---|
| 161 | if (!num)
|
---|
| 162 | num = 1;
|
---|
| 163 | do {
|
---|
| 164 | if (b+num-1 > fs->super->s_blocks_count)
|
---|
| 165 | b = fs->super->s_first_data_block;
|
---|
| 166 | if (ext2fs_fast_test_block_bitmap_range(map, b, num)) {
|
---|
| 167 | *ret = b;
|
---|
| 168 | return 0;
|
---|
| 169 | }
|
---|
| 170 | b++;
|
---|
| 171 | } while (b != finish);
|
---|
| 172 | return EXT2_ET_BLOCK_ALLOC_FAIL;
|
---|
| 173 | }
|
---|
| 174 |
|
---|