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)
51 fprintf (stderr, "e2fsck: %s\n", msg);
52 if (ctx->fs && ctx->fs->io) {
53 ext2fs_mmp_stop(ctx->fs);
54 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
55 io_channel_flush(ctx->fs->io);
57 fprintf(stderr, "e2fsck: io manager magic bad!\n");
59 ctx->flags |= E2F_FLAG_ABORT;
60 if (ctx->flags & E2F_FLAG_SETJMP_OK)
61 longjmp(ctx->abort_loc, 1);
65 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
66 const char *description)
71 #ifdef DEBUG_ALLOCATE_MEMORY
72 printf("Allocating %u bytes for %s...\n", size, description);
76 sprintf(buf, "Can't allocate %s\n", description);
77 fatal_error(ctx, buf);
83 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
84 const char *str, int len)
94 strncpy(ret, str, len);
102 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
105 int e2fsck_strnlen(const char * s, int count)
109 while (count-- && *cp)
116 static int read_a_char(void)
123 if (e2fsck_global_ctx &&
124 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
137 int ask_yn(const char * string, int def)
141 const char *short_yes = _("yY");
142 const char *short_no = _("nN");
144 #ifdef HAVE_TERMIOS_H
145 struct termios termios, tmp;
147 tcgetattr (0, &termios);
149 tmp.c_lflag &= ~(ICANON | ECHO);
152 tcsetattr (0, TCSANOW, &tmp);
156 defstr = _(_("<y>"));
158 defstr = _(_("<n>"));
160 defstr = _(" (y/n)");
161 printf("%s%s? ", string, defstr);
164 if ((c = read_a_char()) == EOF)
167 #ifdef HAVE_TERMIOS_H
168 tcsetattr (0, TCSANOW, &termios);
170 if (e2fsck_global_ctx &&
171 e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
173 longjmp(e2fsck_global_ctx->abort_loc, 1);
175 puts(_("cancelled!\n"));
178 if (strchr(short_yes, (char) c)) {
182 else if (strchr(short_no, (char) c)) {
186 else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
193 #ifdef HAVE_TERMIOS_H
194 tcsetattr (0, TCSANOW, &termios);
199 int ask (e2fsck_t ctx, const char * string, int def)
201 if (ctx->options & E2F_OPT_NO) {
202 printf (_("%s? no\n\n"), string);
205 if (ctx->options & E2F_OPT_YES) {
206 printf (_("%s? yes\n\n"), string);
209 if (ctx->options & E2F_OPT_PREEN) {
210 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
213 return ask_yn(string, def);
216 void e2fsck_read_bitmaps(e2fsck_t ctx)
218 ext2_filsys fs = ctx->fs;
222 if (ctx->invalid_bitmaps) {
223 com_err(ctx->program_name, 0,
224 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
229 old_op = ehandler_operation(_("reading inode and block bitmaps"));
230 retval = ext2fs_read_bitmaps(fs);
231 ehandler_operation(old_op);
233 com_err(ctx->program_name, retval,
234 _("while retrying to read bitmaps for %s"),
240 void e2fsck_write_bitmaps(e2fsck_t ctx)
242 ext2_filsys fs = ctx->fs;
246 old_op = ehandler_operation(_("writing block and inode bitmaps"));
247 retval = ext2fs_write_bitmaps(fs);
248 ehandler_operation(old_op);
250 com_err(ctx->program_name, retval,
251 _("while rewriting block and inode bitmaps for %s"),
257 void preenhalt(e2fsck_t ctx)
259 ext2_filsys fs = ctx->fs;
261 if (!(ctx->options & E2F_OPT_PREEN))
263 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
264 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
266 ctx->flags |= E2F_FLAG_EXITING;
268 fs->super->s_state |= EXT2_ERROR_FS;
269 ext2fs_mark_super_dirty(fs);
272 exit(FSCK_UNCORRECTED);
275 #ifdef RESOURCE_TRACK
276 void init_resource_track(struct resource_track *track, io_channel channel)
278 #ifdef HAVE_GETRUSAGE
281 io_stats io_start = 0;
283 track->brk_start = sbrk(0);
284 gettimeofday(&track->time_start, 0);
285 #ifdef HAVE_GETRUSAGE
287 memset(&r, 0, sizeof(struct rusage));
289 getrusage(RUSAGE_SELF, &r);
290 track->user_start = r.ru_utime;
291 track->system_start = r.ru_stime;
293 track->user_start.tv_sec = track->user_start.tv_usec = 0;
294 track->system_start.tv_sec = track->system_start.tv_usec = 0;
296 track->bytes_read = 0;
297 track->bytes_written = 0;
298 if (channel && channel->manager && channel->manager->get_stats)
299 channel->manager->get_stats(channel, &io_start);
301 track->bytes_read = io_start->bytes_read;
302 track->bytes_written = io_start->bytes_written;
307 #define _INLINE_ __inline__
312 static _INLINE_ float timeval_subtract(struct timeval *tv1,
315 return ((tv1->tv_sec - tv2->tv_sec) +
316 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
319 void print_resource_track(e2fsck_t ctx, const char *desc,
320 struct resource_track *track, io_channel channel)
322 #ifdef HAVE_GETRUSAGE
326 struct mallinfo malloc_info;
328 struct timeval time_end;
330 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
331 (!desc && !(ctx->options & E2F_OPT_TIME)))
334 e2fsck_clear_progbar(ctx);
335 gettimeofday(&time_end, 0);
338 printf("%s: ", desc);
341 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
343 malloc_info = mallinfo();
344 printf(_("Memory used: %luk/%luk (%luk/%luk), "),
345 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
346 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
348 printf(_("Memory used: %lu, "),
349 (unsigned long) (((char *) sbrk(0)) -
350 ((char *) track->brk_start)));
352 #ifdef HAVE_GETRUSAGE
353 getrusage(RUSAGE_SELF, &r);
355 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
356 timeval_subtract(&time_end, &track->time_start),
357 timeval_subtract(&r.ru_utime, &track->user_start),
358 timeval_subtract(&r.ru_stime, &track->system_start));
360 printf(_("elapsed time: %6.3f\n"),
361 timeval_subtract(&time_end, &track->time_start));
363 #define mbytes(x) (((x) + 1048575) / 1048576)
364 if (channel && channel->manager && channel->manager->get_stats) {
366 unsigned long long bytes_read = 0;
367 unsigned long long bytes_written = 0;
370 printf("%s: ", desc);
372 channel->manager->get_stats(channel, &delta);
374 bytes_read = delta->bytes_read - track->bytes_read;
375 bytes_written = delta->bytes_written -
376 track->bytes_written;
378 printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
379 mbytes(bytes_read), mbytes(bytes_written),
380 (double)mbytes(bytes_read + bytes_written) /
381 timeval_subtract(&time_end, &track->time_start));
384 #endif /* RESOURCE_TRACK */
386 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
387 struct ext2_inode * inode, const char *proc)
391 retval = ext2fs_read_inode(ctx->fs, ino, inode);
393 com_err("ext2fs_read_inode", retval,
394 _("while reading inode %lu in %s"), ino, proc);
399 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
400 struct ext2_inode *inode, int bufsize,
405 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
407 com_err("ext2fs_read_inode_full", retval,
408 _("while reading inode %lu in %s"), ino, proc);
413 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
414 struct ext2_inode * inode, int bufsize,
419 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
421 com_err("ext2fs_write_inode", retval,
422 _("while writing inode %lu in %s"), ino, proc);
427 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
428 struct ext2_inode * inode, const char *proc)
432 retval = ext2fs_write_inode(ctx->fs, ino, inode);
434 com_err("ext2fs_write_inode", retval,
435 _("while writing inode %lu in %s"), ino, proc);
441 void mtrace_print(char *mesg)
443 FILE *malloc_get_mallstream();
444 FILE *f = malloc_get_mallstream();
447 fprintf(f, "============= %s\n", mesg);
451 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
454 struct ext2_super_block *sb;
455 io_channel io = NULL;
458 blk_t superblock, ret_sb = 8193;
460 if (fs && fs->super) {
461 ret_sb = (fs->super->s_blocks_per_group +
462 fs->super->s_first_data_block);
464 ctx->superblock = ret_sb;
465 ctx->blocksize = fs->blocksize;
471 if (ctx->blocksize) {
472 ret_sb = ctx->blocksize * 8;
473 if (ctx->blocksize == 1024)
475 ctx->superblock = ret_sb;
478 ctx->superblock = ret_sb;
479 ctx->blocksize = 1024;
482 if (!name || !manager)
485 if (manager->open(name, 0, &io) != 0)
488 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
490 sb = (struct ext2_super_block *) buf;
492 for (blocksize = EXT2_MIN_BLOCK_SIZE;
493 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
494 superblock = blocksize*8;
495 if (blocksize == 1024)
497 io_channel_set_blksize(io, blocksize);
498 if (io_channel_read_blk64(io, superblock,
499 -SUPERBLOCK_SIZE, buf))
501 #ifdef WORDS_BIGENDIAN
502 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
503 ext2fs_swap_super(sb);
505 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
506 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
509 ctx->superblock = superblock;
510 ctx->blocksize = blocksize;
518 io_channel_close(io);
520 ext2fs_free_mem(&buf);
525 * Given a mode, return the ext2 file type
527 int ext2_file_type(unsigned int mode)
529 if (LINUX_S_ISREG(mode))
530 return EXT2_FT_REG_FILE;
532 if (LINUX_S_ISDIR(mode))
535 if (LINUX_S_ISCHR(mode))
536 return EXT2_FT_CHRDEV;
538 if (LINUX_S_ISBLK(mode))
539 return EXT2_FT_BLKDEV;
541 if (LINUX_S_ISLNK(mode))
542 return EXT2_FT_SYMLINK;
544 if (LINUX_S_ISFIFO(mode))
547 if (LINUX_S_ISSOCK(mode))
553 #define STRIDE_LENGTH 8
555 * Helper function which zeros out _num_ blocks starting at _blk_. In
556 * case of an error, the details of the error is returned via _ret_blk_
557 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
558 * success, and an error code on an error.
560 * As a special case, if the first argument is NULL, then it will
561 * attempt to free the static zeroizing buffer. (This is to keep
562 * programs that check for memory leaks happy.)
564 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
565 blk_t *ret_blk, int *ret_count)
567 int j, count, next_update, next_update_incr;
571 /* If fs is null, clean up the static buffer and return */
579 /* Allocate the zeroizing buffer if necessary */
581 buf = malloc(fs->blocksize * STRIDE_LENGTH);
583 com_err("malloc", ENOMEM,
584 _("while allocating zeroizing buffer"));
587 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
589 /* OK, do the write loop */
591 next_update_incr = num / 100;
592 if (next_update_incr < 1)
593 next_update_incr = 1;
594 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
596 if (count > STRIDE_LENGTH)
597 count = STRIDE_LENGTH;
598 retval = io_channel_write_blk64(fs->io, blk, count, buf);
611 * Check to see if a filesystem is in /proc/filesystems.
612 * Returns 1 if found, 0 if not
614 int fs_proc_check(const char *fs_name)
617 char buf[80], *cp, *t;
619 f = fopen("/proc/filesystems", "r");
623 if (!fgets(buf, sizeof(buf), f))
627 while (*cp && !isspace(*cp))
630 while (*cp && isspace(*cp))
632 if ((t = strchr(cp, '\n')) != NULL)
634 if ((t = strchr(cp, '\t')) != NULL)
636 if ((t = strchr(cp, ' ')) != NULL)
638 if (!strcmp(fs_name, cp)) {
648 * Check to see if a filesystem is available as a module
649 * Returns 1 if found, 0 if not
651 int check_for_modules(const char *fs_name)
656 char buf[1024], *cp, *t;
661 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
667 if (!fgets(buf, sizeof(buf), f))
669 if ((cp = strchr(buf, ':')) != NULL)
673 if ((cp = strrchr(buf, '/')) != NULL)
680 if (!strcmp(t, ".ko"))
683 if (!strcmp(cp, fs_name)) {
689 #endif /* __linux__ */
694 * Helper function that does the right thing if write returns a
695 * partial write, or an EGAIN/EINTR error.
697 int write_all(int fd, char *buf, size_t count)
703 ret = write(fd, buf, count);
705 if ((errno == EAGAIN) || (errno == EINTR))
716 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
720 printf("MMP check failed: %s\n", msg);
722 time_t t = mmp->mmp_time;
724 printf("MMP error info: last update: %s node: %s device: %s\n",
725 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
729 errcode_t e2fsck_mmp_update(ext2_filsys fs)
733 retval = ext2fs_mmp_update(fs);
734 if (retval == EXT2_ET_MMP_CHANGE_ABORT)
735 dump_mmp_msg(fs->mmp_cmp,
736 _("UNEXPECTED INCONSISTENCY: the filesystem is "
737 "being modified while fsck is running.\n"));