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()
26 #define read_a_char() getchar()
36 #include <sys/resource.h>
38 void fatal_error(e2fsck_t ctx, const char *msg)
41 fprintf (stderr, "e2fsck: %s\n", msg);
42 if (ctx->fs && ctx->fs->io) {
43 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
44 io_channel_flush(ctx->fs->io);
46 fprintf(stderr, "e2fsck: io manager magic bad!\n");
48 ctx->flags |= E2F_FLAG_ABORT;
49 if (ctx->flags & E2F_FLAG_SETJMP_OK)
50 longjmp(ctx->abort_loc, 1);
54 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
55 const char *description)
60 #ifdef DEBUG_ALLOCATE_MEMORY
61 printf("Allocating %d bytes for %s...\n", size, description);
65 sprintf(buf, "Can't allocate %s\n", description);
66 fatal_error(ctx, buf);
72 int ask_yn(const char * string, int def)
76 const char *short_yes = _("yY");
77 const char *short_no = _("nN");
80 struct termios termios, tmp;
82 tcgetattr (0, &termios);
84 tmp.c_lflag &= ~(ICANON | ECHO);
87 tcsetattr (0, TCSANOW, &tmp);
96 printf("%s%s? ", string, defstr);
99 if ((c = read_a_char()) == EOF)
101 if (strchr(short_yes, (char) c)) {
105 else if (strchr(short_no, (char) c)) {
109 else if ((c == ' ' || c == '\n') && (def != -1))
116 #ifdef HAVE_TERMIOS_H
117 tcsetattr (0, TCSANOW, &termios);
122 int ask (e2fsck_t ctx, const char * string, int def)
124 if (ctx->options & E2F_OPT_NO) {
125 printf (_("%s? no\n\n"), string);
128 if (ctx->options & E2F_OPT_YES) {
129 printf (_("%s? yes\n\n"), string);
132 if (ctx->options & E2F_OPT_PREEN) {
133 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
136 return ask_yn(string, def);
139 void e2fsck_read_bitmaps(e2fsck_t ctx)
141 ext2_filsys fs = ctx->fs;
144 if (ctx->invalid_bitmaps) {
145 com_err(ctx->program_name, 0,
146 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
151 ehandler_operation(_("reading inode and block bitmaps"));
152 retval = ext2fs_read_bitmaps(fs);
153 ehandler_operation(0);
155 com_err(ctx->program_name, retval,
156 _("while retrying to read bitmaps for %s"),
162 void e2fsck_write_bitmaps(e2fsck_t ctx)
164 ext2_filsys fs = ctx->fs;
167 if (ext2fs_test_bb_dirty(fs)) {
168 ehandler_operation(_("writing block bitmaps"));
169 retval = ext2fs_write_block_bitmap(fs);
170 ehandler_operation(0);
172 com_err(ctx->program_name, retval,
173 _("while retrying to write block bitmaps for %s"),
179 if (ext2fs_test_ib_dirty(fs)) {
180 ehandler_operation(_("writing inode bitmaps"));
181 retval = ext2fs_write_inode_bitmap(fs);
182 ehandler_operation(0);
184 com_err(ctx->program_name, retval,
185 _("while retrying to write inode bitmaps for %s"),
192 void preenhalt(e2fsck_t ctx)
194 ext2_filsys fs = ctx->fs;
196 if (!(ctx->options & E2F_OPT_PREEN))
198 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
199 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
202 fs->super->s_state |= EXT2_ERROR_FS;
203 ext2fs_mark_super_dirty(fs);
206 exit(FSCK_UNCORRECTED);
209 #ifdef RESOURCE_TRACK
210 void init_resource_track(struct resource_track *track)
212 #ifdef HAVE_GETRUSAGE
216 track->brk_start = sbrk(0);
217 gettimeofday(&track->time_start, 0);
218 #ifdef HAVE_GETRUSAGE
220 memset(&r, 0, sizeof(struct rusage));
222 getrusage(RUSAGE_SELF, &r);
223 track->user_start = r.ru_utime;
224 track->system_start = r.ru_stime;
226 track->user_start.tv_sec = track->user_start.tv_usec = 0;
227 track->system_start.tv_sec = track->system_start.tv_usec = 0;
232 #define _INLINE_ __inline__
237 static _INLINE_ float timeval_subtract(struct timeval *tv1,
240 return ((tv1->tv_sec - tv2->tv_sec) +
241 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
244 void print_resource_track(const char *desc, struct resource_track *track)
246 #ifdef HAVE_GETRUSAGE
250 struct mallinfo malloc_info;
252 struct timeval time_end;
254 gettimeofday(&time_end, 0);
257 printf("%s: ", desc);
260 #define kbytes(x) (((x) + 1023) / 1024)
262 malloc_info = mallinfo();
263 printf(_("Memory used: %dk/%dk (%dk/%dk), "),
264 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
265 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
267 printf(_("Memory used: %d, "),
268 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
270 #ifdef HAVE_GETRUSAGE
271 getrusage(RUSAGE_SELF, &r);
273 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
274 timeval_subtract(&time_end, &track->time_start),
275 timeval_subtract(&r.ru_utime, &track->user_start),
276 timeval_subtract(&r.ru_stime, &track->system_start));
278 printf(_("elapsed time: %6.3f\n"),
279 timeval_subtract(&time_end, &track->time_start));
282 #endif /* RESOURCE_TRACK */
284 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
285 struct ext2_inode * inode, const char *proc)
289 retval = ext2fs_read_inode(ctx->fs, ino, inode);
291 com_err("ext2fs_read_inode", retval,
292 _("while reading inode %ld in %s"), ino, proc);
297 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
298 struct ext2_inode * inode, const char *proc)
302 retval = ext2fs_write_inode(ctx->fs, ino, inode);
304 com_err("ext2fs_write_inode", retval,
305 _("while writing inode %ld in %s"), ino, proc);
311 void mtrace_print(char *mesg)
313 FILE *malloc_get_mallstream();
314 FILE *f = malloc_get_mallstream();
317 fprintf(f, "============= %s\n", mesg);
321 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
324 struct ext2_super_block *sb;
325 io_channel io = NULL;
328 blk_t superblock, ret_sb = 8193;
330 if (fs && fs->super) {
331 ret_sb = (fs->super->s_blocks_per_group +
332 fs->super->s_first_data_block);
334 ctx->superblock = ret_sb;
335 ctx->blocksize = fs->blocksize;
341 if (ctx->blocksize) {
342 ret_sb = ctx->blocksize * 8;
343 if (ctx->blocksize == 1024)
345 ctx->superblock = ret_sb;
348 ctx->superblock = ret_sb;
349 ctx->blocksize = 1024;
352 if (!name || !manager)
355 if (manager->open(name, 0, &io) != 0)
358 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
360 sb = (struct ext2_super_block *) buf;
362 for (blocksize = EXT2_MIN_BLOCK_SIZE;
363 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
364 superblock = blocksize*8;
365 if (blocksize == 1024)
367 io_channel_set_blksize(io, blocksize);
368 if (io_channel_read_blk(io, superblock,
369 -SUPERBLOCK_SIZE, buf))
371 #ifdef EXT2FS_ENABLE_SWAPFS
372 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
373 ext2fs_swap_super(sb);
375 if (sb->s_magic == EXT2_SUPER_MAGIC) {
376 ret_sb = ctx->superblock = superblock;
377 ctx->blocksize = blocksize;
384 io_channel_close(io);
386 ext2fs_free_mem(&buf);
391 * Given a mode, return the ext2 file type
393 int ext2_file_type(unsigned int mode)
395 if (LINUX_S_ISREG(mode))
396 return EXT2_FT_REG_FILE;
398 if (LINUX_S_ISDIR(mode))
401 if (LINUX_S_ISCHR(mode))
402 return EXT2_FT_CHRDEV;
404 if (LINUX_S_ISBLK(mode))
405 return EXT2_FT_BLKDEV;
407 if (LINUX_S_ISLNK(mode))
408 return EXT2_FT_SYMLINK;
410 if (LINUX_S_ISFIFO(mode))
413 if (LINUX_S_ISSOCK(mode))