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 void ext2fs_clear_block_uninit(ext2_filsys fs, dgrp_t group)
44 if (group >= fs->group_desc_count ||
45 !ext2fs_has_group_desc_csum(fs) ||
46 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
49 /* uninit block bitmaps are now initialized in read_bitmaps() */
51 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
52 ext2fs_group_desc_csum_set(fs, group);
53 ext2fs_mark_super_dirty(fs);
54 ext2fs_mark_bb_dirty(fs);
58 * Check for uninit inode bitmaps and deal with them appropriately
60 static void check_inode_uninit(ext2_filsys fs, ext2fs_inode_bitmap map,
65 if (group >= fs->group_desc_count ||
66 !ext2fs_has_group_desc_csum(fs) ||
67 !(ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
70 ino = (group * fs->super->s_inodes_per_group) + 1;
71 for (i=0; i < fs->super->s_inodes_per_group; i++, ino++)
72 ext2fs_fast_unmark_inode_bitmap2(map, ino);
74 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
75 /* Mimics what the kernel does */
76 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
77 ext2fs_group_desc_csum_set(fs, group);
78 ext2fs_mark_ib_dirty(fs);
79 ext2fs_mark_super_dirty(fs);
83 * Right now, just search forward from the parent directory's block
84 * group to find the next free inode.
86 * Should have a special policy for directories.
88 errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir,
89 int mode EXT2FS_ATTR((unused)),
90 ext2fs_inode_bitmap map, ext2_ino_t *ret)
92 ext2_ino_t start_inode = 0;
93 ext2_ino_t i, ino_in_group, upto, first_zero;
97 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
102 return EXT2_ET_NO_INODE_BITMAP;
105 group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
106 start_inode = (group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
108 if (start_inode < EXT2_FIRST_INODE(fs->super))
109 start_inode = EXT2_FIRST_INODE(fs->super);
110 if (start_inode > fs->super->s_inodes_count)
111 return EXT2_ET_INODE_ALLOC_FAIL;
114 ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super);
115 group = (i - 1) / EXT2_INODES_PER_GROUP(fs->super);
117 check_inode_uninit(fs, map, group);
118 upto = i + (EXT2_INODES_PER_GROUP(fs->super) - ino_in_group);
119 if (i < start_inode && upto >= start_inode)
120 upto = start_inode - 1;
121 if (upto > fs->super->s_inodes_count)
122 upto = fs->super->s_inodes_count;
124 retval = ext2fs_find_first_zero_inode_bitmap2(map, i, upto,
130 if (retval != ENOENT)
131 return EXT2_ET_INODE_ALLOC_FAIL;
133 if (i > fs->super->s_inodes_count)
134 i = EXT2_FIRST_INODE(fs->super);
135 } while (i != start_inode);
137 if (ext2fs_test_inode_bitmap2(map, i))
138 return EXT2_ET_INODE_ALLOC_FAIL;
144 * Stupid algorithm --- we now just search forward starting from the
145 * goal. Should put in a smarter one someday....
147 errcode_t ext2fs_new_block3(ext2_filsys fs, blk64_t goal,
148 ext2fs_block_bitmap map, blk64_t *ret,
149 struct blk_alloc_ctx *ctx)
153 errcode_t (*gab)(ext2_filsys fs, blk64_t goal, blk64_t *ret);
154 errcode_t (*gab2)(ext2_filsys, blk64_t, blk64_t *,
155 struct blk_alloc_ctx *);
157 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
161 * In case there are clients out there whose get_alloc_block
162 * handlers call ext2fs_new_block2 with a NULL block map,
163 * temporarily swap out the function pointer so that we don't
164 * end up in an infinite loop.
166 if (fs->get_alloc_block2) {
167 gab2 = fs->get_alloc_block2;
168 fs->get_alloc_block2 = NULL;
169 retval = gab2(fs, goal, &b, ctx);
170 fs->get_alloc_block2 = gab2;
172 } else if (fs->get_alloc_block) {
173 gab = fs->get_alloc_block;
174 fs->get_alloc_block = NULL;
175 retval = gab(fs, goal, &b);
176 fs->get_alloc_block = gab;
183 return EXT2_ET_NO_BLOCK_BITMAP;
184 if (!goal || (goal >= ext2fs_blocks_count(fs->super)))
185 goal = fs->super->s_first_data_block;
186 goal &= ~EXT2FS_CLUSTER_MASK(fs);
188 retval = ext2fs_find_first_zero_block_bitmap2(map,
189 goal, ext2fs_blocks_count(fs->super) - 1, &b);
190 if ((retval == ENOENT) && (goal != fs->super->s_first_data_block))
191 retval = ext2fs_find_first_zero_block_bitmap2(map,
192 fs->super->s_first_data_block, goal - 1, &b);
194 if (retval == ENOENT)
195 return EXT2_ET_BLOCK_ALLOC_FAIL;
199 ext2fs_clear_block_uninit(fs, ext2fs_group_of_blk2(fs, b));
204 errcode_t ext2fs_new_block2(ext2_filsys fs, blk64_t goal,
205 ext2fs_block_bitmap map, blk64_t *ret)
207 return ext2fs_new_block3(fs, goal, map, ret, NULL);
210 errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
211 ext2fs_block_bitmap map, blk_t *ret)
215 retval = ext2fs_new_block2(fs, goal, map, &val);
222 * This function zeros out the allocated block, and updates all of the
223 * appropriate filesystem records.
225 errcode_t ext2fs_alloc_block3(ext2_filsys fs, blk64_t goal, char *block_buf,
226 blk64_t *ret, struct blk_alloc_ctx *ctx)
231 if (fs->get_alloc_block2) {
232 retval = (fs->get_alloc_block2)(fs, goal, &block, ctx);
235 } else if (fs->get_alloc_block) {
236 retval = (fs->get_alloc_block)(fs, goal, &block);
240 if (!fs->block_map) {
241 retval = ext2fs_read_block_bitmap(fs);
246 retval = ext2fs_new_block3(fs, goal, 0, &block, ctx);
252 memset(block_buf, 0, fs->blocksize);
253 retval = io_channel_write_blk64(fs->io, block, 1, block_buf);
255 retval = ext2fs_zero_blocks2(fs, block, 1, NULL, NULL);
259 ext2fs_block_alloc_stats2(fs, block, +1);
266 errcode_t ext2fs_alloc_block2(ext2_filsys fs, blk64_t goal,
267 char *block_buf, blk64_t *ret)
269 return ext2fs_alloc_block3(fs, goal, block_buf, ret, NULL);
272 errcode_t ext2fs_alloc_block(ext2_filsys fs, blk_t goal,
273 char *block_buf, blk_t *ret)
276 blk64_t ret64, goal64 = goal;
277 retval = ext2fs_alloc_block3(fs, goal64, block_buf, &ret64, NULL);
283 errcode_t ext2fs_get_free_blocks2(ext2_filsys fs, blk64_t start, blk64_t finish,
284 int num, ext2fs_block_bitmap map, blk64_t *ret)
289 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
294 return EXT2_ET_NO_BLOCK_BITMAP;
296 b = fs->super->s_first_data_block;
301 c_ratio = 1 << ext2fs_get_bitmap_granularity(map);
303 finish &= ~(c_ratio -1);
305 if (b + num - 1 >= ext2fs_blocks_count(fs->super)) {
307 return EXT2_ET_BLOCK_ALLOC_FAIL;
308 b = fs->super->s_first_data_block;
310 if (ext2fs_fast_test_block_bitmap_range2(map, b, num)) {
315 } while (b != finish);
316 return EXT2_ET_BLOCK_ALLOC_FAIL;
319 errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
320 int num, ext2fs_block_bitmap map, blk_t *ret)
324 retval = ext2fs_get_free_blocks2(fs, start, finish, num, map, &val);
330 void ext2fs_set_alloc_block_callback(ext2_filsys fs,
331 errcode_t (*func)(ext2_filsys fs,
334 errcode_t (**old)(ext2_filsys fs,
338 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
342 *old = fs->get_alloc_block;
344 fs->get_alloc_block = func;
347 blk64_t ext2fs_find_inode_goal(ext2_filsys fs, ext2_ino_t ino,
348 struct ext2_inode *inode, blk64_t lblk)
352 struct ext2fs_extent extent;
353 ext2_extent_handle_t handle = NULL;
356 /* Make sure data stored in inode->i_block is neither fast symlink nor
359 if (inode == NULL || ext2fs_is_fast_symlink(inode) ||
360 inode->i_flags & EXT4_INLINE_DATA_FL)
363 if (inode->i_flags & EXT4_EXTENTS_FL) {
364 err = ext2fs_extent_open2(fs, ino, inode, &handle);
367 err = ext2fs_extent_goto2(handle, 0, lblk);
370 err = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
373 ext2fs_extent_free(handle);
374 return extent.e_pblk + (lblk - extent.e_lblk);
377 /* block mapped file; see if block zero is mapped? */
378 if (inode->i_block[0])
379 return inode->i_block[0];
382 ext2fs_extent_free(handle);
383 log_flex = fs->super->s_log_groups_per_flex;
384 group = ext2fs_group_of_ino(fs, ino);
386 group = group & ~((1 << (log_flex)) - 1);
387 return ext2fs_group_first_block2(fs, group);
391 * Starting at _goal_, scan around the filesystem to find a run of free blocks
392 * that's at least _len_ blocks long. Possible flags:
393 * - EXT2_NEWRANGE_EXACT_GOAL: The range of blocks must start at _goal_.
394 * - EXT2_NEWRANGE_MIN_LENGTH: do not return a allocation shorter than _len_.
395 * - EXT2_NEWRANGE_ZERO_BLOCKS: Zero blocks pblk to pblk+plen before returning.
397 * The starting block is returned in _pblk_ and the length is returned via
398 * _plen_. The blocks are not marked in the bitmap; the caller must mark
399 * however much of the returned run they actually use, hopefully via
400 * ext2fs_block_alloc_stats_range().
402 * This function can return a range that is longer than what was requested.
404 errcode_t ext2fs_new_range(ext2_filsys fs, int flags, blk64_t goal,
405 blk64_t len, ext2fs_block_bitmap map, blk64_t *pblk,
409 blk64_t start, end, b;
411 blk64_t max_blocks = ext2fs_blocks_count(fs->super);
412 errcode_t (*nrf)(ext2_filsys fs, int flags, blk64_t goal,
413 blk64_t len, blk64_t *pblk, blk64_t *plen);
415 dbg_printf("%s: flags=0x%x goal=%llu len=%llu\n", __func__, flags,
417 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
418 if (len == 0 || (flags & ~EXT2_NEWRANGE_ALL_FLAGS))
419 return EXT2_ET_INVALID_ARGUMENT;
421 if (!map && fs->new_range) {
423 * In case there are clients out there whose new_range
424 * handlers call ext2fs_new_range with a NULL block map,
425 * temporarily swap out the function pointer so that we don't
426 * end up in an infinite loop.
429 fs->new_range = NULL;
430 retval = nrf(fs, flags, goal, len, pblk, plen);
441 return EXT2_ET_NO_BLOCK_BITMAP;
442 if (!goal || goal >= ext2fs_blocks_count(fs->super))
443 goal = fs->super->s_first_data_block;
446 while (!looped || start <= goal) {
447 retval = ext2fs_find_first_zero_block_bitmap2(map, start,
450 if (retval == ENOENT) {
452 * If there are no free blocks beyond the starting
453 * point, try scanning the whole filesystem, unless the
454 * user told us only to allocate from _goal_, or if
455 * we're already scanning the whole filesystem.
457 if (flags & EXT2_NEWRANGE_FIXED_GOAL ||
458 start == fs->super->s_first_data_block)
460 start = fs->super->s_first_data_block;
465 if (flags & EXT2_NEWRANGE_FIXED_GOAL && start != goal)
468 b = min(start + len - 1, max_blocks - 1);
469 retval = ext2fs_find_first_set_block_bitmap2(map, start, b,
471 if (retval == ENOENT)
476 if (!(flags & EXT2_NEWRANGE_MIN_LENGTH) ||
477 (end - start) >= len) {
481 dbg_printf("%s: new_range goal=%llu--%llu "
482 "blk=%llu--%llu %llu\n",
483 __func__, goal, goal + len - 1,
484 *pblk, *pblk + *plen - 1, *plen);
486 for (b = start; b < end;
487 b += fs->super->s_blocks_per_group)
488 ext2fs_clear_block_uninit(fs,
489 ext2fs_group_of_blk2(fs, b));
493 if (flags & EXT2_NEWRANGE_FIXED_GOAL)
496 if (start >= max_blocks) {
500 start = fs->super->s_first_data_block;
505 retval = EXT2_ET_BLOCK_ALLOC_FAIL;
510 void ext2fs_set_new_range_callback(ext2_filsys fs,
511 errcode_t (*func)(ext2_filsys fs, int flags, blk64_t goal,
512 blk64_t len, blk64_t *pblk, blk64_t *plen),
513 errcode_t (**old)(ext2_filsys fs, int flags, blk64_t goal,
514 blk64_t len, blk64_t *pblk, blk64_t *plen))
516 if (!fs || fs->magic != EXT2_ET_MAGIC_EXT2FS_FILSYS)
520 *old = fs->new_range;
522 fs->new_range = func;
525 errcode_t ext2fs_alloc_range(ext2_filsys fs, int flags, blk64_t goal,
526 blk_t len, blk64_t *ret)
528 int newr_flags = EXT2_NEWRANGE_MIN_LENGTH;
532 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
533 if (len == 0 || (flags & ~EXT2_ALLOCRANGE_ALL_FLAGS))
534 return EXT2_ET_INVALID_ARGUMENT;
536 if (flags & EXT2_ALLOCRANGE_FIXED_GOAL)
537 newr_flags |= EXT2_NEWRANGE_FIXED_GOAL;
539 retval = ext2fs_new_range(fs, newr_flags, goal, len, NULL, ret, &plen);
544 return EXT2_ET_BLOCK_ALLOC_FAIL;
546 if (flags & EXT2_ALLOCRANGE_ZERO_BLOCKS) {
547 retval = ext2fs_zero_blocks2(fs, *ret, len, NULL, NULL);
552 ext2fs_block_alloc_stats_range(fs, *ret, len, +1);