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 unsigned char mtime_key[] = "filesystem MTIME";
27 unsigned char uuid_key[] = "filesystem UUID";
28 unsigned char blksize_key[] = "filesystem BLKSIZE";
32 static void usage(char *prg_name)
35 _("Usage: %s <transaction file> <filesystem>\n"), prg_name);
40 static int check_filesystem(TDB_CONTEXT *tdb, io_channel channel)
45 TDB_DATA tdb_key, tdb_data;
46 struct ext2_super_block super;
48 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
49 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
52 retval, _("Failed to read the file system data \n"));
56 tdb_key.dptr = mtime_key;
57 tdb_key.dsize = sizeof(mtime_key);
58 tdb_data = tdb_fetch(tdb, tdb_key);
60 retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
61 com_err(prg_name, retval,
62 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
66 s_mtime = *(__u32 *)tdb_data.dptr;
67 if (super.s_mtime != s_mtime) {
70 _("The file system Mount time didn't match %u\n"),
77 tdb_key.dptr = uuid_key;
78 tdb_key.dsize = sizeof(uuid_key);
79 tdb_data = tdb_fetch(tdb, tdb_key);
81 retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
82 com_err(prg_name, retval,
83 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
86 memcpy(s_uuid, tdb_data.dptr, sizeof(s_uuid));
87 if (memcmp(s_uuid, super.s_uuid, sizeof(s_uuid))) {
89 _("The file system UUID didn't match \n"));
96 static int set_blk_size(TDB_CONTEXT *tdb, io_channel channel)
100 TDB_DATA tdb_key, tdb_data;
102 tdb_key.dptr = blksize_key;
103 tdb_key.dsize = sizeof(blksize_key);
104 tdb_data = tdb_fetch(tdb, tdb_key);
105 if (!tdb_data.dptr) {
106 retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
107 com_err(prg_name, retval,
108 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
112 block_size = *(int *)tdb_data.dptr;
114 printf("Block size %d\n", block_size);
116 io_channel_set_blksize(channel, block_size);
121 int main(int argc, char *argv[])
129 unsigned long blk_num;
130 char *device_name, *tdb_file;
131 io_manager manager = unix_io_manager;
134 setlocale(LC_MESSAGES, "");
135 setlocale(LC_CTYPE, "");
136 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
137 textdomain(NLS_CAT_NAME);
141 while((c = getopt(argc, argv, "f")) != EOF) {
151 if (argc != optind+2)
154 tdb_file = argv[optind];
155 device_name = argv[optind+1];
157 tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0600);
160 com_err(prg_name, errno,
161 _("Failed tdb_open %s\n"), tdb_file);
165 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
167 com_err(prg_name, retval, _("Error while determining whether "
168 "%s is mounted.\n"), device_name);
172 if (mount_flags & EXT2_MF_MOUNTED) {
173 com_err(prg_name, retval, _("e2undo should only be run on "
174 "unmounted file system\n"));
178 retval = manager->open(device_name,
179 IO_FLAG_EXCLUSIVE | IO_FLAG_RW, &channel);
181 com_err(prg_name, retval,
182 _("Failed to open %s\n"), device_name);
186 if (!force && check_filesystem(tdb, channel)) {
190 if (set_blk_size(tdb, channel)) {
194 for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) {
195 if (!strcmp((char *) key.dptr, (char *) mtime_key) ||
196 !strcmp((char *) key.dptr, (char *) uuid_key) ||
197 !strcmp((char *) key.dptr, (char *) blksize_key)) {
201 data = tdb_fetch(tdb, key);
204 _("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
207 blk_num = *(unsigned long *)key.dptr;
208 printf(_("Replayed transaction of size %zd at location %ld\n"),
209 data.dsize, blk_num);
210 retval = io_channel_write_blk(channel, blk_num,
211 -data.dsize, data.dptr);
213 com_err(prg_name, retval,
214 _("Failed write %s\n"),
219 io_channel_close(channel);