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;
241 if (ctx->invalid_bitmaps) {
242 com_err(ctx->program_name, 0,
243 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
248 old_op = ehandler_operation(_("reading inode and block bitmaps"));
249 retval = ext2fs_read_bitmaps(fs);
250 ehandler_operation(old_op);
252 com_err(ctx->program_name, retval,
253 _("while retrying to read bitmaps for %s"),
259 void e2fsck_write_bitmaps(e2fsck_t ctx)
261 ext2_filsys fs = ctx->fs;
265 old_op = ehandler_operation(_("writing block and inode bitmaps"));
266 retval = ext2fs_write_bitmaps(fs);
267 ehandler_operation(old_op);
269 com_err(ctx->program_name, retval,
270 _("while rewriting block and inode bitmaps for %s"),
276 void preenhalt(e2fsck_t ctx)
278 ext2_filsys fs = ctx->fs;
280 if (!(ctx->options & E2F_OPT_PREEN))
282 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
283 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
285 ctx->flags |= E2F_FLAG_EXITING;
287 fs->super->s_state |= EXT2_ERROR_FS;
288 ext2fs_mark_super_dirty(fs);
291 exit(FSCK_UNCORRECTED);
294 #ifdef RESOURCE_TRACK
295 void init_resource_track(struct resource_track *track, io_channel channel)
297 #ifdef HAVE_GETRUSAGE
300 io_stats io_start = 0;
302 track->brk_start = sbrk(0);
303 gettimeofday(&track->time_start, 0);
304 #ifdef HAVE_GETRUSAGE
306 memset(&r, 0, sizeof(struct rusage));
308 getrusage(RUSAGE_SELF, &r);
309 track->user_start = r.ru_utime;
310 track->system_start = r.ru_stime;
312 track->user_start.tv_sec = track->user_start.tv_usec = 0;
313 track->system_start.tv_sec = track->system_start.tv_usec = 0;
315 track->bytes_read = 0;
316 track->bytes_written = 0;
317 if (channel && channel->manager && channel->manager->get_stats)
318 channel->manager->get_stats(channel, &io_start);
320 track->bytes_read = io_start->bytes_read;
321 track->bytes_written = io_start->bytes_written;
326 #define _INLINE_ __inline__
331 static _INLINE_ float timeval_subtract(struct timeval *tv1,
334 return ((tv1->tv_sec - tv2->tv_sec) +
335 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
338 void print_resource_track(e2fsck_t ctx, const char *desc,
339 struct resource_track *track, io_channel channel)
341 #ifdef HAVE_GETRUSAGE
345 struct mallinfo malloc_info;
347 struct timeval time_end;
349 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
350 (!desc && !(ctx->options & E2F_OPT_TIME)))
353 e2fsck_clear_progbar(ctx);
354 gettimeofday(&time_end, 0);
357 printf("%s: ", desc);
360 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
362 malloc_info = mallinfo();
363 printf(_("Memory used: %luk/%luk (%luk/%luk), "),
364 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
365 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
367 printf(_("Memory used: %lu, "),
368 (unsigned long) (((char *) sbrk(0)) -
369 ((char *) track->brk_start)));
371 #ifdef HAVE_GETRUSAGE
372 getrusage(RUSAGE_SELF, &r);
374 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
375 timeval_subtract(&time_end, &track->time_start),
376 timeval_subtract(&r.ru_utime, &track->user_start),
377 timeval_subtract(&r.ru_stime, &track->system_start));
379 printf(_("elapsed time: %6.3f\n"),
380 timeval_subtract(&time_end, &track->time_start));
382 #define mbytes(x) (((x) + 1048575) / 1048576)
383 if (channel && channel->manager && channel->manager->get_stats) {
385 unsigned long long bytes_read = 0;
386 unsigned long long bytes_written = 0;
389 printf("%s: ", desc);
391 channel->manager->get_stats(channel, &delta);
393 bytes_read = delta->bytes_read - track->bytes_read;
394 bytes_written = delta->bytes_written -
395 track->bytes_written;
397 printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
398 mbytes(bytes_read), mbytes(bytes_written),
399 (double)mbytes(bytes_read + bytes_written) /
400 timeval_subtract(&time_end, &track->time_start));
403 #endif /* RESOURCE_TRACK */
405 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
406 struct ext2_inode * inode, const char *proc)
410 retval = ext2fs_read_inode(ctx->fs, ino, inode);
412 com_err("ext2fs_read_inode", retval,
413 _("while reading inode %lu in %s"), ino, proc);
418 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
419 struct ext2_inode *inode, int bufsize,
424 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
426 com_err("ext2fs_read_inode_full", retval,
427 _("while reading inode %lu in %s"), ino, proc);
432 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
433 struct ext2_inode * inode, int bufsize,
438 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
440 com_err("ext2fs_write_inode", retval,
441 _("while writing inode %lu in %s"), ino, proc);
446 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
447 struct ext2_inode * inode, const char *proc)
451 retval = ext2fs_write_inode(ctx->fs, ino, inode);
453 com_err("ext2fs_write_inode", retval,
454 _("while writing inode %lu in %s"), ino, proc);
460 void mtrace_print(char *mesg)
462 FILE *malloc_get_mallstream();
463 FILE *f = malloc_get_mallstream();
466 fprintf(f, "============= %s\n", mesg);
470 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
473 struct ext2_super_block *sb;
474 io_channel io = NULL;
477 blk_t superblock, ret_sb = 8193;
479 if (fs && fs->super) {
480 ret_sb = (fs->super->s_blocks_per_group +
481 fs->super->s_first_data_block);
483 ctx->superblock = ret_sb;
484 ctx->blocksize = fs->blocksize;
490 if (ctx->blocksize) {
491 ret_sb = ctx->blocksize * 8;
492 if (ctx->blocksize == 1024)
494 ctx->superblock = ret_sb;
497 ctx->superblock = ret_sb;
498 ctx->blocksize = 1024;
501 if (!name || !manager)
504 if (manager->open(name, 0, &io) != 0)
507 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
509 sb = (struct ext2_super_block *) buf;
511 for (blocksize = EXT2_MIN_BLOCK_SIZE;
512 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
513 superblock = blocksize*8;
514 if (blocksize == 1024)
516 io_channel_set_blksize(io, blocksize);
517 if (io_channel_read_blk64(io, superblock,
518 -SUPERBLOCK_SIZE, buf))
520 #ifdef WORDS_BIGENDIAN
521 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
522 ext2fs_swap_super(sb);
524 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
525 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
528 ctx->superblock = superblock;
529 ctx->blocksize = blocksize;
537 io_channel_close(io);
539 ext2fs_free_mem(&buf);
544 * Given a mode, return the ext2 file type
546 int ext2_file_type(unsigned int mode)
548 if (LINUX_S_ISREG(mode))
549 return EXT2_FT_REG_FILE;
551 if (LINUX_S_ISDIR(mode))
554 if (LINUX_S_ISCHR(mode))
555 return EXT2_FT_CHRDEV;
557 if (LINUX_S_ISBLK(mode))
558 return EXT2_FT_BLKDEV;
560 if (LINUX_S_ISLNK(mode))
561 return EXT2_FT_SYMLINK;
563 if (LINUX_S_ISFIFO(mode))
566 if (LINUX_S_ISSOCK(mode))
572 #define STRIDE_LENGTH 8
574 * Helper function which zeros out _num_ blocks starting at _blk_. In
575 * case of an error, the details of the error is returned via _ret_blk_
576 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
577 * success, and an error code on an error.
579 * As a special case, if the first argument is NULL, then it will
580 * attempt to free the static zeroizing buffer. (This is to keep
581 * programs that check for memory leaks happy.)
583 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
584 blk_t *ret_blk, int *ret_count)
586 int j, count, next_update, next_update_incr;
590 /* If fs is null, clean up the static buffer and return */
598 /* Allocate the zeroizing buffer if necessary */
600 buf = malloc(fs->blocksize * STRIDE_LENGTH);
602 com_err("malloc", ENOMEM,
603 _("while allocating zeroizing buffer"));
606 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
608 /* OK, do the write loop */
610 next_update_incr = num / 100;
611 if (next_update_incr < 1)
612 next_update_incr = 1;
613 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
615 if (count > STRIDE_LENGTH)
616 count = STRIDE_LENGTH;
617 retval = io_channel_write_blk64(fs->io, blk, count, buf);
630 * Check to see if a filesystem is in /proc/filesystems.
631 * Returns 1 if found, 0 if not
633 int fs_proc_check(const char *fs_name)
636 char buf[80], *cp, *t;
638 f = fopen("/proc/filesystems", "r");
642 if (!fgets(buf, sizeof(buf), f))
646 while (*cp && !isspace(*cp))
649 while (*cp && isspace(*cp))
651 if ((t = strchr(cp, '\n')) != NULL)
653 if ((t = strchr(cp, '\t')) != NULL)
655 if ((t = strchr(cp, ' ')) != NULL)
657 if (!strcmp(fs_name, cp)) {
667 * Check to see if a filesystem is available as a module
668 * Returns 1 if found, 0 if not
670 int check_for_modules(const char *fs_name)
675 char buf[1024], *cp, *t;
680 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
686 if (!fgets(buf, sizeof(buf), f))
688 if ((cp = strchr(buf, ':')) != NULL)
692 if ((cp = strrchr(buf, '/')) != NULL)
699 if (!strcmp(t, ".ko"))
702 if (!strcmp(cp, fs_name)) {
708 #endif /* __linux__ */
713 * Helper function that does the right thing if write returns a
714 * partial write, or an EGAIN/EINTR error.
716 int write_all(int fd, char *buf, size_t count)
722 ret = write(fd, buf, count);
724 if ((errno == EAGAIN) || (errno == EINTR))
735 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
739 printf("MMP check failed: %s\n", msg);
741 time_t t = mmp->mmp_time;
743 printf("MMP error info: last update: %s node: %s device: %s\n",
744 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
748 errcode_t e2fsck_mmp_update(ext2_filsys fs)
752 retval = ext2fs_mmp_update(fs);
753 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
754 dump_mmp_msg(fs->mmp_cmp,
755 _("UNEXPECTED INCONSISTENCY: the filesystem is "
756 "being modified while fsck is running.\n"));