~ubuntu-branches/debian/jessie/arcboot/jessie

« back to all changes in this revision

Viewing changes to e2fslib/alloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Guenther
  • Date: 2004-03-02 12:01:14 UTC
  • Revision ID: james.westby@ubuntu.com-20040302120114-0pukal9hlpt3k0l7
Tags: 0.3.8.1
correct subarch detection for IP32

Show diffs side-by-side

added added

removed removed

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