diff options
-rw-r--r-- | e2fsprogs/mke2fs.c | 434 | ||||
-rw-r--r-- | e2fsprogs/tune2fs.c | 183 | ||||
-rw-r--r-- | e2fsprogs/util.c | 52 | ||||
-rw-r--r-- | e2fsprogs/util.h | 5 |
4 files changed, 307 insertions, 367 deletions
diff --git a/e2fsprogs/mke2fs.c b/e2fsprogs/mke2fs.c index 6cd5bd420..ab50717e7 100644 --- a/e2fsprogs/mke2fs.c +++ b/e2fsprogs/mke2fs.c @@ -40,7 +40,7 @@ #define ZAP_BOOTBLOCK #endif -static const char * device_name /* = NULL */; +static const char * device_name; /* Command line options */ static int cflag; @@ -57,7 +57,7 @@ static struct ext2_super_block param; static char *creator_os; static char *volume_label; static char *mount_dir; -static char *journal_device; +static char *journal_device = NULL; static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */ static int sys_page_size = 4096; @@ -167,6 +167,56 @@ static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk) } /* + * Busybox stuff + */ +static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)__attribute__ ((format (printf, 2, 3))); +static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...) +{ + va_list ap; + + if (retval) { + va_start(ap, fmt); + bb_fprintf(stderr,"\nCould not "); + bb_vfprintf(stderr, fmt, ap); + bb_fprintf(stderr, "\n"); + va_end(ap); + exit(EXIT_FAILURE); + } +} + +static void mke2fs_verbose(const char *fmt, ...)__attribute__ ((format (printf, 1, 2))); +static void mke2fs_verbose(const char *fmt, ...) +{ + va_list ap; + + if (!quiet) { + va_start(ap, fmt); + bb_vfprintf(stdout, fmt, ap); + fflush(stdout); + va_end(ap); + } +} + +static void mke2fs_verbose_done(void) +{ + mke2fs_verbose("done\n"); +} + +static void mke2fs_warning_msg(int retval, char *fmt, ... )__attribute__ ((format (printf, 2, 3))); +static void mke2fs_warning_msg(int retval, char *fmt, ... ) +{ + va_list ap; + + if (retval) { + va_start(ap, fmt); + bb_fprintf(stderr,"\nWarning: "); + bb_vfprintf(stderr, fmt, ap); + bb_fprintf(stderr, "\n"); + va_end(ap); + } +} + +/* * Reads the bad blocks list from a file */ static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, @@ -175,15 +225,10 @@ static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, FILE *f; errcode_t retval; - f = fopen(bad_blocks_file, "r"); - if (!f) { - bb_perror_msg_and_die("Could not read bad blocks file %s", bad_blocks_file); - } + f = bb_xfopen(bad_blocks_file, "r"); retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); fclose (f); - if (retval) { - bb_error_msg_and_die("Could not read bad blocks list"); - } + mke2fs_error_msg_and_die(retval, "read bad blocks from list"); } /* @@ -198,18 +243,14 @@ static void test_disk(ext2_filsys fs, badblocks_list *bb_list) sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize, quiet ? "" : "-s ", (cflag > 1) ? "-w " : "", fs->device_name, fs->super->s_blocks_count); - if (!quiet) - printf("Running command: %s\n", buf); + mke2fs_verbose("Running command: %s\n", buf); f = popen(buf, "r"); if (!f) { bb_perror_msg_and_die("Could not run '%s'", buf); } retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); pclose(f); - if (retval) { - bb_error_msg_and_die( - "Could not get list of bad blocks from program"); - } + mke2fs_error_msg_and_die(retval, "read bad blocks from program"); } static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) @@ -254,10 +295,9 @@ static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) for (j=0; j < fs->desc_blocks+1; j++) { if (ext2fs_badblocks_list_test(bb_list, group_block + j)) { - if (!group_bad) - bb_error_msg( - "Warning: the backup superblock/group descriptors at block %d contain\n" - " bad blocks\n", group_block); + mke2fs_warning_msg(!group_bad, + "the backup superblock/group descriptors at block %d contain\n" + "bad blocks\n", group_block); group_bad++; group = ext2fs_group_of_blk(fs, group_block+j); fs->group_desc[group].bg_free_blocks_count++; @@ -271,9 +311,8 @@ static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) * Mark all the bad blocks as used... */ retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); - if (retval) { - bb_error_msg_and_die("while marking bad blocks as used"); - } + mke2fs_error_msg_and_die(retval, "mark bad blocks as used"); + while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) ext2fs_mark_block_bitmap(fs->block_map, blk); ext2fs_badblocks_list_iterate_end(bb_iter); @@ -329,7 +368,7 @@ static void progress_close(struct progress_struct *progress) { if (progress->format[0] == 0) return; - fputs("done \n", stdout); + printf("%-28s\n", "done"); } @@ -409,12 +448,9 @@ static void write_inode_tables(ext2_filsys fs) num = fs->inode_blocks_per_group; retval = zero_blocks(fs, blk, num, 0, &blk, &num); - if (retval) { - bb_error_msg_and_die( - "\nCould not write %d blocks " - "in inode table starting at %d.", - num, blk); - } + mke2fs_error_msg_and_die(retval, + "write %d blocks in inode table starting at %d.", + num, blk); if (sync_kludge) { if (sync_kludge == 1) sync(); @@ -432,21 +468,15 @@ static void create_root_dir(ext2_filsys fs) struct ext2_inode inode; retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); - if (retval) { - bb_error_msg_and_die("Could not create root dir"); - } + mke2fs_error_msg_and_die(retval, "create root dir"); if (geteuid()) { retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); - if (retval) { - bb_error_msg_and_die("Could not read root inode"); - } + mke2fs_error_msg_and_die(retval, "read root inode"); inode.i_uid = getuid(); if (inode.i_uid) inode.i_gid = getgid(); retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); - if (retval) { - bb_error_msg_and_die("Could not set root inode ownership"); - } + mke2fs_error_msg_and_die(retval, "set root inode ownership"); } } @@ -455,27 +485,29 @@ static void create_lost_and_found(ext2_filsys fs) errcode_t retval; ext2_ino_t ino; const char *name = "lost+found"; - int i; + int i = 1; + char *msg = "create"; int lpf_size = 0; fs->umask = 077; retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); if (retval) { - bb_error_msg_and_die("Could not create lost+found"); + goto CREATE_LOST_AND_FOUND_ERROR; } retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); if (retval) { - bb_error_msg_and_die("Could not look up lost+found"); + msg = "lookup"; + goto CREATE_LOST_AND_FOUND_ERROR; } - for (i=1; i < EXT2_NDIR_BLOCKS; i++) { + for (; i < EXT2_NDIR_BLOCKS; i++) { if ((lpf_size += fs->blocksize) >= 16*1024) break; retval = ext2fs_expand_dir(fs, ino); - if (retval) { - bb_error_msg_and_die("Could not expand lost+found"); - } + msg = "expand"; +CREATE_LOST_AND_FOUND_ERROR: + mke2fs_error_msg_and_die(retval, "%s %s", msg, name); } } @@ -487,10 +519,7 @@ static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) fs->group_desc[0].bg_free_inodes_count--; fs->super->s_free_inodes_count--; retval = ext2fs_update_bb_inode(fs, bb_list); - if (retval) { - bb_error_msg_and_die("Could not set bad block inode"); - } - + mke2fs_error_msg_and_die(retval, "set bad block inode"); } static void reserve_inodes(ext2_filsys fs) @@ -514,6 +543,7 @@ static void reserve_inodes(ext2_filsys fs) static void zap_sector(ext2_filsys fs, int sect, int nsect) { char *buf; + char *fmt = "could not %s %d"; int retval; unsigned int *magic; @@ -523,7 +553,7 @@ static void zap_sector(ext2_filsys fs, int sect, int nsect) /* Check for a BSD disklabel, and don't erase it if so */ retval = io_channel_read_blk(fs->io, 0, -512, buf); if (retval) - bb_error_msg("Warning: could not read block 0"); + mke2fs_warning_msg(retval, fmt, "read block", 0); else { magic = (unsigned int *) (buf + BSD_LABEL_OFFSET); if ((*magic == BSD_DISKMAGIC) || @@ -537,8 +567,7 @@ static void zap_sector(ext2_filsys fs, int sect, int nsect) retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf); io_channel_set_blksize(fs->io, fs->blocksize); free(buf); - if (retval) - bb_error_msg("Warning: could not erase sector %d", sect); + mke2fs_warning_msg(retval, fmt, "erase sector", sect); } static void create_journal_dev(ext2_filsys fs) @@ -546,14 +575,13 @@ static void create_journal_dev(ext2_filsys fs) struct progress_struct progress; errcode_t retval; char *buf; + char *fmt = "%s journal superblock"; blk_t blk; int count; retval = ext2fs_create_journal_superblock(fs, fs->super->s_blocks_count, 0, &buf); - if (retval) { - bb_error_msg_and_die("Could not init journal superblock"); - } + mke2fs_error_msg_and_die(retval, fmt, "init"); if (quiet) memset(&progress, 0, sizeof(progress)); else @@ -562,66 +590,58 @@ static void create_journal_dev(ext2_filsys fs) retval = zero_blocks(fs, 0, fs->super->s_blocks_count, &progress, &blk, &count); - if (retval) { - bb_error_msg_and_die("Could not zero journal device (block %u, count %d)", + mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)", blk, count); - } zero_blocks(0, 0, 0, 0, 0, 0); retval = io_channel_write_blk(fs->io, fs->super->s_first_data_block+1, 1, buf); - if (retval) { - bb_error_msg_and_die("Could not write journal superblock"); - } + mke2fs_error_msg_and_die(retval, fmt, "write"); progress_close(&progress); } static void show_stats(ext2_filsys fs) { struct ext2_super_block *s = fs->super; - char buf[80]; char *os; blk_t group_block; dgrp_t i; int need, col_left; - if (param.s_blocks_count != s->s_blocks_count) - bb_error_msg("warning: %d blocks unused\n", - param.s_blocks_count - s->s_blocks_count); - - memset(buf, 0, sizeof(buf)); - strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); - printf("Filesystem label=%s\n", buf); - fputs("OS type: ", stdout); + mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count), + "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count); os = e2p_os2string(fs->super->s_creator_os); - fputs(os, stdout); + printf( "Filesystem label=%.*s\n" + "OS type: %s\n" + "Block size=%u (log=%u)\n" + "Fragment size=%u (log=%u)\n" + "%u inodes, %u blocks\n" + "%u blocks (%2.2f%%) reserved for the super user\n" + "First data block=%u\n", + (int) sizeof(s->s_volume_name), + s->s_volume_name, + os, + fs->blocksize, s->s_log_block_size, + fs->fragsize, s->s_log_frag_size, + s->s_inodes_count, s->s_blocks_count, + s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count, + s->s_first_data_block); free(os); - printf("\nBlock size=%u (log=%u)\n", fs->blocksize, - s->s_log_block_size); - printf("Fragment size=%u (log=%u)\n", fs->fragsize, - s->s_log_frag_size); - printf("%u inodes, %u blocks\n", s->s_inodes_count, - s->s_blocks_count); - printf("%u blocks (%2.2f%%) reserved for the super user\n", - s->s_r_blocks_count, - 100.0 * s->s_r_blocks_count / s->s_blocks_count); - printf("First data block=%u\n", s->s_first_data_block); - if (s->s_reserved_gdt_blocks) + if (s->s_reserved_gdt_blocks) { printf("Maximum filesystem blocks=%lu\n", (s->s_reserved_gdt_blocks + fs->desc_blocks) * (fs->blocksize / sizeof(struct ext2_group_desc)) * s->s_blocks_per_group); - if (fs->group_desc_count > 1) - printf("%u block groups\n", fs->group_desc_count); - else - printf("%u block group\n", fs->group_desc_count); - printf("%u blocks per group, %u fragments per group\n", - s->s_blocks_per_group, s->s_frags_per_group); - printf("%u inodes per group\n", s->s_inodes_per_group); - + } + printf( "%u block group%s\n" + "%u blocks per group, %u fragments per group\n" + "%u inodes per group\n", + fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "", + s->s_blocks_per_group, s->s_frags_per_group, + s->s_inodes_per_group); if (fs->group_desc_count == 1) { - printf("\n"); + puts(""); return; } @@ -642,7 +662,7 @@ static void show_stats(ext2_filsys fs) col_left -= need; printf("%u", group_block); } - printf("\n\n"); + puts("\n"); } /* @@ -651,22 +671,18 @@ static void show_stats(ext2_filsys fs) */ static int set_os(struct ext2_super_block *sb, char *os) { - if (isdigit (*os)) + if (isdigit (*os)) { sb->s_creator_os = atoi (os); - else if (strcasecmp(os, "linux") == 0) - sb->s_creator_os = EXT2_OS_LINUX; - else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) - sb->s_creator_os = EXT2_OS_HURD; - else if (strcasecmp(os, "masix") == 0) - sb->s_creator_os = EXT2_OS_MASIX; - else if (strcasecmp(os, "freebsd") == 0) - sb->s_creator_os = EXT2_OS_FREEBSD; - else if (strcasecmp(os, "lites") == 0) - sb->s_creator_os = EXT2_OS_LITES; - else - return 0; + return 1; + } - return 1; + if((sb->s_creator_os = e2p_string2os(os)) >= 0) { + return 1; + } else if (!strcasecmp("GNU", os)) { + sb->s_creator_os = EXT2_OS_HURD; + return 1; + } + return 0; } #define PATH_SET "PATH=/sbin" @@ -772,7 +788,6 @@ static __u32 ok_features[3] = { EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */ }; - static int PRS(int argc, char *argv[]) { int b, c; @@ -832,26 +847,25 @@ static int PRS(int argc, char *argv[]) } #endif - if (argc && *argv) { - /* If called as mkfs.ext3, create a journal inode */ - if (!strcmp(*argv + strlen(*argv) - 9, "mkfs.ext3")) - journal_size = -1; - } + /* If called as mkfs.ext3, create a journal inode */ + if (last_char_is(bb_applet_name, '3')) + journal_size = -1; while ((c = getopt (argc, argv, "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) { switch (c) { case 'b': - blocksize = strtol(optarg, &tmp, 0); + if (safe_strtoi(optarg, &blocksize)) + goto BLOCKSIZE_ERROR; b = (blocksize > 0) ? blocksize : -blocksize; if (b < EXT2_MIN_BLOCK_SIZE || - b > EXT2_MAX_BLOCK_SIZE || *tmp) { + b > EXT2_MAX_BLOCK_SIZE) { +BLOCKSIZE_ERROR: bb_error_msg_and_die("bad block size - %s", optarg); } - if (blocksize > 4096) - bb_error_msg( - "Warning: blocksize %d not usable on most systems", - blocksize); + mke2fs_warning_msg((blocksize > 4096), + "blocksize %d not usable on most systems", + blocksize); if (blocksize > 0) param.s_log_block_size = int_log2(blocksize >> @@ -862,20 +876,15 @@ static int PRS(int argc, char *argv[]) cflag++; break; case 'f': - size = strtoul(optarg, &tmp, 0); - if (size < EXT2_MIN_BLOCK_SIZE || - size > EXT2_MAX_BLOCK_SIZE || *tmp) { + if (safe_strtoi(optarg, &size) || size < EXT2_MIN_BLOCK_SIZE || size > EXT2_MAX_BLOCK_SIZE ){ bb_error_msg_and_die("bad fragment size - %s", optarg); } param.s_log_frag_size = int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); - bb_error_msg( - "Warning: fragments not supported. " - "Ignoring -f option"); + mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option"); break; case 'g': - param.s_blocks_per_group = strtoul(optarg, &tmp, 0); - if (*tmp) { + if (safe_strtoi(optarg, ¶m.s_blocks_per_group)) { bb_error_msg_and_die("Illegal number for blocks per group"); } if ((param.s_blocks_per_group % 8) != 0) { @@ -883,14 +892,13 @@ static int PRS(int argc, char *argv[]) } break; case 'i': - inode_ratio = strtoul(optarg, &tmp, 0); - if (inode_ratio < EXT2_MIN_BLOCK_SIZE || - inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 || - *tmp) { - bb_error_msg_and_die("bad inode ratio %s (min %d/max %d", + if (safe_strtoi(optarg, &inode_ratio) + || inode_ratio < EXT2_MIN_BLOCK_SIZE + || inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024) { + bb_error_msg_and_die("bad inode ratio %s (min %d/max %d)", optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE); - } + } break; case 'J': parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg); @@ -905,8 +913,7 @@ static int PRS(int argc, char *argv[]) bad_blocks_filename = optarg; break; case 'm': - reserved_ratio = strtoul(optarg, &tmp, 0); - if (reserved_ratio > 50 || *tmp) { + if (safe_strtoi(optarg, &reserved_ratio) || reserved_ratio > 50 ) { bb_error_msg_and_die("bad reserved blocks percent - %s", optarg); } break; @@ -934,8 +941,7 @@ static int PRS(int argc, char *argv[]) break; #ifdef EXT2_DYNAMIC_REV case 'I': - inode_size = strtoul(optarg, &tmp, 0); - if (*tmp) { + if (safe_strtoi(optarg, &inode_size)) { bb_error_msg_and_die("bad inode size - %s", optarg); } break; @@ -983,19 +989,18 @@ static int PRS(int argc, char *argv[]) break; case 'V': /* Print version number and exit */ - show_version_only++; + show_version_only = 1; + quiet = 0; break; default: bb_show_usage(); } } - if ((optind == argc) && !show_version_only) + if ((optind == argc) /*&& !show_version_only*/) bb_show_usage(); device_name = argv[optind++]; - if (!quiet || show_version_only) - bb_error_msg("mke2fs %s (%s)", E2FSPROGS_VERSION, - E2FSPROGS_DATE); + mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); if (show_version_only) { return 0; @@ -1018,9 +1023,7 @@ static int PRS(int argc, char *argv[]) retval = ext2fs_open(journal_device, EXT2_FLAG_JOURNAL_DEV_OK, 0, 0, io_ptr, &jfs); - if (retval) { - bb_error_msg_and_die("Could not open journal device %s", journal_device); - } + mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device); if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) { bb_error_msg_and_die( "Journal dev blocksize (%d) smaller than " @@ -1034,29 +1037,23 @@ static int PRS(int argc, char *argv[]) } if (blocksize > sys_page_size) { + mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)", + blocksize, sys_page_size); if (!force) { - bb_error_msg("%d-byte blocks too big for system (max %d)", - blocksize, sys_page_size); proceed_question(); } - bb_error_msg( - "Warning: %d-byte blocks too big for system " - "(max %d), forced to continue", - blocksize, sys_page_size); + bb_error_msg("Forced to continue"); } - if ((blocksize > 4096) && - (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) - bb_error_msg( - "\nWarning: some 2.4 kernels do not support " - "blocksizes greater than 4096 \n\tusing ext3." - " Use -b 4096 if this is an issue for you\n"); + mke2fs_warning_msg(((blocksize > 4096) && + (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)), + "some 2.4 kernels do not support " + "blocksizes greater than 4096 using ext3.\n" + "Use -b 4096 if this is an issue for you\n"); if (optind < argc) { param.s_blocks_count = parse_num_blocks(argv[optind++], param.s_log_block_size); - if (!param.s_blocks_count) { - bb_error_msg_and_die("bad blocks count - %s", argv[optind - 1]); - } + mke2fs_error_msg_and_die(!param.s_blocks_count, "bad blocks count - %s", argv[optind - 1]); } if (optind < argc) bb_show_usage(); @@ -1074,8 +1071,7 @@ static int PRS(int argc, char *argv[]) param.s_feature_incompat)) param.s_rev_level = 1; /* Create a revision 1 filesystem */ - if (!force) - check_plausibility(device_name); + check_plausibility(device_name , force); check_mount(device_name, force, "filesystem"); param.s_log_frag_size = param.s_log_block_size; @@ -1097,13 +1093,12 @@ static int PRS(int argc, char *argv[]) } } - if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { - bb_error_msg_and_die("Could not determine filesystem size"); - } + mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size"); + if (!param.s_blocks_count) { if (retval == EXT2_ET_UNIMPLEMENTED) { - bb_error_msg_and_die( - "Couldn't determine device size; you " + mke2fs_error_msg_and_die(1, + "determine device size; you " "must specify\nthe size of the " "filesystem"); } else { @@ -1145,9 +1140,7 @@ static int PRS(int argc, char *argv[]) /* Get the hardware sector size, if available */ retval = ext2fs_get_device_sectsize(device_name, §or_size); - if (retval) { - bb_error_msg_and_die("Could not determine hardware sector size"); - } + mke2fs_error_msg_and_die(retval, "determine hardware sector size"); if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL) sector_size = atoi(tmp); @@ -1182,10 +1175,9 @@ static int PRS(int argc, char *argv[]) inode_size, EXT2_GOOD_OLD_INODE_SIZE, blocksize); } - if (inode_size != EXT2_GOOD_OLD_INODE_SIZE) - bb_error_msg( - "Warning: %d-byte inodes not usable on most systems", - inode_size); + mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE), + "%d-byte inodes not usable on most systems", + inode_size); param.s_inode_size = inode_size; } @@ -1203,16 +1195,22 @@ static int PRS(int argc, char *argv[]) return 1; } +static void clean_up(void) +{ + if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device); +} + int mke2fs_main (int argc, char *argv[]) { errcode_t retval; ext2_filsys fs; badblocks_list bb_list = 0; - int journal_blocks; unsigned int i; int val; io_manager io_ptr; + if (ENABLE_FEATURE_CLEAN_UP) + atexit(clean_up); if(!PRS(argc, argv)) return 0; @@ -1228,9 +1226,7 @@ int mke2fs_main (int argc, char *argv[]) */ retval = ext2fs_initialize(device_name, 0, ¶m, io_ptr, &fs); - if (retval) { - bb_error_msg_and_die("Could not set up superblock"); - } + mke2fs_error_msg_and_die(retval, "set up superblock"); /* * Wipe out the old on-disk superblock @@ -1277,20 +1273,14 @@ int mke2fs_main (int argc, char *argv[]) * Set the volume label... */ if (volume_label) { - memset(fs->super->s_volume_name, 0, - sizeof(fs->super->s_volume_name)); - strncpy(fs->super->s_volume_name, volume_label, - sizeof(fs->super->s_volume_name)); + snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label); } /* * Set the last mount directory */ if (mount_dir) { - memset(fs->super->s_last_mounted, 0, - sizeof(fs->super->s_last_mounted)); - strncpy(fs->super->s_last_mounted, mount_dir, - sizeof(fs->super->s_last_mounted)); + snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir); } if (!quiet || noaction) @@ -1313,9 +1303,7 @@ int mke2fs_main (int argc, char *argv[]) handle_bad_blocks(fs, bb_list); fs->stride = fs_stride; retval = ext2fs_allocate_tables(fs); - if (retval) { - bb_error_msg_and_die("Could not allocate filesystem tables"); - } + mke2fs_error_msg_and_die(retval, "allocate filesystem tables"); if (super_only) { fs->super->s_state |= EXT2_ERROR_FS; fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); @@ -1342,9 +1330,7 @@ int mke2fs_main (int argc, char *argv[]) retval = zero_blocks(fs, start, blocks - start, NULL, &ret_blk, NULL); - if (retval) { - bb_error_msg("Could not zero block %u at end of filesystem", ret_blk); - } + mke2fs_warning_msg(retval, "could not zero block %u at end of filesystem", ret_blk); write_inode_tables(fs); create_root_dir(fs); create_lost_and_found(fs); @@ -1353,72 +1339,22 @@ int mke2fs_main (int argc, char *argv[]) if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) { retval = ext2fs_create_resize_inode(fs); - if (retval) { - bb_error_msg_and_die("Could not reserve blocks for online resize"); - } + mke2fs_error_msg_and_die(retval, "reserve blocks for online resize"); } } if (journal_device) { - ext2_filsys jfs; - - if (!force) - check_plausibility(journal_device); - check_mount(journal_device, force, "journal"); - - retval = ext2fs_open(journal_device, EXT2_FLAG_RW| - EXT2_FLAG_JOURNAL_DEV_OK, 0, - fs->blocksize, unix_io_manager, &jfs); - if (retval) { - bb_error_msg_and_die("Could not open journal device %s", journal_device); - } - if (!quiet) { - printf("Adding journal to device %s: ", journal_device); - fflush(stdout); - } - retval = ext2fs_add_journal_device(fs, jfs); - if(retval) { - bb_error_msg_and_die("Could not add journal to device %s", journal_device); - } - if (!quiet) - printf("done\n"); - ext2fs_close(jfs); - free(journal_device); + make_journal_device(journal_device, fs, quiet, force); } else if (journal_size) { - journal_blocks = figure_journal_size(journal_size, fs); - - if (!journal_blocks) { - fs->super->s_feature_compat &= - ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; - goto no_journal; - } - if (!quiet) { - printf("Creating journal (%d blocks): ", - journal_blocks); - fflush(stdout); - } - retval = ext2fs_add_journal_inode(fs, journal_blocks, - journal_flags); - if (retval) { - bb_error_msg_and_die("Could not create journal"); - } - if (!quiet) - printf("done\n"); + make_journal_blocks(fs, journal_size, journal_flags, quiet); } -no_journal: - if (!quiet) - printf("Writing superblocks and " - "filesystem accounting information: "); + mke2fs_verbose("Writing superblocks and filesystem accounting information: "); retval = ext2fs_flush(fs); - if (retval) { - bb_error_msg("\nWarning, had trouble writing out superblocks"); - } - if (!quiet) { - printf("done\n\n"); - if (!getenv("MKE2FS_SKIP_CHECK_MSG")) - print_check_message(fs); - } + mke2fs_warning_msg(retval, "had trouble writing out superblocks"); + mke2fs_verbose_done(); + if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG")) + print_check_message(fs); val = ext2fs_close(fs); return (retval || val) ? 1 : 0; } diff --git a/e2fsprogs/tune2fs.c b/e2fsprogs/tune2fs.c index b78b0139d..ccbbc7868 100644 --- a/e2fsprogs/tune2fs.c +++ b/e2fsprogs/tune2fs.c @@ -50,7 +50,7 @@ extern int optind; #include "util.h" #include "blkid/blkid.h" -static char * device_name; +static char * device_name = NULL; static char * new_label, *new_last_mounted, *new_UUID; static char * io_options; static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag; @@ -66,7 +66,7 @@ static char *features_cmd; static char *mntopts_cmd; static int journal_size, journal_flags; -static char *journal_device; +static char *journal_device = NULL; static const char *please_fsck = "Please run e2fsck on the filesystem\n"; @@ -188,26 +188,36 @@ static void remove_journal_inode(ext2_filsys fs) struct ext2_inode inode; errcode_t retval; ino_t ino = fs->super->s_journal_inum; + char *msg = "to read"; + char *s = "journal inode"; retval = ext2fs_read_inode(fs, ino, &inode); - if (retval) - bb_error_msg_and_die("Failed to read journal inode"); + if (retval) + goto REMOVE_JOURNAL_INODE_ERROR; if (ino == EXT2_JOURNAL_INO) { retval = ext2fs_read_bitmaps(fs); - if (retval) - bb_error_msg_and_die("Failed to read bitmaps"); + if (retval) { + msg = "to read bitmaps"; + s = ""; + goto REMOVE_JOURNAL_INODE_ERROR; + } retval = ext2fs_block_iterate(fs, ino, 0, NULL, release_blocks_proc, NULL); - if (retval) - bb_error_msg_and_die("Failed clearing journal inode"); + if (retval) { + msg = "clearing"; + goto REMOVE_JOURNAL_INODE_ERROR; + } memset(&inode, 0, sizeof(inode)); ext2fs_mark_bb_dirty(fs); fs->flags &= ~EXT2_FLAG_SUPER_ONLY; } else inode.i_flags &= ~EXT2_IMMUTABLE_FL; retval = ext2fs_write_inode(fs, ino, &inode); - if (retval) - bb_error_msg_and_die("Failed writing journal inode"); + if (retval) { + msg = "writing"; +REMOVE_JOURNAL_INODE_ERROR: + bb_error_msg_and_die("Failed %s %s", msg, s); + } fs->super->s_journal_inum = 0; ext2fs_mark_super_dirty(fs); } @@ -269,8 +279,8 @@ static void update_feature_set(ext2_filsys fs, char *features) EXT3_FEATURE_INCOMPAT_RECOVER) { bb_error_msg_and_die( "The needs_recovery flag is set. " - "Please run e2fsck before clearing\n" - "the has_journal flag."); + "%s before clearing the has_journal flag.", + please_fsck); } if (sb->s_journal_inum) { remove_journal_inode(fs); @@ -317,48 +327,14 @@ static void update_feature_set(ext2_filsys fs, char *features) */ static void add_journal(ext2_filsys fs) { - unsigned long journal_blocks; - errcode_t retval; - ext2_filsys jfs; - io_manager io_ptr; - if (fs->super->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) { - bb_error_msg("The filesystem already has a journal"); - goto err; + bb_error_msg_and_die("The filesystem already has a journal"); } if (journal_device) { - check_plausibility(journal_device); - check_mount(journal_device, 0, "journal"); - io_ptr = unix_io_manager; - retval = ext2fs_open(journal_device, EXT2_FLAG_RW| - EXT2_FLAG_JOURNAL_DEV_OK, 0, - fs->blocksize, io_ptr, &jfs); - if (retval) { - bb_error_msg("Failed to open journal on %s", journal_device); - goto err; - } - printf("Creating journal on device %s: ", journal_device); - fflush(stdout); - - retval = ext2fs_add_journal_device(fs, jfs); - ext2fs_close(jfs); - if (retval) { - bb_error_msg("Failed to add filesystem to journal on %s", journal_device); - goto err; - } - puts("done"); + make_journal_device(journal_device, fs, 0, 0); } else if (journal_size) { - fputs("Creating journal inode: ", stdout); - fflush(stdout); - journal_blocks = figure_journal_size(journal_size, fs); - - retval = ext2fs_add_journal_inode(fs, journal_blocks, - journal_flags); - if (retval) - bb_error_msg_and_die("Failed to create journal file"); - else - puts("done"); + make_journal_blocks(fs, journal_size, journal_flags, 0); /* * If the filesystem wasn't mounted, we need to force * the block group descriptors out. @@ -368,11 +344,18 @@ static void add_journal(ext2_filsys fs) } print_check_message(fs); return; +} -err: - if (journal_device) - free(journal_device); - exit(1); +/* + * Busybox stuff + */ +static char * x_blkid_get_devname(const char *token) +{ + char * dev_name; + + if (!(dev_name = blkid_get_devname(NULL, token, NULL))) + bb_error_msg_and_die("Unable to resolve '%s'", token); + return dev_name; } #ifdef CONFIG_E2LABEL @@ -383,9 +366,7 @@ static void parse_e2label_options(int argc, char ** argv) io_options = strchr(argv[1], '?'); if (io_options) *io_options++ = 0; - device_name = blkid_get_devname(NULL, argv[1], NULL); - if (!device_name) - bb_error_msg_and_die("Unable to resolve '%s'", argv[1]); + device_name = x_blkid_get_devname(argv[1]); if (argc == 3) { open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK; L_flag = 1; @@ -393,6 +374,8 @@ static void parse_e2label_options(int argc, char ** argv) } else print_label++; } +#else +#define parse_e2label_options(x,y) #endif static time_t parse_time(char *str) @@ -425,17 +408,14 @@ static void parse_tune2fs_options(int argc, char **argv) { int c; char * tmp; - struct group * gr; - struct passwd * pw; printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF) switch (c) { case 'c': - max_mount_count = strtol (optarg, &tmp, 0); - if (*tmp || max_mount_count > 16000) { - bb_error_msg_and_die("bad mounts count - %s", optarg); + if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) { + goto MOUNTS_COUNT_ERROR; } if (max_mount_count == 0) max_mount_count = -1; @@ -443,8 +423,8 @@ static void parse_tune2fs_options(int argc, char **argv) open_flag = EXT2_FLAG_RW; break; case 'C': - mount_count = strtoul (optarg, &tmp, 0); - if (*tmp || mount_count > 16000) { + if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) { +MOUNTS_COUNT_ERROR: bb_error_msg_and_die("bad mounts count - %s", optarg); } C_flag = 1; @@ -467,19 +447,8 @@ static void parse_tune2fs_options(int argc, char **argv) f_flag = 1; break; case 'g': - resgid = strtoul (optarg, &tmp, 0); - if (*tmp) { - gr = getgrnam (optarg); - if (gr == NULL) - tmp = optarg; - else { - resgid = gr->gr_gid; - *tmp =0; - } - } - if (*tmp) { - bb_error_msg_and_die("bad gid/group name - %s", optarg); - } + if (safe_strtoul(optarg, &resgid)) + resgid = bb_xgetgrnam(optarg); g_flag = 1; open_flag = EXT2_FLAG_RW; break; @@ -532,8 +501,7 @@ static void parse_tune2fs_options(int argc, char **argv) EXT2_FLAG_JOURNAL_DEV_OK; break; case 'm': - reserved_ratio = strtoul (optarg, &tmp, 0); - if (*tmp || reserved_ratio > 50) { + if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) { bb_error_msg_and_die("bad reserved block ratio - %s", optarg); } m_flag = 1; @@ -560,8 +528,7 @@ static void parse_tune2fs_options(int argc, char **argv) open_flag = EXT2_FLAG_RW; break; case 'r': - reserved_blocks = strtoul (optarg, &tmp, 0); - if (*tmp) { + if(safe_strtoul(optarg, &reserved_blocks)) { bb_error_msg_and_die("bad reserved blocks count - %s", optarg); } r_flag = 1; @@ -577,19 +544,8 @@ static void parse_tune2fs_options(int argc, char **argv) open_flag = EXT2_FLAG_RW; break; case 'u': - resuid = strtoul (optarg, &tmp, 0); - if (*tmp) { - pw = getpwnam (optarg); - if (pw == NULL) - tmp = optarg; - else { - resuid = pw->pw_uid; - *tmp = 0; - } - } - if (*tmp) { - bb_error_msg_and_die("bad uid/user name - %s", optarg); - } + if (safe_strtoul(optarg, &resuid)) + resuid = bb_xgetpwnam(optarg); u_flag = 1; open_flag = EXT2_FLAG_RW; break; @@ -609,48 +565,45 @@ static void parse_tune2fs_options(int argc, char **argv) io_options = strchr(argv[optind], '?'); if (io_options) *io_options++ = 0; - device_name = blkid_get_devname(NULL, argv[optind], NULL); - if (!device_name) - bb_error_msg_and_die("Unable to resolve '%s'", argv[optind]); + device_name = x_blkid_get_devname(argv[optind]); } #ifdef CONFIG_FINDFS static attribute_noreturn void do_findfs(int argc, char **argv) { - char *dev; - if ((argc != 2) || (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) bb_show_usage(); - dev = blkid_get_devname(NULL, argv[1], NULL); - if (!dev) - bb_error_msg_and_die("Unable to resolve '%s'", argv[1]); - puts(dev); + device_name = x_blkid_get_devname(argv[1]); + puts(device_name); exit(0); } +#else +#define do_findfs(x, y) #endif +static void clean_up(void) +{ + if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name); + if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device); +} + int tune2fs_main(int argc, char **argv) { errcode_t retval; ext2_filsys fs; struct ext2_super_block *sb; io_manager io_ptr; -#if defined(CONFIG_FINDFS) || defined(CONFIG_E2LABEL) - char *program_name = basename(argv[0]); -#endif -#ifdef CONFIG_FINDFS - if (strcmp(program_name, "findfs") == 0) - do_findfs(argc, argv); -#endif - -#ifdef CONFIG_E2LABEL - if (strcmp(program_name, "e2label") == 0) + if (ENABLE_FEATURE_CLEAN_UP) + atexit(clean_up); + + if (ENABLE_FINDFS && (bb_applet_name[0] == 'f')) /* findfs */ + do_findfs(argc, argv); /* no return */ + else if (ENABLE_E2LABEL && (bb_applet_name[0] == 'e')) /* e2label */ parse_e2label_options(argc, argv); else -#endif - parse_tune2fs_options(argc, argv); + parse_tune2fs_options(argc, argv); /* tune2fs */ io_ptr = unix_io_manager; retval = ext2fs_open2(device_name, io_options, open_flag, @@ -662,7 +615,7 @@ int tune2fs_main(int argc, char **argv) /* For e2label emulation */ printf("%.*s\n", (int) sizeof(sb->s_volume_name), sb->s_volume_name); - exit(0); + return 0; } retval = ext2fs_check_if_mounted(device_name, &mount_flags); if (retval) diff --git a/e2fsprogs/util.c b/e2fsprogs/util.c index d4d77b63b..58f9bbb88 100644 --- a/e2fsprogs/util.c +++ b/e2fsprogs/util.c @@ -29,7 +29,7 @@ void proceed_question(void) exit(1); } -void check_plausibility(const char *device) +void check_plausibility(const char *device, int force) { int val; #ifdef CONFIG_LFS @@ -39,7 +39,8 @@ void check_plausibility(const char *device) struct stat s; val = stat(device, &s); #endif - + if (force) + return; if(val == -1) bb_perror_msg_and_die("Could not stat %s", device); if (!S_ISBLK(s.st_mode)) { @@ -205,3 +206,50 @@ void print_check_message(ext2_filsys fs) fs->super->s_max_mnt_count, (double)fs->super->s_checkinterval / (3600 * 24)); } + +void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force) +{ + errcode_t retval; + ext2_filsys jfs; + io_manager io_ptr; + + check_plausibility(journal_device, force); + check_mount(journal_device, force, "journal"); + io_ptr = unix_io_manager; + retval = ext2fs_open(journal_device, EXT2_FLAG_RW| + EXT2_FLAG_JOURNAL_DEV_OK, 0, + fs->blocksize, io_ptr, &jfs); + if (retval) + bb_error_msg_and_die("Could not journal device %s", journal_device); + if(!quiet) + printf("Adding journal to device %s: ", journal_device); + fflush(stdout); + retval = ext2fs_add_journal_device(fs, jfs); + if(retval) + bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device); + if(!quiet) + puts("done"); + ext2fs_close(jfs); +} + +void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet) +{ + unsigned long journal_blocks; + errcode_t retval; + + journal_blocks = figure_journal_size(journal_size, fs); + if (!journal_blocks) { + fs->super->s_feature_compat &= + ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; + return; + } + if(!quiet) + printf("Creating journal (%ld blocks): ", journal_blocks); + fflush(stdout); + retval = ext2fs_add_journal_inode(fs, journal_blocks, + journal_flags); + if(retval) + bb_error_msg_and_die("Could not create journal"); + if(!quiet) + puts("done"); +} diff --git a/e2fsprogs/util.h b/e2fsprogs/util.h index 55cb84937..f6dc08ac1 100644 --- a/e2fsprogs/util.h +++ b/e2fsprogs/util.h @@ -11,8 +11,11 @@ */ extern void proceed_question(void); -extern void check_plausibility(const char *device); +extern void check_plausibility(const char *device, int force); extern void parse_journal_opts(char **, int *, int *, const char *opts); extern void check_mount(const char *device, int force, const char *type); extern int figure_journal_size(int size, ext2_filsys fs); extern void print_check_message(ext2_filsys fs); +extern void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force); +extern void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet); + |