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! */
46 #include <sys/resource.h>
48 void fatal_error(e2fsck_t ctx, const char *msg)
50 ext2_filsys fs = ctx->fs;
51 int exit_value = FSCK_ERROR;
54 fprintf (stderr, "e2fsck: %s\n", msg);
58 ext2fs_mmp_stop(ctx->fs);
59 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
60 io_channel_flush(ctx->fs->io);
62 fprintf(stderr, "e2fsck: io manager magic bad!\n");
64 if (ext2fs_test_changed(fs)) {
65 exit_value |= FSCK_NONDESTRUCT;
66 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
68 if (ctx->mount_flags & EXT2_MF_ISROOT)
69 exit_value |= FSCK_REBOOT;
71 if (!ext2fs_test_valid(fs)) {
72 printf(_("\n%s: ********** WARNING: Filesystem still has "
73 "errors **********\n\n"), ctx->device_name);
74 exit_value |= FSCK_UNCORRECTED;
75 exit_value &= ~FSCK_NONDESTRUCT;
78 ctx->flags |= E2F_FLAG_ABORT;
79 if (ctx->flags & E2F_FLAG_SETJMP_OK)
80 longjmp(ctx->abort_loc, 1);
84 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
85 const char *description)
90 #ifdef DEBUG_ALLOCATE_MEMORY
91 printf("Allocating %u bytes for %s...\n", size, description);
95 sprintf(buf, "Can't allocate %s\n", description);
96 fatal_error(ctx, buf);
102 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
103 const char *str, int len)
113 strncpy(ret, str, len);
121 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
124 int e2fsck_strnlen(const char * s, int count)
128 while (count-- && *cp)
135 static int read_a_char(void)
142 if (e2fsck_global_ctx &&
143 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
156 int ask_yn(const char * string, int def)
160 const char *short_yes = _("yY");
161 const char *short_no = _("nN");
163 #ifdef HAVE_TERMIOS_H
164 struct termios termios, tmp;
166 tcgetattr (0, &termios);
168 tmp.c_lflag &= ~(ICANON | ECHO);
171 tcsetattr (0, TCSANOW, &tmp);
175 defstr = _(_("<y>"));
177 defstr = _(_("<n>"));
179 defstr = _(" (y/n)");
180 printf("%s%s? ", string, defstr);
183 if ((c = read_a_char()) == EOF)
186 #ifdef HAVE_TERMIOS_H
187 tcsetattr (0, TCSANOW, &termios);
189 if (e2fsck_global_ctx &&
190 e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
192 longjmp(e2fsck_global_ctx->abort_loc, 1);
194 puts(_("cancelled!\n"));
197 if (strchr(short_yes, (char) c)) {
201 else if (strchr(short_no, (char) c)) {
205 else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
212 #ifdef HAVE_TERMIOS_H
213 tcsetattr (0, TCSANOW, &termios);
218 int ask (e2fsck_t ctx, const char * string, int def)
220 if (ctx->options & E2F_OPT_NO) {
221 printf (_("%s? no\n\n"), string);
224 if (ctx->options & E2F_OPT_YES) {
225 printf (_("%s? yes\n\n"), string);
228 if (ctx->options & E2F_OPT_PREEN) {
229 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
232 return ask_yn(string, def);
235 void e2fsck_read_bitmaps(e2fsck_t ctx)
237 ext2_filsys fs = ctx->fs;
240 unsigned int save_type;
242 if (ctx->invalid_bitmaps) {
243 com_err(ctx->program_name, 0,
244 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
249 old_op = ehandler_operation(_("reading inode and block bitmaps"));
250 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
252 retval = ext2fs_read_bitmaps(fs);
253 fs->default_bitmap_type = save_type;
254 ehandler_operation(old_op);
256 com_err(ctx->program_name, retval,
257 _("while retrying to read bitmaps for %s"),
263 void e2fsck_write_bitmaps(e2fsck_t ctx)
265 ext2_filsys fs = ctx->fs;
269 old_op = ehandler_operation(_("writing block and inode bitmaps"));
270 retval = ext2fs_write_bitmaps(fs);
271 ehandler_operation(old_op);
273 com_err(ctx->program_name, retval,
274 _("while rewriting block and inode bitmaps for %s"),
280 void preenhalt(e2fsck_t ctx)
282 ext2_filsys fs = ctx->fs;
284 if (!(ctx->options & E2F_OPT_PREEN))
286 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
287 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
289 ctx->flags |= E2F_FLAG_EXITING;
291 fs->super->s_state |= EXT2_ERROR_FS;
292 ext2fs_mark_super_dirty(fs);
295 exit(FSCK_UNCORRECTED);
298 #ifdef RESOURCE_TRACK
299 void init_resource_track(struct resource_track *track, io_channel channel)
301 #ifdef HAVE_GETRUSAGE
304 io_stats io_start = 0;
306 track->brk_start = sbrk(0);
307 gettimeofday(&track->time_start, 0);
308 #ifdef HAVE_GETRUSAGE
310 memset(&r, 0, sizeof(struct rusage));
312 getrusage(RUSAGE_SELF, &r);
313 track->user_start = r.ru_utime;
314 track->system_start = r.ru_stime;
316 track->user_start.tv_sec = track->user_start.tv_usec = 0;
317 track->system_start.tv_sec = track->system_start.tv_usec = 0;
319 track->bytes_read = 0;
320 track->bytes_written = 0;
321 if (channel && channel->manager && channel->manager->get_stats)
322 channel->manager->get_stats(channel, &io_start);
324 track->bytes_read = io_start->bytes_read;
325 track->bytes_written = io_start->bytes_written;
330 #define _INLINE_ __inline__
335 static _INLINE_ float timeval_subtract(struct timeval *tv1,
338 return ((tv1->tv_sec - tv2->tv_sec) +
339 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
342 void print_resource_track(e2fsck_t ctx, const char *desc,
343 struct resource_track *track, io_channel channel)
345 #ifdef HAVE_GETRUSAGE
349 struct mallinfo malloc_info;
351 struct timeval time_end;
353 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
354 (!desc && !(ctx->options & E2F_OPT_TIME)))
357 e2fsck_clear_progbar(ctx);
358 gettimeofday(&time_end, 0);
361 printf("%s: ", desc);
364 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
366 malloc_info = mallinfo();
367 printf(_("Memory used: %luk/%luk (%luk/%luk), "),
368 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
369 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
371 printf(_("Memory used: %lu, "),
372 (unsigned long) (((char *) sbrk(0)) -
373 ((char *) track->brk_start)));
375 #ifdef HAVE_GETRUSAGE
376 getrusage(RUSAGE_SELF, &r);
378 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
379 timeval_subtract(&time_end, &track->time_start),
380 timeval_subtract(&r.ru_utime, &track->user_start),
381 timeval_subtract(&r.ru_stime, &track->system_start));
383 printf(_("elapsed time: %6.3f\n"),
384 timeval_subtract(&time_end, &track->time_start));
386 #define mbytes(x) (((x) + 1048575) / 1048576)
387 if (channel && channel->manager && channel->manager->get_stats) {
389 unsigned long long bytes_read = 0;
390 unsigned long long bytes_written = 0;
393 printf("%s: ", desc);
395 channel->manager->get_stats(channel, &delta);
397 bytes_read = delta->bytes_read - track->bytes_read;
398 bytes_written = delta->bytes_written -
399 track->bytes_written;
401 printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
402 mbytes(bytes_read), mbytes(bytes_written),
403 (double)mbytes(bytes_read + bytes_written) /
404 timeval_subtract(&time_end, &track->time_start));
407 #endif /* RESOURCE_TRACK */
409 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
410 struct ext2_inode * inode, const char *proc)
414 retval = ext2fs_read_inode(ctx->fs, ino, inode);
416 com_err("ext2fs_read_inode", retval,
417 _("while reading inode %lu in %s"), ino, proc);
422 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
423 struct ext2_inode *inode, int bufsize,
428 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
430 com_err("ext2fs_read_inode_full", retval,
431 _("while reading inode %lu in %s"), ino, proc);
436 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
437 struct ext2_inode * inode, int bufsize,
442 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
444 com_err("ext2fs_write_inode", retval,
445 _("while writing inode %lu in %s"), ino, proc);
450 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
451 struct ext2_inode * inode, const char *proc)
455 retval = ext2fs_write_inode(ctx->fs, ino, inode);
457 com_err("ext2fs_write_inode", retval,
458 _("while writing inode %lu in %s"), ino, proc);
464 void mtrace_print(char *mesg)
466 FILE *malloc_get_mallstream();
467 FILE *f = malloc_get_mallstream();
470 fprintf(f, "============= %s\n", mesg);
474 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
477 struct ext2_super_block *sb;
478 io_channel io = NULL;
481 blk_t superblock, ret_sb = 8193;
483 if (fs && fs->super) {
484 ret_sb = (fs->super->s_blocks_per_group +
485 fs->super->s_first_data_block);
487 ctx->superblock = ret_sb;
488 ctx->blocksize = fs->blocksize;
494 if (ctx->blocksize) {
495 ret_sb = ctx->blocksize * 8;
496 if (ctx->blocksize == 1024)
498 ctx->superblock = ret_sb;
501 ctx->superblock = ret_sb;
502 ctx->blocksize = 1024;
505 if (!name || !manager)
508 if (manager->open(name, 0, &io) != 0)
511 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
513 sb = (struct ext2_super_block *) buf;
515 for (blocksize = EXT2_MIN_BLOCK_SIZE;
516 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
517 superblock = blocksize*8;
518 if (blocksize == 1024)
520 io_channel_set_blksize(io, blocksize);
521 if (io_channel_read_blk64(io, superblock,
522 -SUPERBLOCK_SIZE, buf))
524 #ifdef WORDS_BIGENDIAN
525 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
526 ext2fs_swap_super(sb);
528 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
529 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
532 ctx->superblock = superblock;
533 ctx->blocksize = blocksize;
541 io_channel_close(io);
543 ext2fs_free_mem(&buf);
548 * Given a mode, return the ext2 file type
550 int ext2_file_type(unsigned int mode)
552 if (LINUX_S_ISREG(mode))
553 return EXT2_FT_REG_FILE;
555 if (LINUX_S_ISDIR(mode))
558 if (LINUX_S_ISCHR(mode))
559 return EXT2_FT_CHRDEV;
561 if (LINUX_S_ISBLK(mode))
562 return EXT2_FT_BLKDEV;
564 if (LINUX_S_ISLNK(mode))
565 return EXT2_FT_SYMLINK;
567 if (LINUX_S_ISFIFO(mode))
570 if (LINUX_S_ISSOCK(mode))
576 #define STRIDE_LENGTH 8
578 * Helper function which zeros out _num_ blocks starting at _blk_. In
579 * case of an error, the details of the error is returned via _ret_blk_
580 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
581 * success, and an error code on an error.
583 * As a special case, if the first argument is NULL, then it will
584 * attempt to free the static zeroizing buffer. (This is to keep
585 * programs that check for memory leaks happy.)
587 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
588 blk_t *ret_blk, int *ret_count)
590 int j, count, next_update, next_update_incr;
594 /* If fs is null, clean up the static buffer and return */
602 /* Allocate the zeroizing buffer if necessary */
604 buf = malloc(fs->blocksize * STRIDE_LENGTH);
606 com_err("malloc", ENOMEM,
607 _("while allocating zeroizing buffer"));
610 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
612 /* OK, do the write loop */
614 next_update_incr = num / 100;
615 if (next_update_incr < 1)
616 next_update_incr = 1;
617 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
619 if (count > STRIDE_LENGTH)
620 count = STRIDE_LENGTH;
621 retval = io_channel_write_blk64(fs->io, blk, count, buf);
634 * Check to see if a filesystem is in /proc/filesystems.
635 * Returns 1 if found, 0 if not
637 int fs_proc_check(const char *fs_name)
640 char buf[80], *cp, *t;
642 f = fopen("/proc/filesystems", "r");
646 if (!fgets(buf, sizeof(buf), f))
650 while (*cp && !isspace(*cp))
653 while (*cp && isspace(*cp))
655 if ((t = strchr(cp, '\n')) != NULL)
657 if ((t = strchr(cp, '\t')) != NULL)
659 if ((t = strchr(cp, ' ')) != NULL)
661 if (!strcmp(fs_name, cp)) {
671 * Check to see if a filesystem is available as a module
672 * Returns 1 if found, 0 if not
674 int check_for_modules(const char *fs_name)
679 char buf[1024], *cp, *t;
684 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
690 if (!fgets(buf, sizeof(buf), f))
692 if ((cp = strchr(buf, ':')) != NULL)
696 if ((cp = strrchr(buf, '/')) != NULL)
703 if (!strcmp(t, ".ko"))
706 if (!strcmp(cp, fs_name)) {
712 #endif /* __linux__ */
717 * Helper function that does the right thing if write returns a
718 * partial write, or an EGAIN/EINTR error.
720 int write_all(int fd, char *buf, size_t count)
726 ret = write(fd, buf, count);
728 if ((errno == EAGAIN) || (errno == EINTR))
739 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
743 printf("MMP check failed: %s\n", msg);
745 time_t t = mmp->mmp_time;
747 printf("MMP error info: last update: %s node: %s device: %s\n",
748 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
752 errcode_t e2fsck_mmp_update(ext2_filsys fs)
756 retval = ext2fs_mmp_update(fs);
757 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
758 dump_mmp_msg(fs->mmp_cmp,
759 _("UNEXPECTED INCONSISTENCY: the filesystem is "
760 "being modified while fsck is running.\n"));
765 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
766 const char *profile_name, unsigned int *old_type)
772 *old_type = fs->default_bitmap_type;
773 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
774 profile_name, 0, default_type, &type);
775 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
776 "all", 0, type, &type);
777 fs->default_bitmap_type = type ? type : default_type;
780 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
783 ext2fs_inode_bitmap *ret)
786 unsigned int save_type;
788 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
789 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
790 fs->default_bitmap_type = save_type;
794 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
797 ext2fs_block_bitmap *ret)
800 unsigned int save_type;
802 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
803 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
804 fs->default_bitmap_type = save_type;
808 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
811 ext2fs_block_bitmap *ret)
814 unsigned int save_type;
816 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
817 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
818 fs->default_bitmap_type = save_type;