2 * bmove.c --- Move blocks around to make way for a particular
3 * filesystem structure.
5 * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
13 #include <sys/types.h>
16 #include <linux/ext2_fs.h>
17 #include "ext2fs/ext2fs.h"
19 struct process_block_struct {
21 struct ext2_inode * inode;
22 ext2fs_block_bitmap reserve;
28 static int process_block(ext2_filsys fs, blk_t *block_nr,
29 int blockcnt, blk_t ref_block,
30 int ref_offset, void *private)
32 struct process_block_struct *pb = private;
37 block = orig = *block_nr;
41 * Let's see if this is one which we need to relocate
43 if (ext2fs_test_block_bitmap(pb->reserve, block)) {
45 if (++block >= fs->super->s_blocks_count)
46 block = fs->super->s_first_data_block;
51 } while (ext2fs_test_block_bitmap(pb->reserve, block) ||
52 ext2fs_test_block_bitmap(fs->block_map, block));
54 retval = io_channel_read_blk(fs->io, orig, 1, pb->buf);
59 retval = io_channel_write_blk(fs->io, block, 1, pb->buf);
65 ext2fs_mark_block_bitmap(fs->block_map, block);
67 printf("ino=%ld, blockcnt=%d, %ld->%ld\n", pb->ino,
68 blockcnt, orig, block);
71 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
81 errcode_t ext2fs_move_blocks(ext2_filsys fs,
82 ext2fs_block_bitmap reserve,
86 struct ext2_inode inode;
88 struct process_block_struct pb;
92 retval = ext2fs_open_inode_scan(fs, 0, &scan);
99 block_buf = malloc(fs->blocksize * 4);
102 pb.buf = block_buf + fs->blocksize * 3;
105 * If GET_DBLIST is set in the flags field, then we should
106 * gather directory block information while we're doing the
109 if (flags & EXT2_BMOVE_GET_DBLIST) {
111 ext2fs_free_dblist(fs->dblist);
114 retval = ext2fs_init_dblist(fs, 0);
119 retval = ext2fs_get_next_inode(scan, &ino, &inode);
124 if ((inode.i_links_count == 0) ||
125 !ext2fs_inode_has_valid_blocks(&inode))
131 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
132 flags & EXT2_BMOVE_GET_DBLIST);
134 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
142 retval = ext2fs_get_next_inode(scan, &ino, &inode);
143 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)