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 == ' ' || 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(const char *desc, struct resource_track *track,
319 #ifdef HAVE_GETRUSAGE
323 struct mallinfo malloc_info;
325 struct timeval time_end;
327 gettimeofday(&time_end, 0);
330 printf("%s: ", desc);
333 #define kbytes(x) (((unsigned long)(x) + 1023) / 1024)
335 malloc_info = mallinfo();
336 printf(_("Memory used: %luk/%luk (%luk/%luk), "),
337 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
338 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
340 printf(_("Memory used: %lu, "),
341 (unsigned long) (((char *) sbrk(0)) -
342 ((char *) track->brk_start)));
344 #ifdef HAVE_GETRUSAGE
345 getrusage(RUSAGE_SELF, &r);
347 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
348 timeval_subtract(&time_end, &track->time_start),
349 timeval_subtract(&r.ru_utime, &track->user_start),
350 timeval_subtract(&r.ru_stime, &track->system_start));
352 printf(_("elapsed time: %6.3f\n"),
353 timeval_subtract(&time_end, &track->time_start));
355 #define mbytes(x) (((x) + 1048575) / 1048576)
356 if (channel && channel->manager && channel->manager->get_stats) {
358 unsigned long long bytes_read = 0;
359 unsigned long long bytes_written = 0;
362 printf("%s: ", desc);
364 channel->manager->get_stats(channel, &delta);
366 bytes_read = delta->bytes_read - track->bytes_read;
367 bytes_written = delta->bytes_written -
368 track->bytes_written;
370 printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
371 mbytes(bytes_read), mbytes(bytes_written),
372 (double)mbytes(bytes_read + bytes_written) /
373 timeval_subtract(&time_end, &track->time_start));
376 #endif /* RESOURCE_TRACK */
378 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
379 struct ext2_inode * inode, const char *proc)
383 retval = ext2fs_read_inode(ctx->fs, ino, inode);
385 com_err("ext2fs_read_inode", retval,
386 _("while reading inode %lu in %s"), ino, proc);
391 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
392 struct ext2_inode *inode, int bufsize,
397 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
399 com_err("ext2fs_read_inode_full", retval,
400 _("while reading inode %lu in %s"), ino, proc);
405 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
406 struct ext2_inode * inode, int bufsize,
411 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
413 com_err("ext2fs_write_inode", retval,
414 _("while writing inode %lu in %s"), ino, proc);
419 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
420 struct ext2_inode * inode, const char *proc)
424 retval = ext2fs_write_inode(ctx->fs, ino, inode);
426 com_err("ext2fs_write_inode", retval,
427 _("while writing inode %lu in %s"), ino, proc);
433 void mtrace_print(char *mesg)
435 FILE *malloc_get_mallstream();
436 FILE *f = malloc_get_mallstream();
439 fprintf(f, "============= %s\n", mesg);
443 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
446 struct ext2_super_block *sb;
447 io_channel io = NULL;
450 blk_t superblock, ret_sb = 8193;
452 if (fs && fs->super) {
453 ret_sb = (fs->super->s_blocks_per_group +
454 fs->super->s_first_data_block);
456 ctx->superblock = ret_sb;
457 ctx->blocksize = fs->blocksize;
463 if (ctx->blocksize) {
464 ret_sb = ctx->blocksize * 8;
465 if (ctx->blocksize == 1024)
467 ctx->superblock = ret_sb;
470 ctx->superblock = ret_sb;
471 ctx->blocksize = 1024;
474 if (!name || !manager)
477 if (manager->open(name, 0, &io) != 0)
480 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
482 sb = (struct ext2_super_block *) buf;
484 for (blocksize = EXT2_MIN_BLOCK_SIZE;
485 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
486 superblock = blocksize*8;
487 if (blocksize == 1024)
489 io_channel_set_blksize(io, blocksize);
490 if (io_channel_read_blk(io, superblock,
491 -SUPERBLOCK_SIZE, buf))
493 #ifdef WORDS_BIGENDIAN
494 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
495 ext2fs_swap_super(sb);
497 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
498 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
501 ctx->superblock = superblock;
502 ctx->blocksize = blocksize;
510 io_channel_close(io);
512 ext2fs_free_mem(&buf);
517 * Given a mode, return the ext2 file type
519 int ext2_file_type(unsigned int mode)
521 if (LINUX_S_ISREG(mode))
522 return EXT2_FT_REG_FILE;
524 if (LINUX_S_ISDIR(mode))
527 if (LINUX_S_ISCHR(mode))
528 return EXT2_FT_CHRDEV;
530 if (LINUX_S_ISBLK(mode))
531 return EXT2_FT_BLKDEV;
533 if (LINUX_S_ISLNK(mode))
534 return EXT2_FT_SYMLINK;
536 if (LINUX_S_ISFIFO(mode))
539 if (LINUX_S_ISSOCK(mode))
545 #define STRIDE_LENGTH 8
547 * Helper function which zeros out _num_ blocks starting at _blk_. In
548 * case of an error, the details of the error is returned via _ret_blk_
549 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
550 * success, and an error code on an error.
552 * As a special case, if the first argument is NULL, then it will
553 * attempt to free the static zeroizing buffer. (This is to keep
554 * programs that check for memory leaks happy.)
556 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
557 blk_t *ret_blk, int *ret_count)
559 int j, count, next_update, next_update_incr;
563 /* If fs is null, clean up the static buffer and return */
571 /* Allocate the zeroizing buffer if necessary */
573 buf = malloc(fs->blocksize * STRIDE_LENGTH);
575 com_err("malloc", ENOMEM,
576 _("while allocating zeroizing buffer"));
579 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
581 /* OK, do the write loop */
583 next_update_incr = num / 100;
584 if (next_update_incr < 1)
585 next_update_incr = 1;
586 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
588 if (count > STRIDE_LENGTH)
589 count = STRIDE_LENGTH;
590 retval = io_channel_write_blk(fs->io, blk, count, buf);
603 * Check to see if a filesystem is in /proc/filesystems.
604 * Returns 1 if found, 0 if not
606 int fs_proc_check(const char *fs_name)
609 char buf[80], *cp, *t;
611 f = fopen("/proc/filesystems", "r");
615 if (!fgets(buf, sizeof(buf), f))
619 while (*cp && !isspace(*cp))
622 while (*cp && isspace(*cp))
624 if ((t = strchr(cp, '\n')) != NULL)
626 if ((t = strchr(cp, '\t')) != NULL)
628 if ((t = strchr(cp, ' ')) != NULL)
630 if (!strcmp(fs_name, cp)) {
640 * Check to see if a filesystem is available as a module
641 * Returns 1 if found, 0 if not
643 int check_for_modules(const char *fs_name)
648 char buf[1024], *cp, *t;
653 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
659 if (!fgets(buf, sizeof(buf), f))
661 if ((cp = strchr(buf, ':')) != NULL)
665 if ((cp = strrchr(buf, '/')) != NULL)
672 if (!strcmp(t, ".ko"))
675 if (!strcmp(cp, fs_name)) {
681 #endif /* __linux__ */
686 * Helper function that does the right thing if write returns a
687 * partial write, or an EGAIN/EINTR error.
689 int write_all(int fd, char *buf, size_t count)
695 ret = write(fd, buf, count);
697 if ((errno == EAGAIN) || (errno == EINTR))