Whamcloud - gitweb
e2fsck: exit from preenhalt if IO errors were encountered
[tools/e2fsprogs.git] / e2fsck / util.c
index 46ceaa9..efaea4d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * util.c --- miscellaneous utilities
- * 
+ *
  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
  *
  * %Begin-Header%
 #include <unistd.h>
 #include <string.h>
 #include <ctype.h>
+
+#ifdef HAVE_CONIO_H
+#undef HAVE_TERMIOS_H
+#include <conio.h>
+#define read_a_char()  getch()
+#else
+#ifdef HAVE_TERMIOS_H
 #include <termios.h>
+#endif
+#include <stdio.h>
+#endif
+
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
 
 #include "e2fsck.h"
 
+extern e2fsck_t e2fsck_global_ctx;   /* Try your very best not to use this! */
+
 #include <sys/time.h>
 #include <sys/resource.h>
 
-void fatal_error (const char *msg)
+void fatal_error(e2fsck_t ctx, const char *msg)
 {
-       if (msg) 
+       if (msg)
                fprintf (stderr, "e2fsck: %s\n", msg);
+       if (ctx->fs && ctx->fs->io) {
+               if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
+                       io_channel_flush(ctx->fs->io);
+               else
+                       fprintf(stderr, "e2fsck: io manager magic bad!\n");
+       }
+       ctx->flags |= E2F_FLAG_ABORT;
+       if (ctx->flags & E2F_FLAG_SETJMP_OK)
+               longjmp(ctx->abort_loc, 1);
        exit(FSCK_ERROR);
 }
 
-void *allocate_memory(int size, const char *description)
+void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
+                            const char *description)
 {
        void *ret;
        char buf[256];
@@ -38,17 +68,75 @@ void *allocate_memory(int size, const char *description)
        ret = malloc(size);
        if (!ret) {
                sprintf(buf, "Can't allocate %s\n", description);
-               fatal_error(buf);
+               fatal_error(ctx, buf);
        }
        memset(ret, 0, size);
        return ret;
 }
 
