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>
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 e2_blkcnt_t blockcnt, blk_t ref_block,
34 int ref_offset, void *priv_data)
36 struct process_block_struct *pb;
41 pb = (struct process_block_struct *) priv_data;
42 block = orig = *block_nr;
46 * Let's see if this is one which we need to relocate
48 if (ext2fs_test_block_bitmap(pb->reserve, block)) {
50 if (++block >= fs->super->s_blocks_count)
51 block = fs->super->s_first_data_block;
53 pb->error = EXT2_ET_BLOCK_ALLOC_FAIL;
56 } while (ext2fs_test_block_bitmap(pb->reserve, block) ||
57 ext2fs_test_block_bitmap(pb->alloc_map, block));
59 retval = io_channel_read_blk(fs->io, orig, 1, pb->buf);
64 retval = io_channel_write_blk(fs->io, block, 1, pb->buf);
70 ext2fs_mark_block_bitmap(pb->alloc_map, block);
72 if (pb->flags & EXT2_BMOVE_DEBUG)
73 printf("ino=%ld, blockcnt=%lld, %d->%d\n", pb->ino,
74 blockcnt, orig, block);
77 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
78 block, (int) blockcnt);
87 errcode_t ext2fs_move_blocks(ext2_filsys fs,
88 ext2fs_block_bitmap reserve,
89 ext2fs_block_bitmap alloc_map,
93 struct ext2_inode inode;
95 struct process_block_struct pb;
99 retval = ext2fs_open_inode_scan(fs, 0, &scan);
103 pb.reserve = reserve;
105 pb.alloc_map = alloc_map ? alloc_map : fs->block_map;
108 retval = ext2fs_get_mem(fs->blocksize * 4, &block_buf);
111 pb.buf = block_buf + fs->blocksize * 3;
114 * If GET_DBLIST is set in the flags field, then we should
115 * gather directory block information while we're doing the
118 if (flags & EXT2_BMOVE_GET_DBLIST) {
119 ext2fs_free_dblist(fs->dblist);
121 retval = ext2fs_init_dblist(fs, 0);
126 retval = ext2fs_get_next_inode(scan, &ino, &inode);
131 if ((inode.i_links_count == 0) ||
132 !ext2fs_inode_has_valid_blocks(&inode))
138 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
139 flags & EXT2_BMOVE_GET_DBLIST);
141 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
149 retval = ext2fs_get_next_inode(scan, &ino, &inode);
150 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)