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;
23 ext2fs_block_bitmap alloc_map;
30 static int process_block(ext2_filsys fs, blk_t *block_nr,
31 int blockcnt, blk_t ref_block,
32 int ref_offset, void *private)
34 struct process_block_struct *pb = private;
39 block = orig = *block_nr;
43 * Let's see if this is one which we need to relocate
45 if (ext2fs_test_block_bitmap(pb->reserve, block)) {
47 if (++block >= fs->super->s_blocks_count)
48 block = fs->super->s_first_data_block;
53 } while (ext2fs_test_block_bitmap(pb->reserve, block) ||
54 ext2fs_test_block_bitmap(pb->alloc_map, block));
56 retval = io_channel_read_blk(fs->io, orig, 1, pb->buf);
61 retval = io_channel_write_blk(fs->io, block, 1, pb->buf);
67 ext2fs_mark_block_bitmap(pb->alloc_map, block);
69 if (pb->flags & EXT2_BMOVE_DEBUG)
70 printf("ino=%ld, blockcnt=%d, %ld->%ld\n", pb->ino,
71 blockcnt, orig, block);
74 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
84 errcode_t ext2fs_move_blocks(ext2_filsys fs,
85 ext2fs_block_bitmap reserve,
86 ext2fs_block_bitmap alloc_map,
90 struct ext2_inode inode;
92 struct process_block_struct pb;
96 retval = ext2fs_open_inode_scan(fs, 0, &scan);
100 pb.reserve = reserve;
102 pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
105 block_buf = malloc(fs->blocksize * 4);
108 pb.buf = block_buf + fs->blocksize * 3;
111 * If GET_DBLIST is set in the flags field, then we should
112 * gather directory block information while we're doing the
115 if (flags & EXT2_BMOVE_GET_DBLIST) {
117 ext2fs_free_dblist(fs->dblist);
120 retval = ext2fs_init_dblist(fs, 0);
125 retval = ext2fs_get_next_inode(scan, &ino, &inode);
130 if ((inode.i_links_count == 0) ||
131 !ext2fs_inode_has_valid_blocks(&inode))
137 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
138 flags & EXT2_BMOVE_GET_DBLIST);
140 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
148 retval = ext2fs_get_next_inode(scan, &ino, &inode);
149 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)