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 Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #include <stdio.h>
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <time.h>
17 #include <string.h>
18 #if HAVE_SYS_STAT_H
19 #include <sys/stat.h>
20 #endif
21 #if HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24
25 #include "ext2_fs.h"
26 #include "ext2fs.h"
27
28 /*
29 * Check for uninit block bitmaps and deal with them appropriately
30 */
check_block_uninit(ext2_filsys fs,ext2fs_block_bitmap map,dgrp_t group)31 static void check_block_uninit(ext2_filsys fs, ext2fs_block_bitmap map,
32 dgrp_t group)
33 {
34 blk_t i;
35 blk_t blk, super_blk, old_desc_blk, new_desc_blk;
36 int old_desc_blocks;
37
38 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
39 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
40 !(fs->group_desc[group].bg_flags & EXT2_BG_BLOCK_UNINIT))
41 return;
42
43 blk = (group * fs->super->s_blocks_per_group) +
44 fs->super->s_first_data_block;
45
46 ext2fs_super_and_bgd_loc(fs, group, &super_blk,
47 &old_desc_blk, &new_desc_blk, 0);
48
49 if (fs->super->s_feature_incompat &
50 EXT2_FEATURE_INCOMPAT_META_BG)
51 old_desc_blocks = fs->super->s_first_meta_bg;
52 else
53 old_desc_blocks = fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
54
55 for (i=0; i < fs->super->s_blocks_per_group; i++, blk++) {
56 if ((blk == super_blk) ||
57 (old_desc_blk && old_desc_blocks &&
58 (blk >= old_desc_blk) &&
59 (blk < old_desc_blk + old_desc_blocks)) ||
60 (new_desc_blk && (blk == new_desc_blk)) ||
61 (blk == fs->group_desc[group].bg_block_bitmap) ||
62 (blk == fs->group_desc[group].bg_inode_bitmap) ||
63 (blk >= fs->group_desc[group].bg_inode_table &&
64 (blk < fs->group_desc[group].bg_inode_table
65 + fs->inode_blocks_per_group)))
66 ext2fs_fast_mark_block_bitmap(map, blk);
67 else
68 ext2fs_fast_unmark_block_bitmap(map, blk);
69 }
70 fs->group_desc[group].bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
71 ext2fs_group_desc_csum_set(fs, group);
72 }
73
74 /*
75 * Check for uninit inode bitmaps and deal with them appropriately
76 */
check_inode_uninit(ext2_filsys fs,ext2fs_inode_bitmap map,dgrp_t group)77 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
78 dgrp_t group)
79 {
80 ext2_ino_t i, ino;
81
82 if (!(EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
83 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) ||
84 !(fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT))
85 return;
86
87 ino = (group * fs->super->s_inodes_per_group) + 1;
88 for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
89 ext2fs_fast_unmark_inode_bitmap(map, ino);
90
91 fs->group_desc[group].bg_flags &= ~EXT2_BG_INODE_UNINIT;
92 check_block_uninit(fs, fs->block_map, group);
93 }
94
95 /*
96 * Right now, just search forward from the parent directory's block
97 * group to find the next free inode.
98 *
99 * Should have a special policy for directories.
100 */
ext2fs_new_inode(ext2_filsys fs,ext2_ino_t dir,int mode EXT2FS_ATTR ((unused)),ext2fs_inode_bitmap map,ext2_ino_t * ret)101 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
102 int mode EXT2FS_ATTR((unused)),
103 ext2fs_inode_bitmap map, ext2_ino_t *ret)
104 {
105 ext2_ino_t dir_group = 0;
106 ext2_ino_t i;
107 ext2_ino_t start_inode;
108
109 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
110
111 if (!map)
112 map = fs->inode_map;
113 if (!map)
114 return EXT2_ET_NO_INODE_BITMAP;
115
116 if (dir > 0)
117 dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
118
119 start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
120 if (start_inode < EXT2_FIRST_INODE(fs->super))
121 start_inode = EXT2_FIRST_INODE(fs->super);
122 if (start_inode > fs->super->s_inodes_count)
123 return EXT2_ET_INODE_ALLOC_FAIL;
124 i = start_inode;
125
126 do {
127 if (((i - 1) % EXT2_INODES_PER_GROUP(fs->super)) == 0)
128 check_inode_uninit(fs, map, (i - 1) /
129 EXT2_INODES_PER_GROUP(fs->super));
130
131 if (!ext2fs_fast_test_inode_bitmap(map, i))
132 break;
133 i++;
134 if (i > fs->super->s_inodes_count)
135 i = EXT2_FIRST_INODE(fs->super);
136 } while (i != start_inode);
137
138 if (ext2fs_test_inode_bitmap(map, i))
139 return EXT2_ET_INODE_ALLOC_FAIL;
140 *ret = i;
141 return 0;
142 }
143
144 /*
145 * Stupid algorithm --- we now just search forward starting from the
146 * goal. Should put in a smarter one someday....
147 */
ext2fs_new_block(ext2_filsys fs,blk_t goal,ext2fs_block_bitmap map,blk_t * ret)148 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
149 ext2fs_block_bitmap map, blk_t *ret)
150 {
151 blk_t i;
152
153 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
154
155 if (!map)
156 map = fs->block_map;
157 if (!map)
158 return EXT2_ET_NO_BLOCK_BITMAP;
159 if (!goal || (goal >= fs->super->s_blocks_count))
160 goal = fs->super->s_first_data_block;
161 i = goal;
162 check_block_uninit(fs, map,
163 (i - fs->super->s_first_data_block) /
164 EXT2_BLOCKS_PER_GROUP(fs->super));
165 do {
166 if (((i - fs->super->s_first_data_block) %
167 EXT2_BLOCKS_PER_GROUP(fs->super)) == 0)
168 check_block_uninit(fs, map,
169 (i - fs->super->s_first_data_block) /
170 EXT2_BLOCKS_PER_GROUP(fs->super));
171
172 if (!ext2fs_fast_test_block_bitmap(map, i)) {
173 *ret = i;
174 return 0;
175 }
176 i++;
177 if (i >= fs->super->s_blocks_count)
178 i = fs->super->s_first_data_block;
179 } while (i != goal);
180 return EXT2_ET_BLOCK_ALLOC_FAIL;
181 }
182
183 /*
184 * This function zeros out the allocated block, and updates all of the
185 * appropriate filesystem records.
186 */
ext2fs_alloc_block(ext2_filsys fs,blk_t goal,char * block_buf,blk_t * ret)187 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
188 char *block_buf, blk_t *ret)
189 {
190 errcode_t retval;
191 blk_t block;
192 char *buf = 0;
193
194 if (!block_buf) {
195 retval = ext2fs_get_mem(fs->blocksize, &buf);
196 if (retval)
197 return retval;
198 block_buf = buf;
199 }
200 memset(block_buf, 0, fs->blocksize);
201
202 if (fs->get_alloc_block) {
203 blk64_t new;
204
205 retval = (fs->get_alloc_block)(fs, (blk64_t) goal, &new);
206 if (retval)
207 goto fail;
208 block = (blk_t) new;
209 } else {
210 if (!fs->block_map) {
211 retval = ext2fs_read_block_bitmap(fs);
212 if (retval)
213 goto fail;
214 }
215
216 retval = ext2fs_new_block(fs, goal, 0, &block);
217 if (retval)
218 goto fail;
219 }
220
221 retval = io_channel_write_blk(fs->io, block, 1, block_buf);
222 if (retval)
223 goto fail;
224
225 ext2fs_block_alloc_stats(fs, block, +1);
226 *ret = block;
227
228 fail:
229 if (buf)
230 ext2fs_free_mem(&buf);
231 return retval;
232 }
233
ext2fs_get_free_blocks(ext2_filsys fs,blk_t start,blk_t finish,int num,ext2fs_block_bitmap map,blk_t * ret)234 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
235 int num, ext2fs_block_bitmap map, blk_t *ret)
236 {
237 blk_t b = start;
238
239 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
240
241 if (!map)
242 map = fs->block_map;
243 if (!map)
244 return EXT2_ET_NO_BLOCK_BITMAP;
245 if (!b)
246 b = fs->super->s_first_data_block;
247 if (!finish)
248 finish = start;
249 if (!num)
250 num = 1;
251 do {
252 if (b+num-1 > fs->super->s_blocks_count)
253 b = fs->super->s_first_data_block;
254 if (ext2fs_fast_test_block_bitmap_range(map, b, num)) {
255 *ret = b;
256 return 0;
257 }
258 b++;
259 } while (b != finish);
260 return EXT2_ET_BLOCK_ALLOC_FAIL;
261 }
262
ext2fs_set_alloc_block_callback(ext2_filsys fs,errcode_t (* func)(ext2_filsys fs,blk64_t goal,blk64_t * ret),errcode_t (** old)(ext2_filsys fs,blk64_t goal,blk64_t * ret))263 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
264 errcode_t (*func)(ext2_filsys fs,
265 blk64_t goal,
266 blk64_t *ret),
267 errcode_t (**old)(ext2_filsys fs,
268 blk64_t goal,
269 blk64_t *ret))
270 {
271 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
272 return;
273
274 if (old)
275 *old = fs->get_alloc_block;
276
277 fs->get_alloc_block = func;
278 }
279