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! */
44 #include <sys/resource.h>
46 void fatal_error(e2fsck_t ctx, const char *msg)
49 fprintf (stderr, "e2fsck: %s\n", msg);
50 if (ctx->fs && ctx->fs->io) {
51 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
52 io_channel_flush(ctx->fs->io);
54 fprintf(stderr, "e2fsck: io manager magic bad!\n");
56 ctx->flags |= E2F_FLAG_ABORT;
57 if (ctx->flags & E2F_FLAG_SETJMP_OK)
58 longjmp(ctx->abort_loc, 1);
62 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
63 const char *description)
68 #ifdef DEBUG_ALLOCATE_MEMORY
69 printf("Allocating %u bytes for %s...\n", size, description);
73 sprintf(buf, "Can't allocate %s\n", description);
74 fatal_error(ctx, buf);
80 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
81 const char *str, int len)
91 strncpy(ret, str, len);
99 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
102 int e2fsck_strnlen(const char * s, int count)
106 while (count-- && *cp)
113 static int read_a_char(void)
120 if (e2fsck_global_ctx &&
121 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
134 int ask_yn(const char * string, int def)
138 const char *short_yes = _("yY");
139 const char *short_no = _("nN");
141 #ifdef HAVE_TERMIOS_H
142 struct termios termios, tmp;
144 tcgetattr (0, &termios);
146 tmp.c_lflag &= ~(ICANON | ECHO);
149 tcsetattr (0, TCSANOW, &tmp);
153 defstr = _(_("<y>"));
155 defstr = _(_("<n>"));
157 defstr = _(" (y/n)");
158 printf("%s%s? ", string, defstr);
161 if ((c = read_a_char()) == EOF)
164 #ifdef HAVE_TERMIOS_H
165 tcsetattr (0, TCSANOW, &termios);
167 if (e2fsck_global_ctx &&
168 e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
170 longjmp(e2fsck_global_ctx->abort_loc, 1);
172 puts(_("cancelled!\n"));
175 if (strchr(short_yes, (char) c)) {
179 else if (strchr(short_no, (char) c)) {
183 else if ((c == 27 || c == ' ' || c == '\n') && (def != -1))
190 #ifdef HAVE_TERMIOS_H
191 tcsetattr (0, TCSANOW, &termios);
196 int ask (e2fsck_t ctx, const char * string, int def)
198 if (ctx->options & E2F_OPT_NO) {
199 printf (_("%s? no\n\n"), string);
202 if (ctx->options & E2F_OPT_YES) {
203 printf (_("%s? yes\n\n"), string);
206 if (ctx->options & E2F_OPT_PREEN) {
207 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
210 return ask_yn(string, def);
213 void e2fsck_read_bitmaps(e2fsck_t ctx)
215 ext2_filsys fs = ctx->fs;
219 if (ctx->invalid_bitmaps) {
220 com_err(ctx->program_name, 0,
221 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
226 old_op = ehandler_operation(_("reading inode and block bitmaps"));
227 retval = ext2fs_read_bitmaps(fs);
228 ehandler_operation(old_op);
230 com_err(ctx->program_name, retval,
231 _("while retrying to read bitmaps for %s"),
237 void e2fsck_write_bitmaps(e2fsck_t ctx)
239 ext2_filsys fs = ctx->fs;
243 old_op = ehandler_operation(_("writing block and inode bitmaps"));
244 retval = ext2fs_write_bitmaps(fs);
245 ehandler_operation(old_op);
247 com_err(ctx->program_name, retval,
248 _("while rewriting block and inode bitmaps for %s"),
254 void preenhalt(e2fsck_t ctx)
256 ext2_filsys fs = ctx->fs;
258 if (!(ctx->options & E2F_OPT_PREEN))
260 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
261 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
263 ctx->flags |= E2F_FLAG_EXITING;
265 fs->super->s_state |= EXT2_ERROR_FS;
266 ext2fs_mark_super_dirty(fs);
269 exit(FSCK_UNCORRECTED);
272 #ifdef RESOURCE_TRACK
273 void init_resource_track(struct resource_track *track, io_channel channel)
275 #ifdef HAVE_GETRUSAGE
278 io_stats io_start = 0;
280 track->brk_start = sbrk(0);
281 gettimeofday(&track->time_start, 0);
282 #ifdef HAVE_GETRUSAGE
284 memset(&r, 0, sizeof(struct rusage));
286 getrusage(RUSAGE_SELF, &r);
287 track->user_start = r.ru_utime;
288 track->system_start = r.ru_stime;
290 track->user_start.tv_sec = track->user_start.tv_usec = 0;
291 track->system_start.tv_sec = track->system_start.tv_usec = 0;
293 track->bytes_read = 0;
294 track->bytes_written = 0;
295 if (channel && channel->manager && channel->manager->get_stats)
296 channel->manager->get_stats(channel, &io_start);
298 track->bytes_read = io_start->bytes_read;
299 track->bytes_written = io_start->bytes_written;
304 #define _INLINE_ __inline__
309 static _INLINE_ float timeval_subtract(struct timeval *tv1,
312 return ((tv1->tv_sec - tv2->tv_sec) +
313 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
316 void print_resource_track(e2fsck_t ctx, const char *desc,
317 struct resource_track *track, io_channel channel)
319 #ifdef HAVE_GETRUSAGE
323 struct mallinfo malloc_info;
325 struct timeval time_end;
327 if ((desc && !(ctx->options & E2F_OPT_TIME2)) ||
328 (!desc && !(ctx->options & E2F_OPT_TIME)))
331 e2fsck_clear_progbar(ctx);
332 gettimeofday(&time_end, 0);
335 printf("%s: ", desc);
338 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
340 malloc_info = mallinfo();
341 printf(_("Memory used: %luk/%luk (%luk/%luk), "),
342 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
343 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
345 printf(_("Memory used: %lu, "),
346 (unsigned long) (((char *) sbrk(0)) -
347 ((char *) track->brk_start)));
349 #ifdef HAVE_GETRUSAGE
350 getrusage(RUSAGE_SELF, &r);
352 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
353 timeval_subtract(&time_end, &track->time_start),
354 timeval_subtract(&r.ru_utime, &track->user_start),
355 timeval_subtract(&r.ru_stime, &track->system_start));
357 printf(_("elapsed time: %6.3f\n"),
358 timeval_subtract(&time_end, &track->time_start));
360 #define mbytes(x) (((x) + 1048575) / 1048576)
361 if (channel && channel->manager && channel->manager->get_stats) {
363 unsigned long long bytes_read = 0;
364 unsigned long long bytes_written = 0;
367 printf("%s: ", desc);
369 channel->manager->get_stats(channel, &delta);
371 bytes_read = delta->bytes_read - track->bytes_read;
372 bytes_written = delta->bytes_written -
373 track->bytes_written;
375 printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
376 mbytes(bytes_read), mbytes(bytes_written),
377 (double)mbytes(bytes_read + bytes_written) /
378 timeval_subtract(&time_end, &track->time_start));
381 #endif /* RESOURCE_TRACK */
383 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
384 struct ext2_inode * inode, const char *proc)
388 retval = ext2fs_read_inode(ctx->fs, ino, inode);
390 com_err("ext2fs_read_inode", retval,
391 _("while reading inode %lu in %s"), ino, proc);
396 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
397 struct ext2_inode *inode, int bufsize,
402 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
404 com_err("ext2fs_read_inode_full", retval,
405 _("while reading inode %lu in %s"), ino, proc);
410 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
411 struct ext2_inode * inode, int bufsize,
416 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
418 com_err("ext2fs_write_inode", retval,
419 _("while writing inode %lu in %s"), ino, proc);
424 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
425 struct ext2_inode * inode, const char *proc)
429 retval = ext2fs_write_inode(ctx->fs, ino, inode);
431 com_err("ext2fs_write_inode", retval,
432 _("while writing inode %lu in %s"), ino, proc);
438 void mtrace_print(char *mesg)
440 FILE *malloc_get_mallstream();
441 FILE *f = malloc_get_mallstream();
444 fprintf(f, "============= %s\n", mesg);
448 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
451 struct ext2_super_block *sb;
452 io_channel io = NULL;
455 blk_t superblock, ret_sb = 8193;
457 if (fs && fs->super) {
458 ret_sb = (fs->super->s_blocks_per_group +
459 fs->super->s_first_data_block);
461 ctx->superblock = ret_sb;
462 ctx->blocksize = fs->blocksize;
468 if (ctx->blocksize) {
469 ret_sb = ctx->blocksize * 8;
470 if (ctx->blocksize == 1024)
472 ctx->superblock = ret_sb;
475 ctx->superblock = ret_sb;
476 ctx->blocksize = 1024;
479 if (!name || !manager)
482 if (manager->open(name, 0, &io) != 0)
485 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
487 sb = (struct ext2_super_block *) buf;
489 for (blocksize = EXT2_MIN_BLOCK_SIZE;
490 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
491 superblock = blocksize*8;
492 if (blocksize == 1024)
494 io_channel_set_blksize(io, blocksize);
495 if (io_channel_read_blk64(io, superblock,
496 -SUPERBLOCK_SIZE, buf))
498 #ifdef WORDS_BIGENDIAN
499 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
500 ext2fs_swap_super(sb);
502 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
503 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
506 ctx->superblock = superblock;
507 ctx->blocksize = blocksize;
515 io_channel_close(io);
517 ext2fs_free_mem(&buf);
522 * Given a mode, return the ext2 file type
524 int ext2_file_type(unsigned int mode)
526 if (LINUX_S_ISREG(mode))
527 return EXT2_FT_REG_FILE;
529 if (LINUX_S_ISDIR(mode))
532 if (LINUX_S_ISCHR(mode))
533 return EXT2_FT_CHRDEV;
535 if (LINUX_S_ISBLK(mode))
536 return EXT2_FT_BLKDEV;
538 if (LINUX_S_ISLNK(mode))
539 return EXT2_FT_SYMLINK;
541 if (LINUX_S_ISFIFO(mode))
544 if (LINUX_S_ISSOCK(mode))
550 #define STRIDE_LENGTH 8
552 * Helper function which zeros out _num_ blocks starting at _blk_. In
553 * case of an error, the details of the error is returned via _ret_blk_
554 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
555 * success, and an error code on an error.
557 * As a special case, if the first argument is NULL, then it will
558 * attempt to free the static zeroizing buffer. (This is to keep
559 * programs that check for memory leaks happy.)
561 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
562 blk_t *ret_blk, int *ret_count)
564 int j, count, next_update, next_update_incr;
568 /* If fs is null, clean up the static buffer and return */
576 /* Allocate the zeroizing buffer if necessary */
578 buf = malloc(fs->blocksize * STRIDE_LENGTH);
580 com_err("malloc", ENOMEM,
581 _("while allocating zeroizing buffer"));
584 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
586 /* OK, do the write loop */
588 next_update_incr = num / 100;
589 if (next_update_incr < 1)
590 next_update_incr = 1;
591 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
593 if (count > STRIDE_LENGTH)
594 count = STRIDE_LENGTH;
595 retval = io_channel_write_blk64(fs->io, blk, count, buf);
608 * Check to see if a filesystem is in /proc/filesystems.
609 * Returns 1 if found, 0 if not
611 int fs_proc_check(const char *fs_name)
614 char buf[80], *cp, *t;
616 f = fopen("/proc/filesystems", "r");
620 if (!fgets(buf, sizeof(buf), f))
624 while (*cp && !isspace(*cp))
627 while (*cp && isspace(*cp))
629 if ((t = strchr(cp, '\n')) != NULL)
631 if ((t = strchr(cp, '\t')) != NULL)
633 if ((t = strchr(cp, ' ')) != NULL)
635 if (!strcmp(fs_name, cp)) {
645 * Check to see if a filesystem is available as a module
646 * Returns 1 if found, 0 if not
648 int check_for_modules(const char *fs_name)
653 char buf[1024], *cp, *t;
658 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
664 if (!fgets(buf, sizeof(buf), f))
666 if ((cp = strchr(buf, ':')) != NULL)
670 if ((cp = strrchr(buf, '/')) != NULL)
677 if (!strcmp(t, ".ko"))
680 if (!strcmp(cp, fs_name)) {
686 #endif /* __linux__ */
691 * Helper function that does the right thing if write returns a
692 * partial write, or an EGAIN/EINTR error.
694 int write_all(int fd, char *buf, size_t count)
700 ret = write(fd, buf, count);
702 if ((errno == EAGAIN) || (errno == EINTR))