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