2 * alloc.c --- allocate new inodes, blocks for ext2fs
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
23 #include <sys/types.h>
29 #define min(a, b) ((a) < (b) ? (a) : (b))
34 # define dbg_printf(f, a...) do {printf(f, ## a); fflush(stdout); } while (0)
36 # define dbg_printf(f, a...)
40 * Clear the uninit block bitmap flag if necessary
42 static void clear_block_uninit(ext2_filsys fs, dgrp_t group)
44 if (!ext2fs_has_group_desc_csum(fs) ||
45 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
48 /* uninit block bitmaps are now initialized in read_bitmaps() */
50 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
51 ext2fs_group_desc_csum_set(fs, group);
52 ext2fs_mark_super_dirty(fs);
53 ext2fs_mark_bb_dirty(fs);
57 * Check for uninit inode bitmaps and deal with them appropriately
59 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
64 if (!ext2fs_has_group_desc_csum(fs) ||
65 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
68 ino = (group * fs->super->s_inodes_per_group) + 1;
69 for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
70 ext2fs_fast_unmark_inode_bitmap2(map, ino);
72 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
73 /* Mimics what the kernel does */
74 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
75 ext2fs_group_desc_csum_set(fs, group);
76 ext2fs_mark_ib_dirty(fs);
77 ext2fs_mark_super_dirty(fs);
81 * Right now, just search forward from the parent directory's block
82 * group to find the next free inode.
84 * Should have a special policy for directories.
86 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
87 int mode EXT2FS_ATTR((unused)),
88 ext2fs_inode_bitmap map, ext2_ino_t *ret)
90 ext2_ino_t start_inode = 0;
91 ext2_ino_t i, ino_in_group, upto, first_zero;
95 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
100 return EXT2_ET_NO_INODE_BITMAP;
103 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
104 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
106 if (start_inode < EXT2_FIRST_INODE(fs->super))
107 start_inode = EXT2_FIRST_INODE(fs->super);
108 if (start_inode > fs->super->s_inodes_count)
109 return EXT2_ET_INODE_ALLOC_FAIL;
112 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
113 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
115 check_inode_uninit(fs, map, group);
116 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
117 if (i < start_inode && upto >= start_inode)
118 upto = start_inode - 1;
119 if (upto > fs->super->s_inodes_count)
120 upto = fs->super->s_inodes_count;
122 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
128 if (retval != ENOENT)
129 return EXT2_ET_INODE_ALLOC_FAIL;
131 if (i > fs->super->s_inodes_count)
132 i = EXT2_FIRST_INODE(fs->super);
133 } while (i != start_inode);
135 if (ext2fs_test_inode_bitmap2(map, i))
136 return EXT2_ET_INODE_ALLOC_FAIL;
142 * Stupid algorithm --- we now just search forward starting from the
143 * goal. Should put in a smarter one someday....
145 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
146 ext2fs_block_bitmap map, blk64_t *ret)
150 errcode_t (*gab)(ext2_filsys fs, blk64_t goal, blk64_t *ret);
152 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
154 if (!map && fs->get_alloc_block) {
156 * In case there are clients out there whose get_alloc_block
157 * handlers call ext2fs_new_block2 with a NULL block map,
158 * temporarily swap out the function pointer so that we don't
159 * end up in an infinite loop.
161 gab = fs->get_alloc_block;
162 fs->get_alloc_block = NULL;
163 retval = gab(fs, goal, &b);
164 fs->get_alloc_block = gab;
170 return EXT2_ET_NO_BLOCK_BITMAP;
171 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
172 goal = fs->super->s_first_data_block;
173 goal &= ~EXT2FS_CLUSTER_MASK(fs);
175 retval = ext2fs_find_first_zero_block_bitmap2(map,
176 goal, ext2fs_blocks_count(fs->super) - 1, &b);
177 if ((retval == ENOENT) && (goal != fs->super->s_first_data_block))
178 retval = ext2fs_find_first_zero_block_bitmap2(map,
179 fs->super->s_first_data_block, goal - 1, &b);
181 if (retval == ENOENT)
182 return EXT2_ET_BLOCK_ALLOC_FAIL;
186 clear_block_uninit(fs, ext2fs_group_of_blk2(fs, b));
191 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
192 ext2fs_block_bitmap map, blk_t *ret)
196 retval = ext2fs_new_block2(fs, goal, map, &val);
203 * This function zeros out the allocated block, and updates all of the
204 * appropriate filesystem records.
206 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
207 char *block_buf, blk64_t *ret)
212 if (fs->get_alloc_block) {
213 retval = (fs->get_alloc_block)(fs, goal, &block);
217 if (!fs->block_map) {
218 retval = ext2fs_read_block_bitmap(fs);
223 retval = ext2fs_new_block2(fs, goal, 0, &block);
229 memset(block_buf, 0, fs->blocksize);
230 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
232 retval = ext2fs_zero_blocks2(fs, block, 1, NULL, NULL);
236 ext2fs_block_alloc_stats2(fs, block, +1);
243 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
244 char *block_buf, blk_t *ret)
248 retval = ext2fs_alloc_block2(fs, goal, block_buf, &val);
254 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
255 int num, ext2fs_block_bitmap map, blk64_t *ret)
260 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
265 return EXT2_ET_NO_BLOCK_BITMAP;
267 b = fs->super->s_first_data_block;
272 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
274 finish &= ~(c_ratio -1);
276 if (b + num - 1 >= ext2fs_blocks_count(fs->super)) {
278 return EXT2_ET_BLOCK_ALLOC_FAIL;
279 b = fs->super->s_first_data_block;
281 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
286 } while (b != finish);
287 return EXT2_ET_BLOCK_ALLOC_FAIL;
290 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
291 int num, ext2fs_block_bitmap map, blk_t *ret)
295 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
301 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
302 errcode_t (*func)(ext2_filsys fs,
305 errcode_t (**old)(ext2_filsys fs,
309 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
313 *old = fs->get_alloc_block;
315 fs->get_alloc_block = func;
318 blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
319 struct ext2_inode *inode, blk64_t lblk)
323 struct ext2fs_extent extent;
324 ext2_extent_handle_t handle = NULL;
327 if (inode == NULL || ext2fs_inode_data_blocks2(fs, inode) == 0)
330 if (inode->i_flags & EXT4_INLINE_DATA_FL)
333 if (inode->i_flags & EXT4_EXTENTS_FL) {
334 err = ext2fs_extent_open2(fs, ino, inode, &handle);
337 err = ext2fs_extent_goto2(handle, 0, lblk);
340 err = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
343 ext2fs_extent_free(handle);
344 return extent.e_pblk + (lblk - extent.e_lblk);
347 /* block mapped file; see if block zero is mapped? */
348 if (inode->i_block[0])
349 return inode->i_block[0];
352 ext2fs_extent_free(handle);
353 log_flex = fs->super->s_log_groups_per_flex;
354 group = ext2fs_group_of_ino(fs, ino);
356 group = group & ~((1 << (log_flex)) - 1);
357 return ext2fs_group_first_block2(fs, group);
361 * Starting at _goal_, scan around the filesystem to find a run of free blocks
362 * that's at least _len_ blocks long. Possible flags:
363 * - EXT2_NEWRANGE_EXACT_GOAL: The range of blocks must start at _goal_.
364 * - EXT2_NEWRANGE_MIN_LENGTH: do not return a allocation shorter than _len_.
365 * - EXT2_NEWRANGE_ZERO_BLOCKS: Zero blocks pblk to pblk+plen before returning.
367 * The starting block is returned in _pblk_ and the length is returned via
368 * _plen_. The blocks are not marked in the bitmap; the caller must mark
369 * however much of the returned run they actually use, hopefully via
370 * ext2fs_block_alloc_stats_range().
372 * This function can return a range that is longer than what was requested.
374 errcode_t ext2fs_new_range(ext2_filsys fs, int flags, blk64_t goal,
375 blk64_t len, ext2fs_block_bitmap map, blk64_t *pblk,
379 blk64_t start, end, b;
381 blk64_t max_blocks = ext2fs_blocks_count(fs->super);
383 dbg_printf("%s: flags=0x%x goal=%llu len=%llu\n", __func__, flags,
385 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
386 if (len == 0 || (flags & ~EXT2_NEWRANGE_ALL_FLAGS))
387 return EXT2_ET_INVALID_ARGUMENT;
391 return EXT2_ET_NO_BLOCK_BITMAP;
392 if (!goal || goal >= ext2fs_blocks_count(fs->super))
393 goal = fs->super->s_first_data_block;
396 while (!looped || start <= goal) {
397 retval = ext2fs_find_first_zero_block_bitmap2(map, start,
400 if (retval == ENOENT) {
402 * If there are no free blocks beyond the starting
403 * point, try scanning the whole filesystem, unless the
404 * user told us only to allocate from _goal_, or if
405 * we're already scanning the whole filesystem.
407 if (flags & EXT2_NEWRANGE_FIXED_GOAL ||
408 start == fs->super->s_first_data_block)
410 start = fs->super->s_first_data_block;
415 if (flags & EXT2_NEWRANGE_FIXED_GOAL && start != goal)
418 b = min(start + len - 1, max_blocks - 1);
419 retval = ext2fs_find_first_set_block_bitmap2(map, start, b,
421 if (retval == ENOENT)
426 if (!(flags & EXT2_NEWRANGE_MIN_LENGTH) ||
427 (end - start) >= len) {
431 dbg_printf("%s: new_range goal=%llu--%llu "
432 "blk=%llu--%llu %llu\n",
433 __func__, goal, goal + len - 1,
434 *pblk, *pblk + *plen - 1, *plen);
436 for (b = start; b < end;
437 b += fs->super->s_blocks_per_group)
438 clear_block_uninit(fs,
439 ext2fs_group_of_blk2(fs, b));
443 if (flags & EXT2_NEWRANGE_FIXED_GOAL)
446 if (start >= max_blocks) {
450 start = fs->super->s_first_data_block;
455 retval = EXT2_ET_BLOCK_ALLOC_FAIL;
460 errcode_t ext2fs_alloc_range(ext2_filsys fs, int flags, blk64_t goal,
461 blk_t len, blk64_t *ret)
463 int newr_flags = EXT2_NEWRANGE_MIN_LENGTH;
467 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
468 if (len == 0 || (flags & ~EXT2_ALLOCRANGE_ALL_FLAGS))
469 return EXT2_ET_INVALID_ARGUMENT;
471 if (flags & EXT2_ALLOCRANGE_FIXED_GOAL)
472 newr_flags |= EXT2_NEWRANGE_FIXED_GOAL;
474 retval = ext2fs_new_range(fs, newr_flags, goal, len, NULL, ret, &plen);
479 return EXT2_ET_BLOCK_ALLOC_FAIL;
481 if (flags & EXT2_ALLOCRANGE_ZERO_BLOCKS) {
482 retval = ext2fs_zero_blocks2(fs, *ret, len, NULL, NULL);
487 ext2fs_block_alloc_stats_range(fs, *ret, len, +1);