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.
15 #include <sys/types.h>
24 struct process_block_struct {
26 struct ext2_inode * inode;
27 ext2fs_block_bitmap reserve;
28 ext2fs_block_bitmap alloc_map;
35 static int process_block(ext2_filsys fs, blk_t *block_nr,
36 e2_blkcnt_t blockcnt, blk_t ref_block,
37 int ref_offset, void *priv_data)
39 struct process_block_struct *pb;
44 pb = (struct process_block_struct *) priv_data;
45 block = orig = *block_nr;
49 * Let's see if this is one which we need to relocate
51 if (ext2fs_test_block_bitmap2(pb->reserve, block)) {
53 if (++block >= fs->super->s_blocks_count)
54 block = fs->super->s_first_data_block;
56 pb->error = EXT2_ET_BLOCK_ALLOC_FAIL;
59 } while (ext2fs_test_block_bitmap2(pb->reserve, block) ||
60 ext2fs_test_block_bitmap2(pb->alloc_map, block));
62 retval = io_channel_read_blk64(fs->io, orig, 1, pb->buf);
67 retval = io_channel_write_blk64(fs->io, block, 1, pb->buf);
73 ext2fs_mark_block_bitmap2(pb->alloc_map, block);
75 if (pb->flags & EXT2_BMOVE_DEBUG)
76 printf("ino=%ld, blockcnt=%lld, %u->%u\n", pb->ino,
77 blockcnt, orig, block);
80 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
81 block, (int) blockcnt);
90 errcode_t ext2fs_move_blocks(ext2_filsys fs,
91 ext2fs_block_bitmap reserve,
92 ext2fs_block_bitmap alloc_map,
96 struct ext2_inode inode;
98 struct process_block_struct pb;
102 retval = ext2fs_open_inode_scan(fs, 0, &scan);
106 pb.reserve = reserve;
108 pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
111 retval = ext2fs_get_array(4, fs->blocksize, &block_buf);
114 pb.buf = block_buf + fs->blocksize * 3;
117 * If GET_DBLIST is set in the flags field, then we should
118 * gather directory block information while we're doing the
121 if (flags & EXT2_BMOVE_GET_DBLIST) {
123 ext2fs_free_dblist(fs->dblist);
126 retval = ext2fs_init_dblist(fs, 0);
131 retval = ext2fs_get_next_inode(scan, &ino, &inode);
136 if ((inode.i_links_count == 0) ||
137 !ext2fs_inode_has_valid_blocks(&inode))
143 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
144 flags & EXT2_BMOVE_GET_DBLIST);
146 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
154 retval = ext2fs_get_next_inode(scan, &ino, &inode);
155 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)