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 if (ext2fs_test_bb_dirty(fs)) {
241 old_op = ehandler_operation(_("writing block bitmaps"));
242 retval = ext2fs_write_block_bitmap(fs);
243 ehandler_operation(old_op);
245 com_err(ctx->program_name, retval,
246 _("while retrying to write block bitmaps for %s"),
252 if (ext2fs_test_ib_dirty(fs)) {
253 old_op = ehandler_operation(_("writing inode bitmaps"));
254 retval = ext2fs_write_inode_bitmap(fs);
255 ehandler_operation(old_op);
257 com_err(ctx->program_name, retval,
258 _("while retrying to write inode bitmaps for %s"),
265 void preenhalt(e2fsck_t ctx)
267 ext2_filsys fs = ctx->fs;
269 if (!(ctx->options & E2F_OPT_PREEN))
271 fprintf(stderr, _("\n\n%s: UNEXPECTED INCONSISTENCY; "
272 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n"),
275 fs->super->s_state |= EXT2_ERROR_FS;
276 ext2fs_mark_super_dirty(fs);
279 exit(FSCK_UNCORRECTED);
282 #ifdef RESOURCE_TRACK
283 void init_resource_track(struct resource_track *track, io_channel channel)
285 #ifdef HAVE_GETRUSAGE
288 io_stats io_start = 0;
290 track->brk_start = sbrk(0);
291 gettimeofday(&track->time_start, 0);
292 #ifdef HAVE_GETRUSAGE
294 memset(&r, 0, sizeof(struct rusage));
296 getrusage(RUSAGE_SELF, &r);
297 track->user_start = r.ru_utime;
298 track->system_start = r.ru_stime;
300 track->user_start.tv_sec = track->user_start.tv_usec = 0;
301 track->system_start.tv_sec = track->system_start.tv_usec = 0;
303 track->bytes_read = 0;
304 track->bytes_written = 0;
305 if (channel && channel->manager && channel->manager->get_stats)
306 channel->manager->get_stats(channel, &io_start);
308 track->bytes_read = io_start->bytes_read;
309 track->bytes_written = io_start->bytes_written;
314 #define _INLINE_ __inline__
319 static _INLINE_ float timeval_subtract(struct timeval *tv1,
322 return ((tv1->tv_sec - tv2->tv_sec) +
323 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
326 void print_resource_track(const char *desc, struct resource_track *track,
329 #ifdef HAVE_GETRUSAGE
333 struct mallinfo malloc_info;
335 struct timeval time_end;
337 gettimeofday(&time_end, 0);
340 printf("%s: ", desc);
343 #define kbytes(x) (((x) + 1023) / 1024)
345 malloc_info = mallinfo();
346 printf(_("Memory used: %dk/%dk (%dk/%dk), "),
347 kbytes(malloc_info.arena), kbytes(malloc_info.hblkhd),
348 kbytes(malloc_info.uordblks), kbytes(malloc_info.fordblks));
350 printf(_("Memory used: %d, "),
351 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)));
353 #ifdef HAVE_GETRUSAGE
354 getrusage(RUSAGE_SELF, &r);
356 printf(_("time: %5.2f/%5.2f/%5.2f\n"),
357 timeval_subtract(&time_end, &track->time_start),
358 timeval_subtract(&r.ru_utime, &track->user_start),
359 timeval_subtract(&r.ru_stime, &track->system_start));
361 printf(_("elapsed time: %6.3f\n"),
362 timeval_subtract(&time_end, &track->time_start));
364 #define mbytes(x) (((x) + 1048575) / 1048576)
365 if (channel && channel->manager && channel->manager->get_stats) {
367 unsigned long long bytes_read = 0;
368 unsigned long long bytes_written = 0;
371 printf("%s: ", desc);
373 channel->manager->get_stats(channel, &delta);
375 bytes_read = delta->bytes_read - track->bytes_read;
376 bytes_written = delta->bytes_written -
377 track->bytes_written;
379 printf("I/O read: %lluMB, write: %lluMB, rate: %.2fMB/s\n",
380 mbytes(bytes_read), mbytes(bytes_written),
381 (double)mbytes(bytes_read + bytes_written) /
382 timeval_subtract(&time_end, &track->time_start));
385 #endif /* RESOURCE_TRACK */
387 void e2fsck_read_inode(e2fsck_t ctx, unsigned long ino,
388 struct ext2_inode * inode, const char *proc)
392 retval = ext2fs_read_inode(ctx->fs, ino, inode);
394 com_err("ext2fs_read_inode", retval,
395 _("while reading inode %ld in %s"), ino, proc);
400 void e2fsck_read_inode_full(e2fsck_t ctx, unsigned long ino,
401 struct ext2_inode *inode, int bufsize,
406 retval = ext2fs_read_inode_full(ctx->fs, ino, inode, bufsize);
408 com_err("ext2fs_read_inode_full", retval,
409 _("while reading inode %ld in %s"), ino, proc);
414 extern void e2fsck_write_inode_full(e2fsck_t ctx, unsigned long ino,
415 struct ext2_inode * inode, int bufsize,
420 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, bufsize);
422 com_err("ext2fs_write_inode", retval,
423 _("while writing inode %ld in %s"), ino, proc);
428 extern void e2fsck_write_inode(e2fsck_t ctx, unsigned long ino,
429 struct ext2_inode * inode, const char *proc)
433 retval = ext2fs_write_inode(ctx->fs, ino, inode);
435 com_err("ext2fs_write_inode", retval,
436 _("while writing inode %ld in %s"), ino, proc);
442 void mtrace_print(char *mesg)
444 FILE *malloc_get_mallstream();
445 FILE *f = malloc_get_mallstream();
448 fprintf(f, "============= %s\n", mesg);
452 blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name,
455 struct ext2_super_block *sb;
456 io_channel io = NULL;
459 blk_t superblock, ret_sb = 8193;
461 if (fs && fs->super) {
462 ret_sb = (fs->super->s_blocks_per_group +
463 fs->super->s_first_data_block);
465 ctx->superblock = ret_sb;
466 ctx->blocksize = fs->blocksize;
472 if (ctx->blocksize) {
473 ret_sb = ctx->blocksize * 8;
474 if (ctx->blocksize == 1024)
476 ctx->superblock = ret_sb;
479 ctx->superblock = ret_sb;
480 ctx->blocksize = 1024;
483 if (!name || !manager)
486 if (manager->open(name, 0, &io) != 0)
489 if (ext2fs_get_mem(SUPERBLOCK_SIZE, &buf))
491 sb = (struct ext2_super_block *) buf;
493 for (blocksize = EXT2_MIN_BLOCK_SIZE;
494 blocksize <= EXT2_MAX_BLOCK_SIZE ; blocksize *= 2) {
495 superblock = blocksize*8;
496 if (blocksize == 1024)
498 io_channel_set_blksize(io, blocksize);
499 if (io_channel_read_blk(io, superblock,
500 -SUPERBLOCK_SIZE, buf))
502 #ifdef WORDS_BIGENDIAN
503 if (sb->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
504 ext2fs_swap_super(sb);
506 if ((sb->s_magic == EXT2_SUPER_MAGIC) &&
507 (EXT2_BLOCK_SIZE(sb) == blocksize)) {
510 ctx->superblock = superblock;
511 ctx->blocksize = blocksize;
519 io_channel_close(io);
521 ext2fs_free_mem(&buf);
526 * Given a mode, return the ext2 file type
528 int ext2_file_type(unsigned int mode)
530 if (LINUX_S_ISREG(mode))
531 return EXT2_FT_REG_FILE;
533 if (LINUX_S_ISDIR(mode))
536 if (LINUX_S_ISCHR(mode))
537 return EXT2_FT_CHRDEV;
539 if (LINUX_S_ISBLK(mode))
540 return EXT2_FT_BLKDEV;
542 if (LINUX_S_ISLNK(mode))
543 return EXT2_FT_SYMLINK;
545 if (LINUX_S_ISFIFO(mode))
548 if (LINUX_S_ISSOCK(mode))
554 #define STRIDE_LENGTH 8
556 * Helper function which zeros out _num_ blocks starting at _blk_. In
557 * case of an error, the details of the error is returned via _ret_blk_
558 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
559 * success, and an error code on an error.
561 * As a special case, if the first argument is NULL, then it will
562 * attempt to free the static zeroizing buffer. (This is to keep
563 * programs that check for memory leaks happy.)
565 errcode_t e2fsck_zero_blocks(ext2_filsys fs, blk_t blk, int num,
566 blk_t *ret_blk, int *ret_count)
568 int j, count, next_update, next_update_incr;
572 /* If fs is null, clean up the static buffer and return */
580 /* Allocate the zeroizing buffer if necessary */
582 buf = malloc(fs->blocksize * STRIDE_LENGTH);
584 com_err("malloc", ENOMEM,
585 _("while allocating zeroizing buffer"));
588 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
590 /* OK, do the write loop */
592 next_update_incr = num / 100;
593 if (next_update_incr < 1)
594 next_update_incr = 1;
595 for (j = 0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
597 if (count > STRIDE_LENGTH)
598 count = STRIDE_LENGTH;
599 retval = io_channel_write_blk(fs->io, blk, count, buf);