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()
40 #ifdef HAVE_SYS_SYSCTL_H
41 #include <sys/sysctl.h>
46 extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
51 #include <sys/resource.h>
53 void fatal_error(e2fsck_t ctx, const char *msg)
55 ext2_filsys fs = ctx->fs;
56 int exit_value = FSCK_ERROR;
59 fprintf (stderr, "e2fsck: %s\n", msg);
62 if (fs->io && fs->super) {
63 ext2fs_mmp_stop(ctx->fs);
64 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
65 io_channel_flush(ctx->fs->io);
67 log_err(ctx, "e2fsck: io manager magic bad!\n");
69 if (ext2fs_test_changed(fs)) {
70 exit_value |= FSCK_NONDESTRUCT;
71 log_out(ctx, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
73 if (ctx->mount_flags & EXT2_MF_ISROOT)
74 exit_value |= FSCK_REBOOT;
76 if (!ext2fs_test_valid(fs)) {
77 log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
78 "errors **********\n\n"), ctx->device_name);
79 exit_value |= FSCK_UNCORRECTED;
80 exit_value &= ~FSCK_NONDESTRUCT;
83 ctx->flags |= E2F_FLAG_ABORT;
84 if (ctx->flags & E2F_FLAG_SETJMP_OK)
85 longjmp(ctx->abort_loc, 1);
87 fprintf(ctx->logf, "Exit status: %d\n", exit_value);
91 void log_out(e2fsck_t ctx, const char *fmt, ...)
100 vfprintf(ctx->logf, fmt, pvar);
105 void log_err(e2fsck_t ctx, const char *fmt, ...)
110 vfprintf(stderr, fmt, pvar);
114 vfprintf(ctx->logf, fmt, pvar);
119 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
120 const char *description)
125 #ifdef DEBUG_ALLOCATE_MEMORY
126 printf("Allocating %u bytes for %s...\n", size, description);
130 sprintf(buf, "Can't allocate %u bytes for %s\n",
132 fatal_error(ctx, buf);
134 memset(ret, 0, size);
138 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
139 const char *str, int len)
149 strncpy(ret, str, len);
157 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
160 int e2fsck_strnlen(const char * s, int count)
164 while (count-- && *cp)
171 static int read_a_char(void)
178 if (e2fsck_global_ctx &&
179 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
192 int ask_yn(e2fsck_t ctx, const char * string, int def)
196 const char *short_yes = _("yY");
197 const char *short_no = _("nN");
198 const char *short_yesall = _("aA");
199 const char *english_yes = "yY";
200 const char *english_no = "nN";
201 const char *english_yesall = "aA";
202 const char *yesall_prompt = _(" ('a' enables 'yes' to all) ");
203 const char *extra_prompt = "";
204 static int yes_answers;
206 #ifdef HAVE_TERMIOS_H
207 struct termios termios, tmp;
209 if (tcgetattr (0, &termios) < 0)
210 memset(&termios, 0, sizeof(termios));
212 tmp.c_lflag &= ~(ICANON | ECHO);
215 tcsetattr (0, TCSANOW, &tmp);
219 defstr = _(_("<y>"));
221 defstr = _(_("<n>"));
223 defstr = _(" (y/n)");
225 * If the user presses 'y' more than 8 (but less than 12) times in
226 * succession without pressing anything else, display a hint about
229 if (yes_answers > 12)
231 else if (yes_answers > 8)
232 extra_prompt = yesall_prompt;
233 log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr);
236 if ((c = read_a_char()) == EOF)
239 #ifdef HAVE_TERMIOS_H
240 tcsetattr (0, TCSANOW, &termios);
242 if (ctx->flags & E2F_FLAG_SETJMP_OK) {
244 longjmp(e2fsck_global_ctx->abort_loc, 1);
246 log_out(ctx, "%s", _("cancelled!\n"));
250 if (strchr(short_yes, (char) c)) {
253 if (yes_answers >= 0)
256 } else if (strchr(short_no, (char) c)) {
261 } else if (strchr(short_yesall, (char)c)) {
265 ctx->options |= E2F_OPT_YES;
267 } else if (strchr(english_yes, (char) c)) {
269 } else if (strchr(english_no, (char) c)) {
271 } else if (strchr(english_yesall, (char) c)) {
273 } else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) {
279 log_out(ctx, "%s", _("yes to all\n"));
281 log_out(ctx, "%s", _("yes\n"));
283 log_out(ctx, "%s", _("no\n"));
284 #ifdef HAVE_TERMIOS_H
285 tcsetattr (0, TCSANOW, &termios);
290 int ask (e2fsck_t ctx, const char * string, int def)
292 if (ctx->options & E2F_OPT_NO) {
293 log_out(ctx, _("%s? no\n\n"), string);
296 if (ctx->options & E2F_OPT_YES) {
297 log_out(ctx, _("%s? yes\n\n"), string);
300 if (ctx->options & E2F_OPT_PREEN) {
301 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
304 return ask_yn(ctx, string, def);
307 void e2fsck_read_bitmaps(e2fsck_t ctx)
309 ext2_filsys fs = ctx->fs;
312 unsigned int save_type;
315 if (ctx->invalid_bitmaps) {
316 com_err(ctx->program_name, 0,
317 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
322 old_op = ehandler_operation(_("reading inode and block bitmaps"));
323 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
325 flags = ctx->fs->flags;
326 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
327 retval = ext2fs_read_bitmaps(fs);
328 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
329 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
330 fs->default_bitmap_type = save_type;
331 ehandler_operation(old_op);
333 com_err(ctx->program_name, retval,
334 _("while retrying to read bitmaps for %s"),
340 void e2fsck_write_bitmaps(e2fsck_t ctx)
342 ext2_filsys fs = ctx->fs;
346 old_op = ehandler_operation(_("writing block and inode bitmaps"));
347 retval = ext2fs_write_bitmaps(fs);
348 ehandler_operation(old_op);
350 com_err(ctx->program_name, retval,
351 _("while rewriting block and inode bitmaps for %s"),
357 void preenhalt(e2fsck_t ctx)
359 ext2_filsys fs = ctx->fs;
361 if (!(ctx->options & E2F_OPT_PREEN))
363 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
364 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
366 ctx->flags |= E2F_FLAG_EXITING;
368 fs->super->s_state |= EXT2_ERROR_FS;
369 ext2fs_mark_super_dirty(fs);
370 ext2fs_close_free(&fs);
372 exit(FSCK_UNCORRECTED);
375 #ifdef RESOURCE_TRACK
376 void init_resource_track(struct resource_track *track, io_channel channel)
378 #ifdef HAVE_GETRUSAGE
381 io_stats io_start = 0;
383 track->brk_start = sbrk(0);
384 gettimeofday(&track->time_start, 0);
385 #ifdef HAVE_GETRUSAGE
387 memset(&r, 0, sizeof(struct rusage));
389 getrusage(RUSAGE_SELF, &r);
390 track->user_start = r.ru_utime;
391 track->system_start = r.ru_stime;
393 track->user_start.tv_sec = track->user_start.tv_usec = 0;
394 track->system_start.tv_sec = track->system_start.tv_usec = 0;
396 track->bytes_read = 0;
397 track->bytes_written = 0;
398 if (channel && channel->manager && channel->manager->get_stats)
399 channel->manager->get_stats(channel, &io_start);
401 track->bytes_read = io_start->bytes_read;
402 track->bytes_written = io_start->bytes_written;
407 #define _INLINE_ __inline__
412 static _INLINE_ float timeval_subtract(struct timeval *tv1,
415 return ((tv1->tv_sec - tv2->tv_sec) +
416 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
419 void print_resource_track(e2fsck_t ctx, const char *desc,
420 struct resource_track *track, io_channel channel)
422 #ifdef HAVE_GETRUSAGE
426 struct mallinfo malloc_info;
428 struct timeval time_end;
430 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
431 (!desc && !(ctx->options & E2F_OPT_TIME)))
434 e2fsck_clear_progbar(ctx);
435 gettimeofday(&time_end, 0);
438 log_out(ctx, "%s: ", desc);
441 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
443 malloc_info = mallinfo();
444 log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
445 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
446 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
448 log_out(ctx, _("Memory used: %lu, "),
449 (unsigned long) (((char *) sbrk(0)) -
450 ((char *) track->brk_start)));
452 #ifdef HAVE_GETRUSAGE
453 getrusage(RUSAGE_SELF, &r);
455 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
456 timeval_subtract(&time_end, &track->time_start),
457 timeval_subtract(&r.ru_utime, &track->user_start),
458 timeval_subtract(&r.ru_stime, &track->system_start));
460 log_out(ctx, _("elapsed time: %6.3f\n"),
461 timeval_subtract(&time_end, &track->time_start));
463 #define mbytes(x) (((x) + 1048575) / 1048576)
464 if (channel && channel->manager && channel->manager->get_stats) {
466 unsigned long long bytes_read = 0;
467 unsigned long long bytes_written = 0;
470 log_out(ctx, "%s: ", desc);
472 channel->manager->get_stats(channel, &delta);
474 bytes_read = delta->bytes_read - track->bytes_read;
475 bytes_written = delta->bytes_written -
476 track->bytes_written;
478 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
480 mbytes(bytes_read), mbytes(bytes_written),
481 (double)mbytes(bytes_read + bytes_written) /
482 timeval_subtract(&time_end, &track->time_start));
485 #endif /* RESOURCE_TRACK */
487 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
488 struct ext2_inode * inode, const char *proc)
492 retval = ext2fs_read_inode(ctx->fs, ino, inode);
494 com_err("ext2fs_read_inode", retval,
495 _("while reading inode %lu in %s"), ino, proc);
500 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
501 struct ext2_inode *inode, int bufsize,
506 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
508 com_err("ext2fs_read_inode_full", retval,
509 _("while reading inode %lu in %s"), ino, proc);
514 void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
515 struct ext2_inode * inode, int bufsize,
520 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
522 com_err("ext2fs_write_inode", retval,
523 _("while writing inode %lu in %s"), ino, proc);
528 void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
529 struct ext2_inode * inode, const char *proc)
533 retval = ext2fs_write_inode(ctx->fs, ino, inode);
535 com_err("ext2fs_write_inode", retval,
536 _("while writing inode %lu in %s"), ino, proc);
542 void mtrace_print(char *mesg)
544 FILE *malloc_get_mallstream();
545 FILE *f = malloc_get_mallstream();
548 fprintf(f, "============= %s\n", mesg);
552 blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
555 struct ext2_super_block *sb;
556 io_channel io = NULL;
559 blk64_t superblock, ret_sb = 8193;
561 if (fs && fs->super) {
562 ret_sb = (fs->super->s_blocks_per_group +
563 fs->super->s_first_data_block);
565 ctx->superblock = ret_sb;
566 ctx->blocksize = fs->blocksize;
572 if (ctx->blocksize) {
573 ret_sb = ctx->blocksize * 8;
574 if (ctx->blocksize == 1024)
576 ctx->superblock = ret_sb;
579 ctx->superblock = ret_sb;
580 ctx->blocksize = 1024;
583 if (!name || !manager)
586 if (manager->open(name, 0, &io) != 0)
589 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
591 sb = (struct ext2_super_block *) buf;
593 for (blocksize = EXT2_MIN_BLOCK_SIZE;
594 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
595 superblock = blocksize*8;
596 if (blocksize == 1024)
598 io_channel_set_blksize(io, blocksize);
599 if (io_channel_read_blk64(io, superblock,
600 -SUPERBLOCK_SIZE, buf))
602 #ifdef WORDS_BIGENDIAN
603 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
604 ext2fs_swap_super(sb);
606 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
607 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
610 ctx->superblock = superblock;
611 ctx->blocksize = blocksize;
619 io_channel_close(io);
621 ext2fs_free_mem(&buf);
626 * Given a mode, return the ext2 file type
628 int ext2_file_type(unsigned int mode)
630 if (LINUX_S_ISREG(mode))
631 return EXT2_FT_REG_FILE;
633 if (LINUX_S_ISDIR(mode))
636 if (LINUX_S_ISCHR(mode))
637 return EXT2_FT_CHRDEV;
639 if (LINUX_S_ISBLK(mode))
640 return EXT2_FT_BLKDEV;
642 if (LINUX_S_ISLNK(mode))
643 return EXT2_FT_SYMLINK;
645 if (LINUX_S_ISFIFO(mode))
648 if (LINUX_S_ISSOCK(mode))
655 * Check to see if a filesystem is in /proc/filesystems.
656 * Returns 1 if found, 0 if not
658 int fs_proc_check(const char *fs_name)
661 char buf[80], *cp, *t;
663 f = fopen("/proc/filesystems", "r");
667 if (!fgets(buf, sizeof(buf), f))
671 while (*cp && !isspace(*cp))
674 while (*cp && isspace(*cp))
676 if ((t = strchr(cp, '\n')) != NULL)
678 if ((t = strchr(cp, '\t')) != NULL)
680 if ((t = strchr(cp, ' ')) != NULL)
682 if (!strcmp(fs_name, cp)) {
692 * Check to see if a filesystem is available as a module
693 * Returns 1 if found, 0 if not
695 int check_for_modules(const char *fs_name)
700 char buf[1024], *cp, *t;
705 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
711 if (!fgets(buf, sizeof(buf), f))
713 if ((cp = strchr(buf, ':')) != NULL)
717 if ((cp = strrchr(buf, '/')) != NULL)
724 if (!strcmp(t, ".ko"))
727 if (!strcmp(cp, fs_name)) {
733 #endif /* __linux__ */
738 * Helper function that does the right thing if write returns a
739 * partial write, or an EAGAIN/EINTR error.
741 int write_all(int fd, char *buf, size_t count)
747 ret = write(fd, buf, count);
749 if ((errno == EAGAIN) || (errno == EINTR))
760 void dump_mmp_msg(struct mmp_struct *mmp, const char *fmt, ...)
765 printf("MMP check failed: ");
771 time_t t = mmp->mmp_time;
773 printf("MMP_block:\n");
774 printf(" mmp_magic: 0x%x\n", mmp->mmp_magic);
775 printf(" mmp_check_interval: %d\n",
776 mmp->mmp_check_interval);
777 printf(" mmp_sequence: %08x\n", mmp->mmp_seq);
778 printf(" mmp_update_date: %s", ctime(&t));
779 printf(" mmp_update_time: %lld\n", mmp->mmp_time);
780 printf(" mmp_node_name: %s\n", mmp->mmp_nodename);
781 printf(" mmp_device_name: %s\n", mmp->mmp_bdevname);
785 errcode_t e2fsck_mmp_update(ext2_filsys fs)
789 retval = ext2fs_mmp_update(fs);
790 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
791 dump_mmp_msg(fs->mmp_cmp,
792 _("UNEXPECTED INCONSISTENCY: the filesystem is "
793 "being modified while fsck is running.\n"));
798 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
799 const char *profile_name, unsigned int *old_type)
802 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
805 *old_type = fs->default_bitmap_type;
806 profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
807 default_type, &type);
808 profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
809 fs->default_bitmap_type = type ? type : default_type;
812 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
815 ext2fs_inode_bitmap *ret)
818 unsigned int save_type;
820 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
821 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
822 fs->default_bitmap_type = save_type;
826 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
829 ext2fs_block_bitmap *ret)
832 unsigned int save_type;
834 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
835 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
836 fs->default_bitmap_type = save_type;
840 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
843 ext2fs_block_bitmap *ret)
846 unsigned int save_type;
848 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
849 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
850 fs->default_bitmap_type = save_type;
854 /* Return memory size in bytes */
855 unsigned long long get_memory_size(void)
857 #if defined(_SC_PHYS_PAGES)
858 # if defined(_SC_PAGESIZE)
859 return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
860 (unsigned long long)sysconf(_SC_PAGESIZE);
861 # elif defined(_SC_PAGE_SIZE)
862 return (unsigned long long)sysconf(_SC_PHYS_PAGES) *
863 (unsigned long long)sysconf(_SC_PAGE_SIZE);
865 #elif defined(CTL_HW)
866 # if (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
867 # define CTL_HW_INT64
868 # elif (defined(HW_PHYSMEM) || defined(HW_REALMEM))
874 # if defined(HW_MEMSIZE)
876 # elif defined(HW_PHYSMEM64)
877 mib[1] = HW_PHYSMEM64;
878 # elif defined(HW_REALMEM)
880 # elif defined(HW_PYSMEM)
883 # if defined(CTL_HW_INT64)
884 unsigned long long size = 0;
885 # elif defined(CTL_HW_UINT)
886 unsigned int size = 0;
888 # if defined(CTL_HW_INT64) || defined(CTL_HW_UINT)
889 size_t len = sizeof(size);
891 if (sysctl(mib, 2, &size, &len, NULL, 0) == 0)
892 return (unsigned long long)size;
896 # warning "Don't know how to detect memory on your platform?"