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