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
18 #include <sys/utsname.h>
24 #define read_a_char() getch()
41 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);
57 if (fs->io && fs->super) {
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 log_err(ctx, "e2fsck: io manager magic bad!\n");
64 if (ext2fs_test_changed(fs)) {
65 exit_value |= FSCK_NONDESTRUCT;
66 log_out(ctx, _("\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 log_out(ctx, _("\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 log_out(e2fsck_t ctx, const char *fmt, ...)
93 vfprintf(ctx->logf, fmt, pvar);
98 void log_err(e2fsck_t ctx, const char *fmt, ...)
103 vfprintf(stderr, fmt, pvar);
107 vfprintf(ctx->logf, fmt, pvar);
112 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
113 const char *description)
118 #ifdef DEBUG_ALLOCATE_MEMORY
119 printf("Allocating %u bytes for %s...\n", size, description);
123 sprintf(buf, "Can't allocate %u bytes for %s\n",
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, "%s", _("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, "%s", _("yes\n"));
239 log_out(ctx, "%s", _("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 retval = ext2fs_read_bitmaps(fs);
281 fs->default_bitmap_type = save_type;
282 ehandler_operation(old_op);
284 com_err(ctx->program_name, retval,
285 _("while retrying to read bitmaps for %s"),
291 void e2fsck_write_bitmaps(e2fsck_t ctx)
293 ext2_filsys fs = ctx->fs;
297 old_op = ehandler_operation(_("writing block and inode bitmaps"));
298 retval = ext2fs_write_bitmaps(fs);
299 ehandler_operation(old_op);
301 com_err(ctx->program_name, retval,
302 _("while rewriting block and inode bitmaps for %s"),
308 void preenhalt(e2fsck_t ctx)
310 ext2_filsys fs = ctx->fs;
312 if (!(ctx->options & E2F_OPT_PREEN))
314 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
315 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
317 ctx->flags |= E2F_FLAG_EXITING;
319 fs->super->s_state |= EXT2_ERROR_FS;
320 ext2fs_mark_super_dirty(fs);
323 exit(FSCK_UNCORRECTED);
326 #ifdef RESOURCE_TRACK
327 void init_resource_track(struct resource_track *track, io_channel channel)
329 #ifdef HAVE_GETRUSAGE
332 io_stats io_start = 0;
334 track->brk_start = sbrk(0);
335 gettimeofday(&track->time_start, 0);
336 #ifdef HAVE_GETRUSAGE
338 memset(&r, 0, sizeof(struct rusage));
340 getrusage(RUSAGE_SELF, &r);
341 track->user_start = r.ru_utime;
342 track->system_start = r.ru_stime;
344 track->user_start.tv_sec = track->user_start.tv_usec = 0;
345 track->system_start.tv_sec = track->system_start.tv_usec = 0;
347 track->bytes_read = 0;
348 track->bytes_written = 0;
349 if (channel && channel->manager && channel->manager->get_stats)
350 channel->manager->get_stats(channel, &io_start);
352 track->bytes_read = io_start->bytes_read;
353 track->bytes_written = io_start->bytes_written;
358 #define _INLINE_ __inline__
363 static _INLINE_ float timeval_subtract(struct timeval *tv1,
366 return ((tv1->tv_sec - tv2->tv_sec) +
367 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
370 void print_resource_track(e2fsck_t ctx, const char *desc,
371 struct resource_track *track, io_channel channel)
373 #ifdef HAVE_GETRUSAGE
377 struct mallinfo malloc_info;
379 struct timeval time_end;
381 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
382 (!desc && !(ctx->options & E2F_OPT_TIME)))
385 e2fsck_clear_progbar(ctx);
386 gettimeofday(&time_end, 0);
389 log_out(ctx, "%s: ", desc);
392 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
394 malloc_info = mallinfo();
395 log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
396 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
397 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
399 log_out(ctx, _("Memory used: %lu, "),
400 (unsigned long) (((char *) sbrk(0)) -
401 ((char *) track->brk_start)));
403 #ifdef HAVE_GETRUSAGE
404 getrusage(RUSAGE_SELF, &r);
406 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
407 timeval_subtract(&time_end, &track->time_start),
408 timeval_subtract(&r.ru_utime, &track->user_start),
409 timeval_subtract(&r.ru_stime, &track->system_start));
411 log_out(ctx, _("elapsed time: %6.3f\n"),
412 timeval_subtract(&time_end, &track->time_start));
414 #define mbytes(x) (((x) + 1048575) / 1048576)
415 if (channel && channel->manager && channel->manager->get_stats) {
417 unsigned long long bytes_read = 0;
418 unsigned long long bytes_written = 0;
421 log_out(ctx, "%s: ", desc);
423 channel->manager->get_stats(channel, &delta);
425 bytes_read = delta->bytes_read - track->bytes_read;
426 bytes_written = delta->bytes_written -
427 track->bytes_written;
429 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
431 mbytes(bytes_read), mbytes(bytes_written),
432 (double)mbytes(bytes_read + bytes_written) /
433 timeval_subtract(&time_end, &track->time_start));
436 #endif /* RESOURCE_TRACK */
438 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
439 struct ext2_inode * inode, const char *proc)
443 retval = ext2fs_read_inode(ctx->fs, ino, inode);
445 com_err("ext2fs_read_inode", retval,
446 _("while reading inode %lu in %s"), ino, proc);
451 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
452 struct ext2_inode *inode, int bufsize,
457 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
459 com_err("ext2fs_read_inode_full", retval,
460 _("while reading inode %lu in %s"), ino, proc);
465 void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
466 struct ext2_inode * inode, int bufsize,
471 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
473 com_err("ext2fs_write_inode", retval,
474 _("while writing inode %lu in %s"), ino, proc);
479 void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
480 struct ext2_inode * inode, const char *proc)
484 retval = ext2fs_write_inode(ctx->fs, ino, inode);
486 com_err("ext2fs_write_inode", retval,
487 _("while writing inode %lu in %s"), ino, proc);
493 void mtrace_print(char *mesg)
495 FILE *malloc_get_mallstream();
496 FILE *f = malloc_get_mallstream();
499 fprintf(f, "============= %s\n", mesg);
503 blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
506 struct ext2_super_block *sb;
507 io_channel io = NULL;
510 blk64_t superblock, ret_sb = 8193;
512 if (fs && fs->super) {
513 ret_sb = (fs->super->s_blocks_per_group +
514 fs->super->s_first_data_block);
516 ctx->superblock = ret_sb;
517 ctx->blocksize = fs->blocksize;
523 if (ctx->blocksize) {
524 ret_sb = ctx->blocksize * 8;
525 if (ctx->blocksize == 1024)
527 ctx->superblock = ret_sb;
530 ctx->superblock = ret_sb;
531 ctx->blocksize = 1024;
534 if (!name || !manager)
537 if (manager->open(name, 0, &io) != 0)
540 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
542 sb = (struct ext2_super_block *) buf;
544 for (blocksize = EXT2_MIN_BLOCK_SIZE;
545 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
546 superblock = blocksize*8;
547 if (blocksize == 1024)
549 io_channel_set_blksize(io, blocksize);
550 if (io_channel_read_blk64(io, superblock,
551 -SUPERBLOCK_SIZE, buf))
553 #ifdef WORDS_BIGENDIAN
554 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
555 ext2fs_swap_super(sb);
557 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
558 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
561 ctx->superblock = superblock;
562 ctx->blocksize = blocksize;
570 io_channel_close(io);
572 ext2fs_free_mem(&buf);
577 * Given a mode, return the ext2 file type
579 int ext2_file_type(unsigned int mode)
581 if (LINUX_S_ISREG(mode))
582 return EXT2_FT_REG_FILE;
584 if (LINUX_S_ISDIR(mode))
587 if (LINUX_S_ISCHR(mode))
588 return EXT2_FT_CHRDEV;
590 if (LINUX_S_ISBLK(mode))
591 return EXT2_FT_BLKDEV;
593 if (LINUX_S_ISLNK(mode))
594 return EXT2_FT_SYMLINK;
596 if (LINUX_S_ISFIFO(mode))
599 if (LINUX_S_ISSOCK(mode))
605 #define STRIDE_LENGTH 8
607 * Helper function which zeros out _num_ blocks starting at _blk_. In
608 * case of an error, the details of the error is returned via _ret_blk_
609 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
610 * success, and an error code on an error.
612 * As a special case, if the first argument is NULL, then it will
613 * attempt to free the static zeroizing buffer. (This is to keep
614 * programs that check for memory leaks happy.)
616 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
617 blk_t *ret_blk, int *ret_count)
623 /* If fs is null, clean up the static buffer and return */
631 /* Allocate the zeroizing buffer if necessary */
633 buf = malloc(fs->blocksize * STRIDE_LENGTH);
635 com_err("malloc", ENOMEM, "%s",
636 _("while allocating zeroizing buffer"));
639 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
641 /* OK, do the write loop */
642 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
644 if (count > STRIDE_LENGTH)
645 count = STRIDE_LENGTH;
646 retval = io_channel_write_blk64(fs->io, blk, count, buf);
659 * Check to see if a filesystem is in /proc/filesystems.
660 * Returns 1 if found, 0 if not
662 int fs_proc_check(const char *fs_name)
665 char buf[80], *cp, *t;
667 f = fopen("/proc/filesystems", "r");
671 if (!fgets(buf, sizeof(buf), f))
675 while (*cp && !isspace(*cp))
678 while (*cp && isspace(*cp))
680 if ((t = strchr(cp, '\n')) != NULL)
682 if ((t = strchr(cp, '\t')) != NULL)
684 if ((t = strchr(cp, ' ')) != NULL)
686 if (!strcmp(fs_name, cp)) {
696 * Check to see if a filesystem is available as a module
697 * Returns 1 if found, 0 if not
699 int check_for_modules(const char *fs_name)
704 char buf[1024], *cp, *t;
709 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
715 if (!fgets(buf, sizeof(buf), f))
717 if ((cp = strchr(buf, ':')) != NULL)
721 if ((cp = strrchr(buf, '/')) != NULL)
728 if (!strcmp(t, ".ko"))
731 if (!strcmp(cp, fs_name)) {
737 #endif /* __linux__ */
742 * Helper function that does the right thing if write returns a
743 * partial write, or an EGAIN/EINTR error.
745 int write_all(int fd, char *buf, size_t count)
751 ret = write(fd, buf, count);
753 if ((errno == EAGAIN) || (errno == EINTR))
764 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
768 printf("MMP check failed: %s\n", msg);
770 time_t t = mmp->mmp_time;
772 printf("MMP error info: last update: %s node: %s device: %s\n",
773 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
777 errcode_t e2fsck_mmp_update(ext2_filsys fs)
781 retval = ext2fs_mmp_update(fs);
782 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
783 dump_mmp_msg(fs->mmp_cmp,
784 _("UNEXPECTED INCONSISTENCY: the filesystem is "
785 "being modified while fsck is running.\n"));
790 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
791 const char *profile_name, unsigned int *old_type)
796 *old_type = fs->default_bitmap_type;
798 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
799 profile_name, 0, default_type, &type);
800 profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
801 "all", 0, type, &type);
802 fs->default_bitmap_type = type ? type : default_type;
804 fs->default_bitmap_type = default_type;
808 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
811 ext2fs_inode_bitmap *ret)
814 unsigned int save_type;
816 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
817 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
818 fs->default_bitmap_type = save_type;
822 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
825 ext2fs_block_bitmap *ret)
828 unsigned int save_type;
830 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
831 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
832 fs->default_bitmap_type = save_type;
836 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
839 ext2fs_block_bitmap *ret)
842 unsigned int save_type;
844 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
845 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
846 fs->default_bitmap_type = save_type;