1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* e2undo.c - Replay an undo log onto an ext2/3/4 filesystem
*
* Copyright IBM Corporation, 2007
* Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <fcntl.h>
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include "ext2fs/tdb.h"
#include "ext2fs/ext2fs.h"
#include "nls-enable.h"
unsigned char mtime_key[] = "filesystem MTIME";
unsigned char uuid_key[] = "filesystem UUID";
unsigned char blksize_key[] = "filesystem BLKSIZE";
char *prg_name;
static void usage(void)
{
fprintf(stderr,
_("Usage: %s <transaction file> <filesystem>\n"), prg_name);
exit(1);
}
static int check_filesystem(TDB_CONTEXT *tdb, io_channel channel)
{
__u32 s_mtime;
__u8 s_uuid[16];
errcode_t retval;
TDB_DATA tdb_key, tdb_data;
struct ext2_super_block super;
io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
if (retval) {
com_err(prg_name,
retval, _("Failed to read the file system data \n"));
return retval;
}
tdb_key.dptr = mtime_key;
tdb_key.dsize = sizeof(mtime_key);
tdb_data = tdb_fetch(tdb, tdb_key);
if (!tdb_data.dptr) {
retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
com_err(prg_name, retval,
_("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
return retval;
}
s_mtime = *(__u32 *)tdb_data.dptr;
if (super.s_mtime != s_mtime) {
com_err(prg_name, 0,
_("The file system Mount time didn't match %u\n"),
s_mtime);
return -1;
}
tdb_key.dptr = uuid_key;
tdb_key.dsize = sizeof(uuid_key);
tdb_data = tdb_fetch(tdb, tdb_key);
if (!tdb_data.dptr) {
retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
com_err(prg_name, retval,
_("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
return retval;
}
memcpy(s_uuid, tdb_data.dptr, sizeof(s_uuid));
if (memcmp(s_uuid, super.s_uuid, sizeof(s_uuid))) {
com_err(prg_name, 0,
_("The file system UUID didn't match \n"));
return -1;
}
return 0;
}
static int set_blk_size(TDB_CONTEXT *tdb, io_channel channel)
{
int block_size;
errcode_t retval;
TDB_DATA tdb_key, tdb_data;
tdb_key.dptr = blksize_key;
tdb_key.dsize = sizeof(blksize_key);
tdb_data = tdb_fetch(tdb, tdb_key);
if (!tdb_data.dptr) {
retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
com_err(prg_name, retval,
_("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
return retval;
}
block_size = *(int *)tdb_data.dptr;
#ifdef DEBUG
printf("Block size %d\n", block_size);
#endif
io_channel_set_blksize(channel, block_size);
return 0;
}
int main(int argc, char *argv[])
{
int c,force = 0;
TDB_CONTEXT *tdb;
TDB_DATA key, data;
io_channel channel;
errcode_t retval;
int mount_flags;
blk64_t blk_num;
char *device_name, *tdb_file;
io_manager manager = unix_io_manager;
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "");
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
textdomain(NLS_CAT_NAME);
#endif
add_error_table(&et_ext2_error_table);
prg_name = argv[0];
while((c = getopt(argc, argv, "f")) != EOF) {
switch (c) {
case 'f':
force = 1;
break;
default:
usage();
}
}
if (argc != optind + 2)
usage();
tdb_file = argv[optind];
device_name = argv[optind+1];
tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0600);
if (!tdb) {
com_err(prg_name, errno,
_("Failed tdb_open %s\n"), tdb_file);
exit(1);
}
retval = ext2fs_check_if_mounted(device_name, &mount_flags);
if (retval) {
com_err(prg_name, retval, _("Error while determining whether "
"%s is mounted.\n"), device_name);
exit(1);
}
if (mount_flags & EXT2_MF_MOUNTED) {
com_err(prg_name, retval, _("e2undo should only be run on "
"unmounted file system\n"));
exit(1);
}
retval = manager->open(device_name,
IO_FLAG_EXCLUSIVE | IO_FLAG_RW, &channel);
if (retval) {
com_err(prg_name, retval,
_("Failed to open %s\n"), device_name);
exit(1);
}
if (!force && check_filesystem(tdb, channel)) {
exit(1);
}
if (set_blk_size(tdb, channel)) {
exit(1);
}
for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) {
if (!strcmp((char *) key.dptr, (char *) mtime_key) ||
!strcmp((char *) key.dptr, (char *) uuid_key) ||
!strcmp((char *) key.dptr, (char *) blksize_key)) {
continue;
}
data = tdb_fetch(tdb, key);
if (!data.dptr) {
com_err(prg_name, 0,
_("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
exit(1);
}
blk_num = *(unsigned long *)key.dptr;
printf(_("Replayed transaction of size %zd at location %llu\n"),
data.dsize, blk_num);
retval = io_channel_write_blk64(channel, blk_num,
-data.dsize, data.dptr);
if (retval == -1) {
com_err(prg_name, retval,
_("Failed write %s\n"),
strerror(errno));
exit(1);
}
}
io_channel_close(channel);
tdb_close(tdb);
return 0;
}
|