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