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);
85 void log_out(e2fsck_t ctx, const char *fmt, ...)
94 vfprintf(ctx->logf, fmt, pvar);
99 void log_err(e2fsck_t ctx, const char *fmt, ...)
104 vfprintf(stderr, fmt, pvar);
108 vfprintf(ctx->logf, fmt, pvar);
113 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
114 const char *description)
119 #ifdef DEBUG_ALLOCATE_MEMORY
120 printf("Allocating %u bytes for %s...\n", size, description);
124 sprintf(buf, "Can't allocate %u bytes for %s\n",
126 fatal_error(ctx, buf);
128 memset(ret, 0, size);
132 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
133 const char *str, int len)
143 strncpy(ret, str, len);
151 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
154 int e2fsck_strnlen(const char * s, int count)
158 while (count-- && *cp)
165 static int read_a_char(void)
172 if (e2fsck_global_ctx &&
173 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
186 int ask_yn(e2fsck_t ctx, const char * string, int def)
190 const char *short_yes = _("yY");
191 const char *short_no = _("nN");
192 const char *short_yesall = _("aA");
193 const char *yesall_prompt = _(" ('a' enables 'yes' to all) ");
194 const char *extra_prompt = "";
195 static int yes_answers;
197 #ifdef HAVE_TERMIOS_H
198 struct termios termios, tmp;
200 tcgetattr (0, &termios);
202 tmp.c_lflag &= ~(ICANON | ECHO);
205 tcsetattr (0, TCSANOW, &tmp);
209 defstr = _(_("<y>"));
211 defstr = _(_("<n>"));
213 defstr = _(" (y/n)");
215 * If the user presses 'y' more than 8 (but less than 12) times in
216 * succession without pressing anything else, display a hint about
219 if (yes_answers > 12)
221 else if (yes_answers > 8)
222 extra_prompt = yesall_prompt;
223 log_out(ctx, "%s%s%s? ", string, extra_prompt, defstr);
226 if ((c = read_a_char()) == EOF)
229 #ifdef HAVE_TERMIOS_H
230 tcsetattr (0, TCSANOW, &termios);
232 if (ctx->flags & E2F_FLAG_SETJMP_OK) {
234 longjmp(e2fsck_global_ctx->abort_loc, 1);
236 log_out(ctx, "%s", _("cancelled!\n"));
240 if (strchr(short_yes, (char) c)) {
242 if (yes_answers >= 0)
245 } else if (strchr(short_no, (char) c)) {
249 } else if (strchr(short_yesall, (char)c)) {
252 ctx->options |= E2F_OPT_YES;
254 } else if ((c == 27 || c == ' ' || c == '\n') && (def != -1)) {
260 log_out(ctx, "%s", _("yes to all\n"));
262 log_out(ctx, "%s", _("yes\n"));
264 log_out(ctx, "%s", _("no\n"));
265 #ifdef HAVE_TERMIOS_H
266 tcsetattr (0, TCSANOW, &termios);
271 int ask (e2fsck_t ctx, const char * string, int def)
273 if (ctx->options & E2F_OPT_NO) {
274 log_out(ctx, _("%s? no\n\n"), string);
277 if (ctx->options & E2F_OPT_YES) {
278 log_out(ctx, _("%s? yes\n\n"), string);
281 if (ctx->options & E2F_OPT_PREEN) {
282 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
285 return ask_yn(ctx, string, def);
288 void e2fsck_read_bitmaps(e2fsck_t ctx)
290 ext2_filsys fs = ctx->fs;
293 unsigned int save_type;
296 if (ctx->invalid_bitmaps) {
297 com_err(ctx->program_name, 0,
298 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
303 old_op = ehandler_operation(_("reading inode and block bitmaps"));
304 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
306 flags = ctx->fs->flags;
307 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
308 retval = ext2fs_read_bitmaps(fs);
309 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
310 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
311 fs->default_bitmap_type = save_type;
312 ehandler_operation(old_op);
314 com_err(ctx->program_name, retval,
315 _("while retrying to read bitmaps for %s"),
321 void e2fsck_write_bitmaps(e2fsck_t ctx)
323 ext2_filsys fs = ctx->fs;
327 old_op = ehandler_operation(_("writing block and inode bitmaps"));
328 retval = ext2fs_write_bitmaps(fs);
329 ehandler_operation(old_op);
331 com_err(ctx->program_name, retval,
332 _("while rewriting block and inode bitmaps for %s"),
338 void preenhalt(e2fsck_t ctx)
340 ext2_filsys fs = ctx->fs;
342 if (!(ctx->options & E2F_OPT_PREEN))
344 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
345 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
347 ctx->flags |= E2F_FLAG_EXITING;
349 fs->super->s_state |= EXT2_ERROR_FS;
350 ext2fs_mark_super_dirty(fs);
351 ext2fs_close_free(&fs);
353 exit(FSCK_UNCORRECTED);
356 #ifdef RESOURCE_TRACK
357 void init_resource_track(struct resource_track *track, io_channel channel)
359 #ifdef HAVE_GETRUSAGE
362 io_stats io_start = 0;
364 track->brk_start = sbrk(0);
365 gettimeofday(&track->time_start, 0);
366 #ifdef HAVE_GETRUSAGE
368 memset(&r, 0, sizeof(struct rusage));
370 getrusage(RUSAGE_SELF, &r);
371 track->user_start = r.ru_utime;
372 track->system_start = r.ru_stime;
374 track->user_start.tv_sec = track->user_start.tv_usec = 0;
375 track->system_start.tv_sec = track->system_start.tv_usec = 0;
377 track->bytes_read = 0;
378 track->bytes_written = 0;
379 if (channel && channel->manager && channel->manager->get_stats)
380 channel->manager->get_stats(channel, &io_start);
382 track->bytes_read = io_start->bytes_read;
383 track->bytes_written = io_start->bytes_written;
388 #define _INLINE_ __inline__
393 static _INLINE_ float timeval_subtract(struct timeval *tv1,
396 return ((tv1->tv_sec - tv2->tv_sec) +
397 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
400 void print_resource_track(e2fsck_t ctx, const char *desc,
401 struct resource_track *track, io_channel channel)
403 #ifdef HAVE_GETRUSAGE
407 struct mallinfo malloc_info;
409 struct timeval time_end;
411 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
412 (!desc && !(ctx->options & E2F_OPT_TIME)))
415 e2fsck_clear_progbar(ctx);
416 gettimeofday(&time_end, 0);
419 log_out(ctx, "%s: ", desc);
422 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
424 malloc_info = mallinfo();
425 log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
426 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
427 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
429 log_out(ctx, _("Memory used: %lu, "),
430 (unsigned long) (((char *) sbrk(0)) -
431 ((char *) track->brk_start)));
433 #ifdef HAVE_GETRUSAGE
434 getrusage(RUSAGE_SELF, &r);
436 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
437 timeval_subtract(&time_end, &track->time_start),
438 timeval_subtract(&r.ru_utime, &track->user_start),
439 timeval_subtract(&r.ru_stime, &track->system_start));
441 log_out(ctx, _("elapsed time: %6.3f\n"),
442 timeval_subtract(&time_end, &track->time_start));
444 #define mbytes(x) (((x) + 1048575) / 1048576)
445 if (channel && channel->manager && channel->manager->get_stats) {
447 unsigned long long bytes_read = 0;
448 unsigned long long bytes_written = 0;
451 log_out(ctx, "%s: ", desc);
453 channel->manager->get_stats(channel, &delta);
455 bytes_read = delta->bytes_read - track->bytes_read;
456 bytes_written = delta->bytes_written -
457 track->bytes_written;
459 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
461 mbytes(bytes_read), mbytes(bytes_written),
462 (double)mbytes(bytes_read + bytes_written) /
463 timeval_subtract(&time_end, &track->time_start));
466 #endif /* RESOURCE_TRACK */
468 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
469 struct ext2_inode * inode, const char *proc)
473 retval = ext2fs_read_inode(ctx->fs, ino, inode);
475 com_err("ext2fs_read_inode", retval,
476 _("while reading inode %lu in %s"), ino, proc);
481 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
482 struct ext2_inode *inode, int bufsize,
487 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
489 com_err("ext2fs_read_inode_full", retval,
490 _("while reading inode %lu in %s"), ino, proc);
495 void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
496 struct ext2_inode * inode, int bufsize,
501 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
503 com_err("ext2fs_write_inode", retval,
504 _("while writing inode %lu in %s"), ino, proc);
509 void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
510 struct ext2_inode * inode, const char *proc)
514 retval = ext2fs_write_inode(ctx->fs, ino, inode);
516 com_err("ext2fs_write_inode", retval,
517 _("while writing inode %lu in %s"), ino, proc);
523 void mtrace_print(char *mesg)
525 FILE *malloc_get_mallstream();
526 FILE *f = malloc_get_mallstream();
529 fprintf(f, "============= %s\n", mesg);
533 blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
536 struct ext2_super_block *sb;
537 io_channel io = NULL;
540 blk64_t superblock, ret_sb = 8193;
542 if (fs && fs->super) {
543 ret_sb = (fs->super->s_blocks_per_group +
544 fs->super->s_first_data_block);
546 ctx->superblock = ret_sb;
547 ctx->blocksize = fs->blocksize;
553 if (ctx->blocksize) {
554 ret_sb = ctx->blocksize * 8;
555 if (ctx->blocksize == 1024)
557 ctx->superblock = ret_sb;
560 ctx->superblock = ret_sb;
561 ctx->blocksize = 1024;
564 if (!name || !manager)
567 if (manager->open(name, 0, &io) != 0)
570 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
572 sb = (struct ext2_super_block *) buf;
574 for (blocksize = EXT2_MIN_BLOCK_SIZE;
575 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
576 superblock = blocksize*8;
577 if (blocksize == 1024)
579 io_channel_set_blksize(io, blocksize);
580 if (io_channel_read_blk64(io, superblock,
581 -SUPERBLOCK_SIZE, buf))
583 #ifdef WORDS_BIGENDIAN
584 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
585 ext2fs_swap_super(sb);
587 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
588 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
591 ctx->superblock = superblock;
592 ctx->blocksize = blocksize;
600 io_channel_close(io);
602 ext2fs_free_mem(&buf);
607 * Given a mode, return the ext2 file type
609 int ext2_file_type(unsigned int mode)
611 if (LINUX_S_ISREG(mode))
612 return EXT2_FT_REG_FILE;
614 if (LINUX_S_ISDIR(mode))
617 if (LINUX_S_ISCHR(mode))
618 return EXT2_FT_CHRDEV;
620 if (LINUX_S_ISBLK(mode))
621 return EXT2_FT_BLKDEV;
623 if (LINUX_S_ISLNK(mode))
624 return EXT2_FT_SYMLINK;
626 if (LINUX_S_ISFIFO(mode))
629 if (LINUX_S_ISSOCK(mode))
636 * Check to see if a filesystem is in /proc/filesystems.
637 * Returns 1 if found, 0 if not
639 int fs_proc_check(const char *fs_name)
642 char buf[80], *cp, *t;
644 f = fopen("/proc/filesystems", "r");
648 if (!fgets(buf, sizeof(buf), f))
652 while (*cp && !isspace(*cp))
655 while (*cp && isspace(*cp))
657 if ((t = strchr(cp, '\n')) != NULL)
659 if ((t = strchr(cp, '\t')) != NULL)
661 if ((t = strchr(cp, ' ')) != NULL)
663 if (!strcmp(fs_name, cp)) {
673 * Check to see if a filesystem is available as a module
674 * Returns 1 if found, 0 if not
676 int check_for_modules(const char *fs_name)
681 char buf[1024], *cp, *t;
686 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
692 if (!fgets(buf, sizeof(buf), f))
694 if ((cp = strchr(buf, ':')) != NULL)
698 if ((cp = strrchr(buf, '/')) != NULL)
705 if (!strcmp(t, ".ko"))
708 if (!strcmp(cp, fs_name)) {
714 #endif /* __linux__ */
719 * Helper function that does the right thing if write returns a
720 * partial write, or an EGAIN/EINTR error.
722 int write_all(int fd, char *buf, size_t count)
728 ret = write(fd, buf, count);
730 if ((errno == EAGAIN) || (errno == EINTR))
741 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
745 printf("MMP check failed: %s\n", msg);
747 time_t t = mmp->mmp_time;
749 printf("MMP error info: last update: %s node: %s device: %s\n",
750 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
754 errcode_t e2fsck_mmp_update(ext2_filsys fs)
758 retval = ext2fs_mmp_update(fs);
759 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
760 dump_mmp_msg(fs->mmp_cmp,
761 _("UNEXPECTED INCONSISTENCY: the filesystem is "
762 "being modified while fsck is running.\n"));
767 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
768 const char *profile_name, unsigned int *old_type)
771 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
774 *old_type = fs->default_bitmap_type;
775 profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
776 default_type, &type);
777 profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
778 fs->default_bitmap_type = type ? type : default_type;
781 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
784 ext2fs_inode_bitmap *ret)
787 unsigned int save_type;
789 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
790 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
791 fs->default_bitmap_type = save_type;
795 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
798 ext2fs_block_bitmap *ret)
801 unsigned int save_type;
803 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
804 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
805 fs->default_bitmap_type = save_type;
809 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
812 ext2fs_block_bitmap *ret)
815 unsigned int save_type;
817 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
818 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
819 fs->default_bitmap_type = save_type;