* %End-Header%
*/
+#include "config.h"
#include <stdlib.h>
+#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
+#ifdef __linux__
+#include <sys/utsname.h>
+#endif
#ifdef HAVE_CONIO_H
#undef HAVE_TERMIOS_H
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
-#include <stdio.h>
#endif
#ifdef HAVE_MALLOC_H
extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
+#include <stdarg.h>
+#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
void fatal_error(e2fsck_t ctx, const char *msg)
{
+ ext2_filsys fs = ctx->fs;
+ int exit_value = FSCK_ERROR;
+
if (msg)
fprintf (stderr, "e2fsck: %s\n", msg);
- if (ctx->fs && ctx->fs->io) {
+ if (!fs)
+ goto out;
+ if (fs->io) {
+ ext2fs_mmp_stop(ctx->fs);
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");
+ log_err(ctx, "e2fsck: io manager magic bad!\n");
}
+ if (ext2fs_test_changed(fs)) {
+ exit_value |= FSCK_NONDESTRUCT;
+ log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
+ ctx->device_name);
+ if (ctx->mount_flags & EXT2_MF_ISROOT)
+ exit_value |= FSCK_REBOOT;
+ }
+ if (!ext2fs_test_valid(fs)) {
+ log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
+ "errors **********\n\n"), ctx->device_name);
+ exit_value |= FSCK_UNCORRECTED;
+ exit_value &= ~FSCK_NONDESTRUCT;
+ }
+out:
ctx->flags |= E2F_FLAG_ABORT;
if (ctx->flags & E2F_FLAG_SETJMP_OK)
longjmp(ctx->abort_loc, 1);
- exit(FSCK_ERROR);
+ exit(exit_value);
+}
+
+void log_out(e2fsck_t ctx, const char *fmt, ...)
+{
+ va_list pvar;
+
+ va_start(pvar, fmt);
+ vprintf(fmt, pvar);
+ va_end(pvar);
+ if (ctx->logf) {
+ va_start(pvar, fmt);
+ vfprintf(ctx->logf, fmt, pvar);
+ va_end(pvar);
+ }
+}
+
+void log_err(e2fsck_t ctx, const char *fmt, ...)
+{
+ va_list pvar;
+
+ va_start(pvar, fmt);
+ vfprintf(stderr, fmt, pvar);
+ va_end(pvar);
+ if (ctx->logf) {
+ va_start(pvar, fmt);
+ vfprintf(ctx->logf, fmt, pvar);
+ va_end(pvar);
+ }
}
void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
char buf[256];
#ifdef DEBUG_ALLOCATE_MEMORY
- printf("Allocating %d bytes for %s...\n", size, description);
+ printf("Allocating %u bytes for %s...\n", size, description);
#endif
ret = malloc(size);
if (!ret) {
}
#endif
-int ask_yn(const char * string, int def)
+int ask_yn(e2fsck_t ctx, const char * string, int def)
{
int c;
const char *defstr;
defstr = _(_("<n>"));
else
defstr = _(" (y/n)");
- printf("%s%s? ", string, defstr);
+ log_out(ctx, "%s%s? ", string, defstr);
while (1) {
fflush (stdout);
if ((c = read_a_char()) == EOF)
#ifdef HAVE_TERMIOS_H
tcsetattr (0, TCSANOW, &termios);
#endif
- if (e2fsck_global_ctx &&
- e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
- puts("\n");
+ if (ctx->flags & E2F_FLAG_SETJMP_OK) {
+ log_out(ctx, "\n");
longjmp(e2fsck_global_ctx->abort_loc, 1);
}
- puts(_("cancelled!\n"));
+ log_out(ctx, _("cancelled!\n"));
return 0;
}
if (strchr(short_yes, (char) c)) {
def = 0;
break;
}
- else if ((c == ' ' || c == '\n') && (def != -1))
+ else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
break;
}
if (def)
- puts(_("yes\n"));
+ log_out(ctx, _("yes\n"));
else
- puts (_("no\n"));
+ log_out(ctx, _("no\n"));
#ifdef HAVE_TERMIOS_H
tcsetattr (0, TCSANOW, &termios);
#endif
int ask (e2fsck_t ctx, const char * string, int def)
{
if (ctx->options & E2F_OPT_NO) {
- printf (_("%s? no\n\n"), string);
+ log_out(ctx, _("%s? no\n\n"), string);
return 0;
}
if (ctx->options & E2F_OPT_YES) {
- printf (_("%s? yes\n\n"), string);
+ log_out(ctx, _("%s? yes\n\n"), string);
return 1;
}
if (ctx->options & E2F_OPT_PREEN) {
- printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
+ log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
return def;
}
- return ask_yn(string, def);
+ return ask_yn(ctx, string, def);
}
void e2fsck_read_bitmaps(e2fsck_t ctx)
ext2_filsys fs = ctx->fs;
errcode_t retval;
const char *old_op;
+ unsigned int save_type;
if (ctx->invalid_bitmaps) {
com_err(ctx->program_name, 0,
}
old_op = ehandler_operation(_("reading inode and block bitmaps"));
+ e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
+ &save_type);
retval = ext2fs_read_bitmaps(fs);
+ fs->default_bitmap_type = save_type;
ehandler_operation(old_op);
if (retval) {
com_err(ctx->program_name, retval,
if (!(ctx->options & E2F_OPT_PREEN))
return;
- fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
+ log_err(ctx, _("\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;
((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
}
-void print_resource_track(const char *desc, struct resource_track *track,
- io_channel channel)
+void print_resource_track(e2fsck_t ctx, const char *desc,
+ struct resource_track *track, io_channel channel)
{
#ifdef HAVE_GETRUSAGE
struct rusage r;
#endif
struct timeval time_end;
+ if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
+ (!desc && !(ctx->options & E2F_OPT_TIME)))
+ return;
+
+ e2fsck_clear_progbar(ctx);
gettimeofday(&time_end, 0);
if (desc)
- printf("%s: ", desc);
+ log_out(ctx, "%s: ", desc);
#ifdef HAVE_MALLINFO
-#define kbytes(x) (((x) + 1023) / 1024)
+#define kbytes(x) (((unsigned long)(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));
+ log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
+ 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)));
+ log_out(ctx, _("Memory used: %lu, "),
+ (unsigned long) (((char *) sbrk(0)) -
+ ((char *) track->brk_start)));
#endif
#ifdef HAVE_GETRUSAGE
getrusage(RUSAGE_SELF, &r);
- 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));
+ log_out(ctx, _("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(_("elapsed time: %6.3f\n"),
- timeval_subtract(&time_end, &track->time_start));
+ log_out(ctx, _("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) {
unsigned long long bytes_written = 0;
if (desc)
- printf("%s: ", desc);
+ log_out(ctx, "%s: ", desc);
channel->manager->get_stats(channel, &delta);
if (delta) {
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));
+ log_out(ctx, "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 */
retval = ext2fs_read_inode(ctx->fs, ino, inode);
if (retval) {
com_err("ext2fs_read_inode", retval,
- _("while reading inode %ld in %s"), ino, proc);
+ _("while reading inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
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);
+ _("while reading inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
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);
+ _("while writing inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
retval = ext2fs_write_inode(ctx->fs, ino, inode);
if (retval) {
com_err("ext2fs_write_inode", retval,
- _("while writing inode %ld in %s"), ino, proc);
+ _("while writing inode %lu in %s"), ino, proc);
fatal_error(ctx, 0);
}
}
if (blocksize == 1024)
superblock++;
io_channel_set_blksize(io, blocksize);
- if (io_channel_read_blk(io, superblock,
+ if (io_channel_read_blk64(io, superblock,
-SUPERBLOCK_SIZE, buf))
continue;
#ifdef WORDS_BIGENDIAN
count = num - j;
if (count > STRIDE_LENGTH)
count = STRIDE_LENGTH;
- retval = io_channel_write_blk(fs->io, blk, count, buf);
+ retval = io_channel_write_blk64(fs->io, blk, count, buf);
if (retval) {
if (ret_count)
*ret_count = count;
}
return 0;
}
+
+/*
+ * Check to see if a filesystem is in /proc/filesystems.
+ * Returns 1 if found, 0 if not
+ */
+int fs_proc_check(const char *fs_name)
+{
+ FILE *f;
+ char buf[80], *cp, *t;
+
+ f = fopen("/proc/filesystems", "r");
+ if (!f)
+ return (0);
+ while (!feof(f)) {
+ if (!fgets(buf, sizeof(buf), f))
+ break;
+ cp = buf;
+ if (!isspace(*cp)) {
+ while (*cp && !isspace(*cp))
+ cp++;
+ }
+ while (*cp && isspace(*cp))
+ cp++;
+ if ((t = strchr(cp, '\n')) != NULL)
+ *t = 0;
+ if ((t = strchr(cp, '\t')) != NULL)
+ *t = 0;
+ if ((t = strchr(cp, ' ')) != NULL)
+ *t = 0;
+ if (!strcmp(fs_name, cp)) {
+ fclose(f);
+ return (1);
+ }
+ }
+ fclose(f);
+ return (0);
+}
+
+/*
+ * Check to see if a filesystem is available as a module
+ * Returns 1 if found, 0 if not
+ */
+int check_for_modules(const char *fs_name)
+{
+#ifdef __linux__
+ struct utsname uts;
+ FILE *f;
+ char buf[1024], *cp, *t;
+ int i;
+
+ if (uname(&uts))
+ return (0);
+ snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
+
+ f = fopen(buf, "r");
+ if (!f)
+ return (0);
+ while (!feof(f)) {
+ if (!fgets(buf, sizeof(buf), f))
+ break;
+ if ((cp = strchr(buf, ':')) != NULL)
+ *cp = 0;
+ else
+ continue;
+ if ((cp = strrchr(buf, '/')) != NULL)
+ cp++;
+ else
+ cp = buf;
+ i = strlen(cp);
+ if (i > 3) {
+ t = cp + i - 3;
+ if (!strcmp(t, ".ko"))
+ *t = 0;
+ }
+ if (!strcmp(cp, fs_name)) {
+ fclose(f);
+ return (1);
+ }
+ }
+ fclose(f);
+#endif /* __linux__ */
+ return (0);
+}
+
+/*
+ * Helper function that does the right thing if write returns a
+ * partial write, or an EGAIN/EINTR error.
+ */
+int write_all(int fd, char *buf, size_t count)
+{
+ ssize_t ret;
+ int c = 0;
+
+ while (count > 0) {
+ ret = write(fd, buf, count);
+ if (ret < 0) {
+ if ((errno == EAGAIN) || (errno == EINTR))
+ continue;
+ return -1;
+ }
+ count -= ret;
+ buf += ret;
+ c += ret;
+ }
+ return c;
+}
+
+void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
+{
+
+ if (msg)
+ printf("MMP check failed: %s\n", msg);
+ if (mmp) {
+ time_t t = mmp->mmp_time;
+
+ printf("MMP error info: last update: %s node: %s device: %s\n",
+ ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
+ }
+}
+
+errcode_t e2fsck_mmp_update(ext2_filsys fs)
+{
+ errcode_t retval;
+
+ retval = ext2fs_mmp_update(fs);
+ if (retval == EXT2_ET_MMP_CHANGE_ABORT)
+ dump_mmp_msg(fs->mmp_cmp,
+ _("UNEXPECTED INCONSISTENCY: the filesystem is "
+ "being modified while fsck is running.\n"));
+
+ return retval;
+}
+
+void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
+ const char *profile_name, unsigned int *old_type)
+{
+ unsigned type;
+ errcode_t retval;
+
+ if (old_type)
+ *old_type = fs->default_bitmap_type;
+ profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
+ profile_name, 0, default_type, &type);
+ profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
+ "all", 0, type, &type);
+ fs->default_bitmap_type = type ? type : default_type;
+}
+
+errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
+ int deftype,
+ const char *name,
+ ext2fs_inode_bitmap *ret)
+{
+ errcode_t retval;
+ unsigned int save_type;
+
+ e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
+ retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
+ fs->default_bitmap_type = save_type;
+ return retval;
+}
+
+errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
+ int deftype,
+ const char *name,
+ ext2fs_block_bitmap *ret)
+{
+ errcode_t retval;
+ unsigned int save_type;
+
+ e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
+ retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
+ fs->default_bitmap_type = save_type;
+ return retval;
+}
+
+errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
+ int deftype,
+ const char *name,
+ ext2fs_block_bitmap *ret)
+{
+ errcode_t retval;
+ unsigned int save_type;
+
+ e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
+ retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
+ fs->default_bitmap_type = save_type;
+ return retval;
+}