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()
40 #ifdef HAVE_SYS_SYSCTL_H
41 #include <sys/sysctl.h>
46 extern e2fsck_t e2fsck_global_ctx
; /* Try your very best not to use this! */
51 #include <sys/resource.h>
53 void fatal_error(e2fsck_t ctx
, const char *msg
)
55 ext2_filsys fs
= ctx
->fs
;
56 int exit_value
= FSCK_ERROR
;
59 fprintf (stderr
, "e2fsck: %s\n", msg
);
62 if (fs
->io
&& fs
->super
) {
63 ext2fs_mmp_stop(ctx
->fs
);
64 if (ctx
->fs
->io
->magic
== EXT2_ET_MAGIC_IO_CHANNEL
)
65 io_channel_flush(ctx
->fs
->io
);
67 log_err(ctx
, "e2fsck: io manager magic bad!\n");
69 if (ext2fs_test_changed(fs
)) {
70 exit_value
|= FSCK_NONDESTRUCT
;
71 log_out(ctx
, _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
73 if (ctx
->mount_flags
& EXT2_MF_ISROOT
)
74 exit_value
|= FSCK_REBOOT
;
76 if (!ext2fs_test_valid(fs
)) {
77 log_out(ctx
, _("\n%s: ********** WARNING: Filesystem still has "
78 "errors **********\n\n"), ctx
->device_name
);
79 exit_value
|= FSCK_UNCORRECTED
;
80 exit_value
&= ~FSCK_NONDESTRUCT
;
83 ctx
->flags
|= E2F_FLAG_ABORT
;
84 if (ctx
->flags
& E2F_FLAG_SETJMP_OK
)
85 longjmp(ctx
->abort_loc
, 1);
87 fprintf(ctx
->logf
, "Exit status: %d\n", exit_value
);
91 void log_out(e2fsck_t ctx
, const char *fmt
, ...)
100 vfprintf(ctx
->logf
, fmt
, pvar
);
105 void log_err(e2fsck_t ctx
, const char *fmt
, ...)
110 vfprintf(stderr
, fmt
, pvar
);
114 vfprintf(ctx
->logf
, fmt
, pvar
);
119 void *e2fsck_allocate_memory(e2fsck_t ctx
, unsigned int size
,
120 const char *description
)
125 #ifdef DEBUG_ALLOCATE_MEMORY
126 printf("Allocating %u bytes for %s...\n", size
, description
);
130 sprintf(buf
, "Can't allocate %u bytes for %s\n",
132 fatal_error(ctx
, buf
);
134 memset(ret
, 0, size
);
138 char *string_copy(e2fsck_t ctx
EXT2FS_ATTR((unused
)),
139 const char *str
, int len
)
149 strncpy(ret
, str
, len
);
157 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
160 int e2fsck_strnlen(const char * s
, int count
)
164 while (count
-- && *cp
)
171 static int read_a_char(void)
178 if (e2fsck_global_ctx
&&
179 (e2fsck_global_ctx
->flags
& E2F_FLAG_CANCEL
)) {
192 int ask_yn(e2fsck_t ctx
, const char * string
, int def
)
196 const char *short_yes
= _("yY");
197 const char *short_no
= _("nN");
198 const char *short_yesall
= _("aA");
199 const char *english_yes
= "yY";
200 const char *english_no
= "nN";
201 const char *english_yesall
= "aA";
202 const char *yesall_prompt
= _(" ('a' enables 'yes' to all) ");
203 const char *extra_prompt
= "";
204 static int yes_answers
;
206 #ifdef HAVE_TERMIOS_H
207 struct termios termios
= {0, }, tmp
;
209 tcgetattr (0, &termios
);
211 tmp
.c_lflag
&= ~(ICANON
| ECHO
);
214 tcsetattr (0, TCSANOW
, &tmp
);
218 defstr
= _(_("<y>"));
220 defstr
= _(_("<n>"));
222 defstr
= _(" (y/n)");
224 * If the user presses 'y' more than 8 (but less than 12) times in
225 * succession without pressing anything else, display a hint about
228 if (yes_answers
> 12)
230 else if (yes_answers
> 8)
231 extra_prompt
= yesall_prompt
;
232 log_out(ctx
, "%s%s%s? ", string
, extra_prompt
, defstr
);
235 if ((c
= read_a_char()) == EOF
)
238 #ifdef HAVE_TERMIOS_H
239 tcsetattr (0, TCSANOW
, &termios
);
241 if (ctx
->flags
& E2F_FLAG_SETJMP_OK
) {
243 longjmp(e2fsck_global_ctx
->abort_loc
, 1);
245 log_out(ctx
, "%s", _("cancelled!\n"));
249 if (strchr(short_yes
, (char) c
)) {
252 if (yes_answers
>= 0)
255 } else if (strchr(short_no
, (char) c
)) {
260 } else if (strchr(short_yesall
, (char)c
)) {
264 ctx
->options
|= E2F_OPT_YES
;
266 } else if (strchr(english_yes
, (char) c
)) {
268 } else if (strchr(english_no
, (char) c
)) {
270 } else if (strchr(english_yesall
, (char) c
)) {
272 } else if ((c
== 27 || c
== ' ' || c
== '\n') && (def
!= -1)) {
278 log_out(ctx
, "%s", _("yes to all\n"));
280 log_out(ctx
, "%s", _("yes\n"));
282 log_out(ctx
, "%s", _("no\n"));
283 #ifdef HAVE_TERMIOS_H
284 tcsetattr (0, TCSANOW
, &termios
);
289 int ask (e2fsck_t ctx
, const char * string
, int def
)
291 if (ctx
->options
& E2F_OPT_NO
) {
292 log_out(ctx
, _("%s? no\n\n"), string
);
295 if (ctx
->options
& E2F_OPT_YES
) {
296 log_out(ctx
, _("%s? yes\n\n"), string
);
299 if (ctx
->options
& E2F_OPT_PREEN
) {
300 log_out(ctx
, "%s? %s\n\n", string
, def
? _("yes") : _("no"));
303 return ask_yn(ctx
, string
, def
);
306 void e2fsck_read_bitmaps(e2fsck_t ctx
)
308 ext2_filsys fs
= ctx
->fs
;
311 unsigned int save_type
;
314 if (ctx
->invalid_bitmaps
) {
315 com_err(ctx
->program_name
, 0,
316 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
321 old_op
= ehandler_operation(_("reading inode and block bitmaps"));
322 e2fsck_set_bitmap_type(fs
, EXT2FS_BMAP64_RBTREE
, "fs_bitmaps",
324 flags
= ctx
->fs
->flags
;
325 ctx
->fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
326 retval
= ext2fs_read_bitmaps(fs
);
327 ctx
->fs
->flags
= (flags
& EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
328 (ctx
->fs
->flags
& ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
329 fs
->default_bitmap_type
= save_type
;
330 ehandler_operation(old_op
);
332 com_err(ctx
->program_name
, retval
,
333 _("while retrying to read bitmaps for %s"),
339 void e2fsck_write_bitmaps(e2fsck_t ctx
)
341 ext2_filsys fs
= ctx
->fs
;
345 old_op
= ehandler_operation(_("writing block and inode bitmaps"));
346 retval
= ext2fs_write_bitmaps(fs
);
347 ehandler_operation(old_op
);
349 com_err(ctx
->program_name
, retval
,
350 _("while rewriting block and inode bitmaps for %s"),
356 void preenhalt(e2fsck_t ctx
)
358 ext2_filsys fs
= ctx
->fs
;
360 if (!(ctx
->options
& E2F_OPT_PREEN
))
362 log_err(ctx
, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
363 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
365 ctx
->flags
|= E2F_FLAG_EXITING
;
367 fs
->super
->s_state
|= EXT2_ERROR_FS
;
368 ext2fs_mark_super_dirty(fs
);
369 ext2fs_close_free(&fs
);
371 exit(FSCK_UNCORRECTED
);
374 #ifdef RESOURCE_TRACK
375 void init_resource_track(struct resource_track
*track
, io_channel channel
)
377 #ifdef HAVE_GETRUSAGE
380 io_stats io_start
= 0;
382 track
->brk_start
= sbrk(0);
383 gettimeofday(&track
->time_start
, 0);
384 #ifdef HAVE_GETRUSAGE
386 memset(&r
, 0, sizeof(struct rusage
));
388 getrusage(RUSAGE_SELF
, &r
);
389 track
->user_start
= r
.ru_utime
;
390 track
->system_start
= r
.ru_stime
;
392 track
->user_start
.tv_sec
= track
->user_start
.tv_usec
= 0;
393 track
->system_start
.tv_sec
= track
->system_start
.tv_usec
= 0;
395 track
->bytes_read
= 0;
396 track
->bytes_written
= 0;
397 if (channel
&& channel
->manager
&& channel
->manager
->get_stats
)
398 channel
->manager
->get_stats(channel
, &io_start
);
400 track
->bytes_read
= io_start
->bytes_read
;
401 track
->bytes_written
= io_start
->bytes_written
;
406 #define _INLINE_ __inline__
411 static _INLINE_
float timeval_subtract(struct timeval
*tv1
,
414 return ((tv1
->tv_sec
- tv2
->tv_sec
) +
415 ((float) (tv1
->tv_usec
- tv2
->tv_usec
)) / 1000000);
418 void print_resource_track(e2fsck_t ctx
, const char *desc
,
419 struct resource_track
*track
, io_channel channel
)
421 #ifdef HAVE_GETRUSAGE
425 struct mallinfo malloc_info
;
427 struct timeval time_end
;
429 if ((desc
&& !(ctx
->options
& E2F_OPT_TIME2
)) ||
430 (!desc
&& !(ctx
->options
& E2F_OPT_TIME
)))
433 e2fsck_clear_progbar(ctx
);
434 gettimeofday(&time_end
, 0);
437 log_out(ctx
, "%s: ", desc
);
440 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
442 malloc_info
= mallinfo();
443 log_out(ctx
, _("Memory used: %luk/%luk (%luk/%luk), "),
444 kbytes(malloc_info
.arena
), kbytes(malloc_info
.hblkhd
),
445 kbytes(malloc_info
.uordblks
), kbytes(malloc_info
.fordblks
));
447 log_out(ctx
, _("Memory used: %lu, "),
448 (unsigned long) (((char *) sbrk(0)) -
449 ((char *) track
->brk_start
)));
451 #ifdef HAVE_GETRUSAGE
452 getrusage(RUSAGE_SELF
, &r
);
454 log_out(ctx
, _("time: %5.2f/%5.2f/%5.2f\n"),
455 timeval_subtract(&time_end
, &track
->time_start
),
456 timeval_subtract(&r
.ru_utime
, &track
->user_start
),
457 timeval_subtract(&r
.ru_stime
, &track
->system_start
));
459 log_out(ctx
, _("elapsed time: %6.3f\n"),
460 timeval_subtract(&time_end
, &track
->time_start
));
462 #define mbytes(x) (((x) + 1048575) / 1048576)
463 if (channel
&& channel
->manager
&& channel
->manager
->get_stats
) {
465 unsigned long long bytes_read
= 0;
466 unsigned long long bytes_written
= 0;
469 log_out(ctx
, "%s: ", desc
);
471 channel
->manager
->get_stats(channel
, &delta
);
473 bytes_read
= delta
->bytes_read
- track
->bytes_read
;
474 bytes_written
= delta
->bytes_written
-
475 track
->bytes_written
;
477 log_out(ctx
, "I/O read: %lluMB, write: %lluMB, "
479 mbytes(bytes_read
), mbytes(bytes_written
),
480 (double)mbytes(bytes_read
+ bytes_written
) /
481 timeval_subtract(&time_end
, &track
->time_start
));
484 #endif /* RESOURCE_TRACK */
486 void e2fsck_read_inode(e2fsck_t ctx
, unsigned long ino
,
487 struct ext2_inode
* inode
, const char *proc
)
491 retval
= ext2fs_read_inode(ctx
->fs
, ino
, inode
);
493 com_err("ext2fs_read_inode", retval
,
494 _("while reading inode %lu in %s"), ino
, proc
);
499 void e2fsck_read_inode_full(e2fsck_t ctx
, unsigned long ino
,
500 struct ext2_inode
*inode
, int bufsize
,
505 retval
= ext2fs_read_inode_full(ctx
->fs
, ino
, inode
, bufsize
);
507 com_err("ext2fs_read_inode_full", retval
,
508 _("while reading inode %lu in %s"), ino
, proc
);
513 void e2fsck_write_inode_full(e2fsck_t ctx
, unsigned long ino
,
514 struct ext2_inode
* inode
, int bufsize
,
519 retval
= ext2fs_write_inode_full(ctx
->fs
, ino
, inode
, bufsize
);
521 com_err("ext2fs_write_inode", retval
,
522 _("while writing inode %lu in %s"), ino
, proc
);
527 void e2fsck_write_inode(e2fsck_t ctx
, unsigned long ino
,
528 struct ext2_inode
* inode
, const char *proc
)
532 retval
= ext2fs_write_inode(ctx
->fs
, ino
, inode
);
534 com_err("ext2fs_write_inode", retval
,
535 _("while writing inode %lu in %s"), ino
, proc
);
541 void mtrace_print(char *mesg
)
543 FILE *malloc_get_mallstream();
544 FILE *f
= malloc_get_mallstream();
547 fprintf(f
, "============= %s\n", mesg
);
551 blk64_t
get_backup_sb(e2fsck_t ctx
, ext2_filsys fs
, const char *name
,
554 struct ext2_super_block
*sb
;
555 io_channel io
= NULL
;
558 blk64_t superblock
, ret_sb
= 8193;
560 if (fs
&& fs
->super
) {
561 ret_sb
= (fs
->super
->s_blocks_per_group
+
562 fs
->super
->s_first_data_block
);
564 ctx
->superblock
= ret_sb
;
565 ctx
->blocksize
= fs
->blocksize
;
571 if (ctx
->blocksize
) {
572 ret_sb
= ctx
->blocksize
* 8;
573 if (ctx
->blocksize
== 1024)
575 ctx
->superblock
= ret_sb
;
578 ctx
->superblock
= ret_sb
;
579 ctx
->blocksize
= 1024;
582 if (!name
|| !manager
)
585 if (manager
->open(name
, 0, &io
) != 0)
588 if (ext2fs_get_mem(SUPERBLOCK_SIZE
, &buf
))
590 sb
= (struct ext2_super_block
*) buf
;
592 for (blocksize
= EXT2_MIN_BLOCK_SIZE
;
593 blocksize
<= EXT2_MAX_BLOCK_SIZE
; blocksize
*= 2) {
594 superblock
= blocksize
*8;
595 if (blocksize
== 1024)
597 io_channel_set_blksize(io
, blocksize
);
598 if (io_channel_read_blk64(io
, superblock
,
599 -SUPERBLOCK_SIZE
, buf
))
601 #ifdef WORDS_BIGENDIAN
602 if (sb
->s_magic
== ext2fs_swab16(EXT2_SUPER_MAGIC
))
603 ext2fs_swap_super(sb
);
605 if ((sb
->s_magic
== EXT2_SUPER_MAGIC
) &&
606 (EXT2_BLOCK_SIZE(sb
) == blocksize
)) {
609 ctx
->superblock
= superblock
;
610 ctx
->blocksize
= blocksize
;
618 io_channel_close(io
);
620 ext2fs_free_mem(&buf
);
625 * Given a mode, return the ext2 file type
627 int ext2_file_type(unsigned int mode
)
629 if (LINUX_S_ISREG(mode
))
630 return EXT2_FT_REG_FILE
;
632 if (LINUX_S_ISDIR(mode
))
635 if (LINUX_S_ISCHR(mode
))
636 return EXT2_FT_CHRDEV
;
638 if (LINUX_S_ISBLK(mode
))
639 return EXT2_FT_BLKDEV
;
641 if (LINUX_S_ISLNK(mode
))
642 return EXT2_FT_SYMLINK
;
644 if (LINUX_S_ISFIFO(mode
))
647 if (LINUX_S_ISSOCK(mode
))
654 * Check to see if a filesystem is in /proc/filesystems.
655 * Returns 1 if found, 0 if not
657 int fs_proc_check(const char *fs_name
)
660 char buf
[80], *cp
, *t
;
662 f
= fopen("/proc/filesystems", "r");
666 if (!fgets(buf
, sizeof(buf
), f
))
670 while (*cp
&& !isspace(*cp
))
673 while (*cp
&& isspace(*cp
))
675 if ((t
= strchr(cp
, '\n')) != NULL
)
677 if ((t
= strchr(cp
, '\t')) != NULL
)
679 if ((t
= strchr(cp
, ' ')) != NULL
)
681 if (!strcmp(fs_name
, cp
)) {
691 * Check to see if a filesystem is available as a module
692 * Returns 1 if found, 0 if not
694 int check_for_modules(const char *fs_name
)
699 char buf
[1024], *cp
, *t
;
704 snprintf(buf
, sizeof(buf
), "/lib/modules/%s/modules.dep", uts
.release
);
710 if (!fgets(buf
, sizeof(buf
), f
))
712 if ((cp
= strchr(buf
, ':')) != NULL
)
716 if ((cp
= strrchr(buf
, '/')) != NULL
)
723 if (!strcmp(t
, ".ko"))
726 if (!strcmp(cp
, fs_name
)) {
732 #endif /* __linux__ */
737 * Helper function that does the right thing if write returns a
738 * partial write, or an EAGAIN/EINTR error.
740 int write_all(int fd
, char *buf
, size_t count
)
746 ret
= write(fd
, buf
, count
);
748 if ((errno
== EAGAIN
) || (errno
== EINTR
))
759 void dump_mmp_msg(struct mmp_struct
*mmp
, const char *msg
)
763 printf("MMP check failed: %s\n", msg
);
765 time_t t
= mmp
->mmp_time
;
767 printf("MMP error info: last update: %s node: %s device: %s\n",
768 ctime(&t
), mmp
->mmp_nodename
, mmp
->mmp_bdevname
);
772 errcode_t
e2fsck_mmp_update(ext2_filsys fs
)
776 retval
= ext2fs_mmp_update(fs
);
777 if (retval
== EXT2_ET_MMP_CHANGE_ABORT
)
778 dump_mmp_msg(fs
->mmp_cmp
,
779 _("UNEXPECTED INCONSISTENCY: the filesystem is "
780 "being modified while fsck is running.\n"));
785 void e2fsck_set_bitmap_type(ext2_filsys fs
, unsigned int default_type
,
786 const char *profile_name
, unsigned int *old_type
)
789 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
792 *old_type
= fs
->default_bitmap_type
;
793 profile_get_uint(ctx
->profile
, "bitmaps", profile_name
, 0,
794 default_type
, &type
);
795 profile_get_uint(ctx
->profile
, "bitmaps", "all", 0, type
, &type
);
796 fs
->default_bitmap_type
= type
? type
: default_type
;
799 errcode_t
e2fsck_allocate_inode_bitmap(ext2_filsys fs
, const char *descr
,
802 ext2fs_inode_bitmap
*ret
)
805 unsigned int save_type
;
807 e2fsck_set_bitmap_type(fs
, deftype
, name
, &save_type
);
808 retval
= ext2fs_allocate_inode_bitmap(fs
, descr
, ret
);
809 fs
->default_bitmap_type
= save_type
;
813 errcode_t
e2fsck_allocate_block_bitmap(ext2_filsys fs
, const char *descr
,
816 ext2fs_block_bitmap
*ret
)
819 unsigned int save_type
;
821 e2fsck_set_bitmap_type(fs
, deftype
, name
, &save_type
);
822 retval
= ext2fs_allocate_block_bitmap(fs
, descr
, ret
);
823 fs
->default_bitmap_type
= save_type
;
827 errcode_t
e2fsck_allocate_subcluster_bitmap(ext2_filsys fs
, const char *descr
,
830 ext2fs_block_bitmap
*ret
)
833 unsigned int save_type
;
835 e2fsck_set_bitmap_type(fs
, deftype
, name
, &save_type
);
836 retval
= ext2fs_allocate_subcluster_bitmap(fs
, descr
, ret
);
837 fs
->default_bitmap_type
= save_type
;
841 /* Return memory size in bytes */
842 unsigned long long get_memory_size(void)
844 #if defined(_SC_PHYS_PAGES)
845 # if defined(_SC_PAGESIZE)
846 return (unsigned long long)sysconf(_SC_PHYS_PAGES
) *
847 (unsigned long long)sysconf(_SC_PAGESIZE
);
848 # elif defined(_SC_PAGE_SIZE)
849 return (unsigned long long)sysconf(_SC_PHYS_PAGES
) *
850 (unsigned long long)sysconf(_SC_PAGE_SIZE
);
852 #elif defined(CTL_HW)
853 # if (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
854 # define CTL_HW_INT64
855 # elif (defined(HW_PHYSMEM) || defined(HW_REALMEM))
861 # if defined(HW_MEMSIZE)
863 # elif defined(HW_PHYSMEM64)
864 mib
[1] = HW_PHYSMEM64
;
865 # elif defined(HW_REALMEM)
867 # elif defined(HW_PYSMEM)
870 # if defined(CTL_HW_INT64)
871 unsigned long long size
= 0;
872 # elif defined(CTL_HW_UINT)
873 unsigned int size
= 0;
875 # if defined(CTL_HW_INT64) || defined(CTL_HW_UINT)
876 size_t len
= sizeof(size
);
878 if (sysctl(mib
, 2, &size
, &len
, NULL
, 0) == 0)
879 return (unsigned long long)size
;
883 # warning "Don't know how to detect memory on your platform?"