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);
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 %s\n", description);
125 fatal_error(ctx, buf);
127 memset(ret, 0, size);
131 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
132 const char *str, int len)
142 strncpy(ret, str, len);
150 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
153 int e2fsck_strnlen(const char * s, int count)
157 while (count-- && *cp)
164 static int read_a_char(void)
171 if (e2fsck_global_ctx &&
172 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
185 int ask_yn(e2fsck_t ctx, const char * string, int def)
189 const char *short_yes = _("yY");
190 const char *short_no = _("nN");
192 #ifdef HAVE_TERMIOS_H
193 struct termios termios, tmp;
195 tcgetattr (0, &termios);
197 tmp.c_lflag &= ~(ICANON | ECHO);
200 tcsetattr (0, TCSANOW, &tmp);
204 defstr = _(_("<y>"));
206 defstr = _(_("<n>"));
208 defstr = _(" (y/n)");
209 log_out(ctx, "%s%s? ", string, defstr);
212 if ((c = read_a_char()) == EOF)
215 #ifdef HAVE_TERMIOS_H
216 tcsetattr (0, TCSANOW, &termios);
218 if (ctx->flags & E2F_FLAG_SETJMP_OK) {
220 longjmp(e2fsck_global_ctx->abort_loc, 1);
222 log_out(ctx, _("cancelled!\n"));
225 if (strchr(short_yes, (char) c)) {
229 else if (strchr(short_no, (char) c)) {
233 else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
237 log_out(ctx, _("yes\n"));
239 log_out(ctx, _("no\n"));
240 #ifdef HAVE_TERMIOS_H
241 tcsetattr (0, TCSANOW, &termios);
246 int ask (e2fsck_t ctx, const char * string, int def)
248 if (ctx->options & E2F_OPT_NO) {
249 log_out(ctx, _("%s? no\n\n"), string);
252 if (ctx->options & E2F_OPT_YES) {
253 log_out(ctx, _("%s? yes\n\n"), string);
256 if (ctx->options & E2F_OPT_PREEN) {
257 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
260 return ask_yn(ctx, string, def);
263 void e2fsck_read_bitmaps(e2fsck_t ctx)
265 ext2_filsys fs = ctx->fs;
268 unsigned int save_type;
270 if (ctx->invalid_bitmaps) {
271 com_err(ctx->program_name, 0,
272 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
277 old_op = ehandler_operation(_("reading inode and block bitmaps"));
278 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
280 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
281 retval = ext2fs_read_bitmaps(fs);
282 ctx->fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
283 fs->default_bitmap_type = save_type;
284 ehandler_operation(old_op);
286 com_err(ctx->program_name, retval,
287 _("while retrying to read bitmaps for %s"),
293 void e2fsck_write_bitmaps(e2fsck_t ctx)
295 ext2_filsys fs = ctx->fs;
299 old_op = ehandler_operation(_("writing block and inode bitmaps"));
300 retval = ext2fs_write_bitmaps(fs);
301 ehandler_operation(old_op);
303 com_err(ctx->program_name, retval,
304 _("while rewriting block and inode bitmaps for %s"),
310 void preenhalt(e2fsck_t ctx)
312 ext2_filsys fs = ctx->fs;
314 if (!(ctx->options & E2F_OPT_PREEN))
316 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
317 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
319 ctx->flags |= E2F_FLAG_EXITING;
321 fs->super->s_state |= EXT2_ERROR_FS;
322 ext2fs_mark_super_dirty(fs);
325 exit(FSCK_UNCORRECTED);
328 #ifdef RESOURCE_TRACK
329 void init_resource_track(struct resource_track *track, io_channel channel)
331 #ifdef HAVE_GETRUSAGE
334 io_stats io_start = 0;
336 track->brk_start = sbrk(0);
337 gettimeofday(&track->time_start, 0);
338 #ifdef HAVE_GETRUSAGE
340 memset(&r, 0, sizeof(struct rusage));
342 getrusage(RUSAGE_SELF, &r);
343 track->user_start = r.ru_utime;
344 track->system_start = r.ru_stime;
346 track->user_start.tv_sec = track->user_start.tv_usec = 0;
347 track->system_start.tv_sec = track->system_start.tv_usec = 0;
349 track->bytes_read = 0;
350 track->bytes_written = 0;
351 if (channel && channel->manager && channel->manager->get_stats)
352 channel->manager->get_stats(channel, &io_start);
354 track->bytes_read = io_start->bytes_read;
355 track->bytes_written = io_start->bytes_written;
360 #define _INLINE_ __inline__
365 static _INLINE_ float timeval_subtract(struct timeval *tv1,
368 return ((tv1->tv_sec - tv2->tv_sec) +
369 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
372 void print_resource_track(e2fsck_t ctx, const char *desc,
373 struct resource_track *track, io_channel channel)
375 #ifdef HAVE_GETRUSAGE
379 struct mallinfo malloc_info;
381 struct timeval time_end;
383 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
384 (!desc && !(ctx->options & E2F_OPT_TIME)))
387 e2fsck_clear_progbar(ctx);
388 gettimeofday(&time_end, 0);
391 log_out(ctx, "%s: ", desc);
394 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
396 malloc_info = mallinfo();
397 log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
398 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
399 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
401 log_out(ctx, _("Memory used: %lu, "),
402 (unsigned long) (((char *) sbrk(0)) -
403 ((char *) track->brk_start)));
405 #ifdef HAVE_GETRUSAGE
406 getrusage(RUSAGE_SELF, &r);
408 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
409 timeval_subtract(&time_end, &track->time_start),
410 timeval_subtract(&r.ru_utime, &track->user_start),
411 timeval_subtract(&r.ru_stime, &track->system_start));
413 log_out(ctx, _("elapsed time: %6.3f\n"),
414 timeval_subtract(&time_end, &track->time_start));
416 #define mbytes(x) (((x) + 1048575) / 1048576)
417 if (channel && channel->manager && channel->manager->get_stats) {
419 unsigned long long bytes_read = 0;
420 unsigned long long bytes_written = 0;
423 log_out(ctx, "%s: ", desc);
425 channel->manager->get_stats(channel, &delta);
427 bytes_read = delta->bytes_read - track->bytes_read;
428 bytes_written = delta->bytes_written -
429 track->bytes_written;
431 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
433 mbytes(bytes_read), mbytes(bytes_written),
434 (double)mbytes(bytes_read + bytes_written) /
435 timeval_subtract(&time_end, &track->time_start));
438 #endif /* RESOURCE_TRACK */
440 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
441 struct ext2_inode * inode, const char *proc)
445 retval = ext2fs_read_inode(ctx->fs, ino, inode);
447 com_err("ext2fs_read_inode", retval,
448 _("while reading inode %lu in %s"), ino, proc);
453 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
454 struct ext2_inode *inode, int bufsize,
459 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
461 com_err("ext2fs_read_inode_full", retval,
462 _("while reading inode %lu in %s"), ino, proc);
467 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
468 struct ext2_inode * inode, int bufsize,
473 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
475 com_err("ext2fs_write_inode", retval,
476 _("while writing inode %lu in %s"), ino, proc);
481 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
482 struct ext2_inode * inode, const char *proc)
486 retval = ext2fs_write_inode(ctx->fs, ino, inode);
488 com_err("ext2fs_write_inode", retval,
489 _("while writing inode %lu in %s"), ino, proc);
495 void mtrace_print(char *mesg)
497 FILE *malloc_get_mallstream();
498 FILE *f = malloc_get_mallstream();
501 fprintf(f, "============= %s\n", mesg);
505 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
508 struct ext2_super_block *sb;
509 io_channel io = NULL;
512 blk_t superblock, ret_sb = 8193;
514 if (fs && fs->super) {
515 ret_sb = (fs->super->s_blocks_per_group +
516 fs->super->s_first_data_block);
518 ctx->superblock = ret_sb;
519 ctx->blocksize = fs->blocksize;
525 if (ctx->blocksize) {
526 ret_sb = ctx->blocksize * 8;
527 if (ctx->blocksize == 1024)
529 ctx->superblock = ret_sb;
532 ctx->superblock = ret_sb;
533 ctx->blocksize = 1024;
536 if (!name || !manager)
539 if (manager->open(name, 0, &io) != 0)
542 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
544 sb = (struct ext2_super_block *) buf;
546 for (blocksize = EXT2_MIN_BLOCK_SIZE;
547 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
548 superblock = blocksize*8;
549 if (blocksize == 1024)
551 io_channel_set_blksize(io, blocksize);
552 if (io_channel_read_blk64(io, superblock,
553 -SUPERBLOCK_SIZE, buf))
555 #ifdef WORDS_BIGENDIAN
556 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
557 ext2fs_swap_super(sb);
559 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
560 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
563 ctx->superblock = superblock;
564 ctx->blocksize = blocksize;
572 io_channel_close(io);
574 ext2fs_free_mem(&buf);
579 * Given a mode, return the ext2 file type
581 int ext2_file_type(unsigned int mode)
583 if (LINUX_S_ISREG(mode))
584 return EXT2_FT_REG_FILE;
586 if (LINUX_S_ISDIR(mode))
589 if (LINUX_S_ISCHR(mode))
590 return EXT2_FT_CHRDEV;
592 if (LINUX_S_ISBLK(mode))
593 return EXT2_FT_BLKDEV;
595 if (LINUX_S_ISLNK(mode))
596 return EXT2_FT_SYMLINK;
598 if (LINUX_S_ISFIFO(mode))
601 if (LINUX_S_ISSOCK(mode))
607 #define STRIDE_LENGTH 8
609 * Helper function which zeros out _num_ blocks starting at _blk_. In
610 * case of an error, the details of the error is returned via _ret_blk_
611 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
612 * success, and an error code on an error.
614 * As a special case, if the first argument is NULL, then it will
615 * attempt to free the static zeroizing buffer. (This is to keep
616 * programs that check for memory leaks happy.)
618 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
619 blk_t *ret_blk, int *ret_count)
625 /* If fs is null, clean up the static buffer and return */
633 /* Allocate the zeroizing buffer if necessary */
635 buf = malloc(fs->blocksize * STRIDE_LENGTH);
637 com_err("malloc", ENOMEM,
638 _("while allocating zeroizing buffer"));
641 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
643 /* OK, do the write loop */
644 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
646 if (count > STRIDE_LENGTH)
647 count = STRIDE_LENGTH;
648 retval = io_channel_write_blk64(fs->io, blk, count, buf);
661 * Check to see if a filesystem is in /proc/filesystems.
662 * Returns 1 if found, 0 if not
664 int fs_proc_check(const char *fs_name)
667 char buf[80], *cp, *t;
669 f = fopen("/proc/filesystems", "r");
673 if (!fgets(buf, sizeof(buf), f))
677 while (*cp && !isspace(*cp))
680 while (*cp && isspace(*cp))
682 if ((t = strchr(cp, '\n')) != NULL)
684 if ((t = strchr(cp, '\t')) != NULL)
686 if ((t = strchr(cp, ' ')) != NULL)
688 if (!strcmp(fs_name, cp)) {
698 * Check to see if a filesystem is available as a module
699 * Returns 1 if found, 0 if not
701 int check_for_modules(const char *fs_name)
706 char buf[1024], *cp, *t;
711 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
717 if (!fgets(buf, sizeof(buf), f))
719 if ((cp = strchr(buf, ':')) != NULL)
723 if ((cp = strrchr(buf, '/')) != NULL)
730 if (!strcmp(t, ".ko"))
733 if (!strcmp(cp, fs_name)) {
739 #endif /* __linux__ */
744 * Helper function that does the right thing if write returns a
745 * partial write, or an EGAIN/EINTR error.
747 int write_all(int fd, char *buf, size_t count)
753 ret = write(fd, buf, count);
755 if ((errno == EAGAIN) || (errno == EINTR))
766 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
770 printf("MMP check failed: %s\n", msg);
772 time_t t = mmp->mmp_time;
774 printf("MMP error info: last update: %s node: %s device: %s\n",
775 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
779 errcode_t e2fsck_mmp_update(ext2_filsys fs)
783 retval = ext2fs_mmp_update(fs);
784 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
785 dump_mmp_msg(fs->mmp_cmp,
786 _("UNEXPECTED INCONSISTENCY: the filesystem is "
787 "being modified while fsck is running.\n"));
792 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
793 const char *profile_name, unsigned int *old_type)
798 *old_type = fs->default_bitmap_type;
799 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
800 profile_name, 0, default_type, &type);
801 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
802 "all", 0, type, &type);
803 fs->default_bitmap_type = type ? type : default_type;
806 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
809 ext2fs_inode_bitmap *ret)
812 unsigned int save_type;
814 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
815 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
816 fs->default_bitmap_type = save_type;
820 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
823 ext2fs_block_bitmap *ret)
826 unsigned int save_type;
828 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
829 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
830 fs->default_bitmap_type = save_type;
834 errcode_t e2fsck_allocate_subcluster_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_subcluster_bitmap(fs, descr, ret);
844 fs->default_bitmap_type = save_type;