blob: 8e8ad488e819d26eff643f8bba632e3be42a8155 [file] [log] [blame]
Theodore Ts'o1e1da291997-06-09 14:51:29 +00001/*
2 * bmove.c --- Move blocks around to make way for a particular
3 * filesystem structure.
4 *
5 * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
12#include <stdlib.h>
13#include <sys/types.h>
14#include <sys/time.h>
15
16#include <linux/ext2_fs.h>
17#include "ext2fs/ext2fs.h"
18
19struct process_block_struct {
20 ino_t ino;
21 struct ext2_inode * inode;
22 ext2fs_block_bitmap reserve;
23 errcode_t error;
24 char *buf;
25 int add_dir;
26};
27
28static int process_block(ext2_filsys fs, blk_t *block_nr,
29 int blockcnt, blk_t ref_block,
30 int ref_offset, void *private)
31{
32 struct process_block_struct *pb = private;
33 errcode_t retval;
34 int ret;
35 blk_t block, orig;
36
37 block = orig = *block_nr;
38 ret = 0;
39
40 /*
41 * Let's see if this is one which we need to relocate
42 */
43 if (ext2fs_test_block_bitmap(pb->reserve, block)) {
44 do {
45 if (++block >= fs->super->s_blocks_count)
46 block = fs->super->s_first_data_block;
47 if (block == orig) {
48 pb->error = ENOSPC;
49 return BLOCK_ABORT;
50 }
51 } while (ext2fs_test_block_bitmap(pb->reserve, block) ||
52 ext2fs_test_block_bitmap(fs->block_map, block));
53
54 retval = io_channel_read_blk(fs->io, orig, 1, pb->buf);
55 if (retval) {
56 pb->error = retval;
57 return BLOCK_ABORT;
58 }
59 retval = io_channel_write_blk(fs->io, block, 1, pb->buf);
60 if (retval) {
61 pb->error = retval;
62 return BLOCK_ABORT;
63 }
64 *block_nr = block;
65 ext2fs_mark_block_bitmap(fs->block_map, block);
66 ret = BLOCK_CHANGED;
67 printf("ino=%ld, blockcnt=%d, %ld->%ld\n", pb->ino,
68 blockcnt, orig, block);
69 }
70 if (pb->add_dir) {
71 retval = ext2fs_add_dir_block(fs->dblist, pb->ino,
72 block, blockcnt);
73 if (retval) {
74 pb->error = retval;
75 ret |= BLOCK_ABORT;
76 }
77 }
78 return ret;
79}
80
81errcode_t ext2fs_move_blocks(ext2_filsys fs,
82 ext2fs_block_bitmap reserve,
83 int flags)
84{
85 ino_t ino;
86 struct ext2_inode inode;
87 errcode_t retval;
88 struct process_block_struct pb;
89 ext2_inode_scan scan;
90 char *block_buf;
91
92 retval = ext2fs_open_inode_scan(fs, 0, &scan);
93 if (retval)
94 return retval;
95
96 pb.reserve = reserve;
97 pb.error = 0;
98
99 block_buf = malloc(fs->blocksize * 4);
100 if (!block_buf)
101 return ENOMEM;
102 pb.buf = block_buf + fs->blocksize * 3;
103
104 /*
105 * If GET_DBLIST is set in the flags field, then we should
106 * gather directory block information while we're doing the
107 * block move.
108 */
109 if (flags & EXT2_BMOVE_GET_DBLIST) {
110 if (fs->dblist) {
111 ext2fs_free_dblist(fs->dblist);
112 fs->dblist = NULL;
113 }
114 retval = ext2fs_init_dblist(fs, 0);
115 if (retval)
116 return retval;
117 }
118
119 retval = ext2fs_get_next_inode(scan, &ino, &inode);
120 if (retval)
121 return retval;
122
123 while (ino) {
124 if ((inode.i_links_count == 0) ||
125 !ext2fs_inode_has_valid_blocks(&inode))
126 goto next;
127
128 pb.ino = ino;
129 pb.inode = &inode;
130
131 pb.add_dir = (LINUX_S_ISDIR(inode.i_mode) &&
132 flags & EXT2_BMOVE_GET_DBLIST);
133
134 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
135 process_block, &pb);
136 if (retval)
137 return retval;
138 if (pb.error)
139 return pb.error;
140
141 next:
142 retval = ext2fs_get_next_inode(scan, &ino, &inode);
143 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
144 goto next;
145 }
146 return 0;
147}
148