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>
18 #include <linux/ext2_fs.h>
19 #include "ext2fs/ext2fs.h"
21 struct process_block_struct {
23 struct ext2_inode * inode;
24 ext2fs_block_bitmap reserve;
25 ext2fs_block_bitmap alloc_map;
32 static int process_block(ext2_filsys fs, blk_t *block_nr,
33 int blockcnt, blk_t ref_block,
34 int ref_offset, void *private)
36 struct process_block_struct *pb = private;
41 block = orig = *block_nr;
45 * Let's see if this is one which we need to relocate
47 if (ext2fs_test_block_bitmap(pb->reserve, block)) {
49 if (++block >= fs->super->s_blocks_count)
50 block = fs->super->s_first_data_block;
55 } while (ext2fs_test_block_bitmap(pb->reserve, block) ||
56 ext2fs_test_block_bitmap(pb->alloc_map, block));
58 retval = io_channel_read_blk(fs->io, orig, 1, pb->buf);
63 retval = io_channel_write_blk(fs->io, block, 1, pb->buf);
69 ext2fs_mark_block_bitmap(pb->alloc_map, block);
71 if (pb->flags & EXT2_BMOVE_DEBUG)
72 printf("ino=%ld, blockcnt=%d, %ld->%ld\n", pb->ino,
73 blockcnt, orig, block);
76 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
86 errcode_t ext2fs_move_blocks(ext2_filsys fs,
87 ext2fs_block_bitmap reserve,
88 ext2fs_block_bitmap alloc_map,
92 struct ext2_inode inode;
94 struct process_block_struct pb;
98 retval = ext2fs_open_inode_scan(fs, 0, &scan);
102 pb.reserve = reserve;
104 pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
107 block_buf = malloc(fs->blocksize * 4);
110 pb.buf = block_buf + fs->blocksize * 3;
113 * If GET_DBLIST is set in the flags field, then we should
114 * gather directory block information while we're doing the
117 if (flags & EXT2_BMOVE_GET_DBLIST) {
119 ext2fs_free_dblist(fs->dblist);
122 retval = ext2fs_init_dblist(fs, 0);
127 retval = ext2fs_get_next_inode(scan, &ino, &inode);
132 if ((inode.i_links_count == 0) ||
133 !ext2fs_inode_has_valid_blocks(&inode))
139 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
140 flags & EXT2_BMOVE_GET_DBLIST);
142 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
150 retval = ext2fs_get_next_inode(scan, &ino, &inode);
151 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)