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 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);
81 if (e2fsck_global_ctx &&
82 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
95 int ask_yn(const char * string, int def)
99 const char *short_yes = _("yY");
100 const char *short_no = _("nN");
102 #ifdef HAVE_TERMIOS_H
103 struct termios termios, tmp;
105 tcgetattr (0, &termios);
107 tmp.c_lflag &= ~(ICANON | ECHO);
110 tcsetattr (0, TCSANOW, &tmp);
114 defstr = _(_("<y>"));
116 defstr = _(_("<n>"));
118 defstr = _(" (y/n)");
119 printf("%s%s? ", string, defstr);
122 if ((c = read_a_char()) == EOF)
125 #ifdef HAVE_TERMIOS_H
126 tcsetattr (0, TCSANOW, &termios);
128 if (e2fsck_global_ctx &&
129 e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
131 longjmp(e2fsck_global_ctx->abort_loc, 1);
133 puts(_("cancelled!\n"));
136 if (strchr(short_yes, (char) c)) {
140 else if (strchr(short_no, (char) c)) {
144 else if ((c == ' ' || c == '\n') && (def != -1))
151 #ifdef HAVE_TERMIOS_H
152 tcsetattr (0, TCSANOW, &termios);
157 int ask (e2fsck_t ctx, const char * string, int def)
159 if (ctx->options & E2F_OPT_NO) {
160 printf (_("%s? no\n\n"), string);
163 if (ctx->options & E2F_OPT_YES) {
164 printf (_("%s? yes\n\n"), string);
167 if (ctx->options & E2F_OPT_PREEN) {
168 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
171 return ask_yn(string, def);
174 void e2fsck_read_bitmaps(e2fsck_t ctx)
176 ext2_filsys fs = ctx->fs;
179 if (ctx->invalid_bitmaps) {
180 com_err(ctx->program_name, 0,
181 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
186 ehandler_operation(_("reading inode and block bitmaps"));
187 retval = ext2fs_read_bitmaps(fs);
188 ehandler_operation(0);
190 com_err(ctx->program_name, retval,
191 _("while retrying to read bitmaps for %s"),
197 void e2fsck_write_bitmaps(e2fsck_t ctx)
199 ext2_filsys fs = ctx->fs;
202 if (ext2fs_test_bb_dirty(fs)) {
203 ehandler_operation(_("writing block bitmaps"));
204 retval = ext2fs_write_block_bitmap(fs);
205 ehandler_operation(0);
207 com_err(ctx->program_name, retval,
208 _("while retrying to write block bitmaps for %s"),
214 if (ext2fs_test_ib_dirty(fs)) {
215 ehandler_operation(_("writing inode bitmaps"));
216 retval = ext2fs_write_inode_bitmap(fs);
217 ehandler_operation(0);
219 com_err(ctx->program_name, retval,
220 _("while retrying to write inode bitmaps for %s"),
227 void preenhalt(e2fsck_t ctx)
229 ext2_filsys fs = ctx->fs;
231 if (!(ctx->options & E2F_OPT_PREEN))
233 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
234 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
237 fs->super->s_state |= EXT2_ERROR_FS;
238 ext2fs_mark_super_dirty(fs);
241 exit(FSCK_UNCORRECTED);
244 #ifdef RESOURCE_TRACK
245 void init_resource_track(struct resource_track *track)
247 #ifdef HAVE_GETRUSAGE
251 track->brk_start = sbrk(0);
252 gettimeofday(&track->time_start, 0);
253 #ifdef HAVE_GETRUSAGE
255 memset(&r, 0, sizeof(struct rusage));
257 getrusage(RUSAGE_SELF, &r);
258 track->user_start = r.ru_utime;
259 track->system_start = r.ru_stime;
261 track->user_start.tv_sec = track->user_start.tv_usec = 0;
262 track->system_start.tv_sec = track->system_start.tv_usec = 0;
267 #define _INLINE_ __inline__
272 static _INLINE_ float timeval_subtract(struct timeval *tv1,
275 return ((tv1->tv_sec - tv2->tv_sec) +
276 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
279 void print_resource_track(const char *desc, struct resource_track *track)
281 #ifdef HAVE_GETRUSAGE
285 struct mallinfo malloc_info;
287 struct timeval time_end;
289 gettimeofday(&time_end, 0);
292 printf("%s: ", desc);
295 #define kbytes(x) (((x) + 1023) / 1024)
297 malloc_info = mallinfo();
298 printf(_("Memory used: %dk/%dk (%dk/%dk), "),
299 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
300 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
302 printf(_("Memory used: %d, "),
303 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
305 #ifdef HAVE_GETRUSAGE
306 getrusage(RUSAGE_SELF, &r);
308 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
309 timeval_subtract(&time_end, &track->time_start),
310 timeval_subtract(&r.ru_utime, &track->user_start),
311 timeval_subtract(&r.ru_stime, &track->system_start));
313 printf(_("elapsed time: %6.3f\n"),
314 timeval_subtract(&time_end, &track->time_start));
317 #endif /* RESOURCE_TRACK */
319 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
320 struct ext2_inode * inode, const char *proc)
324 retval = ext2fs_read_inode(ctx->fs, ino, inode);
326 com_err("ext2fs_read_inode", retval,
327 _("while reading inode %ld in %s"), ino, proc);
332 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
333 struct ext2_inode * inode, const char *proc)
337 retval = ext2fs_write_inode(ctx->fs, ino, inode);
339 com_err("ext2fs_write_inode", retval,
340 _("while writing inode %ld in %s"), ino, proc);
346 void mtrace_print(char *mesg)
348 FILE *malloc_get_mallstream();
349 FILE *f = malloc_get_mallstream();
352 fprintf(f, "============= %s\n", mesg);
356 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
359 struct ext2_super_block *sb;
360 io_channel io = NULL;
363 blk_t superblock, ret_sb = 8193;
365 if (fs && fs->super) {
366 ret_sb = (fs->super->s_blocks_per_group +
367 fs->super->s_first_data_block);
369 ctx->superblock = ret_sb;
370 ctx->blocksize = fs->blocksize;
376 if (ctx->blocksize) {
377 ret_sb = ctx->blocksize * 8;
378 if (ctx->blocksize == 1024)
380 ctx->superblock = ret_sb;
383 ctx->superblock = ret_sb;
384 ctx->blocksize = 1024;
387 if (!name || !manager)
390 if (manager->open(name, 0, &io) != 0)
393 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
395 sb = (struct ext2_super_block *) buf;
397 for (blocksize = EXT2_MIN_BLOCK_SIZE;
398 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
399 superblock = blocksize*8;
400 if (blocksize == 1024)
402 io_channel_set_blksize(io, blocksize);
403 if (io_channel_read_blk(io, superblock,
404 -SUPERBLOCK_SIZE, buf))
406 #ifdef EXT2FS_ENABLE_SWAPFS
407 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
408 ext2fs_swap_super(sb);
410 if (sb->s_magic == EXT2_SUPER_MAGIC) {
411 ret_sb = ctx->superblock = superblock;
412 ctx->blocksize = blocksize;
419 io_channel_close(io);
421 ext2fs_free_mem(&buf);
426 * Given a mode, return the ext2 file type
428 int ext2_file_type(unsigned int mode)
430 if (LINUX_S_ISREG(mode))
431 return EXT2_FT_REG_FILE;
433 if (LINUX_S_ISDIR(mode))
436 if (LINUX_S_ISCHR(mode))
437 return EXT2_FT_CHRDEV;
439 if (LINUX_S_ISBLK(mode))
440 return EXT2_FT_BLKDEV;
442 if (LINUX_S_ISLNK(mode))
443 return EXT2_FT_SYMLINK;
445 if (LINUX_S_ISFIFO(mode))
448 if (LINUX_S_ISSOCK(mode))