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