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");
193 #ifdef HAVE_TERMIOS_H
194 struct termios termios, tmp;
196 tcgetattr (0, &termios);
198 tmp.c_lflag &= ~(ICANON | ECHO);
201 tcsetattr (0, TCSANOW, &tmp);
205 defstr = _(_("<y>"));
207 defstr = _(_("<n>"));
209 defstr = _(" (y/n)");
210 log_out(ctx, "%s%s? ", string, defstr);
213 if ((c = read_a_char()) == EOF)
216 #ifdef HAVE_TERMIOS_H
217 tcsetattr (0, TCSANOW, &termios);
219 if (ctx->flags & E2F_FLAG_SETJMP_OK) {
221 longjmp(e2fsck_global_ctx->abort_loc, 1);
223 log_out(ctx, "%s", _("cancelled!\n"));
226 if (strchr(short_yes, (char) c)) {
230 else if (strchr(short_no, (char) c)) {
234 else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
238 log_out(ctx, "%s", _("yes\n"));
240 log_out(ctx, "%s", _("no\n"));
241 #ifdef HAVE_TERMIOS_H
242 tcsetattr (0, TCSANOW, &termios);
247 int ask (e2fsck_t ctx, const char * string, int def)
249 if (ctx->options & E2F_OPT_NO) {
250 log_out(ctx, _("%s? no\n\n"), string);
253 if (ctx->options & E2F_OPT_YES) {
254 log_out(ctx, _("%s? yes\n\n"), string);
257 if (ctx->options & E2F_OPT_PREEN) {
258 log_out(ctx, "%s? %s\n\n", string, def ? _("yes") : _("no"));
261 return ask_yn(ctx, string, def);
264 void e2fsck_read_bitmaps(e2fsck_t ctx)
266 ext2_filsys fs = ctx->fs;
269 unsigned int save_type;
271 if (ctx->invalid_bitmaps) {
272 com_err(ctx->program_name, 0,
273 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
278 old_op = ehandler_operation(_("reading inode and block bitmaps"));
279 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, "fs_bitmaps",
281 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
282 retval = ext2fs_read_bitmaps(fs);
283 ctx->fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
284 fs->default_bitmap_type = save_type;
285 ehandler_operation(old_op);
287 com_err(ctx->program_name, retval,
288 _("while retrying to read bitmaps for %s"),
294 void e2fsck_write_bitmaps(e2fsck_t ctx)
296 ext2_filsys fs = ctx->fs;
300 old_op = ehandler_operation(_("writing block and inode bitmaps"));
301 retval = ext2fs_write_bitmaps(fs);
302 ehandler_operation(old_op);
304 com_err(ctx->program_name, retval,
305 _("while rewriting block and inode bitmaps for %s"),
311 void preenhalt(e2fsck_t ctx)
313 ext2_filsys fs = ctx->fs;
315 if (!(ctx->options & E2F_OPT_PREEN))
317 log_err(ctx, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
318 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
320 ctx->flags |= E2F_FLAG_EXITING;
322 fs->super->s_state |= EXT2_ERROR_FS;
323 ext2fs_mark_super_dirty(fs);
324 ext2fs_close_free(&fs);
326 exit(FSCK_UNCORRECTED);
329 #ifdef RESOURCE_TRACK
330 void init_resource_track(struct resource_track *track, io_channel channel)
332 #ifdef HAVE_GETRUSAGE
335 io_stats io_start = 0;
337 track->brk_start = sbrk(0);
338 gettimeofday(&track->time_start, 0);
339 #ifdef HAVE_GETRUSAGE
341 memset(&r, 0, sizeof(struct rusage));
343 getrusage(RUSAGE_SELF, &r);
344 track->user_start = r.ru_utime;
345 track->system_start = r.ru_stime;
347 track->user_start.tv_sec = track->user_start.tv_usec = 0;
348 track->system_start.tv_sec = track->system_start.tv_usec = 0;
350 track->bytes_read = 0;
351 track->bytes_written = 0;
352 if (channel && channel->manager && channel->manager->get_stats)
353 channel->manager->get_stats(channel, &io_start);
355 track->bytes_read = io_start->bytes_read;
356 track->bytes_written = io_start->bytes_written;
361 #define _INLINE_ __inline__
366 static _INLINE_ float timeval_subtract(struct timeval *tv1,
369 return ((tv1->tv_sec - tv2->tv_sec) +
370 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
373 void print_resource_track(e2fsck_t ctx, const char *desc,
374 struct resource_track *track, io_channel channel)
376 #ifdef HAVE_GETRUSAGE
380 struct mallinfo malloc_info;
382 struct timeval time_end;
384 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
385 (!desc && !(ctx->options & E2F_OPT_TIME)))
388 e2fsck_clear_progbar(ctx);
389 gettimeofday(&time_end, 0);
392 log_out(ctx, "%s: ", desc);
395 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
397 malloc_info = mallinfo();
398 log_out(ctx, _("Memory used: %luk/%luk (%luk/%luk), "),
399 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
400 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
402 log_out(ctx, _("Memory used: %lu, "),
403 (unsigned long) (((char *) sbrk(0)) -
404 ((char *) track->brk_start)));
406 #ifdef HAVE_GETRUSAGE
407 getrusage(RUSAGE_SELF, &r);
409 log_out(ctx, _("time: %5.2f/%5.2f/%5.2f\n"),
410 timeval_subtract(&time_end, &track->time_start),
411 timeval_subtract(&r.ru_utime, &track->user_start),
412 timeval_subtract(&r.ru_stime, &track->system_start));
414 log_out(ctx, _("elapsed time: %6.3f\n"),
415 timeval_subtract(&time_end, &track->time_start));
417 #define mbytes(x) (((x) + 1048575) / 1048576)
418 if (channel && channel->manager && channel->manager->get_stats) {
420 unsigned long long bytes_read = 0;
421 unsigned long long bytes_written = 0;
424 log_out(ctx, "%s: ", desc);
426 channel->manager->get_stats(channel, &delta);
428 bytes_read = delta->bytes_read - track->bytes_read;
429 bytes_written = delta->bytes_written -
430 track->bytes_written;
432 log_out(ctx, "I/O read: %lluMB, write: %lluMB, "
434 mbytes(bytes_read), mbytes(bytes_written),
435 (double)mbytes(bytes_read + bytes_written) /
436 timeval_subtract(&time_end, &track->time_start));
439 #endif /* RESOURCE_TRACK */
441 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
442 struct ext2_inode * inode, const char *proc)
446 retval = ext2fs_read_inode(ctx->fs, ino, inode);
448 com_err("ext2fs_read_inode", retval,
449 _("while reading inode %lu in %s"), ino, proc);
454 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
455 struct ext2_inode *inode, int bufsize,
460 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
462 com_err("ext2fs_read_inode_full", retval,
463 _("while reading inode %lu in %s"), ino, proc);
468 void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
469 struct ext2_inode * inode, int bufsize,
474 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
476 com_err("ext2fs_write_inode", retval,
477 _("while writing inode %lu in %s"), ino, proc);
482 void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
483 struct ext2_inode * inode, const char *proc)
487 retval = ext2fs_write_inode(ctx->fs, ino, inode);
489 com_err("ext2fs_write_inode", retval,
490 _("while writing inode %lu in %s"), ino, proc);
496 void mtrace_print(char *mesg)
498 FILE *malloc_get_mallstream();
499 FILE *f = malloc_get_mallstream();
502 fprintf(f, "============= %s\n", mesg);
506 blk64_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
509 struct ext2_super_block *sb;
510 io_channel io = NULL;
513 blk64_t superblock, ret_sb = 8193;
515 if (fs && fs->super) {
516 ret_sb = (fs->super->s_blocks_per_group +
517 fs->super->s_first_data_block);
519 ctx->superblock = ret_sb;
520 ctx->blocksize = fs->blocksize;
526 if (ctx->blocksize) {
527 ret_sb = ctx->blocksize * 8;
528 if (ctx->blocksize == 1024)
530 ctx->superblock = ret_sb;
533 ctx->superblock = ret_sb;
534 ctx->blocksize = 1024;
537 if (!name || !manager)
540 if (manager->open(name, 0, &io) != 0)
543 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
545 sb = (struct ext2_super_block *) buf;
547 for (blocksize = EXT2_MIN_BLOCK_SIZE;
548 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
549 superblock = blocksize*8;
550 if (blocksize == 1024)
552 io_channel_set_blksize(io, blocksize);
553 if (io_channel_read_blk64(io, superblock,
554 -SUPERBLOCK_SIZE, buf))
556 #ifdef WORDS_BIGENDIAN
557 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
558 ext2fs_swap_super(sb);
560 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
561 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
564 ctx->superblock = superblock;
565 ctx->blocksize = blocksize;
573 io_channel_close(io);
575 ext2fs_free_mem(&buf);
580 * Given a mode, return the ext2 file type
582 int ext2_file_type(unsigned int mode)
584 if (LINUX_S_ISREG(mode))
585 return EXT2_FT_REG_FILE;
587 if (LINUX_S_ISDIR(mode))
590 if (LINUX_S_ISCHR(mode))
591 return EXT2_FT_CHRDEV;
593 if (LINUX_S_ISBLK(mode))
594 return EXT2_FT_BLKDEV;
596 if (LINUX_S_ISLNK(mode))
597 return EXT2_FT_SYMLINK;
599 if (LINUX_S_ISFIFO(mode))
602 if (LINUX_S_ISSOCK(mode))
608 #define STRIDE_LENGTH 8
610 * Helper function which zeros out _num_ blocks starting at _blk_. In
611 * case of an error, the details of the error is returned via _ret_blk_
612 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
613 * success, and an error code on an error.
615 * As a special case, if the first argument is NULL, then it will
616 * attempt to free the static zeroizing buffer. (This is to keep
617 * programs that check for memory leaks happy.)
619 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
620 blk_t *ret_blk, int *ret_count)
626 /* If fs is null, clean up the static buffer and return */
634 /* Allocate the zeroizing buffer if necessary */
636 buf = malloc(fs->blocksize * STRIDE_LENGTH);
638 com_err("malloc", ENOMEM, "%s",
639 _("while allocating zeroizing buffer"));
642 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
644 /* OK, do the write loop */
645 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
647 if (count > STRIDE_LENGTH)
648 count = STRIDE_LENGTH;
649 retval = io_channel_write_blk64(fs->io, blk, count, buf);
662 * Check to see if a filesystem is in /proc/filesystems.
663 * Returns 1 if found, 0 if not
665 int fs_proc_check(const char *fs_name)
668 char buf[80], *cp, *t;
670 f = fopen("/proc/filesystems", "r");
674 if (!fgets(buf, sizeof(buf), f))
678 while (*cp && !isspace(*cp))
681 while (*cp && isspace(*cp))
683 if ((t = strchr(cp, '\n')) != NULL)
685 if ((t = strchr(cp, '\t')) != NULL)
687 if ((t = strchr(cp, ' ')) != NULL)
689 if (!strcmp(fs_name, cp)) {
699 * Check to see if a filesystem is available as a module
700 * Returns 1 if found, 0 if not
702 int check_for_modules(const char *fs_name)
707 char buf[1024], *cp, *t;
712 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
718 if (!fgets(buf, sizeof(buf), f))
720 if ((cp = strchr(buf, ':')) != NULL)
724 if ((cp = strrchr(buf, '/')) != NULL)
731 if (!strcmp(t, ".ko"))
734 if (!strcmp(cp, fs_name)) {
740 #endif /* __linux__ */
745 * Helper function that does the right thing if write returns a
746 * partial write, or an EGAIN/EINTR error.
748 int write_all(int fd, char *buf, size_t count)
754 ret = write(fd, buf, count);
756 if ((errno == EAGAIN) || (errno == EINTR))
767 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
771 printf("MMP check failed: %s\n", msg);
773 time_t t = mmp->mmp_time;
775 printf("MMP error info: last update: %s node: %s device: %s\n",
776 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
780 errcode_t e2fsck_mmp_update(ext2_filsys fs)
784 retval = ext2fs_mmp_update(fs);
785 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
786 dump_mmp_msg(fs->mmp_cmp,
787 _("UNEXPECTED INCONSISTENCY: the filesystem is "
788 "being modified while fsck is running.\n"));
793 void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
794 const char *profile_name, unsigned int *old_type)
797 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
800 *old_type = fs->default_bitmap_type;
801 profile_get_uint(ctx->profile, "bitmaps", profile_name, 0,
802 default_type, &type);
803 profile_get_uint(ctx->profile, "bitmaps", "all", 0, type, &type);
804 fs->default_bitmap_type = type ? type : default_type;
807 errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
810 ext2fs_inode_bitmap *ret)
813 unsigned int save_type;
815 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
816 retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
817 fs->default_bitmap_type = save_type;
821 errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
824 ext2fs_block_bitmap *ret)
827 unsigned int save_type;
829 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
830 retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
831 fs->default_bitmap_type = save_type;
835 errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
838 ext2fs_block_bitmap *ret)
841 unsigned int save_type;
843 e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
844 retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
845 fs->default_bitmap_type = save_type;