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
20 #define read_a_char() getch()
34 extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
37 #include <sys/resource.h>
39 void fatal_error(e2fsck_t ctx, const char *msg)
42 fprintf (stderr, "e2fsck: %s\n", msg);
43 if (ctx->fs && ctx->fs->io) {
44 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
45 io_channel_flush(ctx->fs->io);
47 fprintf(stderr, "e2fsck: io manager magic bad!\n");
49 ctx->flags |= E2F_FLAG_ABORT;
50 if (ctx->flags & E2F_FLAG_SETJMP_OK)
51 longjmp(ctx->abort_loc, 1);
55 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
56 const char *description)
61 #ifdef DEBUG_ALLOCATE_MEMORY
62 printf("Allocating %d bytes for %s...\n", size, description);
66 sprintf(buf, "Can't allocate %s\n", description);
67 fatal_error(ctx, buf);
73 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
74 const char *str, int len)
84 strncpy(ret, str, len);
92 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
95 int e2fsck_strnlen(const char * s, int count)
99 while (count-- && *cp)
106 static int read_a_char(void)
113 if (e2fsck_global_ctx &&
114 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
127 int ask_yn(const char * string, int def)
131 const char *short_yes = _("yY");
132 const char *short_no = _("nN");
134 #ifdef HAVE_TERMIOS_H
135 struct termios termios, tmp;
137 tcgetattr (0, &termios);
139 tmp.c_lflag &= ~(ICANON | ECHO);
142 tcsetattr (0, TCSANOW, &tmp);
146 defstr = _(_("<y>"));
148 defstr = _(_("<n>"));
150 defstr = _(" (y/n)");
151 printf("%s%s? ", string, defstr);
154 if ((c = read_a_char()) == EOF)
157 #ifdef HAVE_TERMIOS_H
158 tcsetattr (0, TCSANOW, &termios);
160 if (e2fsck_global_ctx &&
161 e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
163 longjmp(e2fsck_global_ctx->abort_loc, 1);
165 puts(_("cancelled!\n"));
168 if (strchr(short_yes, (char) c)) {
172 else if (strchr(short_no, (char) c)) {
176 else if ((c == ' ' || c == '\n') && (def != -1))
183 #ifdef HAVE_TERMIOS_H
184 tcsetattr (0, TCSANOW, &termios);
189 int ask (e2fsck_t ctx, const char * string, int def)
191 if (ctx->options & E2F_OPT_NO) {
192 printf (_("%s? no\n\n"), string);
195 if (ctx->options & E2F_OPT_YES) {
196 printf (_("%s? yes\n\n"), string);
199 if (ctx->options & E2F_OPT_PREEN) {
200 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
203 return ask_yn(string, def);
206 void e2fsck_read_bitmaps(e2fsck_t ctx)
208 ext2_filsys fs = ctx->fs;
212 if (ctx->invalid_bitmaps) {
213 com_err(ctx->program_name, 0,
214 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
219 old_op = ehandler_operation(_("reading inode and block bitmaps"));
220 retval = ext2fs_read_bitmaps(fs);
221 ehandler_operation(old_op);
223 com_err(ctx->program_name, retval,
224 _("while retrying to read bitmaps for %s"),
230 void e2fsck_write_bitmaps(e2fsck_t ctx)
232 ext2_filsys fs = ctx->fs;
236 if (ext2fs_test_bb_dirty(fs)) {
237 old_op = ehandler_operation(_("writing block bitmaps"));
238 retval = ext2fs_write_block_bitmap(fs);
239 ehandler_operation(old_op);
241 com_err(ctx->program_name, retval,
242 _("while retrying to write block bitmaps for %s"),
248 if (ext2fs_test_ib_dirty(fs)) {
249 old_op = ehandler_operation(_("writing inode bitmaps"));
250 retval = ext2fs_write_inode_bitmap(fs);
251 ehandler_operation(old_op);
253 com_err(ctx->program_name, retval,
254 _("while retrying to write inode bitmaps for %s"),
261 void preenhalt(e2fsck_t ctx)
263 ext2_filsys fs = ctx->fs;
265 if (!(ctx->options & E2F_OPT_PREEN))
267 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
268 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
271 fs->super->s_state |= EXT2_ERROR_FS;
272 ext2fs_mark_super_dirty(fs);
275 exit(FSCK_UNCORRECTED);
278 #ifdef RESOURCE_TRACK
279 void init_resource_track(struct resource_track *track, io_channel channel)
281 #ifdef HAVE_GETRUSAGE
284 io_stats io_start = 0;
286 track->brk_start = sbrk(0);
287 gettimeofday(&track->time_start, 0);
288 #ifdef HAVE_GETRUSAGE
290 memset(&r, 0, sizeof(struct rusage));
292 getrusage(RUSAGE_SELF, &r);
293 track->user_start = r.ru_utime;
294 track->system_start = r.ru_stime;
296 track->user_start.tv_sec = track->user_start.tv_usec = 0;
297 track->system_start.tv_sec = track->system_start.tv_usec = 0;
299 track->bytes_read = 0;
300 track->bytes_written = 0;
301 if (channel && channel->manager && channel->manager->get_stats)
302 channel->manager->get_stats(channel, &io_start);
304 track->bytes_read = io_start->bytes_read;
305 track->bytes_written = io_start->bytes_written;
310 #define _INLINE_ __inline__
315 static _INLINE_ float timeval_subtract(struct timeval *tv1,
318 return ((tv1->tv_sec - tv2->tv_sec) +
319 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
322 void print_resource_track(const char *desc, struct resource_track *track,
325 #ifdef HAVE_GETRUSAGE
329 struct mallinfo malloc_info;
331 struct timeval time_end;
333 gettimeofday(&time_end, 0);
336 printf("%s: ", desc);
339 #define kbytes(x) (((x) + 1023) / 1024)
341 malloc_info = mallinfo();
342 printf(_("Memory used: %dk/%dk (%dk/%dk), "),
343 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
344 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
346 printf(_("Memory used: %d, "),
347 (int) (((char *) sbrk(0)) - ((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 %ld 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 %ld 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 %ld 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 %ld 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_blk(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))