2 * bmove.c --- Move blocks around to make way for a particular
3 * filesystem structure.
5 * Copyright (C) 1997 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
20 #include <sys/types.h>
29 struct process_block_struct {
31 struct ext2_inode * inode;
32 ext2fs_block_bitmap reserve;
33 ext2fs_block_bitmap alloc_map;
40 static int process_block(ext2_filsys fs, blk64_t *block_nr,
41 e2_blkcnt_t blockcnt, blk64_t ref_block,
42 int ref_offset, void *priv_data)
44 struct process_block_struct *pb;
49 pb = (struct process_block_struct *) priv_data;
50 block = orig = *block_nr;
54 * Let's see if this is one which we need to relocate
56 if (ext2fs_test_block_bitmap2(pb->reserve, block)) {
58 if (++block >= ext2fs_blocks_count(fs->super))
59 block = fs->super->s_first_data_block;
61 pb->error = EXT2_ET_BLOCK_ALLOC_FAIL;
64 } while (ext2fs_test_block_bitmap2(pb->reserve, block) ||
65 ext2fs_test_block_bitmap2(pb->alloc_map, block));
67 retval = io_channel_read_blk64(fs->io, orig, 1, pb->buf);
72 retval = io_channel_write_blk64(fs->io, block, 1, pb->buf);
78 ext2fs_mark_block_bitmap2(pb->alloc_map, block);
80 if (pb->flags & EXT2_BMOVE_DEBUG)
81 printf("ino=%u, blockcnt=%lld, %llu->%llu\n",
82 (unsigned) pb->ino, blockcnt,
83 (unsigned long long) orig,
84 (unsigned long long) block);
87 retval = ext2fs_add_dir_block2(fs->dblist, pb->ino,
97 errcode_t ext2fs_move_blocks(ext2_filsys fs,
98 ext2fs_block_bitmap reserve,
99 ext2fs_block_bitmap alloc_map,
103 struct ext2_inode inode;
105 struct process_block_struct pb;
106 ext2_inode_scan scan;
109 retval = ext2fs_open_inode_scan(fs, 0, &scan);
113 pb.reserve = reserve;
115 pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
118 retval = ext2fs_get_array(4, fs->blocksize, &block_buf);
121 pb.buf = block_buf + fs->blocksize * 3;
124 * If GET_DBLIST is set in the flags field, then we should
125 * gather directory block information while we're doing the
128 if (flags & EXT2_BMOVE_GET_DBLIST) {
130 ext2fs_free_dblist(fs->dblist);
133 retval = ext2fs_init_dblist(fs, 0);
138 retval = ext2fs_get_next_inode(scan, &ino, &inode);
143 if ((inode.i_links_count == 0) ||
144 !ext2fs_inode_has_valid_blocks2(fs, &inode))
150 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
151 flags & EXT2_BMOVE_GET_DBLIST);
153 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
161 retval = ext2fs_get_next_inode(scan, &ino, &inode);
162 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)