2 * util.c --- miscellaneous utilities
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
19 #include <sys/utsname.h>
25 #define read_a_char() getch()
42 extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
47 #include <sys/resource.h>
49 void fatal_error(e2fsck_t ctx, const char *msg)
51 ext2_filsys fs = ctx->fs;
52 int exit_value = FSCK_ERROR;
55 fprintf (stderr, "e2fsck: %s\n", msg);
58 if (fs->io && fs->super) {
59 ext2fs_mmp_stop(ctx->fs);
60 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
61 io_channel_flush(ctx->fs->io);
63 log_err(ctx, "e2fsck: io manager magic bad!\n");
65 if (ext2fs_test_changed(fs)) {
66 exit_value |= FSCK_NONDESTRUCT;
67 log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
69 if (ctx->mount_flags & EXT2_MF_ISROOT)
70 exit_value |= FSCK_REBOOT;
72 if (!ext2fs_test_valid(fs)) {
73 log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
74 "errors **********\n\n"), ctx->device_name);
75 exit_value |= FSCK_UNCORRECTED;
76 exit_value &= ~FSCK_NONDESTRUCT;
79 ctx->flags |= E2F_FLAG_ABORT;
80 if (ctx->flags & E2F_FLAG_SETJMP_OK)
81 longjmp(ctx->abort_loc, 1);
83 fprintf(ctx->logf, "Exit status: %d\n", exit_value);
87 void log_out(e2fsck_t ctx, const char *fmt, ...)
96 vfprintf(ctx->logf, fmt, pvar);
101 void log_err(e2fsck_t ctx, const char *fmt, ...)
106 vfprintf(stderr, fmt, pvar);
110 vfprintf(ctx->logf, fmt, pvar);
115 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned long size,
116 const char *description)
121 #ifdef DEBUG_ALLOCATE_MEMORY
122 printf("Allocating %lu bytes for %s...\n", size, description);
124 if (ext2fs_get_memzero(size, &ret)) {
125 sprintf(buf, "Can't allocate %lu bytes for %s\n",
127 fatal_error(ctx, buf);
133 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
134 const char *str, size_t len)
144 strncpy(ret, str, len);
152 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
155 int e2fsck_strnlen(const char * s, int count)
159 while (count-- && *cp)
166 static int read_a_char(void)
173 if (e2fsck_global_ctx &&
174 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
187 int ask_yn(e2fsck_t ctx, const char * string, int def)
191 const char *short_yes = _("yY");
192 const char *short_no = _("nN");
193 const char *short_yesall = _("aA");
194 const char *english_yes = "yY";
195 const char *english_no = "nN";
196 const char *english_yesall = "aA";
197 const char *yesall_prompt = _(" ('a' enables 'yes' to all) ");
198 const char *extra_prompt = "";
199 static int yes_answers;
201 #ifdef HAVE_TERMIOS_H
202 struct termios termios, tmp;
204 if (tcgetattr (0, &termios) < 0)
205 memset(&termios, 0, sizeof(termios));
207 tmp.c_lflag &= ~(ICANON | ECHO);
210 tcsetattr (0, TCSANOW, &tmp);
214 defstr = _(_("<y>"));
216 defstr = _(_("<n>"));
218 defstr = _(" (y/n)");
220 * If the user presses 'y' more than 8 (but less than 12) times in
221 * succession without pressing anything else, display a hint about
224 if (yes_answers > 12)
226 else if (yes_answers > 8)
227 extra_prompt = yesall_prompt;
228 log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr);
231 if ((c = read_a_char()) == EOF)
234 #ifdef HAVE_TERMIOS_H
235 tcsetattr (0, TCSANOW, &termios);
237 if (ctx->flags & E2F_FLAG_SETJMP_OK) {
239 longjmp(e2fsck_global_ctx->abort_loc, 1);
241 log_out(ctx, "%s", _("cancelled!\n"));
245 if (strchr(short_yes, (char) c)) {
248 if (yes_answers >= 0)
251 } else if (strchr(short_no, (char) c)) {
256 } else if (strchr(short_yesall, (char)c)) {
260 ctx->options |= E2F_OPT_YES;
262 } else if (strchr(english_yes, (char) c)) {
264 } else if (strchr(english_no, (char) c)) {
266 } else if (strchr(english_yesall, (char) c)) {
268 } else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) {
274 log_out(ctx, "%s", _("yes to all\n"));
276 log_out(ctx, "%s", _("yes\n"));
278 log_out(ctx, "%s", _("no\n"));
279 #ifdef HAVE_TERMIOS_H
280 tcsetattr (0, TCSANOW, &termios);
285 int ask (e2fsck_t ctx, const char * string, int def)
287 if (ctx->options & E2F_OPT_NO) {
288 log_out(ctx, _("%s? no\n\n"), string);
291 if (ctx->options & E2F_OPT_YES) {
292 log_out(ctx, _("%s? yes\n\n"), string);
295 if (ctx->options & E2F_OPT_PREEN) {
296 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
299 return ask_yn(ctx, string, def);
302 void e2fsck_read_bitmaps(e2fsck_t ctx)
304 ext2_filsys fs = ctx->fs;
307 unsigned int save_type;
310 if (ctx->invalid_bitmaps) {
311 com_err(ctx->program_name, 0,
312 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
317 old_op = ehandler_operation(_("reading inode and block bitmaps"));
318 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
320 flags = ctx->fs->flags;
321 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
322 retval = ext2fs_read_bitmaps(fs);
323 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
324 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
325 fs->default_bitmap_type = save_type;
326 ehandler_operation(old_op);
328 com_err(ctx->program_name, retval,
329 _("while retrying to read bitmaps for %s"),
335 void e2fsck_write_bitmaps(e2fsck_t ctx)
337 ext2_filsys fs = ctx->fs;
341 old_op = ehandler_operation(_("writing block and inode bitmaps"));
342 retval = ext2fs_write_bitmaps(fs);
343 ehandler_operation(old_op);
345 com_err(ctx->program_name, retval,
346 _("while rewriting block and inode bitmaps for %s"),
352 void preenhalt(e2fsck_t ctx)
354 ext2_filsys fs = ctx->fs;
356 if (!(ctx->options & E2F_OPT_PREEN))
358 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
359 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
361 ctx->flags |= E2F_FLAG_EXITING;
363 fs->super->s_state |= EXT2_ERROR_FS;
364 ext2fs_mark_super_dirty(fs);
365 ext2fs_close_free(&fs);
367 exit(FSCK_UNCORRECTED);
370 #ifdef RESOURCE_TRACK
371 void init_resource_track(struct resource_track *track, io_channel channel)
373 #ifdef HAVE_GETRUSAGE
376 io_stats io_start = 0;
378 track->brk_start = sbrk(0);
379 gettimeofday(&track->time_start, 0);
380 #ifdef HAVE_GETRUSAGE
382 memset(&r, 0, sizeof(struct rusage));
384 getrusage(RUSAGE_SELF, &r);
385 track->user_start = r.ru_utime;
386 track->system_start = r.ru_stime;
388 track->user_start.tv_sec = track->user_start.tv_usec = 0;
389 track->system_start.tv_sec = track->system_start.tv_usec = 0;
391 track->bytes_read = 0;
392 track->bytes_written = 0;
393 if (channel && channel->manager && channel->manager->get_stats)
394 channel->manager->get_stats(channel, &io_start);
396 track->bytes_read = io_start->bytes_read;
397 track->bytes_written = io_start->bytes_written;
402 #define _INLINE_ __inline__
407 static _INLINE_ float timeval_subtract(struct timeval *tv1,
410 return ((tv1->tv_sec - tv2->tv_sec) +
411 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
414 void print_resource_track(e2fsck_t ctx, const char *desc,
415 struct resource_track *track, io_channel channel)
417 #ifdef HAVE_GETRUSAGE
420 struct timeval time_end;
422 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
423 (!desc && !(ctx->options & E2F_OPT_TIME)))
426 e2fsck_clear_progbar(ctx);
427 gettimeofday(&time_end, 0);
430 log_out(ctx, "%s: ", desc);
432 #define kbytes(x) (((unsigned long long)(x) + 1023) / 1024)
433 #ifdef HAVE_MALLINFO2
435 struct mallinfo2 malloc_info = mallinfo2();
437 log_out(ctx, _("Memory used: %lluk/%lluk (%lluk/%lluk), "),
438 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
439 kbytes(malloc_info.uordblks),
440 kbytes(malloc_info.fordblks));
442 #elif defined HAVE_MALLINFO
443 /* don't use mallinfo() if over 2GB used, since it returns "int" */
444 if ((unsigned long)((char *)sbrk(0) - (char *)track->brk_start) <
446 struct mallinfo malloc_info = mallinfo();
448 log_out(ctx, _("Memory used: %lluk/%lluk (%lluk/%lluk), "),
449 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
450 kbytes(malloc_info.uordblks),
451 kbytes(malloc_info.fordblks));
454 log_out(ctx, _("Memory used: %lluk, "),
455 kbytes(((char *)sbrk(0)) - ((char *)track->brk_start)));
457 #ifdef HAVE_GETRUSAGE
458 getrusage(RUSAGE_SELF, &r);
460 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
461 timeval_subtract(&time_end, &track->time_start),
462 timeval_subtract(&r.ru_utime, &track->user_start),
463 timeval_subtract(&r.ru_stime, &track->system_start));
465 log_out(ctx, _("elapsed time: %6.3f\n"),
466 timeval_subtract(&time_end, &track->time_start));
468 #define mbytes(x) (((x) + 1048575) / 1048576)
469 if (channel && channel->manager && channel->manager->get_stats) {
471 unsigned long long bytes_read = 0;
472 unsigned long long bytes_written = 0;
475 log_out(ctx, "%s: ", desc);
477 channel->manager->get_stats(channel, &delta);
479 bytes_read = delta->bytes_read - track->bytes_read;
480 bytes_written = delta->bytes_written -
481 track->bytes_written;
483 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
485 mbytes(bytes_read), mbytes(bytes_written),
486 (double)mbytes(bytes_read + bytes_written) /
487 timeval_subtract(&time_end, &track->time_start));
490 #endif /* RESOURCE_TRACK */
492 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
493 struct ext2_inode * inode, const char *proc)
497 retval = ext2fs_read_inode(ctx->fs, ino, inode);
499 com_err("ext2fs_read_inode", retval,
500 _("while reading inode %lu in %s"), ino, proc);
505 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
506 struct ext2_inode *inode, int bufsize,
511 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
513 com_err("ext2fs_read_inode_full", retval,
514 _("while reading inode %lu in %s"), ino, proc);
519 void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
520 struct ext2_inode * inode, int bufsize,
525 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
527 com_err("ext2fs_write_inode", retval,
528 _("while writing inode %lu in %s"), ino, proc);
533 void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
534 struct ext2_inode * inode, const char *proc)
538 retval = ext2fs_write_inode(ctx->fs, ino, inode);
540 com_err("ext2fs_write_inode", retval,
541 _("while writing inode %lu in %s"), ino, proc);
547 void mtrace_print(char *mesg)
549 FILE *malloc_get_mallstream();
550 FILE *f = malloc_get_mallstream();
553 fprintf(f, "============= %s\n", mesg);
557 blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
560 struct ext2_super_block *sb;
561 io_channel io = NULL;
564 blk64_t superblock, ret_sb = 8193;
566 if (fs && fs->super) {
567 ret_sb = (fs->super->s_blocks_per_group +
568 fs->super->s_first_data_block);
570 ctx->superblock = ret_sb;
571 ctx->blocksize = fs->blocksize;
577 if (ctx->blocksize) {
578 ret_sb = ctx->blocksize * 8;
579 if (ctx->blocksize == 1024)
581 ctx->superblock = ret_sb;
584 ctx->superblock = ret_sb;
585 ctx->blocksize = 1024;
588 if (!name || !manager)
591 if (manager->open(name, 0, &io) != 0)
594 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
596 sb = (struct ext2_super_block *) buf;
598 for (blocksize = EXT2_MIN_BLOCK_SIZE;
599 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
600 superblock = blocksize*8;
601 if (blocksize == 1024)
603 io_channel_set_blksize(io, blocksize);
604 if (io_channel_read_blk64(io, superblock,
605 -SUPERBLOCK_SIZE, buf))
607 #ifdef WORDS_BIGENDIAN
608 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
609 ext2fs_swap_super(sb);
611 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
612 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
615 ctx->superblock = superblock;
616 ctx->blocksize = blocksize;
624 io_channel_close(io);
626 ext2fs_free_mem(&buf);
631 * Given a mode, return the ext2 file type
633 int ext2_file_type(unsigned int mode)
635 if (LINUX_S_ISREG(mode))
636 return EXT2_FT_REG_FILE;
638 if (LINUX_S_ISDIR(mode))
641 if (LINUX_S_ISCHR(mode))
642 return EXT2_FT_CHRDEV;
644 if (LINUX_S_ISBLK(mode))
645 return EXT2_FT_BLKDEV;
647 if (LINUX_S_ISLNK(mode))
648 return EXT2_FT_SYMLINK;
650 if (LINUX_S_ISFIFO(mode))
653 if (LINUX_S_ISSOCK(mode))
660 * Check to see if a filesystem is in /proc/filesystems.
661 * Returns 1 if found, 0 if not
663 int fs_proc_check(const char *fs_name)
666 char buf[80], *cp, *t;
668 f = fopen("/proc/filesystems", "r");
672 if (!fgets(buf, sizeof(buf), f))
676 while (*cp && !isspace(*cp))
679 while (*cp && isspace(*cp))
681 if ((t = strchr(cp, '\n')) != NULL)
683 if ((t = strchr(cp, '\t')) != NULL)
685 if ((t = strchr(cp, ' ')) != NULL)
687 if (!strcmp(fs_name, cp)) {
697 * Check to see if a filesystem is available as a module
698 * Returns 1 if found, 0 if not
700 int check_for_modules(const char *fs_name)
705 char buf[1024], *cp, *t;
710 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
716 if (!fgets(buf, sizeof(buf), f))
718 if ((cp = strchr(buf, ':')) != NULL)
722 if ((cp = strrchr(buf, '/')) != NULL)
729 if (!strcmp(t, ".ko"))
732 if (!strcmp(cp, fs_name)) {
738 #endif /* __linux__ */
743 * Helper function that does the right thing if write returns a
744 * partial write, or an EAGAIN/EINTR error.
746 int write_all(int fd, char *buf, size_t count)
752 ret = write(fd, buf, count);
754 if ((errno == EAGAIN) || (errno == EINTR))
765 void dump_mmp_msg(struct mmp_struct *mmp, const char *fmt, ...)
770 printf("MMP check failed: ");
776 time_t t = mmp->mmp_time;
778 printf("MMP_block:\n");
779 printf(" mmp_magic: 0x%x\n", mmp->mmp_magic);
780 printf(" mmp_check_interval: %d\n",
781 mmp->mmp_check_interval);
782 printf(" mmp_sequence: %08x\n", mmp->mmp_seq);
783 printf(" mmp_update_date: %s", ctime(&t));
784 printf(" mmp_update_time: %lld\n",
785 (long long) mmp->mmp_time);
786 printf(" mmp_node_name: %.*s\n",
787 EXT2_LEN_STR(mmp->mmp_nodename));
788 printf(" mmp_device_name: %.*s\n",
789 EXT2_LEN_STR(mmp->mmp_bdevname));
793 errcode_t e2fsck_mmp_update(ext2_filsys fs)
797 retval = ext2fs_mmp_update(fs);
798 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
799 dump_mmp_msg(fs->mmp_cmp,
800 _("UNEXPECTED INCONSISTENCY: the filesystem is "
801 "being modified while fsck is running.\n"));
806 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
807 const char *profile_name, unsigned int *old_type)
810 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
813 *old_type = fs->default_bitmap_type;
814 profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
815 default_type, &type);
816 profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
817 fs->default_bitmap_type = type ? type : default_type;
820 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
823 ext2fs_inode_bitmap *ret)
826 unsigned int save_type;
828 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
829 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
830 fs->default_bitmap_type = save_type;
834 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
837 ext2fs_block_bitmap *ret)
840 unsigned int save_type;
842 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
843 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
844 fs->default_bitmap_type = save_type;
848 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
851 ext2fs_block_bitmap *ret)
854 unsigned int save_type;
856 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
857 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
858 fs->default_bitmap_type = save_type;
862 /* Return memory size in bytes */
863 unsigned long long get_memory_size(void)
865 #if defined(_SC_PHYS_PAGES)
866 # if defined(_SC_PAGESIZE)
867 return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
868 (unsigned long long)sysconf(_SC_PAGESIZE);
869 # elif defined(_SC_PAGE_SIZE)
870 return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
871 (unsigned long long)sysconf(_SC_PAGE_SIZE);
873 #elif defined(CTL_HW)
874 # if (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
875 # define CTL_HW_INT64
876 # elif (defined(HW_PHYSMEM) || defined(HW_REALMEM))
882 # if defined(HW_MEMSIZE)
884 # elif defined(HW_PHYSMEM64)
885 mib[1] = HW_PHYSMEM64;
886 # elif defined(HW_REALMEM)
888 # elif defined(HW_PYSMEM)
891 # if defined(CTL_HW_INT64)
892 unsigned long long size = 0;
893 # elif defined(CTL_HW_UINT)
894 unsigned int size = 0;
898 # warning "Don't know how to detect memory on your platform?"