xref: /aosp_15_r20/external/e2fsprogs/misc/e2undo.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * e2undo.c - Replay an undo log onto an ext2/3/4 filesystem
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright IBM Corporation, 2007
5*6a54128fSAndroid Build Coastguard Worker  * Author Aneesh Kumar K.V <[email protected]>
6*6a54128fSAndroid Build Coastguard Worker  *
7*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
8*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Public
9*6a54128fSAndroid Build Coastguard Worker  * License.
10*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
11*6a54128fSAndroid Build Coastguard Worker  */
12*6a54128fSAndroid Build Coastguard Worker 
13*6a54128fSAndroid Build Coastguard Worker #include "config.h"
14*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
15*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
16*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETOPT_H
17*6a54128fSAndroid Build Coastguard Worker #include <getopt.h>
18*6a54128fSAndroid Build Coastguard Worker #endif
19*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
20*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
21*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
22*6a54128fSAndroid Build Coastguard Worker #endif
23*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
24*6a54128fSAndroid Build Coastguard Worker #include <libgen.h>
25*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2fs.h"
26*6a54128fSAndroid Build Coastguard Worker #include "support/nls-enable.h"
27*6a54128fSAndroid Build Coastguard Worker 
28*6a54128fSAndroid Build Coastguard Worker #undef DEBUG
29*6a54128fSAndroid Build Coastguard Worker 
30*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
31*6a54128fSAndroid Build Coastguard Worker # define dbg_printf(f, a...)  do {printf(f, ## a); fflush(stdout); } while (0)
32*6a54128fSAndroid Build Coastguard Worker #else
33*6a54128fSAndroid Build Coastguard Worker # define dbg_printf(f, a...)
34*6a54128fSAndroid Build Coastguard Worker #endif
35*6a54128fSAndroid Build Coastguard Worker 
36*6a54128fSAndroid Build Coastguard Worker /*
37*6a54128fSAndroid Build Coastguard Worker  * Undo file format: The file is cut up into undo_header.block_size blocks.
38*6a54128fSAndroid Build Coastguard Worker  * The first block contains the header.
39*6a54128fSAndroid Build Coastguard Worker  * The second block contains the superblock.
40*6a54128fSAndroid Build Coastguard Worker  * There is then a repeating series of blocks as follows:
41*6a54128fSAndroid Build Coastguard Worker  *   A key block, which contains undo_keys to map the following data blocks.
42*6a54128fSAndroid Build Coastguard Worker  *   Data blocks
43*6a54128fSAndroid Build Coastguard Worker  * (Note that there are pointers to the first key block and the sb, so this
44*6a54128fSAndroid Build Coastguard Worker  * order isn't strictly necessary.)
45*6a54128fSAndroid Build Coastguard Worker  */
46*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MAGIC "E2UNDO02"
47*6a54128fSAndroid Build Coastguard Worker #define KEYBLOCK_MAGIC 0xCADECADE
48*6a54128fSAndroid Build Coastguard Worker 
49*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_STATE_FINISHED	0x1	/* undo file is complete */
50*6a54128fSAndroid Build Coastguard Worker 
51*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MIN_BLOCK_SIZE	1024	/* undo blocks are no less than 1KB */
52*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MAX_BLOCK_SIZE	1048576	/* undo blocks are no more than 1MB */
53*6a54128fSAndroid Build Coastguard Worker 
54*6a54128fSAndroid Build Coastguard Worker struct undo_header {
55*6a54128fSAndroid Build Coastguard Worker 	char magic[8];		/* "E2UNDO02" */
56*6a54128fSAndroid Build Coastguard Worker 	__le64 num_keys;	/* how many keys? */
57*6a54128fSAndroid Build Coastguard Worker 	__le64 super_offset;	/* where in the file is the superblock copy? */
58*6a54128fSAndroid Build Coastguard Worker 	__le64 key_offset;	/* where do the key/data block chunks start? */
59*6a54128fSAndroid Build Coastguard Worker 	__le32 block_size;	/* block size of the undo file */
60*6a54128fSAndroid Build Coastguard Worker 	__le32 fs_block_size;	/* block size of the target device */
61*6a54128fSAndroid Build Coastguard Worker 	__le32 sb_crc;		/* crc32c of the superblock */
62*6a54128fSAndroid Build Coastguard Worker 	__le32 state;		/* e2undo state flags */
63*6a54128fSAndroid Build Coastguard Worker 	__le32 f_compat;	/* compatible features (none so far) */
64*6a54128fSAndroid Build Coastguard Worker 	__le32 f_incompat;	/* incompatible features (none so far) */
65*6a54128fSAndroid Build Coastguard Worker 	__le32 f_rocompat;	/* ro compatible features (none so far) */
66*6a54128fSAndroid Build Coastguard Worker 	__le32 pad32;		/* padding for fs_offset */
67*6a54128fSAndroid Build Coastguard Worker 	__le64 fs_offset;	/* filesystem offset */
68*6a54128fSAndroid Build Coastguard Worker 	__u8 padding[436];	/* padding */
69*6a54128fSAndroid Build Coastguard Worker 	__le32 header_crc;	/* crc32c of the header (but not this field) */
70*6a54128fSAndroid Build Coastguard Worker };
71*6a54128fSAndroid Build Coastguard Worker 
72*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_MAX_EXTENT_BLOCKS	512	/* max extent size, in blocks */
73*6a54128fSAndroid Build Coastguard Worker 
74*6a54128fSAndroid Build Coastguard Worker struct undo_key {
75*6a54128fSAndroid Build Coastguard Worker 	__le64 fsblk;		/* where in the fs does the block go */
76*6a54128fSAndroid Build Coastguard Worker 	__le32 blk_crc;		/* crc32c of the block */
77*6a54128fSAndroid Build Coastguard Worker 	__le32 size;		/* how many bytes in this block? */
78*6a54128fSAndroid Build Coastguard Worker };
79*6a54128fSAndroid Build Coastguard Worker 
80*6a54128fSAndroid Build Coastguard Worker struct undo_key_block {
81*6a54128fSAndroid Build Coastguard Worker 	__le32 magic;		/* KEYBLOCK_MAGIC number */
82*6a54128fSAndroid Build Coastguard Worker 	__le32 crc;		/* block checksum */
83*6a54128fSAndroid Build Coastguard Worker 	__le64 reserved;	/* zero */
84*6a54128fSAndroid Build Coastguard Worker #if __GNUC_PREREQ (4, 8)
85*6a54128fSAndroid Build Coastguard Worker #pragma GCC diagnostic push
86*6a54128fSAndroid Build Coastguard Worker #pragma GCC diagnostic ignored "-Wpedantic"
87*6a54128fSAndroid Build Coastguard Worker #endif
88*6a54128fSAndroid Build Coastguard Worker 	struct undo_key keys[0];	/* keys, which come immediately after */
89*6a54128fSAndroid Build Coastguard Worker #if __GNUC_PREREQ (4, 8)
90*6a54128fSAndroid Build Coastguard Worker #pragma GCC diagnostic pop
91*6a54128fSAndroid Build Coastguard Worker #endif
92*6a54128fSAndroid Build Coastguard Worker };
93*6a54128fSAndroid Build Coastguard Worker 
94*6a54128fSAndroid Build Coastguard Worker struct undo_key_info {
95*6a54128fSAndroid Build Coastguard Worker 	blk64_t fsblk;
96*6a54128fSAndroid Build Coastguard Worker 	blk64_t fileblk;
97*6a54128fSAndroid Build Coastguard Worker 	__u32 blk_crc;
98*6a54128fSAndroid Build Coastguard Worker 	unsigned int size;
99*6a54128fSAndroid Build Coastguard Worker };
100*6a54128fSAndroid Build Coastguard Worker 
101*6a54128fSAndroid Build Coastguard Worker struct undo_context {
102*6a54128fSAndroid Build Coastguard Worker 	struct undo_header hdr;
103*6a54128fSAndroid Build Coastguard Worker 	io_channel undo_file;
104*6a54128fSAndroid Build Coastguard Worker 	unsigned int blocksize, fs_blocksize;
105*6a54128fSAndroid Build Coastguard Worker 	blk64_t super_block;
106*6a54128fSAndroid Build Coastguard Worker 	size_t num_keys;
107*6a54128fSAndroid Build Coastguard Worker 	struct undo_key_info *keys;
108*6a54128fSAndroid Build Coastguard Worker };
109*6a54128fSAndroid Build Coastguard Worker #define KEYS_PER_BLOCK(d) (((d)->blocksize / sizeof(struct undo_key)) - 1)
110*6a54128fSAndroid Build Coastguard Worker 
111*6a54128fSAndroid Build Coastguard Worker #define E2UNDO_FEATURE_COMPAT_FS_OFFSET 0x1	/* the filesystem offset */
112*6a54128fSAndroid Build Coastguard Worker 
e2undo_has_feature_fs_offset(struct undo_header * header)113*6a54128fSAndroid Build Coastguard Worker static inline int e2undo_has_feature_fs_offset(struct undo_header *header) {
114*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_le32_to_cpu(header->f_compat) &
115*6a54128fSAndroid Build Coastguard Worker 		E2UNDO_FEATURE_COMPAT_FS_OFFSET;
116*6a54128fSAndroid Build Coastguard Worker }
117*6a54128fSAndroid Build Coastguard Worker 
118*6a54128fSAndroid Build Coastguard Worker static char *prg_name;
119*6a54128fSAndroid Build Coastguard Worker static char *undo_file;
120*6a54128fSAndroid Build Coastguard Worker 
usage(void)121*6a54128fSAndroid Build Coastguard Worker static void usage(void)
122*6a54128fSAndroid Build Coastguard Worker {
123*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr,
124*6a54128fSAndroid Build Coastguard Worker 		_("Usage: %s [-f] [-h] [-n] [-o offset] [-v] [-z undo_file] <transaction file> <filesystem>\n"), prg_name);
125*6a54128fSAndroid Build Coastguard Worker 	exit(1);
126*6a54128fSAndroid Build Coastguard Worker }
127*6a54128fSAndroid Build Coastguard Worker 
dump_header(struct undo_header * hdr)128*6a54128fSAndroid Build Coastguard Worker static void dump_header(struct undo_header *hdr)
129*6a54128fSAndroid Build Coastguard Worker {
130*6a54128fSAndroid Build Coastguard Worker 	printf("nr keys:\t%llu\n",
131*6a54128fSAndroid Build Coastguard Worker 	       (unsigned long long) ext2fs_le64_to_cpu(hdr->num_keys));
132*6a54128fSAndroid Build Coastguard Worker 	printf("super block:\t%llu\n",
133*6a54128fSAndroid Build Coastguard Worker 	       (unsigned long long) ext2fs_le64_to_cpu(hdr->super_offset));
134*6a54128fSAndroid Build Coastguard Worker 	printf("key block:\t%llu\n",
135*6a54128fSAndroid Build Coastguard Worker 	       (unsigned long long) ext2fs_le64_to_cpu(hdr->key_offset));
136*6a54128fSAndroid Build Coastguard Worker 	printf("block size:\t%u\n", ext2fs_le32_to_cpu(hdr->block_size));
137*6a54128fSAndroid Build Coastguard Worker 	printf("fs block size:\t%u\n", ext2fs_le32_to_cpu(hdr->fs_block_size));
138*6a54128fSAndroid Build Coastguard Worker 	printf("super crc:\t0x%x\n", ext2fs_le32_to_cpu(hdr->sb_crc));
139*6a54128fSAndroid Build Coastguard Worker 	printf("state:\t\t0x%x\n", ext2fs_le32_to_cpu(hdr->state));
140*6a54128fSAndroid Build Coastguard Worker 	printf("compat:\t\t0x%x\n", ext2fs_le32_to_cpu(hdr->f_compat));
141*6a54128fSAndroid Build Coastguard Worker 	printf("incompat:\t0x%x\n", ext2fs_le32_to_cpu(hdr->f_incompat));
142*6a54128fSAndroid Build Coastguard Worker 	printf("rocompat:\t0x%x\n", ext2fs_le32_to_cpu(hdr->f_rocompat));
143*6a54128fSAndroid Build Coastguard Worker 	if (e2undo_has_feature_fs_offset(hdr))
144*6a54128fSAndroid Build Coastguard Worker 		printf("fs offset:\t%llu\n",
145*6a54128fSAndroid Build Coastguard Worker 		       (unsigned long long) ext2fs_le64_to_cpu(hdr->fs_offset));
146*6a54128fSAndroid Build Coastguard Worker 	printf("header crc:\t0x%x\n", ext2fs_le32_to_cpu(hdr->header_crc));
147*6a54128fSAndroid Build Coastguard Worker }
148*6a54128fSAndroid Build Coastguard Worker 
print_undo_mismatch(struct ext2_super_block * fs_super,struct ext2_super_block * undo_super)149*6a54128fSAndroid Build Coastguard Worker static void print_undo_mismatch(struct ext2_super_block *fs_super,
150*6a54128fSAndroid Build Coastguard Worker 				struct ext2_super_block *undo_super)
151*6a54128fSAndroid Build Coastguard Worker {
152*6a54128fSAndroid Build Coastguard Worker 	printf("%s",
153*6a54128fSAndroid Build Coastguard Worker 	       _("The file system superblock doesn't match the undo file.\n"));
154*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(fs_super->s_uuid, undo_super->s_uuid,
155*6a54128fSAndroid Build Coastguard Worker 		   sizeof(fs_super->s_uuid)))
156*6a54128fSAndroid Build Coastguard Worker 		printf("%s", _("UUID does not match.\n"));
157*6a54128fSAndroid Build Coastguard Worker 	if (fs_super->s_mtime != undo_super->s_mtime)
158*6a54128fSAndroid Build Coastguard Worker 		printf("%s", _("Last mount time does not match.\n"));
159*6a54128fSAndroid Build Coastguard Worker 	if (fs_super->s_wtime != undo_super->s_wtime)
160*6a54128fSAndroid Build Coastguard Worker 		printf("%s", _("Last write time does not match.\n"));
161*6a54128fSAndroid Build Coastguard Worker 	if (fs_super->s_kbytes_written != undo_super->s_kbytes_written)
162*6a54128fSAndroid Build Coastguard Worker 		printf("%s", _("Lifetime write counter does not match.\n"));
163*6a54128fSAndroid Build Coastguard Worker }
164*6a54128fSAndroid Build Coastguard Worker 
check_filesystem(struct undo_context * ctx,io_channel channel)165*6a54128fSAndroid Build Coastguard Worker static int check_filesystem(struct undo_context *ctx, io_channel channel)
166*6a54128fSAndroid Build Coastguard Worker {
167*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block super, *sb;
168*6a54128fSAndroid Build Coastguard Worker 	char *buf;
169*6a54128fSAndroid Build Coastguard Worker 	__u32 sb_crc;
170*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
171*6a54128fSAndroid Build Coastguard Worker 
172*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
173*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
174*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
175*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval,
176*6a54128fSAndroid Build Coastguard Worker 			"%s", _("while reading filesystem superblock."));
177*6a54128fSAndroid Build Coastguard Worker 		return retval;
178*6a54128fSAndroid Build Coastguard Worker 	}
179*6a54128fSAndroid Build Coastguard Worker 
180*6a54128fSAndroid Build Coastguard Worker 	/*
181*6a54128fSAndroid Build Coastguard Worker 	 * Compare the FS and the undo file superblock so that we can't apply
182*6a54128fSAndroid Build Coastguard Worker 	 * e2undo "patches" out of order.
183*6a54128fSAndroid Build Coastguard Worker 	 */
184*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(ctx->blocksize, &buf);
185*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
186*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, "%s", _("while allocating memory"));
187*6a54128fSAndroid Build Coastguard Worker 		return retval;
188*6a54128fSAndroid Build Coastguard Worker 	}
189*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_read_blk64(ctx->undo_file, ctx->super_block,
190*6a54128fSAndroid Build Coastguard Worker 				       -SUPERBLOCK_SIZE, buf);
191*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
192*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, "%s", _("while fetching superblock"));
193*6a54128fSAndroid Build Coastguard Worker 		goto out;
194*6a54128fSAndroid Build Coastguard Worker 	}
195*6a54128fSAndroid Build Coastguard Worker 	sb = (struct ext2_super_block *)buf;
196*6a54128fSAndroid Build Coastguard Worker 	sb->s_magic = ~sb->s_magic;
197*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(&super, buf, sizeof(super))) {
198*6a54128fSAndroid Build Coastguard Worker 		print_undo_mismatch(&super, (struct ext2_super_block *)buf);
199*6a54128fSAndroid Build Coastguard Worker 		retval = -1;
200*6a54128fSAndroid Build Coastguard Worker 		goto out;
201*6a54128fSAndroid Build Coastguard Worker 	}
202*6a54128fSAndroid Build Coastguard Worker 	sb_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf, SUPERBLOCK_SIZE);
203*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_le32_to_cpu(ctx->hdr.sb_crc) != sb_crc) {
204*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr,
205*6a54128fSAndroid Build Coastguard Worker 			_("Undo file superblock checksum doesn't match.\n"));
206*6a54128fSAndroid Build Coastguard Worker 		retval = -1;
207*6a54128fSAndroid Build Coastguard Worker 		goto out;
208*6a54128fSAndroid Build Coastguard Worker 	}
209*6a54128fSAndroid Build Coastguard Worker 
210*6a54128fSAndroid Build Coastguard Worker out:
211*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&buf);
212*6a54128fSAndroid Build Coastguard Worker 	return retval;
213*6a54128fSAndroid Build Coastguard Worker }
214*6a54128fSAndroid Build Coastguard Worker 
key_compare(const void * a,const void * b)215*6a54128fSAndroid Build Coastguard Worker static int key_compare(const void *a, const void *b)
216*6a54128fSAndroid Build Coastguard Worker {
217*6a54128fSAndroid Build Coastguard Worker 	const struct undo_key_info *ka, *kb;
218*6a54128fSAndroid Build Coastguard Worker 
219*6a54128fSAndroid Build Coastguard Worker 	ka = a;
220*6a54128fSAndroid Build Coastguard Worker 	kb = b;
221*6a54128fSAndroid Build Coastguard Worker 	return ka->fsblk - kb->fsblk;
222*6a54128fSAndroid Build Coastguard Worker }
223*6a54128fSAndroid Build Coastguard Worker 
e2undo_setup_tdb(const char * name,io_manager * io_ptr)224*6a54128fSAndroid Build Coastguard Worker static int e2undo_setup_tdb(const char *name, io_manager *io_ptr)
225*6a54128fSAndroid Build Coastguard Worker {
226*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = 0;
227*6a54128fSAndroid Build Coastguard Worker 	const char *tdb_dir;
228*6a54128fSAndroid Build Coastguard Worker 	char *tdb_file = NULL;
229*6a54128fSAndroid Build Coastguard Worker 	char *dev_name, *tmp_name;
230*6a54128fSAndroid Build Coastguard Worker 
231*6a54128fSAndroid Build Coastguard Worker 	/* (re)open a specific undo file */
232*6a54128fSAndroid Build Coastguard Worker 	if (undo_file && undo_file[0] != 0) {
233*6a54128fSAndroid Build Coastguard Worker 		retval = set_undo_io_backing_manager(*io_ptr);
234*6a54128fSAndroid Build Coastguard Worker 		if (retval)
235*6a54128fSAndroid Build Coastguard Worker 			goto err;
236*6a54128fSAndroid Build Coastguard Worker 		*io_ptr = undo_io_manager;
237*6a54128fSAndroid Build Coastguard Worker 		retval = set_undo_io_backup_file(undo_file);
238*6a54128fSAndroid Build Coastguard Worker 		if (retval)
239*6a54128fSAndroid Build Coastguard Worker 			goto err;
240*6a54128fSAndroid Build Coastguard Worker 		printf(_("Overwriting existing filesystem; this can be undone "
241*6a54128fSAndroid Build Coastguard Worker 			 "using the command:\n"
242*6a54128fSAndroid Build Coastguard Worker 			 "    e2undo %s %s\n\n"),
243*6a54128fSAndroid Build Coastguard Worker 			 undo_file, name);
244*6a54128fSAndroid Build Coastguard Worker 		return retval;
245*6a54128fSAndroid Build Coastguard Worker 	}
246*6a54128fSAndroid Build Coastguard Worker 
247*6a54128fSAndroid Build Coastguard Worker 	/*
248*6a54128fSAndroid Build Coastguard Worker 	 * Configuration via a conf file would be
249*6a54128fSAndroid Build Coastguard Worker 	 * nice
250*6a54128fSAndroid Build Coastguard Worker 	 */
251*6a54128fSAndroid Build Coastguard Worker 	tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
252*6a54128fSAndroid Build Coastguard Worker 	if (!tdb_dir)
253*6a54128fSAndroid Build Coastguard Worker 		tdb_dir = "/var/lib/e2fsprogs";
254*6a54128fSAndroid Build Coastguard Worker 
255*6a54128fSAndroid Build Coastguard Worker 	if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
256*6a54128fSAndroid Build Coastguard Worker 	    access(tdb_dir, W_OK))
257*6a54128fSAndroid Build Coastguard Worker 		return 0;
258*6a54128fSAndroid Build Coastguard Worker 
259*6a54128fSAndroid Build Coastguard Worker 	tmp_name = strdup(name);
260*6a54128fSAndroid Build Coastguard Worker 	if (!tmp_name)
261*6a54128fSAndroid Build Coastguard Worker 		goto errout;
262*6a54128fSAndroid Build Coastguard Worker 	dev_name = basename(tmp_name);
263*6a54128fSAndroid Build Coastguard Worker 	tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
264*6a54128fSAndroid Build Coastguard Worker 	if (!tdb_file) {
265*6a54128fSAndroid Build Coastguard Worker 		free(tmp_name);
266*6a54128fSAndroid Build Coastguard Worker 		goto errout;
267*6a54128fSAndroid Build Coastguard Worker 	}
268*6a54128fSAndroid Build Coastguard Worker 	sprintf(tdb_file, "%s/e2undo-%s.e2undo", tdb_dir, dev_name);
269*6a54128fSAndroid Build Coastguard Worker 	free(tmp_name);
270*6a54128fSAndroid Build Coastguard Worker 
271*6a54128fSAndroid Build Coastguard Worker 	if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
272*6a54128fSAndroid Build Coastguard Worker 		retval = errno;
273*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval,
274*6a54128fSAndroid Build Coastguard Worker 			_("while trying to delete %s"), tdb_file);
275*6a54128fSAndroid Build Coastguard Worker 		goto errout;
276*6a54128fSAndroid Build Coastguard Worker 	}
277*6a54128fSAndroid Build Coastguard Worker 
278*6a54128fSAndroid Build Coastguard Worker 	retval = set_undo_io_backing_manager(*io_ptr);
279*6a54128fSAndroid Build Coastguard Worker 	if (retval)
280*6a54128fSAndroid Build Coastguard Worker 		goto errout;
281*6a54128fSAndroid Build Coastguard Worker 	*io_ptr = undo_io_manager;
282*6a54128fSAndroid Build Coastguard Worker 	retval = set_undo_io_backup_file(tdb_file);
283*6a54128fSAndroid Build Coastguard Worker 	if (retval)
284*6a54128fSAndroid Build Coastguard Worker 		goto errout;
285*6a54128fSAndroid Build Coastguard Worker 	printf(_("Overwriting existing filesystem; this can be undone "
286*6a54128fSAndroid Build Coastguard Worker 		 "using the command:\n"
287*6a54128fSAndroid Build Coastguard Worker 		 "    e2undo %s %s\n\n"),
288*6a54128fSAndroid Build Coastguard Worker 		 tdb_file, name);
289*6a54128fSAndroid Build Coastguard Worker 
290*6a54128fSAndroid Build Coastguard Worker 	free(tdb_file);
291*6a54128fSAndroid Build Coastguard Worker 	return 0;
292*6a54128fSAndroid Build Coastguard Worker errout:
293*6a54128fSAndroid Build Coastguard Worker 	free(tdb_file);
294*6a54128fSAndroid Build Coastguard Worker err:
295*6a54128fSAndroid Build Coastguard Worker 	com_err(prg_name, retval, "while trying to setup undo file\n");
296*6a54128fSAndroid Build Coastguard Worker 	return retval;
297*6a54128fSAndroid Build Coastguard Worker }
298*6a54128fSAndroid Build Coastguard Worker 
main(int argc,char * argv[])299*6a54128fSAndroid Build Coastguard Worker int main(int argc, char *argv[])
300*6a54128fSAndroid Build Coastguard Worker {
301*6a54128fSAndroid Build Coastguard Worker 	int c, force = 0, dry_run = 0, verbose = 0, dump = 0;
302*6a54128fSAndroid Build Coastguard Worker 	io_channel channel;
303*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
304*6a54128fSAndroid Build Coastguard Worker 	int mount_flags, csum_error = 0, io_error = 0;
305*6a54128fSAndroid Build Coastguard Worker 	size_t i, keys_per_block;
306*6a54128fSAndroid Build Coastguard Worker 	char *device_name, *tdb_file;
307*6a54128fSAndroid Build Coastguard Worker 	io_manager manager = unix_io_manager;
308*6a54128fSAndroid Build Coastguard Worker 	struct undo_context undo_ctx;
309*6a54128fSAndroid Build Coastguard Worker 	char *buf;
310*6a54128fSAndroid Build Coastguard Worker 	struct undo_key_block *keyb;
311*6a54128fSAndroid Build Coastguard Worker 	struct undo_key *dkey;
312*6a54128fSAndroid Build Coastguard Worker 	struct undo_key_info *ikey;
313*6a54128fSAndroid Build Coastguard Worker 	__u32 key_crc, blk_crc, hdr_crc;
314*6a54128fSAndroid Build Coastguard Worker 	blk64_t lblk;
315*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs;
316*6a54128fSAndroid Build Coastguard Worker 	__u64 offset = 0;
317*6a54128fSAndroid Build Coastguard Worker 	char opt_offset_string[40] = { 0 };
318*6a54128fSAndroid Build Coastguard Worker 
319*6a54128fSAndroid Build Coastguard Worker #ifdef ENABLE_NLS
320*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_MESSAGES, "");
321*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_CTYPE, "");
322*6a54128fSAndroid Build Coastguard Worker 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
323*6a54128fSAndroid Build Coastguard Worker 	textdomain(NLS_CAT_NAME);
324*6a54128fSAndroid Build Coastguard Worker 	set_com_err_gettext(gettext);
325*6a54128fSAndroid Build Coastguard Worker #endif
326*6a54128fSAndroid Build Coastguard Worker 	add_error_table(&et_ext2_error_table);
327*6a54128fSAndroid Build Coastguard Worker 
328*6a54128fSAndroid Build Coastguard Worker 	prg_name = argv[0];
329*6a54128fSAndroid Build Coastguard Worker 	while ((c = getopt(argc, argv, "fhno:vz:")) != EOF) {
330*6a54128fSAndroid Build Coastguard Worker 		switch (c) {
331*6a54128fSAndroid Build Coastguard Worker 		case 'f':
332*6a54128fSAndroid Build Coastguard Worker 			force = 1;
333*6a54128fSAndroid Build Coastguard Worker 			break;
334*6a54128fSAndroid Build Coastguard Worker 		case 'h':
335*6a54128fSAndroid Build Coastguard Worker 			dump = 1;
336*6a54128fSAndroid Build Coastguard Worker 			break;
337*6a54128fSAndroid Build Coastguard Worker 		case 'n':
338*6a54128fSAndroid Build Coastguard Worker 			dry_run = 1;
339*6a54128fSAndroid Build Coastguard Worker 			break;
340*6a54128fSAndroid Build Coastguard Worker 		case 'o':
341*6a54128fSAndroid Build Coastguard Worker 			offset = strtoull(optarg, &buf, 0);
342*6a54128fSAndroid Build Coastguard Worker 			if (*buf) {
343*6a54128fSAndroid Build Coastguard Worker 				com_err(prg_name, 0,
344*6a54128fSAndroid Build Coastguard Worker 						_("illegal offset - %s"), optarg);
345*6a54128fSAndroid Build Coastguard Worker 				exit(1);
346*6a54128fSAndroid Build Coastguard Worker 			}
347*6a54128fSAndroid Build Coastguard Worker 			/* used to indicate that an offset was specified */
348*6a54128fSAndroid Build Coastguard Worker 			opt_offset_string[0] = 1;
349*6a54128fSAndroid Build Coastguard Worker 			break;
350*6a54128fSAndroid Build Coastguard Worker 		case 'v':
351*6a54128fSAndroid Build Coastguard Worker 			verbose = 1;
352*6a54128fSAndroid Build Coastguard Worker 			break;
353*6a54128fSAndroid Build Coastguard Worker 		case 'z':
354*6a54128fSAndroid Build Coastguard Worker 			undo_file = optarg;
355*6a54128fSAndroid Build Coastguard Worker 			break;
356*6a54128fSAndroid Build Coastguard Worker 		default:
357*6a54128fSAndroid Build Coastguard Worker 			usage();
358*6a54128fSAndroid Build Coastguard Worker 		}
359*6a54128fSAndroid Build Coastguard Worker 	}
360*6a54128fSAndroid Build Coastguard Worker 
361*6a54128fSAndroid Build Coastguard Worker 	if (argc != optind + 2)
362*6a54128fSAndroid Build Coastguard Worker 		usage();
363*6a54128fSAndroid Build Coastguard Worker 
364*6a54128fSAndroid Build Coastguard Worker 	tdb_file = argv[optind];
365*6a54128fSAndroid Build Coastguard Worker 	device_name = argv[optind+1];
366*6a54128fSAndroid Build Coastguard Worker 
367*6a54128fSAndroid Build Coastguard Worker 	if (undo_file && strcmp(tdb_file, undo_file) == 0) {
368*6a54128fSAndroid Build Coastguard Worker 		printf(_("Will not write to an undo file while replaying it.\n"));
369*6a54128fSAndroid Build Coastguard Worker 		exit(1);
370*6a54128fSAndroid Build Coastguard Worker 	}
371*6a54128fSAndroid Build Coastguard Worker 
372*6a54128fSAndroid Build Coastguard Worker 	/* Interpret the undo file */
373*6a54128fSAndroid Build Coastguard Worker 	retval = manager->open(tdb_file, IO_FLAG_EXCLUSIVE,
374*6a54128fSAndroid Build Coastguard Worker 			       &undo_ctx.undo_file);
375*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
376*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, errno,
377*6a54128fSAndroid Build Coastguard Worker 				_("while opening undo file `%s'\n"), tdb_file);
378*6a54128fSAndroid Build Coastguard Worker 		exit(1);
379*6a54128fSAndroid Build Coastguard Worker 	}
380*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_read_blk64(undo_ctx.undo_file, 0,
381*6a54128fSAndroid Build Coastguard Worker 				       -(int)sizeof(undo_ctx.hdr),
382*6a54128fSAndroid Build Coastguard Worker 				       &undo_ctx.hdr);
383*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
384*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, _("while reading undo file"));
385*6a54128fSAndroid Build Coastguard Worker 		exit(1);
386*6a54128fSAndroid Build Coastguard Worker 	}
387*6a54128fSAndroid Build Coastguard Worker 	if (memcmp(undo_ctx.hdr.magic, E2UNDO_MAGIC,
388*6a54128fSAndroid Build Coastguard Worker 		    sizeof(undo_ctx.hdr.magic))) {
389*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("%s: Not an undo file.\n"), tdb_file);
390*6a54128fSAndroid Build Coastguard Worker 		exit(1);
391*6a54128fSAndroid Build Coastguard Worker 	}
392*6a54128fSAndroid Build Coastguard Worker 	if (dump) {
393*6a54128fSAndroid Build Coastguard Worker 		dump_header(&undo_ctx.hdr);
394*6a54128fSAndroid Build Coastguard Worker 		exit(1);
395*6a54128fSAndroid Build Coastguard Worker 	}
396*6a54128fSAndroid Build Coastguard Worker 	hdr_crc = ext2fs_crc32c_le(~0, (unsigned char *)&undo_ctx.hdr,
397*6a54128fSAndroid Build Coastguard Worker 				   sizeof(struct undo_header) -
398*6a54128fSAndroid Build Coastguard Worker 				   sizeof(__u32));
399*6a54128fSAndroid Build Coastguard Worker 	if (!force && ext2fs_le32_to_cpu(undo_ctx.hdr.header_crc) != hdr_crc) {
400*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("%s: Header checksum doesn't match.\n"),
401*6a54128fSAndroid Build Coastguard Worker 			tdb_file);
402*6a54128fSAndroid Build Coastguard Worker 		exit(1);
403*6a54128fSAndroid Build Coastguard Worker 	}
404*6a54128fSAndroid Build Coastguard Worker 	undo_ctx.blocksize = ext2fs_le32_to_cpu(undo_ctx.hdr.block_size);
405*6a54128fSAndroid Build Coastguard Worker 	undo_ctx.fs_blocksize = ext2fs_le32_to_cpu(undo_ctx.hdr.fs_block_size);
406*6a54128fSAndroid Build Coastguard Worker 	if (undo_ctx.blocksize == 0 || undo_ctx.fs_blocksize == 0) {
407*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("%s: Corrupt undo file header.\n"), tdb_file);
408*6a54128fSAndroid Build Coastguard Worker 		exit(1);
409*6a54128fSAndroid Build Coastguard Worker 	}
410*6a54128fSAndroid Build Coastguard Worker 	if (!force && undo_ctx.blocksize > E2UNDO_MAX_BLOCK_SIZE) {
411*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("%s: Undo block size too large.\n"),
412*6a54128fSAndroid Build Coastguard Worker 			tdb_file);
413*6a54128fSAndroid Build Coastguard Worker 		exit(1);
414*6a54128fSAndroid Build Coastguard Worker 	}
415*6a54128fSAndroid Build Coastguard Worker 	if (!force && undo_ctx.blocksize < E2UNDO_MIN_BLOCK_SIZE) {
416*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("%s: Undo block size too small.\n"),
417*6a54128fSAndroid Build Coastguard Worker 			tdb_file);
418*6a54128fSAndroid Build Coastguard Worker 		exit(1);
419*6a54128fSAndroid Build Coastguard Worker 	}
420*6a54128fSAndroid Build Coastguard Worker 	undo_ctx.super_block = ext2fs_le64_to_cpu(undo_ctx.hdr.super_offset);
421*6a54128fSAndroid Build Coastguard Worker 	undo_ctx.num_keys = ext2fs_le64_to_cpu(undo_ctx.hdr.num_keys);
422*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(undo_ctx.undo_file, undo_ctx.blocksize);
423*6a54128fSAndroid Build Coastguard Worker 	/*
424*6a54128fSAndroid Build Coastguard Worker 	 * Do not compare undo_ctx.hdr.f_compat with the available compatible
425*6a54128fSAndroid Build Coastguard Worker 	 * features set, because a "missing" compatible feature should
426*6a54128fSAndroid Build Coastguard Worker 	 * not cause any problems.
427*6a54128fSAndroid Build Coastguard Worker 	 */
428*6a54128fSAndroid Build Coastguard Worker 	if (!force && (undo_ctx.hdr.f_incompat || undo_ctx.hdr.f_rocompat)) {
429*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("%s: Unknown undo file feature set.\n"),
430*6a54128fSAndroid Build Coastguard Worker 			tdb_file);
431*6a54128fSAndroid Build Coastguard Worker 		exit(1);
432*6a54128fSAndroid Build Coastguard Worker 	}
433*6a54128fSAndroid Build Coastguard Worker 
434*6a54128fSAndroid Build Coastguard Worker 	/* open the fs */
435*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_check_if_mounted(device_name, &mount_flags);
436*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
437*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, _("Error while determining whether "
438*6a54128fSAndroid Build Coastguard Worker 				"%s is mounted."), device_name);
439*6a54128fSAndroid Build Coastguard Worker 		exit(1);
440*6a54128fSAndroid Build Coastguard Worker 	}
441*6a54128fSAndroid Build Coastguard Worker 
442*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_MOUNTED) {
443*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, "%s", _("e2undo should only be run "
444*6a54128fSAndroid Build Coastguard Worker 						"on unmounted filesystems"));
445*6a54128fSAndroid Build Coastguard Worker 		exit(1);
446*6a54128fSAndroid Build Coastguard Worker 	}
447*6a54128fSAndroid Build Coastguard Worker 
448*6a54128fSAndroid Build Coastguard Worker 	if (undo_file) {
449*6a54128fSAndroid Build Coastguard Worker 		retval = e2undo_setup_tdb(device_name, &manager);
450*6a54128fSAndroid Build Coastguard Worker 		if (retval)
451*6a54128fSAndroid Build Coastguard Worker 			exit(1);
452*6a54128fSAndroid Build Coastguard Worker 	}
453*6a54128fSAndroid Build Coastguard Worker 
454*6a54128fSAndroid Build Coastguard Worker 	retval = manager->open(device_name,
455*6a54128fSAndroid Build Coastguard Worker 			       IO_FLAG_EXCLUSIVE | (dry_run ? 0 : IO_FLAG_RW),
456*6a54128fSAndroid Build Coastguard Worker 			       &channel);
457*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
458*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval,
459*6a54128fSAndroid Build Coastguard Worker 				_("while opening `%s'"), device_name);
460*6a54128fSAndroid Build Coastguard Worker 		exit(1);
461*6a54128fSAndroid Build Coastguard Worker 	}
462*6a54128fSAndroid Build Coastguard Worker 
463*6a54128fSAndroid Build Coastguard Worker 	if (*opt_offset_string || e2undo_has_feature_fs_offset(&undo_ctx.hdr)) {
464*6a54128fSAndroid Build Coastguard Worker 		if (!*opt_offset_string)
465*6a54128fSAndroid Build Coastguard Worker 			offset = ext2fs_le64_to_cpu(undo_ctx.hdr.fs_offset);
466*6a54128fSAndroid Build Coastguard Worker 		retval = snprintf(opt_offset_string, sizeof(opt_offset_string),
467*6a54128fSAndroid Build Coastguard Worker 				  "offset=%llu", (unsigned long long) offset);
468*6a54128fSAndroid Build Coastguard Worker 		if ((size_t) retval >= sizeof(opt_offset_string)) {
469*6a54128fSAndroid Build Coastguard Worker 			/* should not happen... */
470*6a54128fSAndroid Build Coastguard Worker 			com_err(prg_name, 0, _("specified offset is too large"));
471*6a54128fSAndroid Build Coastguard Worker 			exit(1);
472*6a54128fSAndroid Build Coastguard Worker 		}
473*6a54128fSAndroid Build Coastguard Worker 		io_channel_set_options(channel, opt_offset_string);
474*6a54128fSAndroid Build Coastguard Worker 	}
475*6a54128fSAndroid Build Coastguard Worker 
476*6a54128fSAndroid Build Coastguard Worker 	if (!force && check_filesystem(&undo_ctx, channel))
477*6a54128fSAndroid Build Coastguard Worker 		exit(1);
478*6a54128fSAndroid Build Coastguard Worker 
479*6a54128fSAndroid Build Coastguard Worker 	/* prepare to read keys */
480*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(sizeof(struct undo_key_info) * undo_ctx.num_keys,
481*6a54128fSAndroid Build Coastguard Worker 				&undo_ctx.keys);
482*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
483*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, "%s", _("while allocating memory"));
484*6a54128fSAndroid Build Coastguard Worker 		exit(1);
485*6a54128fSAndroid Build Coastguard Worker 	}
486*6a54128fSAndroid Build Coastguard Worker 	ikey = undo_ctx.keys;
487*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(undo_ctx.blocksize, &keyb);
488*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
489*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, "%s", _("while allocating memory"));
490*6a54128fSAndroid Build Coastguard Worker 		exit(1);
491*6a54128fSAndroid Build Coastguard Worker 	}
492*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(E2UNDO_MAX_EXTENT_BLOCKS * undo_ctx.blocksize,
493*6a54128fSAndroid Build Coastguard Worker 				&buf);
494*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
495*6a54128fSAndroid Build Coastguard Worker 		com_err(prg_name, retval, "%s", _("while allocating memory"));
496*6a54128fSAndroid Build Coastguard Worker 		exit(1);
497*6a54128fSAndroid Build Coastguard Worker 	}
498*6a54128fSAndroid Build Coastguard Worker 
499*6a54128fSAndroid Build Coastguard Worker 	/* load keys */
500*6a54128fSAndroid Build Coastguard Worker 	keys_per_block = KEYS_PER_BLOCK(&undo_ctx);
501*6a54128fSAndroid Build Coastguard Worker 	lblk = ext2fs_le64_to_cpu(undo_ctx.hdr.key_offset);
502*6a54128fSAndroid Build Coastguard Worker 	dbg_printf("nr_keys=%lu, kpb=%zu, blksz=%u\n",
503*6a54128fSAndroid Build Coastguard Worker 		   undo_ctx.num_keys, keys_per_block, undo_ctx.blocksize);
504*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < undo_ctx.num_keys; i += keys_per_block) {
505*6a54128fSAndroid Build Coastguard Worker 		size_t j, max_j;
506*6a54128fSAndroid Build Coastguard Worker 		__le32 crc;
507*6a54128fSAndroid Build Coastguard Worker 
508*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(undo_ctx.undo_file,
509*6a54128fSAndroid Build Coastguard Worker 					       lblk, 1, keyb);
510*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
511*6a54128fSAndroid Build Coastguard Worker 			com_err(prg_name, retval, "%s", _("while reading keys"));
512*6a54128fSAndroid Build Coastguard Worker 			if (force) {
513*6a54128fSAndroid Build Coastguard Worker 				io_error = 1;
514*6a54128fSAndroid Build Coastguard Worker 				undo_ctx.num_keys = i - 1;
515*6a54128fSAndroid Build Coastguard Worker 				break;
516*6a54128fSAndroid Build Coastguard Worker 			}
517*6a54128fSAndroid Build Coastguard Worker 			exit(1);
518*6a54128fSAndroid Build Coastguard Worker 		}
519*6a54128fSAndroid Build Coastguard Worker 
520*6a54128fSAndroid Build Coastguard Worker 		/* check keys */
521*6a54128fSAndroid Build Coastguard Worker 		if (!force &&
522*6a54128fSAndroid Build Coastguard Worker 		    ext2fs_le32_to_cpu(keyb->magic) != KEYBLOCK_MAGIC) {
523*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("%s: wrong key magic at %llu\n"),
524*6a54128fSAndroid Build Coastguard Worker 				tdb_file, (unsigned long long) lblk);
525*6a54128fSAndroid Build Coastguard Worker 			exit(1);
526*6a54128fSAndroid Build Coastguard Worker 		}
527*6a54128fSAndroid Build Coastguard Worker 		crc = keyb->crc;
528*6a54128fSAndroid Build Coastguard Worker 		keyb->crc = 0;
529*6a54128fSAndroid Build Coastguard Worker 		key_crc = ext2fs_crc32c_le(~0, (unsigned char *)keyb,
530*6a54128fSAndroid Build Coastguard Worker 					   undo_ctx.blocksize);
531*6a54128fSAndroid Build Coastguard Worker 		if (!force && ext2fs_le32_to_cpu(crc) != key_crc) {
532*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr,
533*6a54128fSAndroid Build Coastguard Worker 				_("%s: key block checksum error at %llu.\n"),
534*6a54128fSAndroid Build Coastguard Worker 				tdb_file, (unsigned long long) lblk);
535*6a54128fSAndroid Build Coastguard Worker 			exit(1);
536*6a54128fSAndroid Build Coastguard Worker 		}
537*6a54128fSAndroid Build Coastguard Worker 
538*6a54128fSAndroid Build Coastguard Worker 		/* load keys from key block */
539*6a54128fSAndroid Build Coastguard Worker 		lblk++;
540*6a54128fSAndroid Build Coastguard Worker 		max_j = undo_ctx.num_keys - i;
541*6a54128fSAndroid Build Coastguard Worker 		if (max_j > keys_per_block)
542*6a54128fSAndroid Build Coastguard Worker 			max_j = keys_per_block;
543*6a54128fSAndroid Build Coastguard Worker 		for (j = 0, dkey = keyb->keys;
544*6a54128fSAndroid Build Coastguard Worker 		     j < max_j;
545*6a54128fSAndroid Build Coastguard Worker 		     j++, ikey++, dkey++) {
546*6a54128fSAndroid Build Coastguard Worker 			ikey->fsblk = ext2fs_le64_to_cpu(dkey->fsblk);
547*6a54128fSAndroid Build Coastguard Worker 			ikey->fileblk = lblk;
548*6a54128fSAndroid Build Coastguard Worker 			ikey->blk_crc = ext2fs_le32_to_cpu(dkey->blk_crc);
549*6a54128fSAndroid Build Coastguard Worker 			ikey->size = ext2fs_le32_to_cpu(dkey->size);
550*6a54128fSAndroid Build Coastguard Worker 			lblk += (ikey->size + undo_ctx.blocksize - 1) /
551*6a54128fSAndroid Build Coastguard Worker 				undo_ctx.blocksize;
552*6a54128fSAndroid Build Coastguard Worker 
553*6a54128fSAndroid Build Coastguard Worker 			if (E2UNDO_MAX_EXTENT_BLOCKS * undo_ctx.blocksize <
554*6a54128fSAndroid Build Coastguard Worker 			    ikey->size) {
555*6a54128fSAndroid Build Coastguard Worker 				com_err(prg_name, retval,
556*6a54128fSAndroid Build Coastguard Worker 					_("%s: block %llu is too long."),
557*6a54128fSAndroid Build Coastguard Worker 					tdb_file,
558*6a54128fSAndroid Build Coastguard Worker 					(unsigned long long) ikey->fsblk);
559*6a54128fSAndroid Build Coastguard Worker 				exit(1);
560*6a54128fSAndroid Build Coastguard Worker 			}
561*6a54128fSAndroid Build Coastguard Worker 
562*6a54128fSAndroid Build Coastguard Worker 			/* check each block's crc */
563*6a54128fSAndroid Build Coastguard Worker 			retval = io_channel_read_blk64(undo_ctx.undo_file,
564*6a54128fSAndroid Build Coastguard Worker 						       ikey->fileblk,
565*6a54128fSAndroid Build Coastguard Worker 						       -(int)ikey->size,
566*6a54128fSAndroid Build Coastguard Worker 						       buf);
567*6a54128fSAndroid Build Coastguard Worker 			if (retval) {
568*6a54128fSAndroid Build Coastguard Worker 				com_err(prg_name, retval,
569*6a54128fSAndroid Build Coastguard Worker 					_("while fetching block %llu."),
570*6a54128fSAndroid Build Coastguard Worker 					(unsigned long long) ikey->fileblk);
571*6a54128fSAndroid Build Coastguard Worker 				if (!force)
572*6a54128fSAndroid Build Coastguard Worker 					exit(1);
573*6a54128fSAndroid Build Coastguard Worker 				io_error = 1;
574*6a54128fSAndroid Build Coastguard Worker 				continue;
575*6a54128fSAndroid Build Coastguard Worker 			}
576*6a54128fSAndroid Build Coastguard Worker 
577*6a54128fSAndroid Build Coastguard Worker 			blk_crc = ext2fs_crc32c_le(~0, (unsigned char *)buf,
578*6a54128fSAndroid Build Coastguard Worker 						   ikey->size);
579*6a54128fSAndroid Build Coastguard Worker 			if (blk_crc != ikey->blk_crc) {
580*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
581*6a54128fSAndroid Build Coastguard Worker 					_("checksum error in filesystem block "
582*6a54128fSAndroid Build Coastguard Worker 					  "%llu (undo blk %llu)\n"),
583*6a54128fSAndroid Build Coastguard Worker 					(unsigned long long) ikey->fsblk,
584*6a54128fSAndroid Build Coastguard Worker 					(unsigned long long) ikey->fileblk);
585*6a54128fSAndroid Build Coastguard Worker 				if (!force)
586*6a54128fSAndroid Build Coastguard Worker 					exit(1);
587*6a54128fSAndroid Build Coastguard Worker 				csum_error = 1;
588*6a54128fSAndroid Build Coastguard Worker 			}
589*6a54128fSAndroid Build Coastguard Worker 		}
590*6a54128fSAndroid Build Coastguard Worker 	}
591*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&keyb);
592*6a54128fSAndroid Build Coastguard Worker 
593*6a54128fSAndroid Build Coastguard Worker 	/* sort keys in fs block order */
594*6a54128fSAndroid Build Coastguard Worker 	qsort(undo_ctx.keys, undo_ctx.num_keys, sizeof(struct undo_key_info),
595*6a54128fSAndroid Build Coastguard Worker 	      key_compare);
596*6a54128fSAndroid Build Coastguard Worker 
597*6a54128fSAndroid Build Coastguard Worker 	/* replay */
598*6a54128fSAndroid Build Coastguard Worker 	io_channel_set_blksize(channel, undo_ctx.fs_blocksize);
599*6a54128fSAndroid Build Coastguard Worker 	for (i = 0, ikey = undo_ctx.keys; i < undo_ctx.num_keys; i++, ikey++) {
600*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(undo_ctx.undo_file,
601*6a54128fSAndroid Build Coastguard Worker 					       ikey->fileblk,
602*6a54128fSAndroid Build Coastguard Worker 					       -(int)ikey->size,
603*6a54128fSAndroid Build Coastguard Worker 					       buf);
604*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
605*6a54128fSAndroid Build Coastguard Worker 			com_err(prg_name, retval,
606*6a54128fSAndroid Build Coastguard Worker 				_("while fetching block %llu."),
607*6a54128fSAndroid Build Coastguard Worker 				(unsigned long long) ikey->fileblk);
608*6a54128fSAndroid Build Coastguard Worker 			io_error = 1;
609*6a54128fSAndroid Build Coastguard Worker 			continue;
610*6a54128fSAndroid Build Coastguard Worker 		}
611*6a54128fSAndroid Build Coastguard Worker 
612*6a54128fSAndroid Build Coastguard Worker 		if (verbose)
613*6a54128fSAndroid Build Coastguard Worker 			printf("Replayed block of size %u from %llu to %llu\n",
614*6a54128fSAndroid Build Coastguard Worker 			       ikey->size, (unsigned long long) ikey->fileblk,
615*6a54128fSAndroid Build Coastguard Worker 			       (unsigned long long) ikey->fsblk);
616*6a54128fSAndroid Build Coastguard Worker 		if (dry_run)
617*6a54128fSAndroid Build Coastguard Worker 			continue;
618*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(channel, ikey->fsblk,
619*6a54128fSAndroid Build Coastguard Worker 						-(int)ikey->size, buf);
620*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
621*6a54128fSAndroid Build Coastguard Worker 			com_err(prg_name, retval,
622*6a54128fSAndroid Build Coastguard Worker 				_("while writing block %llu."),
623*6a54128fSAndroid Build Coastguard Worker 				(unsigned long long) ikey->fsblk);
624*6a54128fSAndroid Build Coastguard Worker 			io_error = 1;
625*6a54128fSAndroid Build Coastguard Worker 		}
626*6a54128fSAndroid Build Coastguard Worker 	}
627*6a54128fSAndroid Build Coastguard Worker 
628*6a54128fSAndroid Build Coastguard Worker 	if (csum_error)
629*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("Undo file corruption; run e2fsck NOW!\n"));
630*6a54128fSAndroid Build Coastguard Worker 	if (io_error)
631*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("IO error during replay; run e2fsck NOW!\n"));
632*6a54128fSAndroid Build Coastguard Worker 	if (!(ext2fs_le32_to_cpu(undo_ctx.hdr.state) & E2UNDO_STATE_FINISHED)) {
633*6a54128fSAndroid Build Coastguard Worker 		force = 1;
634*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("Incomplete undo record; run e2fsck.\n"));
635*6a54128fSAndroid Build Coastguard Worker 	}
636*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&buf);
637*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&undo_ctx.keys);
638*6a54128fSAndroid Build Coastguard Worker 	io_channel_close(channel);
639*6a54128fSAndroid Build Coastguard Worker 
640*6a54128fSAndroid Build Coastguard Worker 	/* If there were problems, try to force a fsck */
641*6a54128fSAndroid Build Coastguard Worker 	if (!dry_run && (force || csum_error || io_error)) {
642*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_open2(device_name, NULL,
643*6a54128fSAndroid Build Coastguard Worker 				   EXT2_FLAG_RW | EXT2_FLAG_64BITS, 0, 0,
644*6a54128fSAndroid Build Coastguard Worker 				   manager, &fs);
645*6a54128fSAndroid Build Coastguard Worker 		if (retval)
646*6a54128fSAndroid Build Coastguard Worker 			goto out;
647*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_state &= ~EXT2_VALID_FS;
648*6a54128fSAndroid Build Coastguard Worker 		if (csum_error || io_error)
649*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_state |= EXT2_ERROR_FS;
650*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
651*6a54128fSAndroid Build Coastguard Worker 		ext2fs_close_free(&fs);
652*6a54128fSAndroid Build Coastguard Worker 	}
653*6a54128fSAndroid Build Coastguard Worker 
654*6a54128fSAndroid Build Coastguard Worker out:
655*6a54128fSAndroid Build Coastguard Worker 	io_channel_close(undo_ctx.undo_file);
656*6a54128fSAndroid Build Coastguard Worker 
657*6a54128fSAndroid Build Coastguard Worker 	return csum_error;
658*6a54128fSAndroid Build Coastguard Worker }
659