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()
38 extern e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
41 #include <sys/resource.h>
43 void fatal_error(e2fsck_t ctx, const char *msg)
46 fprintf (stderr, "e2fsck: %s\n", msg);
47 if (ctx->fs && ctx->fs->io) {
48 if (ctx->fs->io->magic == EXT2_ET_MAGIC_IO_CHANNEL)
49 io_channel_flush(ctx->fs->io);
51 fprintf(stderr, "e2fsck: io manager magic bad!\n");
53 ctx->flags |= E2F_FLAG_ABORT;
54 if (ctx->flags & E2F_FLAG_SETJMP_OK)
55 longjmp(ctx->abort_loc, 1);
59 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
60 const char *description)
65 #ifdef DEBUG_ALLOCATE_MEMORY
66 printf("Allocating %d bytes for %s...\n", size, description);
70 sprintf(buf, "Can't allocate %s\n", description);
71 fatal_error(ctx, buf);
77 char *string_copy(e2fsck_t ctx EXT2FS_ATTR((unused)),
78 const char *str, int len)
88 strncpy(ret, str, len);
96 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
99 int e2fsck_strnlen(const char * s, int count)
103 while (count-- && *cp)
110 static int read_a_char(void)
117 if (e2fsck_global_ctx &&
118 (e2fsck_global_ctx->flags & E2F_FLAG_CANCEL)) {
131 int ask_yn(const char * string, int def)
135 const char *short_yes = _("yY");
136 const char *short_no = _("nN");
138 #ifdef HAVE_TERMIOS_H
139 struct termios termios, tmp;
141 tcgetattr (0, &termios);
143 tmp.c_lflag &= ~(ICANON | ECHO);
146 tcsetattr (0, TCSANOW, &tmp);
150 defstr = _(_("<y>"));
152 defstr = _(_("<n>"));
154 defstr = _(" (y/n)");
155 printf("%s%s? ", string, defstr);
158 if ((c = read_a_char()) == EOF)
161 #ifdef HAVE_TERMIOS_H
162 tcsetattr (0, TCSANOW, &termios);
164 if (e2fsck_global_ctx &&
165 e2fsck_global_ctx->flags & E2F_FLAG_SETJMP_OK) {
167 longjmp(e2fsck_global_ctx->abort_loc, 1);
169 puts(_("cancelled!\n"));
172 if (strchr(short_yes, (char) c)) {
176 else if (strchr(short_no, (char) c)) {
180 else if ((c == ' ' || c == '\n') && (def != -1))
187 #ifdef HAVE_TERMIOS_H
188 tcsetattr (0, TCSANOW, &termios);
193 int ask (e2fsck_t ctx, const char * string, int def)
195 if (ctx->options & E2F_OPT_NO) {
196 printf (_("%s? no\n\n"), string);
199 if (ctx->options & E2F_OPT_YES) {
200 printf (_("%s? yes\n\n"), string);
203 if (ctx->options & E2F_OPT_PREEN) {
204 printf ("%s? %s\n\n", string, def ? _("yes") : _("no"));
207 return ask_yn(string, def);
210 void e2fsck_read_bitmaps(e2fsck_t ctx)
212 ext2_filsys fs = ctx->fs;
216 if (ctx->invalid_bitmaps) {
217 com_err(ctx->program_name, 0,
218 _("e2fsck_read_bitmaps: illegal bitmap block(s) for %s"),
223 old_op = ehandler_operation(_("reading inode and block bitmaps"));
224 retval = ext2fs_read_bitmaps(fs);
225 ehandler_operation(old_op);
227 com_err(ctx->program_name, retval,
228 _("while retrying to read bitmaps for %s"),
234 void e2fsck_write_bitmaps(e2fsck_t ctx)
236 ext2_filsys fs = ctx->fs;
240 old_op = ehandler_operation(_("writing block and inode bitmaps"));
241 retval = ext2fs_write_bitmaps(fs);
242 ehandler_operation(old_op);
244 com_err(ctx->program_name, retval,
245 _("while rewriting block and inode bitmaps for %s"),
251 void preenhalt(e2fsck_t ctx)
253 ext2_filsys fs = ctx->fs;
255 if (!(ctx->options & E2F_OPT_PREEN))
257 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
258 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
260 ctx->flags |= E2F_FLAG_EXITING;
262 fs->super->s_state |= EXT2_ERROR_FS;
263 ext2fs_mark_super_dirty(fs);
266 exit(FSCK_UNCORRECTED);
269 #ifdef RESOURCE_TRACK
270 void init_resource_track(struct resource_track *track, io_channel channel)
272 #ifdef HAVE_GETRUSAGE
275 io_stats io_start = 0;
277 track->brk_start = sbrk(0);
278 gettimeofday(&track->time_start, 0);
279 #ifdef HAVE_GETRUSAGE
281 memset(&r, 0, sizeof(struct rusage));
283 getrusage(RUSAGE_SELF, &r);
284 track->user_start = r.ru_utime;
285 track->system_start = r.ru_stime;
287 track->user_start.tv_sec = track->user_start.tv_usec = 0;
288 track->system_start.tv_sec = track->system_start.tv_usec = 0;
290 track->bytes_read = 0;
291 track->bytes_written = 0;
292 if (channel && channel->manager && channel->manager->get_stats)
293 channel->manager->get_stats(channel, &io_start);
295 track->bytes_read = io_start->bytes_read;
296 track->bytes_written = io_start->bytes_written;
301 #define _INLINE_ __inline__
306 static _INLINE_ float timeval_subtract(struct timeval *tv1,
309 return ((tv1->tv_sec - tv2->tv_sec) +
310 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
313 void print_resource_track(const char *desc, struct resource_track *track,
316 #ifdef HAVE_GETRUSAGE
320 struct mallinfo malloc_info;
322 struct timeval time_end;
324 gettimeofday(&time_end, 0);
327 printf("%s: ", desc);
330 #define kbytes(x) (((x) + 1023) / 1024)
332 malloc_info = mallinfo();
333 printf(_("Memory used: %dk/%dk (%dk/%dk), "),
334 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
335 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
337 printf(_("Memory used: %d, "),
338 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
340 #ifdef HAVE_GETRUSAGE
341 getrusage(RUSAGE_SELF, &r);
343 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
344 timeval_subtract(&time_end, &track->time_start),
345 timeval_subtract(&r.ru_utime, &track->user_start),
346 timeval_subtract(&r.ru_stime, &track->system_start));
348 printf(_("elapsed time: %6.3f\n"),
349 timeval_subtract(&time_end, &track->time_start));
351 #define mbytes(x) (((x) + 1048575) / 1048576)
352 if (channel && channel->manager && channel->manager->get_stats) {
354 unsigned long long bytes_read = 0;
355 unsigned long long bytes_written = 0;
358 printf("%s: ", desc);
360 channel->manager->get_stats(channel, &delta);
362 bytes_read = delta->bytes_read - track->bytes_read;
363 bytes_written = delta->bytes_written -
364 track->bytes_written;
366 printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
367 mbytes(bytes_read), mbytes(bytes_written),
368 (double)mbytes(bytes_read + bytes_written) /
369 timeval_subtract(&time_end, &track->time_start));
372 #endif /* RESOURCE_TRACK */
374 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
375 struct ext2_inode * inode, const char *proc)
379 retval = ext2fs_read_inode(ctx->fs, ino, inode);
381 com_err("ext2fs_read_inode", retval,
382 _("while reading inode %ld in %s"), ino, proc);
387 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
388 struct ext2_inode *inode, int bufsize,
393 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
395 com_err("ext2fs_read_inode_full", retval,
396 _("while reading inode %ld in %s"), ino, proc);
401 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
402 struct ext2_inode * inode, int bufsize,
407 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
409 com_err("ext2fs_write_inode", retval,
410 _("while writing inode %ld in %s"), ino, proc);
415 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
416 struct ext2_inode * inode, const char *proc)
420 retval = ext2fs_write_inode(ctx->fs, ino, inode);
422 com_err("ext2fs_write_inode", retval,
423 _("while writing inode %ld in %s"), ino, proc);
429 void mtrace_print(char *mesg)
431 FILE *malloc_get_mallstream();
432 FILE *f = malloc_get_mallstream();
435 fprintf(f, "============= %s\n", mesg);
439 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
442 struct ext2_super_block *sb;
443 io_channel io = NULL;
446 blk_t superblock, ret_sb = 8193;
448 if (fs && fs->super) {
449 ret_sb = (fs->super->s_blocks_per_group +
450 fs->super->s_first_data_block);
452 ctx->superblock = ret_sb;
453 ctx->blocksize = fs->blocksize;
459 if (ctx->blocksize) {
460 ret_sb = ctx->blocksize * 8;
461 if (ctx->blocksize == 1024)
463 ctx->superblock = ret_sb;
466 ctx->superblock = ret_sb;
467 ctx->blocksize = 1024;
470 if (!name || !manager)
473 if (manager->open(name, 0, &io) != 0)
476 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
478 sb = (struct ext2_super_block *) buf;
480 for (blocksize = EXT2_MIN_BLOCK_SIZE;
481 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
482 superblock = blocksize*8;
483 if (blocksize == 1024)
485 io_channel_set_blksize(io, blocksize);
486 if (io_channel_read_blk(io, superblock,
487 -SUPERBLOCK_SIZE, buf))
489 #ifdef WORDS_BIGENDIAN
490 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
491 ext2fs_swap_super(sb);
493 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
494 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
497 ctx->superblock = superblock;
498 ctx->blocksize = blocksize;
506 io_channel_close(io);
508 ext2fs_free_mem(&buf);
513 * Given a mode, return the ext2 file type
515 int ext2_file_type(unsigned int mode)
517 if (LINUX_S_ISREG(mode))
518 return EXT2_FT_REG_FILE;
520 if (LINUX_S_ISDIR(mode))
523 if (LINUX_S_ISCHR(mode))
524 return EXT2_FT_CHRDEV;
526 if (LINUX_S_ISBLK(mode))
527 return EXT2_FT_BLKDEV;
529 if (LINUX_S_ISLNK(mode))
530 return EXT2_FT_SYMLINK;
532 if (LINUX_S_ISFIFO(mode))
535 if (LINUX_S_ISSOCK(mode))
541 #define STRIDE_LENGTH 8
543 * Helper function which zeros out _num_ blocks starting at _blk_. In
544 * case of an error, the details of the error is returned via _ret_blk_
545 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
546 * success, and an error code on an error.
548 * As a special case, if the first argument is NULL, then it will
549 * attempt to free the static zeroizing buffer. (This is to keep
550 * programs that check for memory leaks happy.)
552 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
553 blk_t *ret_blk, int *ret_count)
555 int j, count, next_update, next_update_incr;
559 /* If fs is null, clean up the static buffer and return */
567 /* Allocate the zeroizing buffer if necessary */
569 buf = malloc(fs->blocksize * STRIDE_LENGTH);
571 com_err("malloc", ENOMEM,
572 _("while allocating zeroizing buffer"));
575 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
577 /* OK, do the write loop */
579 next_update_incr = num / 100;
580 if (next_update_incr < 1)
581 next_update_incr = 1;
582 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
584 if (count > STRIDE_LENGTH)
585 count = STRIDE_LENGTH;
586 retval = io_channel_write_blk(fs->io, blk, count, buf);