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