2 * e2undo.c - Replay an undo log onto an ext2/3/4 filesystem
4 * Copyright IBM Corporation, 2007
5 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
8 * This file may be redistributed under the terms of the GNU Public
22 #include "ext2fs/tdb.h"
23 #include "ext2fs/ext2fs.h"
24 #include "nls-enable.h"
26 static unsigned char mtime_key[] = "filesystem MTIME";
27 static unsigned char uuid_key[] = "filesystem UUID";
28 static unsigned char blksize_key[] = "filesystem BLKSIZE";
30 static char *prg_name;
32 static void usage(void)
35 _("Usage: %s <transaction file> <filesystem>\n"), prg_name);
39 static int check_filesystem(TDB_CONTEXT *tdb, io_channel channel)
44 TDB_DATA tdb_key, tdb_data;
45 struct ext2_super_block super;
47 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
48 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
50 com_err(prg_name, retval,
51 "%s", _("Failed to read the file system data \n"));
55 tdb_key.dptr = mtime_key;
56 tdb_key.dsize = sizeof(mtime_key);
57 tdb_data = tdb_fetch(tdb, tdb_key);
59 retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
60 com_err(prg_name, retval,
61 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
65 s_mtime = *(__u32 *)tdb_data.dptr;
66 if (super.s_mtime != s_mtime) {
69 _("The file system Mount time didn't match %u\n"),
76 tdb_key.dptr = uuid_key;
77 tdb_key.dsize = sizeof(uuid_key);
78 tdb_data = tdb_fetch(tdb, tdb_key);
80 retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
81 com_err(prg_name, retval,
82 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
85 memcpy(s_uuid, tdb_data.dptr, sizeof(s_uuid));
86 if (memcmp(s_uuid, super.s_uuid, sizeof(s_uuid))) {
87 com_err(prg_name, 0, "%s",
88 _("The file system UUID didn't match \n"));
95 static int set_blk_size(TDB_CONTEXT *tdb, io_channel channel)
99 TDB_DATA tdb_key, tdb_data;
101 tdb_key.dptr = blksize_key;
102 tdb_key.dsize = sizeof(blksize_key);
103 tdb_data = tdb_fetch(tdb, tdb_key);
104 if (!tdb_data.dptr) {
105 retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
106 com_err(prg_name, retval,
107 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
111 block_size = *(int *)tdb_data.dptr;
113 printf("Block size %d\n", block_size);
115 io_channel_set_blksize(channel, block_size);
120 int main(int argc, char *argv[])
129 char *device_name, *tdb_file;
130 io_manager manager = unix_io_manager;
133 setlocale(LC_MESSAGES, "");
134 setlocale(LC_CTYPE, "");
135 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
136 textdomain(NLS_CAT_NAME);
137 set_com_err_gettext(gettext);
139 add_error_table(&et_ext2_error_table);
142 while((c = getopt(argc, argv, "f")) != EOF) {
152 if (argc != optind + 2)
155 tdb_file = argv[optind];
156 device_name = argv[optind+1];
158 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0600);
161 com_err(prg_name, errno,
162 _("Failed tdb_open %s\n"), tdb_file);
166 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
168 com_err(prg_name, retval, _("Error while determining whether "
169 "%s is mounted.\n"), device_name);
173 if (mount_flags & EXT2_MF_MOUNTED) {
174 com_err(prg_name, retval, "%s", _("e2undo should only be run "
175 "on unmounted file system\n"));
179 retval = manager->open(device_name,
180 IO_FLAG_EXCLUSIVE | IO_FLAG_RW, &channel);
182 com_err(prg_name, retval,
183 _("Failed to open %s\n"), device_name);
187 if (!force && check_filesystem(tdb, channel)) {
191 if (set_blk_size(tdb, channel)) {
195 for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) {
196 if (!strcmp((char *) key.dptr, (char *) mtime_key) ||
197 !strcmp((char *) key.dptr, (char *) uuid_key) ||
198 !strcmp((char *) key.dptr, (char *) blksize_key)) {
202 data = tdb_fetch(tdb, key);
205 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
208 blk_num = *(blk64_t *)key.dptr;
209 printf(_("Replayed transaction of size %zd at location %llu\n"),
210 data.dsize, blk_num);
211 retval = io_channel_write_blk64(channel, blk_num,
212 -data.dsize, data.dptr);
214 com_err(prg_name, retval,
215 _("Failed write %s\n"),
220 io_channel_close(channel);