+char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
+                 const char *str, int len)
+{
+       char    *ret;
+
+       if (!str)
+               return NULL;
+       if (!len)
+               len = strlen(str);
+       ret = malloc(len+1);
+       if (ret) {
+               strncpy(ret, str, len);
+               ret[len] = 0;
+       }
+       return ret;
+}
+
+#ifndef HAVE_STRNLEN
+/*
+ * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
+ * provide our own.
+ */
+int e2fsck_strnlen(const char * s, int count)
+{
+       const char *cp = s;
+
+       while (count-- && *cp)
+               cp++;
+       return cp - s;
+}
+#endif
+
+#ifndef HAVE_CONIO_H
+static int read_a_char(void)
+{
+       char    c;
+       int     r;
+       int     fail = 0;
+
+       while(1) {
+               if (e2fsck_global_ctx &&
+                   (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
+                       return 3;
+               }
+               r = read(0, &c, 1);
+               if (r == 1)
+                       return c;
+               if (fail++ > 100)
+                       break;
+       }
+       return EOF;
+}
+#endif
+
 int ask_yn(const char * string, int def)
 {
        int             c;
-       struct termios  termios, tmp;
        const char      *defstr;
+       const char      *short_yes = _("yY");
+       const char      *short_no = _("nN");
+
+#ifdef HAVE_TERMIOS_H
+       struct termios  termios, tmp;
 
        tcgetattr (0, &termios);
        tmp = termios;
@@ -56,24 +144,36 @@ int ask_yn(const char * string, int def)
        tmp.c_cc[VMIN] = 1;
        tmp.c_cc[VTIME] = 0;
        tcsetattr (0, TCSANOW, &tmp);
+#endif
 
        if (def == 1)
-               defstr = "<y>";
+               defstr = _(_("<y>"));
        else if (def == 0)
-               defstr = "<n>";
+               defstr = _(_("<n>"));
        else
-               defstr = " (y/n)";
+               defstr = _(" (y/n)");
        printf("%s%s? ", string, defstr);
        while (1) {
                fflush (stdout);
-               if ((c = getchar()) == EOF)
+               if ((c = read_a_char()) == EOF)
                        break;
-               c = toupper(c);
-               if (c == 'Y') {
+               if (c == 3) {
+#ifdef HAVE_TERMIOS_H
+                       tcsetattr (0, TCSANOW, &termios);
+#endif
+                       if (e2fsck_global_ctx &&
+                           e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
+                               puts("\n");
+                               longjmp(e2fsck_global_ctx->abort_loc, 1);
+                       }
+                       puts(_("cancelled!\n"));
+                       return 0;
+               }
+               if (strchr(short_yes, (char) c)) {
                        def = 1;
                        break;
                }
-               else if (c == 'N') {
+               else if (strchr(short_no, (char) c)) {
                        def = 0;
                        break;
                }
@@ -81,80 +181,70 @@ int ask_yn(const char * string, int def)
                        break;
        }
        if (def)
-               printf ("yes\n\n");
+               puts(_("yes\n"));
        else
-               printf ("no\n\n");
+               puts (_("no\n"));
+#ifdef HAVE_TERMIOS_H
        tcsetattr (0, TCSANOW, &termios);
+#endif
        return def;
 }
 
 int ask (e2fsck_t ctx, const char * string, int def)
 {
        if (ctx->options & E2F_OPT_NO) {
-               printf ("%s? no\n\n", string);
+               printf (_("%s? no\n\n"), string);
                return 0;
        }
        if (ctx->options & E2F_OPT_YES) {
-               printf ("%s? yes\n\n", string);
+               printf (_("%s? yes\n\n"), string);
                return 1;
        }
        if (ctx->options & E2F_OPT_PREEN) {
-               printf ("%s? %s\n\n", string, def ? "yes" : "no");
+               printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
                return def;
        }
        return ask_yn(string, def);
 }
 
-void read_bitmaps(e2fsck_t ctx)
+void e2fsck_read_bitmaps(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
        errcode_t       retval;
+       const char      *old_op;
 
        if (ctx->invalid_bitmaps) {
                com_err(ctx->program_name, 0,
-                       "read_bitmaps: illegal bitmap block(s) for %s",
+                   _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
                        ctx->device_name);
-               fatal_error(0);
+               fatal_error(ctx, 0);
        }
 
-       ehandler_operation("reading inode and block bitmaps");
+       old_op = ehandler_operation(_("reading inode and block bitmaps"));
        retval = ext2fs_read_bitmaps(fs);
-       ehandler_operation(0);
+       ehandler_operation(old_op);
        if (retval) {
                com_err(ctx->program_name, retval,
-                       "while retrying to read bitmaps for %s",
+                       _("while retrying to read bitmaps for %s"),
                        ctx->device_name);
-               fatal_error(0);
+               fatal_error(ctx, 0);
        }
 }
 
-void write_bitmaps(e2fsck_t ctx)
+void e2fsck_write_bitmaps(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
        errcode_t       retval;
+       const char      *old_op;
 
-       if (ext2fs_test_bb_dirty(fs)) {
-               ehandler_operation("writing block bitmaps");
-               retval = ext2fs_write_block_bitmap(fs);
-               ehandler_operation(0);
-               if (retval) {
-                       com_err(ctx->program_name, retval,
-                               "while retrying to write block bitmaps for %s",
-                               ctx->device_name);
-                       fatal_error(0);
-               }
-       }
-
-       if (ext2fs_test_ib_dirty(fs)) {
-               ehandler_operation("writing inode bitmaps");
-               retval = ext2fs_write_inode_bitmap(fs);
-               ehandler_operation(0);
-               if (retval) {
-                       com_err(ctx->program_name, retval,
-                               "while retrying to write inode bitmaps for %s",
-                               ctx->device_name);
-                       fatal_error(0);
-               }
+       old_op = ehandler_operation(_("writing block and inode bitmaps"));
+       retval = ext2fs_write_bitmaps(fs);
+       ehandler_operation(old_op);
+       if (retval) {
+               com_err(ctx->program_name, retval,
+                       _("while rewriting block and inode bitmaps for %s"),
+                       ctx->device_name);
+               fatal_error(ctx, 0);
        }
 }
 
@@ -164,9 +254,10 @@ void preenhalt(e2fsck_t ctx)
 
        if (!(ctx->options & E2F_OPT_PREEN))
                return;
-       fprintf(stderr, "\n\n%s: UNEXPECTED INCONSISTENCY; "
-               "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n",
+       fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
+               "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
               ctx->device_name);
+       ctx->flags |= E2F_FLAG_EXITING;
        if (fs != NULL) {
                fs->super->s_state |= EXT2_ERROR_FS;
                ext2fs_mark_super_dirty(fs);
@@ -176,17 +267,18 @@ void preenhalt(e2fsck_t ctx)
 }
 
 #ifdef RESOURCE_TRACK
-void init_resource_track(struct resource_track *track)
+void init_resource_track(struct resource_track *track, io_channel channel)
 {
 #ifdef HAVE_GETRUSAGE
        struct rusage r;
 #endif
-       
+       io_stats io_start = 0;
+
        track->brk_start = sbrk(0);
        gettimeofday(&track->time_start, 0);
 #ifdef HAVE_GETRUSAGE
-#ifdef solaris
-       memcpy(&r, 0, sizeof(struct rusage));
+#ifdef sun
+       memset(&r, 0, sizeof(struct rusage));
 #endif
        getrusage(RUSAGE_SELF, &r);
        track->user_start = r.ru_utime;
@@ -195,6 +287,14 @@ void init_resource_track(struct resource_track *track)
        track->user_start.tv_sec = track->user_start.tv_usec = 0;
        track->system_start.tv_sec = track->system_start.tv_usec = 0;
 #endif
+       track->bytes_read = 0;
+       track->bytes_written = 0;
+       if (channel && channel->manager && channel->manager->get_stats)
+               channel->manager->get_stats(channel, &io_start);
+       if (io_start) {
+               track->bytes_read = io_start->bytes_read;
+               track->bytes_written = io_start->bytes_written;
+       }
 }
 
 #ifdef __GNUC__
@@ -210,31 +310,64 @@ static _INLINE_ float timeval_subtract(struct timeval *tv1,
                ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
 }
 
-void print_resource_track(const char *desc, struct resource_track *track)
+void print_resource_track(const char *desc, struct resource_track *track,
+                         io_channel channel)
 {
 #ifdef HAVE_GETRUSAGE
        struct rusage r;
 #endif
+#ifdef HAVE_MALLINFO
+       struct mallinfo malloc_info;
+#endif
        struct timeval time_end;
 
        gettimeofday(&time_end, 0);
 
        if (desc)
-               printf("%s :", desc);
-       
+               printf("%s: ", desc);
+
+#ifdef HAVE_MALLINFO
+#define kbytes(x)      (((x) + 1023) / 1024)
+
+       malloc_info = mallinfo();
+       printf(_("Memory used: %dk/%dk (%dk/%dk), "),
+              kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
+              kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
+#else
+       printf(_("Memory used: %d, "),
+              (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
+#endif
 #ifdef HAVE_GETRUSAGE
        getrusage(RUSAGE_SELF, &r);
 
-       printf("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n",
-              (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
+       printf(_("time: %5.2f/%5.2f/%5.2f\n"),
               timeval_subtract(&time_end, &track->time_start),
               timeval_subtract(&r.ru_utime, &track->user_start),
               timeval_subtract(&r.ru_stime, &track->system_start));
 #else
-       printf("Memory used: %d, elapsed time: %6.3f\n",
-              (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
+       printf(_("elapsed time: %6.3f\n"),
               timeval_subtract(&time_end, &track->time_start));
 #endif
+#define mbytes(x)      (((x) + 1048575) / 1048576)
+       if (channel && channel->manager && channel->manager->get_stats) {
+               io_stats delta = 0;
+               unsigned long long bytes_read = 0;
+               unsigned long long bytes_written = 0;
+
+               if (desc)
+                       printf("%s: ", desc);
+
+               channel->manager->get_stats(channel, &delta);
+               if (delta) {
+                       bytes_read = delta->bytes_read - track->bytes_read;
+                       bytes_written = delta->bytes_written -
+                               track->bytes_written;
+               }
+               printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
+                      mbytes(bytes_read), mbytes(bytes_written),
+                      (double)mbytes(bytes_read + bytes_written) /
+                      timeval_subtract(&time_end, &track->time_start));
+       }
 }
 #endif /* RESOURCE_TRACK */
 
@@ -246,8 +379,36 @@ void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
        retval = ext2fs_read_inode(ctx->fs, ino, inode);
        if (retval) {
                com_err("ext2fs_read_inode", retval,
-                       "while reading inode %ld in %s", ino, proc);
-               fatal_error(0);
+                       _("while reading inode %ld in %s"), ino, proc);
+               fatal_error(ctx, 0);
+       }
+}
+
+void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
+                           struct ext2_inode *inode, int bufsize,
+                           const char *proc)
+{
+       int retval;
+
+       retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
+       if (retval) {
+               com_err("ext2fs_read_inode_full", retval,
+                       _("while reading inode %ld in %s"), ino, proc);
+               fatal_error(ctx, 0);
+       }
+}
+
+extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
+                              struct ext2_inode * inode, int bufsize,
+                              const char *proc)
+{
+       int retval;
+
+       retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
+       if (retval) {
+               com_err("ext2fs_write_inode", retval,
+                       _("while writing inode %ld in %s"), ino, proc);
+               fatal_error(ctx, 0);
        }
 }
 
@@ -259,8 +420,8 @@ extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
        retval = ext2fs_write_inode(ctx->fs, ino, inode);
        if (retval) {
                com_err("ext2fs_write_inode", retval,
-                       "while writing inode %ld in %s", ino, proc);
-               fatal_error(0);
+                       _("while writing inode %ld in %s"), ino, proc);
+               fatal_error(ctx, 0);
        }
 }
 
@@ -275,10 +436,161 @@ void mtrace_print(char *mesg)
 }
 #endif
 
-blk_t get_backup_sb(ext2_filsys fs)
+blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
+                  io_manager manager)
+{
+       struct ext2_super_block *sb;
+       io_channel              io = NULL;
+       void                    *buf = NULL;
+       int                     blocksize;
+       blk_t                   superblock, ret_sb = 8193;
+
+       if (fs && fs->super) {
+               ret_sb = (fs->super->s_blocks_per_group +
+                         fs->super->s_first_data_block);
+               if (ctx) {
+                       ctx->superblock = ret_sb;
+                       ctx->blocksize = fs->blocksize;
+               }
+               return ret_sb;
+       }
+
+       if (ctx) {
+               if (ctx->blocksize) {
+                       ret_sb = ctx->blocksize * 8;
+                       if (ctx->blocksize == 1024)
+                               ret_sb++;
+                       ctx->superblock = ret_sb;
+                       return ret_sb;
+               }
+               ctx->superblock = ret_sb;
+               ctx->blocksize = 1024;
+       }
+
+       if (!name || !manager)
+               goto cleanup;
+
+       if (manager->open(name, 0, &io) != 0)
+               goto cleanup;
+
+       if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
+               goto cleanup;
+       sb = (struct ext2_super_block *) buf;
+
+       for (blocksize = EXT2_MIN_BLOCK_SIZE;
+            blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
+               superblock = blocksize*8;
+               if (blocksize == 1024)
+                       superblock++;
+               io_channel_set_blksize(io, blocksize);
+               if (io_channel_read_blk(io, superblock,
+                                       -SUPERBLOCK_SIZE, buf))
+                       continue;
+#ifdef WORDS_BIGENDIAN
+               if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
+                       ext2fs_swap_super(sb);
+#endif
+               if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
+                   (EXT2_BLOCK_SIZE(sb) == blocksize)) {
+                       ret_sb = superblock;
+                       if (ctx) {
+                               ctx->superblock = superblock;
+                               ctx->blocksize = blocksize;
+                       }
+                       break;
+               }
+       }
+
+cleanup:
+       if (io)
+               io_channel_close(io);
+       if (buf)
+               ext2fs_free_mem(&buf);
+       return (ret_sb);
+}
+
+/*
+ * Given a mode, return the ext2 file type
+ */
+int ext2_file_type(unsigned int mode)
 {
-       if (!fs || !fs->super)
-               return 8193;
-       return fs->super->s_blocks_per_group + 1;
+       if (LINUX_S_ISREG(mode))
+               return EXT2_FT_REG_FILE;
+
+       if (LINUX_S_ISDIR(mode))
+               return EXT2_FT_DIR;
+
+       if (LINUX_S_ISCHR(mode))
+               return EXT2_FT_CHRDEV;
+
+       if (LINUX_S_ISBLK(mode))
+               return EXT2_FT_BLKDEV;
+
+       if (LINUX_S_ISLNK(mode))
+               return EXT2_FT_SYMLINK;
+
+       if (LINUX_S_ISFIFO(mode))
+               return EXT2_FT_FIFO;
+
+       if (LINUX_S_ISSOCK(mode))
+               return EXT2_FT_SOCK;
+
+       return 0;
 }
 
+#define STRIDE_LENGTH 8
+/*
+ * Helper function which zeros out _num_ blocks starting at _blk_.  In
+ * case of an error, the details of the error is returned via _ret_blk_
+ * and _ret_count_ if they are non-NULL pointers.  Returns 0 on
+ * success, and an error code on an error.
+ *
+ * As a special case, if the first argument is NULL, then it will
+ * attempt to free the static zeroizing buffer.  (This is to keep
+ * programs that check for memory leaks happy.)
+ */
+errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
+                            blk_t *ret_blk, int *ret_count)
+{
+       int             j, count, next_update, next_update_incr;
+       static char     *buf;
+       errcode_t       retval;
+
+       /* If fs is null, clean up the static buffer and return */
+       if (!fs) {
+               if (buf) {
+                       free(buf);
+                       buf = 0;
+               }
+               return 0;
+       }
+       /* Allocate the zeroizing buffer if necessary */
+       if (!buf) {
+               buf = malloc(fs->blocksize * STRIDE_LENGTH);
+               if (!buf) {
+                       com_err("malloc", ENOMEM,
+                               _("while allocating zeroizing buffer"));
+                       exit(1);
+               }
+               memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
+       }
+       /* OK, do the write loop */
+       next_update = 0;
+       next_update_incr = num / 100;
+       if (next_update_incr < 1)
+               next_update_incr = 1;
+       for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
+               count = num - j;
+               if (count > STRIDE_LENGTH)
+                       count = STRIDE_LENGTH;
+               retval = io_channel_write_blk(fs->io, blk, count, buf);
+               if (retval) {
+                       if (ret_count)
+                               *ret_count = count;
+                       if (ret_blk)
+                               *ret_blk = blk;
+                       return retval;
+               }
+       }
+       return 0;
+}