aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2005-10-02 08:10:31 +0000
committerMike Frysinger <vapier@gentoo.org>2005-10-02 08:10:31 +0000
commit7f782da048defae66ad8b602a3cca8e7957a3639 (patch)
tree25ee18d0e09ba5e39bd8eac91653142b5e35997d
parent9134dff0785f29c65e44d1b490bba278565c13e1 (diff)
downloadbusybox-w32-7f782da048defae66ad8b602a3cca8e7957a3639.tar.gz
busybox-w32-7f782da048defae66ad8b602a3cca8e7957a3639.tar.bz2
busybox-w32-7f782da048defae66ad8b602a3cca8e7957a3639.zip
excellent shrinkage patch by Tito
-rw-r--r--e2fsprogs/mke2fs.c434
-rw-r--r--e2fsprogs/tune2fs.c183
-rw-r--r--e2fsprogs/util.c52
-rw-r--r--e2fsprogs/util.h5
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 @@
40#define ZAP_BOOTBLOCK 40#define ZAP_BOOTBLOCK
41#endif 41#endif
42 42
43static const char * device_name /* = NULL */; 43static const char * device_name;
44 44
45/* Command line options */ 45/* Command line options */
46static int cflag; 46static int cflag;
@@ -57,7 +57,7 @@ static struct ext2_super_block param;
57static char *creator_os; 57static char *creator_os;
58static char *volume_label; 58static char *volume_label;
59static char *mount_dir; 59static char *mount_dir;
60static char *journal_device; 60static char *journal_device = NULL;
61static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */ 61static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
62 62
63static int sys_page_size = 4096; 63static int sys_page_size = 4096;
@@ -167,6 +167,56 @@ static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
167} 167}
168 168
169/* 169/*
170 * Busybox stuff
171 */
172static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)__attribute__ ((format (printf, 2, 3)));
173static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)
174{
175 va_list ap;
176
177 if (retval) {
178 va_start(ap, fmt);
179 bb_fprintf(stderr,"\nCould not ");
180 bb_vfprintf(stderr, fmt, ap);
181 bb_fprintf(stderr, "\n");
182 va_end(ap);
183 exit(EXIT_FAILURE);
184 }
185}
186
187static void mke2fs_verbose(const char *fmt, ...)__attribute__ ((format (printf, 1, 2)));
188static void mke2fs_verbose(const char *fmt, ...)
189{
190 va_list ap;
191
192 if (!quiet) {
193 va_start(ap, fmt);
194 bb_vfprintf(stdout, fmt, ap);
195 fflush(stdout);
196 va_end(ap);
197 }
198}
199
200static void mke2fs_verbose_done(void)
201{
202 mke2fs_verbose("done\n");
203}
204
205static void mke2fs_warning_msg(int retval, char *fmt, ... )__attribute__ ((format (printf, 2, 3)));
206static void mke2fs_warning_msg(int retval, char *fmt, ... )
207{
208 va_list ap;
209
210 if (retval) {
211 va_start(ap, fmt);
212 bb_fprintf(stderr,"\nWarning: ");
213 bb_vfprintf(stderr, fmt, ap);
214 bb_fprintf(stderr, "\n");
215 va_end(ap);
216 }
217}
218
219/*
170 * Reads the bad blocks list from a file 220 * Reads the bad blocks list from a file
171 */ 221 */
172static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, 222static 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,
175 FILE *f; 225 FILE *f;
176 errcode_t retval; 226 errcode_t retval;
177 227
178 f = fopen(bad_blocks_file, "r"); 228 f = bb_xfopen(bad_blocks_file, "r");
179 if (!f) {
180 bb_perror_msg_and_die("Could not read bad blocks file %s", bad_blocks_file);
181 }
182 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); 229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
183 fclose (f); 230 fclose (f);
184 if (retval) { 231 mke2fs_error_msg_and_die(retval, "read bad blocks from list");
185 bb_error_msg_and_die("Could not read bad blocks list");
186 }
187} 232}
188 233
189/* 234/*
@@ -198,18 +243,14 @@ static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
198 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize, 243 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
199 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "", 244 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
200 fs->device_name, fs->super->s_blocks_count); 245 fs->device_name, fs->super->s_blocks_count);
201 if (!quiet) 246 mke2fs_verbose("Running command: %s\n", buf);
202 printf("Running command: %s\n", buf);
203 f = popen(buf, "r"); 247 f = popen(buf, "r");
204 if (!f) { 248 if (!f) {
205 bb_perror_msg_and_die("Could not run '%s'", buf); 249 bb_perror_msg_and_die("Could not run '%s'", buf);
206 } 250 }
207 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); 251 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
208 pclose(f); 252 pclose(f);
209 if (retval) { 253 mke2fs_error_msg_and_die(retval, "read bad blocks from program");
210 bb_error_msg_and_die(
211 "Could not get list of bad blocks from program");
212 }
213} 254}
214 255
215static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) 256static 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)
254 for (j=0; j < fs->desc_blocks+1; j++) { 295 for (j=0; j < fs->desc_blocks+1; j++) {
255 if (ext2fs_badblocks_list_test(bb_list, 296 if (ext2fs_badblocks_list_test(bb_list,
256 group_block + j)) { 297 group_block + j)) {
257 if (!group_bad) 298 mke2fs_warning_msg(!group_bad,
258 bb_error_msg( 299 "the backup superblock/group descriptors at block %d contain\n"
259 "Warning: the backup superblock/group descriptors at block %d contain\n" 300 "bad blocks\n", group_block);
260 " bad blocks\n", group_block);
261 group_bad++; 301 group_bad++;
262 group = ext2fs_group_of_blk(fs, group_block+j); 302 group = ext2fs_group_of_blk(fs, group_block+j);
263 fs->group_desc[group].bg_free_blocks_count++; 303 fs->group_desc[group].bg_free_blocks_count++;
@@ -271,9 +311,8 @@ static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
271 * Mark all the bad blocks as used... 311 * Mark all the bad blocks as used...
272 */ 312 */
273 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); 313 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
274 if (retval) { 314 mke2fs_error_msg_and_die(retval, "mark bad blocks as used");
275 bb_error_msg_and_die("while marking bad blocks as used"); 315
276 }
277 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) 316 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
278 ext2fs_mark_block_bitmap(fs->block_map, blk); 317 ext2fs_mark_block_bitmap(fs->block_map, blk);
279 ext2fs_badblocks_list_iterate_end(bb_iter); 318 ext2fs_badblocks_list_iterate_end(bb_iter);
@@ -329,7 +368,7 @@ static void progress_close(struct progress_struct *progress)
329{ 368{
330 if (progress->format[0] == 0) 369 if (progress->format[0] == 0)
331 return; 370 return;
332 fputs("done \n", stdout); 371 printf("%-28s\n", "done");
333} 372}
334 373
335 374
@@ -409,12 +448,9 @@ static void write_inode_tables(ext2_filsys fs)
409 num = fs->inode_blocks_per_group; 448 num = fs->inode_blocks_per_group;
410 449
411 retval = zero_blocks(fs, blk, num, 0, &blk, &num); 450 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
412 if (retval) { 451 mke2fs_error_msg_and_die(retval,
413 bb_error_msg_and_die( 452 "write %d blocks in inode table starting at %d.",
414 "\nCould not write %d blocks " 453 num, blk);
415 "in inode table starting at %d.",
416 num, blk);
417 }
418 if (sync_kludge) { 454 if (sync_kludge) {
419 if (sync_kludge == 1) 455 if (sync_kludge == 1)
420 sync(); 456 sync();
@@ -432,21 +468,15 @@ static void create_root_dir(ext2_filsys fs)
432 struct ext2_inode inode; 468 struct ext2_inode inode;
433 469
434 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); 470 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
435 if (retval) { 471 mke2fs_error_msg_and_die(retval, "create root dir");
436 bb_error_msg_and_die("Could not create root dir");
437 }
438 if (geteuid()) { 472 if (geteuid()) {
439 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); 473 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
440 if (retval) { 474 mke2fs_error_msg_and_die(retval, "read root inode");
441 bb_error_msg_and_die("Could not read root inode");
442 }
443 inode.i_uid = getuid(); 475 inode.i_uid = getuid();
444 if (inode.i_uid) 476 if (inode.i_uid)
445 inode.i_gid = getgid(); 477 inode.i_gid = getgid();
446 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); 478 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
447 if (retval) { 479 mke2fs_error_msg_and_die(retval, "set root inode ownership");
448 bb_error_msg_and_die("Could not set root inode ownership");
449 }
450 } 480 }
451} 481}
452 482
@@ -455,27 +485,29 @@ static void create_lost_and_found(ext2_filsys fs)
455 errcode_t retval; 485 errcode_t retval;
456 ext2_ino_t ino; 486 ext2_ino_t ino;
457 const char *name = "lost+found"; 487 const char *name = "lost+found";
458 int i; 488 int i = 1;
489 char *msg = "create";
459 int lpf_size = 0; 490 int lpf_size = 0;
460 491
461 fs->umask = 077; 492 fs->umask = 077;
462 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); 493 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
463 if (retval) { 494 if (retval) {
464 bb_error_msg_and_die("Could not create lost+found"); 495 goto CREATE_LOST_AND_FOUND_ERROR;
465 } 496 }
466 497
467 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); 498 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
468 if (retval) { 499 if (retval) {
469 bb_error_msg_and_die("Could not look up lost+found"); 500 msg = "lookup";
501 goto CREATE_LOST_AND_FOUND_ERROR;
470 } 502 }
471 503
472 for (i=1; i < EXT2_NDIR_BLOCKS; i++) { 504 for (; i < EXT2_NDIR_BLOCKS; i++) {
473 if ((lpf_size += fs->blocksize) >= 16*1024) 505 if ((lpf_size += fs->blocksize) >= 16*1024)
474 break; 506 break;
475 retval = ext2fs_expand_dir(fs, ino); 507 retval = ext2fs_expand_dir(fs, ino);
476 if (retval) { 508 msg = "expand";
477 bb_error_msg_and_die("Could not expand lost+found"); 509CREATE_LOST_AND_FOUND_ERROR:
478 } 510 mke2fs_error_msg_and_die(retval, "%s %s", msg, name);
479 } 511 }
480} 512}
481 513
@@ -487,10 +519,7 @@ static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
487 fs->group_desc[0].bg_free_inodes_count--; 519 fs->group_desc[0].bg_free_inodes_count--;
488 fs->super->s_free_inodes_count--; 520 fs->super->s_free_inodes_count--;
489 retval = ext2fs_update_bb_inode(fs, bb_list); 521 retval = ext2fs_update_bb_inode(fs, bb_list);
490 if (retval) { 522 mke2fs_error_msg_and_die(retval, "set bad block inode");
491 bb_error_msg_and_die("Could not set bad block inode");
492 }
493
494} 523}
495 524
496static void reserve_inodes(ext2_filsys fs) 525static void reserve_inodes(ext2_filsys fs)
@@ -514,6 +543,7 @@ static void reserve_inodes(ext2_filsys fs)
514static void zap_sector(ext2_filsys fs, int sect, int nsect) 543static void zap_sector(ext2_filsys fs, int sect, int nsect)
515{ 544{
516 char *buf; 545 char *buf;
546 char *fmt = "could not %s %d";
517 int retval; 547 int retval;
518 unsigned int *magic; 548 unsigned int *magic;
519 549
@@ -523,7 +553,7 @@ static void zap_sector(ext2_filsys fs, int sect, int nsect)
523 /* Check for a BSD disklabel, and don't erase it if so */ 553 /* Check for a BSD disklabel, and don't erase it if so */
524 retval = io_channel_read_blk(fs->io, 0, -512, buf); 554 retval = io_channel_read_blk(fs->io, 0, -512, buf);
525 if (retval) 555 if (retval)
526 bb_error_msg("Warning: could not read block 0"); 556 mke2fs_warning_msg(retval, fmt, "read block", 0);
527 else { 557 else {
528 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET); 558 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
529 if ((*magic == BSD_DISKMAGIC) || 559 if ((*magic == BSD_DISKMAGIC) ||
@@ -537,8 +567,7 @@ static void zap_sector(ext2_filsys fs, int sect, int nsect)
537 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf); 567 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
538 io_channel_set_blksize(fs->io, fs->blocksize); 568 io_channel_set_blksize(fs->io, fs->blocksize);
539 free(buf); 569 free(buf);
540 if (retval) 570 mke2fs_warning_msg(retval, fmt, "erase sector", sect);
541 bb_error_msg("Warning: could not erase sector %d", sect);
542} 571}
543 572
544static void create_journal_dev(ext2_filsys fs) 573static void create_journal_dev(ext2_filsys fs)
@@ -546,14 +575,13 @@ static void create_journal_dev(ext2_filsys fs)
546 struct progress_struct progress; 575 struct progress_struct progress;
547 errcode_t retval; 576 errcode_t retval;
548 char *buf; 577 char *buf;
578 char *fmt = "%s journal superblock";
549 blk_t blk; 579 blk_t blk;
550 int count; 580 int count;
551 581
552 retval = ext2fs_create_journal_superblock(fs, 582 retval = ext2fs_create_journal_superblock(fs,
553 fs->super->s_blocks_count, 0, &buf); 583 fs->super->s_blocks_count, 0, &buf);
554 if (retval) { 584 mke2fs_error_msg_and_die(retval, fmt, "init");
555 bb_error_msg_and_die("Could not init journal superblock");
556 }
557 if (quiet) 585 if (quiet)
558 memset(&progress, 0, sizeof(progress)); 586 memset(&progress, 0, sizeof(progress));
559 else 587 else
@@ -562,66 +590,58 @@ static void create_journal_dev(ext2_filsys fs)
562 590
563 retval = zero_blocks(fs, 0, fs->super->s_blocks_count, 591 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
564 &progress, &blk, &count); 592 &progress, &blk, &count);
565 if (retval) { 593 mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)",
566 bb_error_msg_and_die("Could not zero journal device (block %u, count %d)",
567 blk, count); 594 blk, count);
568 }
569 zero_blocks(0, 0, 0, 0, 0, 0); 595 zero_blocks(0, 0, 0, 0, 0, 0);
570 596
571 retval = io_channel_write_blk(fs->io, 597 retval = io_channel_write_blk(fs->io,
572 fs->super->s_first_data_block+1, 598 fs->super->s_first_data_block+1,
573 1, buf); 599 1, buf);
574 if (retval) { 600 mke2fs_error_msg_and_die(retval, fmt, "write");
575 bb_error_msg_and_die("Could not write journal superblock");
576 }
577 progress_close(&progress); 601 progress_close(&progress);
578} 602}
579 603
580static void show_stats(ext2_filsys fs) 604static void show_stats(ext2_filsys fs)
581{ 605{
582 struct ext2_super_block *s = fs->super; 606 struct ext2_super_block *s = fs->super;
583 char buf[80];
584 char *os; 607 char *os;
585 blk_t group_block; 608 blk_t group_block;
586 dgrp_t i; 609 dgrp_t i;
587 int need, col_left; 610 int need, col_left;
588 611
589 if (param.s_blocks_count != s->s_blocks_count) 612 mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count),
590 bb_error_msg("warning: %d blocks unused\n", 613 "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count);
591 param.s_blocks_count - s->s_blocks_count);
592
593 memset(buf, 0, sizeof(buf));
594 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
595 printf("Filesystem label=%s\n", buf);
596 fputs("OS type: ", stdout);
597 os = e2p_os2string(fs->super->s_creator_os); 614 os = e2p_os2string(fs->super->s_creator_os);
598 fputs(os, stdout); 615 printf( "Filesystem label=%.*s\n"
616 "OS type: %s\n"
617 "Block size=%u (log=%u)\n"
618 "Fragment size=%u (log=%u)\n"
619 "%u inodes, %u blocks\n"
620 "%u blocks (%2.2f%%) reserved for the super user\n"
621 "First data block=%u\n",
622 (int) sizeof(s->s_volume_name),
623 s->s_volume_name,
624 os,
625 fs->blocksize, s->s_log_block_size,
626 fs->fragsize, s->s_log_frag_size,
627 s->s_inodes_count, s->s_blocks_count,
628 s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count,
629 s->s_first_data_block);
599 free(os); 630 free(os);
600 printf("\nBlock size=%u (log=%u)\n", fs->blocksize, 631 if (s->s_reserved_gdt_blocks) {
601 s->s_log_block_size);
602 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
603 s->s_log_frag_size);
604 printf("%u inodes, %u blocks\n", s->s_inodes_count,
605 s->s_blocks_count);
606 printf("%u blocks (%2.2f%%) reserved for the super user\n",
607 s->s_r_blocks_count,
608 100.0 * s->s_r_blocks_count / s->s_blocks_count);
609 printf("First data block=%u\n", s->s_first_data_block);
610 if (s->s_reserved_gdt_blocks)
611 printf("Maximum filesystem blocks=%lu\n", 632 printf("Maximum filesystem blocks=%lu\n",
612 (s->s_reserved_gdt_blocks + fs->desc_blocks) * 633 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
613 (fs->blocksize / sizeof(struct ext2_group_desc)) * 634 (fs->blocksize / sizeof(struct ext2_group_desc)) *
614 s->s_blocks_per_group); 635 s->s_blocks_per_group);
615 if (fs->group_desc_count > 1) 636 }
616 printf("%u block groups\n", fs->group_desc_count); 637 printf( "%u block group%s\n"
617 else 638 "%u blocks per group, %u fragments per group\n"
618 printf("%u block group\n", fs->group_desc_count); 639 "%u inodes per group\n",
619 printf("%u blocks per group, %u fragments per group\n", 640 fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "",
620 s->s_blocks_per_group, s->s_frags_per_group); 641 s->s_blocks_per_group, s->s_frags_per_group,
621 printf("%u inodes per group\n", s->s_inodes_per_group); 642 s->s_inodes_per_group);
622
623 if (fs->group_desc_count == 1) { 643 if (fs->group_desc_count == 1) {
624 printf("\n"); 644 puts("");
625 return; 645 return;
626 } 646 }
627 647
@@ -642,7 +662,7 @@ static void show_stats(ext2_filsys fs)
642 col_left -= need; 662 col_left -= need;
643 printf("%u", group_block); 663 printf("%u", group_block);
644 } 664 }
645 printf("\n\n"); 665 puts("\n");
646} 666}
647 667
648/* 668/*
@@ -651,22 +671,18 @@ static void show_stats(ext2_filsys fs)
651 */ 671 */
652static int set_os(struct ext2_super_block *sb, char *os) 672static int set_os(struct ext2_super_block *sb, char *os)
653{ 673{
654 if (isdigit (*os)) 674 if (isdigit (*os)) {
655 sb->s_creator_os = atoi (os); 675 sb->s_creator_os = atoi (os);
656 else if (strcasecmp(os, "linux") == 0) 676 return 1;
657 sb->s_creator_os = EXT2_OS_LINUX; 677 }
658 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
659 sb->s_creator_os = EXT2_OS_HURD;
660 else if (strcasecmp(os, "masix") == 0)
661 sb->s_creator_os = EXT2_OS_MASIX;
662 else if (strcasecmp(os, "freebsd") == 0)
663 sb->s_creator_os = EXT2_OS_FREEBSD;
664 else if (strcasecmp(os, "lites") == 0)
665 sb->s_creator_os = EXT2_OS_LITES;
666 else
667 return 0;
668 678
669 return 1; 679 if((sb->s_creator_os = e2p_string2os(os)) >= 0) {
680 return 1;
681 } else if (!strcasecmp("GNU", os)) {
682 sb->s_creator_os = EXT2_OS_HURD;
683 return 1;
684 }
685 return 0;
670} 686}
671 687
672#define PATH_SET "PATH=/sbin" 688#define PATH_SET "PATH=/sbin"
@@ -772,7 +788,6 @@ static __u32 ok_features[3] = {
772 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */ 788 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
773}; 789};
774 790
775
776static int PRS(int argc, char *argv[]) 791static int PRS(int argc, char *argv[])
777{ 792{
778 int b, c; 793 int b, c;
@@ -832,26 +847,25 @@ static int PRS(int argc, char *argv[])
832 } 847 }
833#endif 848#endif
834 849
835 if (argc && *argv) { 850 /* If called as mkfs.ext3, create a journal inode */
836 /* If called as mkfs.ext3, create a journal inode */ 851 if (last_char_is(bb_applet_name, '3'))
837 if (!strcmp(*argv + strlen(*argv) - 9, "mkfs.ext3")) 852 journal_size = -1;
838 journal_size = -1;
839 }
840 853
841 while ((c = getopt (argc, argv, 854 while ((c = getopt (argc, argv,
842 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) { 855 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
843 switch (c) { 856 switch (c) {
844 case 'b': 857 case 'b':
845 blocksize = strtol(optarg, &tmp, 0); 858 if (safe_strtoi(optarg, &blocksize))
859 goto BLOCKSIZE_ERROR;
846 b = (blocksize > 0) ? blocksize : -blocksize; 860 b = (blocksize > 0) ? blocksize : -blocksize;
847 if (b < EXT2_MIN_BLOCK_SIZE || 861 if (b < EXT2_MIN_BLOCK_SIZE ||
848 b > EXT2_MAX_BLOCK_SIZE || *tmp) { 862 b > EXT2_MAX_BLOCK_SIZE) {
863BLOCKSIZE_ERROR:
849 bb_error_msg_and_die("bad block size - %s", optarg); 864 bb_error_msg_and_die("bad block size - %s", optarg);
850 } 865 }
851 if (blocksize > 4096) 866 mke2fs_warning_msg((blocksize > 4096),
852 bb_error_msg( 867 "blocksize %d not usable on most systems",
853 "Warning: blocksize %d not usable on most systems", 868 blocksize);
854 blocksize);
855 if (blocksize > 0) 869 if (blocksize > 0)
856 param.s_log_block_size = 870 param.s_log_block_size =
857 int_log2(blocksize >> 871 int_log2(blocksize >>
@@ -862,20 +876,15 @@ static int PRS(int argc, char *argv[])
862 cflag++; 876 cflag++;
863 break; 877 break;
864 case 'f': 878 case 'f':
865 size = strtoul(optarg, &tmp, 0); 879 if (safe_strtoi(optarg, &size) || size < EXT2_MIN_BLOCK_SIZE || size > EXT2_MAX_BLOCK_SIZE ){
866 if (size < EXT2_MIN_BLOCK_SIZE ||
867 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
868 bb_error_msg_and_die("bad fragment size - %s", optarg); 880 bb_error_msg_and_die("bad fragment size - %s", optarg);
869 } 881 }
870 param.s_log_frag_size = 882 param.s_log_frag_size =
871 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); 883 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
872 bb_error_msg( 884 mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option");
873 "Warning: fragments not supported. "
874 "Ignoring -f option");
875 break; 885 break;
876 case 'g': 886 case 'g':
877 param.s_blocks_per_group = strtoul(optarg, &tmp, 0); 887 if (safe_strtoi(optarg, &param.s_blocks_per_group)) {
878 if (*tmp) {
879 bb_error_msg_and_die("Illegal number for blocks per group"); 888 bb_error_msg_and_die("Illegal number for blocks per group");
880 } 889 }
881 if ((param.s_blocks_per_group % 8) != 0) { 890 if ((param.s_blocks_per_group % 8) != 0) {
@@ -883,14 +892,13 @@ static int PRS(int argc, char *argv[])
883 } 892 }
884 break; 893 break;
885 case 'i': 894 case 'i':
886 inode_ratio = strtoul(optarg, &tmp, 0); 895 if (safe_strtoi(optarg, &inode_ratio)
887 if (inode_ratio < EXT2_MIN_BLOCK_SIZE || 896 || inode_ratio < EXT2_MIN_BLOCK_SIZE
888 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 || 897 || inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024) {
889 *tmp) { 898 bb_error_msg_and_die("bad inode ratio %s (min %d/max %d)",
890 bb_error_msg_and_die("bad inode ratio %s (min %d/max %d",
891 optarg, EXT2_MIN_BLOCK_SIZE, 899 optarg, EXT2_MIN_BLOCK_SIZE,
892 EXT2_MAX_BLOCK_SIZE); 900 EXT2_MAX_BLOCK_SIZE);
893 } 901 }
894 break; 902 break;
895 case 'J': 903 case 'J':
896 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg); 904 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
@@ -905,8 +913,7 @@ static int PRS(int argc, char *argv[])
905 bad_blocks_filename = optarg; 913 bad_blocks_filename = optarg;
906 break; 914 break;
907 case 'm': 915 case 'm':
908 reserved_ratio = strtoul(optarg, &tmp, 0); 916 if (safe_strtoi(optarg, &reserved_ratio) || reserved_ratio > 50 ) {
909 if (reserved_ratio > 50 || *tmp) {
910 bb_error_msg_and_die("bad reserved blocks percent - %s", optarg); 917 bb_error_msg_and_die("bad reserved blocks percent - %s", optarg);
911 } 918 }
912 break; 919 break;
@@ -934,8 +941,7 @@ static int PRS(int argc, char *argv[])
934 break; 941 break;
935#ifdef EXT2_DYNAMIC_REV 942#ifdef EXT2_DYNAMIC_REV
936 case 'I': 943 case 'I':
937 inode_size = strtoul(optarg, &tmp, 0); 944 if (safe_strtoi(optarg, &inode_size)) {
938 if (*tmp) {
939 bb_error_msg_and_die("bad inode size - %s", optarg); 945 bb_error_msg_and_die("bad inode size - %s", optarg);
940 } 946 }
941 break; 947 break;
@@ -983,19 +989,18 @@ static int PRS(int argc, char *argv[])
983 break; 989 break;
984 case 'V': 990 case 'V':
985 /* Print version number and exit */ 991 /* Print version number and exit */
986 show_version_only++; 992 show_version_only = 1;
993 quiet = 0;
987 break; 994 break;
988 default: 995 default:
989 bb_show_usage(); 996 bb_show_usage();
990 } 997 }
991 } 998 }
992 if ((optind == argc) && !show_version_only) 999 if ((optind == argc) /*&& !show_version_only*/)
993 bb_show_usage(); 1000 bb_show_usage();
994 device_name = argv[optind++]; 1001 device_name = argv[optind++];
995 1002
996 if (!quiet || show_version_only) 1003 mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
997 bb_error_msg("mke2fs %s (%s)", E2FSPROGS_VERSION,
998 E2FSPROGS_DATE);
999 1004
1000 if (show_version_only) { 1005 if (show_version_only) {
1001 return 0; 1006 return 0;
@@ -1018,9 +1023,7 @@ static int PRS(int argc, char *argv[])
1018 retval = ext2fs_open(journal_device, 1023 retval = ext2fs_open(journal_device,
1019 EXT2_FLAG_JOURNAL_DEV_OK, 0, 1024 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1020 0, io_ptr, &jfs); 1025 0, io_ptr, &jfs);
1021 if (retval) { 1026 mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device);
1022 bb_error_msg_and_die("Could not open journal device %s", journal_device);
1023 }
1024 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) { 1027 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1025 bb_error_msg_and_die( 1028 bb_error_msg_and_die(
1026 "Journal dev blocksize (%d) smaller than " 1029 "Journal dev blocksize (%d) smaller than "
@@ -1034,29 +1037,23 @@ static int PRS(int argc, char *argv[])
1034 } 1037 }
1035 1038
1036 if (blocksize > sys_page_size) { 1039 if (blocksize > sys_page_size) {
1040 mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)",
1041 blocksize, sys_page_size);
1037 if (!force) { 1042 if (!force) {
1038 bb_error_msg("%d-byte blocks too big for system (max %d)",
1039 blocksize, sys_page_size);
1040 proceed_question(); 1043 proceed_question();
1041 } 1044 }
1042 bb_error_msg( 1045 bb_error_msg("Forced to continue");
1043 "Warning: %d-byte blocks too big for system "
1044 "(max %d), forced to continue",
1045 blocksize, sys_page_size);
1046 } 1046 }
1047 if ((blocksize > 4096) && 1047 mke2fs_warning_msg(((blocksize > 4096) &&
1048 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) 1048 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)),
1049 bb_error_msg( 1049 "some 2.4 kernels do not support "
1050 "\nWarning: some 2.4 kernels do not support " 1050 "blocksizes greater than 4096 using ext3.\n"
1051 "blocksizes greater than 4096 \n\tusing ext3." 1051 "Use -b 4096 if this is an issue for you\n");
1052 " Use -b 4096 if this is an issue for you\n");
1053 1052
1054 if (optind < argc) { 1053 if (optind < argc) {
1055 param.s_blocks_count = parse_num_blocks(argv[optind++], 1054 param.s_blocks_count = parse_num_blocks(argv[optind++],
1056 param.s_log_block_size); 1055 param.s_log_block_size);
1057 if (!param.s_blocks_count) { 1056 mke2fs_error_msg_and_die(!param.s_blocks_count, "bad blocks count - %s", argv[optind - 1]);
1058 bb_error_msg_and_die("bad blocks count - %s", argv[optind - 1]);
1059 }
1060 } 1057 }
1061 if (optind < argc) 1058 if (optind < argc)
1062 bb_show_usage(); 1059 bb_show_usage();
@@ -1074,8 +1071,7 @@ static int PRS(int argc, char *argv[])
1074 param.s_feature_incompat)) 1071 param.s_feature_incompat))
1075 param.s_rev_level = 1; /* Create a revision 1 filesystem */ 1072 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1076 1073
1077 if (!force) 1074 check_plausibility(device_name , force);
1078 check_plausibility(device_name);
1079 check_mount(device_name, force, "filesystem"); 1075 check_mount(device_name, force, "filesystem");
1080 1076
1081 param.s_log_frag_size = param.s_log_block_size; 1077 param.s_log_frag_size = param.s_log_block_size;
@@ -1097,13 +1093,12 @@ static int PRS(int argc, char *argv[])
1097 } 1093 }
1098 } 1094 }
1099 1095
1100 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { 1096 mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size");
1101 bb_error_msg_and_die("Could not determine filesystem size"); 1097
1102 }
1103 if (!param.s_blocks_count) { 1098 if (!param.s_blocks_count) {
1104 if (retval == EXT2_ET_UNIMPLEMENTED) { 1099 if (retval == EXT2_ET_UNIMPLEMENTED) {
1105 bb_error_msg_and_die( 1100 mke2fs_error_msg_and_die(1,
1106 "Couldn't determine device size; you " 1101 "determine device size; you "
1107 "must specify\nthe size of the " 1102 "must specify\nthe size of the "
1108 "filesystem"); 1103 "filesystem");
1109 } else { 1104 } else {
@@ -1145,9 +1140,7 @@ static int PRS(int argc, char *argv[])
1145 1140
1146 /* Get the hardware sector size, if available */ 1141 /* Get the hardware sector size, if available */
1147 retval = ext2fs_get_device_sectsize(device_name, &sector_size); 1142 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
1148 if (retval) { 1143 mke2fs_error_msg_and_die(retval, "determine hardware sector size");
1149 bb_error_msg_and_die("Could not determine hardware sector size");
1150 }
1151 1144
1152 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL) 1145 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1153 sector_size = atoi(tmp); 1146 sector_size = atoi(tmp);
@@ -1182,10 +1175,9 @@ static int PRS(int argc, char *argv[])
1182 inode_size, EXT2_GOOD_OLD_INODE_SIZE, 1175 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1183 blocksize); 1176 blocksize);
1184 } 1177 }
1185 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE) 1178 mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE),
1186 bb_error_msg( 1179 "%d-byte inodes not usable on most systems",
1187 "Warning: %d-byte inodes not usable on most systems", 1180 inode_size);
1188 inode_size);
1189 param.s_inode_size = inode_size; 1181 param.s_inode_size = inode_size;
1190 } 1182 }
1191 1183
@@ -1203,16 +1195,22 @@ static int PRS(int argc, char *argv[])
1203 return 1; 1195 return 1;
1204} 1196}
1205 1197
1198static void clean_up(void)
1199{
1200 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
1201}
1202
1206int mke2fs_main (int argc, char *argv[]) 1203int mke2fs_main (int argc, char *argv[])
1207{ 1204{
1208 errcode_t retval; 1205 errcode_t retval;
1209 ext2_filsys fs; 1206 ext2_filsys fs;
1210 badblocks_list bb_list = 0; 1207 badblocks_list bb_list = 0;
1211 int journal_blocks;
1212 unsigned int i; 1208 unsigned int i;
1213 int val; 1209 int val;
1214 io_manager io_ptr; 1210 io_manager io_ptr;
1215 1211
1212 if (ENABLE_FEATURE_CLEAN_UP)
1213 atexit(clean_up);
1216 if(!PRS(argc, argv)) 1214 if(!PRS(argc, argv))
1217 return 0; 1215 return 0;
1218 1216
@@ -1228,9 +1226,7 @@ int mke2fs_main (int argc, char *argv[])
1228 */ 1226 */
1229 retval = ext2fs_initialize(device_name, 0, &param, 1227 retval = ext2fs_initialize(device_name, 0, &param,
1230 io_ptr, &fs); 1228 io_ptr, &fs);
1231 if (retval) { 1229 mke2fs_error_msg_and_die(retval, "set up superblock");
1232 bb_error_msg_and_die("Could not set up superblock");
1233 }
1234 1230
1235 /* 1231 /*
1236 * Wipe out the old on-disk superblock 1232 * Wipe out the old on-disk superblock
@@ -1277,20 +1273,14 @@ int mke2fs_main (int argc, char *argv[])
1277 * Set the volume label... 1273 * Set the volume label...
1278 */ 1274 */
1279 if (volume_label) { 1275 if (volume_label) {
1280 memset(fs->super->s_volume_name, 0, 1276 snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label);
1281 sizeof(fs->super->s_volume_name));
1282 strncpy(fs->super->s_volume_name, volume_label,
1283 sizeof(fs->super->s_volume_name));
1284 } 1277 }
1285 1278
1286 /* 1279 /*
1287 * Set the last mount directory 1280 * Set the last mount directory
1288 */ 1281 */
1289 if (mount_dir) { 1282 if (mount_dir) {
1290 memset(fs->super->s_last_mounted, 0, 1283 snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir);
1291 sizeof(fs->super->s_last_mounted));
1292 strncpy(fs->super->s_last_mounted, mount_dir,
1293 sizeof(fs->super->s_last_mounted));
1294 } 1284 }
1295 1285
1296 if (!quiet || noaction) 1286 if (!quiet || noaction)
@@ -1313,9 +1303,7 @@ int mke2fs_main (int argc, char *argv[])
1313 handle_bad_blocks(fs, bb_list); 1303 handle_bad_blocks(fs, bb_list);
1314 fs->stride = fs_stride; 1304 fs->stride = fs_stride;
1315 retval = ext2fs_allocate_tables(fs); 1305 retval = ext2fs_allocate_tables(fs);
1316 if (retval) { 1306 mke2fs_error_msg_and_die(retval, "allocate filesystem tables");
1317 bb_error_msg_and_die("Could not allocate filesystem tables");
1318 }
1319 if (super_only) { 1307 if (super_only) {
1320 fs->super->s_state |= EXT2_ERROR_FS; 1308 fs->super->s_state |= EXT2_ERROR_FS;
1321 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); 1309 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
@@ -1342,9 +1330,7 @@ int mke2fs_main (int argc, char *argv[])
1342 retval = zero_blocks(fs, start, blocks - start, 1330 retval = zero_blocks(fs, start, blocks - start,
1343 NULL, &ret_blk, NULL); 1331 NULL, &ret_blk, NULL);
1344 1332
1345 if (retval) { 1333 mke2fs_warning_msg(retval, "could not zero block %u at end of filesystem", ret_blk);
1346 bb_error_msg("Could not zero block %u at end of filesystem", ret_blk);
1347 }
1348 write_inode_tables(fs); 1334 write_inode_tables(fs);
1349 create_root_dir(fs); 1335 create_root_dir(fs);
1350 create_lost_and_found(fs); 1336 create_lost_and_found(fs);
@@ -1353,72 +1339,22 @@ int mke2fs_main (int argc, char *argv[])
1353 if (fs->super->s_feature_compat & 1339 if (fs->super->s_feature_compat &
1354 EXT2_FEATURE_COMPAT_RESIZE_INODE) { 1340 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1355 retval = ext2fs_create_resize_inode(fs); 1341 retval = ext2fs_create_resize_inode(fs);
1356 if (retval) { 1342 mke2fs_error_msg_and_die(retval, "reserve blocks for online resize");
1357 bb_error_msg_and_die("Could not reserve blocks for online resize");
1358 }
1359 } 1343 }
1360 } 1344 }
1361 1345
1362 if (journal_device) { 1346 if (journal_device) {
1363 ext2_filsys jfs; 1347 make_journal_device(journal_device, fs, quiet, force);
1364
1365 if (!force)
1366 check_plausibility(journal_device);
1367 check_mount(journal_device, force, "journal");
1368
1369 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1370 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1371 fs->blocksize, unix_io_manager, &jfs);
1372 if (retval) {
1373 bb_error_msg_and_die("Could not open journal device %s", journal_device);
1374 }
1375 if (!quiet) {
1376 printf("Adding journal to device %s: ", journal_device);
1377 fflush(stdout);
1378 }
1379 retval = ext2fs_add_journal_device(fs, jfs);
1380 if(retval) {
1381 bb_error_msg_and_die("Could not add journal to device %s", journal_device);
1382 }
1383 if (!quiet)
1384 printf("done\n");
1385 ext2fs_close(jfs);
1386 free(journal_device);
1387 } else if (journal_size) { 1348 } else if (journal_size) {
1388 journal_blocks = figure_journal_size(journal_size, fs); 1349 make_journal_blocks(fs, journal_size, journal_flags, quiet);
1389
1390 if (!journal_blocks) {
1391 fs->super->s_feature_compat &=
1392 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1393 goto no_journal;
1394 }
1395 if (!quiet) {
1396 printf("Creating journal (%d blocks): ",
1397 journal_blocks);
1398 fflush(stdout);
1399 }
1400 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1401 journal_flags);
1402 if (retval) {
1403 bb_error_msg_and_die("Could not create journal");
1404 }
1405 if (!quiet)
1406 printf("done\n");
1407 } 1350 }
1408no_journal:
1409 1351
1410 if (!quiet) 1352 mke2fs_verbose("Writing superblocks and filesystem accounting information: ");
1411 printf("Writing superblocks and "
1412 "filesystem accounting information: ");
1413 retval = ext2fs_flush(fs); 1353 retval = ext2fs_flush(fs);
1414 if (retval) { 1354 mke2fs_warning_msg(retval, "had trouble writing out superblocks");
1415 bb_error_msg("\nWarning, had trouble writing out superblocks"); 1355 mke2fs_verbose_done();
1416 } 1356 if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG"))
1417 if (!quiet) { 1357 print_check_message(fs);
1418 printf("done\n\n");
1419 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1420 print_check_message(fs);
1421 }
1422 val = ext2fs_close(fs); 1358 val = ext2fs_close(fs);
1423 return (retval || val) ? 1 : 0; 1359 return (retval || val) ? 1 : 0;
1424} 1360}
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;
50#include "util.h" 50#include "util.h"
51#include "blkid/blkid.h" 51#include "blkid/blkid.h"
52 52
53static char * device_name; 53static char * device_name = NULL;
54static char * new_label, *new_last_mounted, *new_UUID; 54static char * new_label, *new_last_mounted, *new_UUID;
55static char * io_options; 55static char * io_options;
56static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag; 56static 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;
66static char *mntopts_cmd; 66static char *mntopts_cmd;
67 67
68static int journal_size, journal_flags; 68static int journal_size, journal_flags;
69static char *journal_device; 69static char *journal_device = NULL;
70 70
71static const char *please_fsck = "Please run e2fsck on the filesystem\n"; 71static const char *please_fsck = "Please run e2fsck on the filesystem\n";
72 72
@@ -188,26 +188,36 @@ static void remove_journal_inode(ext2_filsys fs)
188 struct ext2_inode inode; 188 struct ext2_inode inode;
189 errcode_t retval; 189 errcode_t retval;
190 ino_t ino = fs->super->s_journal_inum; 190 ino_t ino = fs->super->s_journal_inum;
191 char *msg = "to read";
192 char *s = "journal inode";
191 193
192 retval = ext2fs_read_inode(fs, ino, &inode); 194 retval = ext2fs_read_inode(fs, ino, &inode);
193 if (retval) 195 if (retval)
194 bb_error_msg_and_die("Failed to read journal inode"); 196 goto REMOVE_JOURNAL_INODE_ERROR;
195 if (ino == EXT2_JOURNAL_INO) { 197 if (ino == EXT2_JOURNAL_INO) {
196 retval = ext2fs_read_bitmaps(fs); 198 retval = ext2fs_read_bitmaps(fs);
197 if (retval) 199 if (retval) {
198 bb_error_msg_and_die("Failed to read bitmaps"); 200 msg = "to read bitmaps";
201 s = "";
202 goto REMOVE_JOURNAL_INODE_ERROR;
203 }
199 retval = ext2fs_block_iterate(fs, ino, 0, NULL, 204 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
200 release_blocks_proc, NULL); 205 release_blocks_proc, NULL);
201 if (retval) 206 if (retval) {
202 bb_error_msg_and_die("Failed clearing journal inode"); 207 msg = "clearing";
208 goto REMOVE_JOURNAL_INODE_ERROR;
209 }
203 memset(&inode, 0, sizeof(inode)); 210 memset(&inode, 0, sizeof(inode));
204 ext2fs_mark_bb_dirty(fs); 211 ext2fs_mark_bb_dirty(fs);
205 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 212 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
206 } else 213 } else
207 inode.i_flags &= ~EXT2_IMMUTABLE_FL; 214 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
208 retval = ext2fs_write_inode(fs, ino, &inode); 215 retval = ext2fs_write_inode(fs, ino, &inode);
209 if (retval) 216 if (retval) {
210 bb_error_msg_and_die("Failed writing journal inode"); 217 msg = "writing";
218REMOVE_JOURNAL_INODE_ERROR:
219 bb_error_msg_and_die("Failed %s %s", msg, s);
220 }
211 fs->super->s_journal_inum = 0; 221 fs->super->s_journal_inum = 0;
212 ext2fs_mark_super_dirty(fs); 222 ext2fs_mark_super_dirty(fs);
213} 223}
@@ -269,8 +279,8 @@ static void update_feature_set(ext2_filsys fs, char *features)
269 EXT3_FEATURE_INCOMPAT_RECOVER) { 279 EXT3_FEATURE_INCOMPAT_RECOVER) {
270 bb_error_msg_and_die( 280 bb_error_msg_and_die(
271 "The needs_recovery flag is set. " 281 "The needs_recovery flag is set. "
272 "Please run e2fsck before clearing\n" 282 "%s before clearing the has_journal flag.",
273 "the has_journal flag."); 283 please_fsck);
274 } 284 }
275 if (sb->s_journal_inum) { 285 if (sb->s_journal_inum) {
276 remove_journal_inode(fs); 286 remove_journal_inode(fs);
@@ -317,48 +327,14 @@ static void update_feature_set(ext2_filsys fs, char *features)
317 */ 327 */
318static void add_journal(ext2_filsys fs) 328static void add_journal(ext2_filsys fs)
319{ 329{
320 unsigned long journal_blocks;
321 errcode_t retval;
322 ext2_filsys jfs;
323 io_manager io_ptr;
324
325 if (fs->super->s_feature_compat & 330 if (fs->super->s_feature_compat &
326 EXT3_FEATURE_COMPAT_HAS_JOURNAL) { 331 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
327 bb_error_msg("The filesystem already has a journal"); 332 bb_error_msg_and_die("The filesystem already has a journal");
328 goto err;
329 } 333 }
330 if (journal_device) { 334 if (journal_device) {
331 check_plausibility(journal_device); 335 make_journal_device(journal_device, fs, 0, 0);
332 check_mount(journal_device, 0, "journal");
333 io_ptr = unix_io_manager;
334 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
335 EXT2_FLAG_JOURNAL_DEV_OK, 0,
336 fs->blocksize, io_ptr, &jfs);
337 if (retval) {
338 bb_error_msg("Failed to open journal on %s", journal_device);
339 goto err;
340 }
341 printf("Creating journal on device %s: ", journal_device);
342 fflush(stdout);
343
344 retval = ext2fs_add_journal_device(fs, jfs);
345 ext2fs_close(jfs);
346 if (retval) {
347 bb_error_msg("Failed to add filesystem to journal on %s", journal_device);
348 goto err;
349 }
350 puts("done");
351 } else if (journal_size) { 336 } else if (journal_size) {
352 fputs("Creating journal inode: ", stdout); 337 make_journal_blocks(fs, journal_size, journal_flags, 0);
353 fflush(stdout);
354 journal_blocks = figure_journal_size(journal_size, fs);
355
356 retval = ext2fs_add_journal_inode(fs, journal_blocks,
357 journal_flags);
358 if (retval)
359 bb_error_msg_and_die("Failed to create journal file");
360 else
361 puts("done");
362 /* 338 /*
363 * If the filesystem wasn't mounted, we need to force 339 * If the filesystem wasn't mounted, we need to force
364 * the block group descriptors out. 340 * the block group descriptors out.
@@ -368,11 +344,18 @@ static void add_journal(ext2_filsys fs)
368 } 344 }
369 print_check_message(fs); 345 print_check_message(fs);
370 return; 346 return;
347}
371 348
372err: 349/*
373 if (journal_device) 350 * Busybox stuff
374 free(journal_device); 351 */
375 exit(1); 352static char * x_blkid_get_devname(const char *token)
353{
354 char * dev_name;
355
356 if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
357 bb_error_msg_and_die("Unable to resolve '%s'", token);
358 return dev_name;
376} 359}
377 360
378#ifdef CONFIG_E2LABEL 361#ifdef CONFIG_E2LABEL
@@ -383,9 +366,7 @@ static void parse_e2label_options(int argc, char ** argv)
383 io_options = strchr(argv[1], '?'); 366 io_options = strchr(argv[1], '?');
384 if (io_options) 367 if (io_options)
385 *io_options++ = 0; 368 *io_options++ = 0;
386 device_name = blkid_get_devname(NULL, argv[1], NULL); 369 device_name = x_blkid_get_devname(argv[1]);
387 if (!device_name)
388 bb_error_msg_and_die("Unable to resolve '%s'", argv[1]);
389 if (argc == 3) { 370 if (argc == 3) {
390 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK; 371 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
391 L_flag = 1; 372 L_flag = 1;
@@ -393,6 +374,8 @@ static void parse_e2label_options(int argc, char ** argv)
393 } else 374 } else
394 print_label++; 375 print_label++;
395} 376}
377#else
378#define parse_e2label_options(x,y)
396#endif 379#endif
397 380
398static time_t parse_time(char *str) 381static time_t parse_time(char *str)
@@ -425,17 +408,14 @@ static void parse_tune2fs_options(int argc, char **argv)
425{ 408{
426 int c; 409 int c;
427 char * tmp; 410 char * tmp;
428 struct group * gr;
429 struct passwd * pw;
430 411
431 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); 412 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
432 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF) 413 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
433 switch (c) 414 switch (c)
434 { 415 {
435 case 'c': 416 case 'c':
436 max_mount_count = strtol (optarg, &tmp, 0); 417 if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) {
437 if (*tmp || max_mount_count > 16000) { 418 goto MOUNTS_COUNT_ERROR;
438 bb_error_msg_and_die("bad mounts count - %s", optarg);
439 } 419 }
440 if (max_mount_count == 0) 420 if (max_mount_count == 0)
441 max_mount_count = -1; 421 max_mount_count = -1;
@@ -443,8 +423,8 @@ static void parse_tune2fs_options(int argc, char **argv)
443 open_flag = EXT2_FLAG_RW; 423 open_flag = EXT2_FLAG_RW;
444 break; 424 break;
445 case 'C': 425 case 'C':
446 mount_count = strtoul (optarg, &tmp, 0); 426 if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) {
447 if (*tmp || mount_count > 16000) { 427MOUNTS_COUNT_ERROR:
448 bb_error_msg_and_die("bad mounts count - %s", optarg); 428 bb_error_msg_and_die("bad mounts count - %s", optarg);
449 } 429 }
450 C_flag = 1; 430 C_flag = 1;
@@ -467,19 +447,8 @@ static void parse_tune2fs_options(int argc, char **argv)
467 f_flag = 1; 447 f_flag = 1;
468 break; 448 break;
469 case 'g': 449 case 'g':
470 resgid = strtoul (optarg, &tmp, 0); 450 if (safe_strtoul(optarg, &resgid))
471 if (*tmp) { 451 resgid = bb_xgetgrnam(optarg);
472 gr = getgrnam (optarg);
473 if (gr == NULL)
474 tmp = optarg;
475 else {
476 resgid = gr->gr_gid;
477 *tmp =0;
478 }
479 }
480 if (*tmp) {
481 bb_error_msg_and_die("bad gid/group name - %s", optarg);
482 }
483 g_flag = 1; 452 g_flag = 1;
484 open_flag = EXT2_FLAG_RW; 453 open_flag = EXT2_FLAG_RW;
485 break; 454 break;
@@ -532,8 +501,7 @@ static void parse_tune2fs_options(int argc, char **argv)
532 EXT2_FLAG_JOURNAL_DEV_OK; 501 EXT2_FLAG_JOURNAL_DEV_OK;
533 break; 502 break;
534 case 'm': 503 case 'm':
535 reserved_ratio = strtoul (optarg, &tmp, 0); 504 if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) {
536 if (*tmp || reserved_ratio > 50) {
537 bb_error_msg_and_die("bad reserved block ratio - %s", optarg); 505 bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
538 } 506 }
539 m_flag = 1; 507 m_flag = 1;
@@ -560,8 +528,7 @@ static void parse_tune2fs_options(int argc, char **argv)
560 open_flag = EXT2_FLAG_RW; 528 open_flag = EXT2_FLAG_RW;
561 break; 529 break;
562 case 'r': 530 case 'r':
563 reserved_blocks = strtoul (optarg, &tmp, 0); 531 if(safe_strtoul(optarg, &reserved_blocks)) {
564 if (*tmp) {
565 bb_error_msg_and_die("bad reserved blocks count - %s", optarg); 532 bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
566 } 533 }
567 r_flag = 1; 534 r_flag = 1;
@@ -577,19 +544,8 @@ static void parse_tune2fs_options(int argc, char **argv)
577 open_flag = EXT2_FLAG_RW; 544 open_flag = EXT2_FLAG_RW;
578 break; 545 break;
579 case 'u': 546 case 'u':
580 resuid = strtoul (optarg, &tmp, 0); 547 if (safe_strtoul(optarg, &resuid))
581 if (*tmp) { 548 resuid = bb_xgetpwnam(optarg);
582 pw = getpwnam (optarg);
583 if (pw == NULL)
584 tmp = optarg;
585 else {
586 resuid = pw->pw_uid;
587 *tmp = 0;
588 }
589 }
590 if (*tmp) {
591 bb_error_msg_and_die("bad uid/user name - %s", optarg);
592 }
593 u_flag = 1; 549 u_flag = 1;
594 open_flag = EXT2_FLAG_RW; 550 open_flag = EXT2_FLAG_RW;
595 break; 551 break;
@@ -609,48 +565,45 @@ static void parse_tune2fs_options(int argc, char **argv)
609 io_options = strchr(argv[optind], '?'); 565 io_options = strchr(argv[optind], '?');
610 if (io_options) 566 if (io_options)
611 *io_options++ = 0; 567 *io_options++ = 0;
612 device_name = blkid_get_devname(NULL, argv[optind], NULL); 568 device_name = x_blkid_get_devname(argv[optind]);
613 if (!device_name)
614 bb_error_msg_and_die("Unable to resolve '%s'", argv[optind]);
615} 569}
616 570
617#ifdef CONFIG_FINDFS 571#ifdef CONFIG_FINDFS
618static attribute_noreturn void do_findfs(int argc, char **argv) 572static attribute_noreturn void do_findfs(int argc, char **argv)
619{ 573{
620 char *dev;
621
622 if ((argc != 2) || 574 if ((argc != 2) ||
623 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) 575 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
624 bb_show_usage(); 576 bb_show_usage();
625 dev = blkid_get_devname(NULL, argv[1], NULL); 577 device_name = x_blkid_get_devname(argv[1]);
626 if (!dev) 578 puts(device_name);
627 bb_error_msg_and_die("Unable to resolve '%s'", argv[1]);
628 puts(dev);
629 exit(0); 579 exit(0);
630} 580}
581#else
582#define do_findfs(x, y)
631#endif 583#endif
632 584
585static void clean_up(void)
586{
587 if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
588 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
589}
590
633int tune2fs_main(int argc, char **argv) 591int tune2fs_main(int argc, char **argv)
634{ 592{
635 errcode_t retval; 593 errcode_t retval;
636 ext2_filsys fs; 594 ext2_filsys fs;
637 struct ext2_super_block *sb; 595 struct ext2_super_block *sb;
638 io_manager io_ptr; 596 io_manager io_ptr;
639#if defined(CONFIG_FINDFS) || defined(CONFIG_E2LABEL)
640 char *program_name = basename(argv[0]);
641#endif
642 597
643#ifdef CONFIG_FINDFS 598 if (ENABLE_FEATURE_CLEAN_UP)
644 if (strcmp(program_name, "findfs") == 0) 599 atexit(clean_up);
645 do_findfs(argc, argv); 600
646#endif 601 if (ENABLE_FINDFS && (bb_applet_name[0] == 'f')) /* findfs */
647 602 do_findfs(argc, argv); /* no return */
648#ifdef CONFIG_E2LABEL 603 else if (ENABLE_E2LABEL && (bb_applet_name[0] == 'e')) /* e2label */
649 if (strcmp(program_name, "e2label") == 0)
650 parse_e2label_options(argc, argv); 604 parse_e2label_options(argc, argv);
651 else 605 else
652#endif 606 parse_tune2fs_options(argc, argv); /* tune2fs */
653 parse_tune2fs_options(argc, argv);
654 607
655 io_ptr = unix_io_manager; 608 io_ptr = unix_io_manager;
656 retval = ext2fs_open2(device_name, io_options, open_flag, 609 retval = ext2fs_open2(device_name, io_options, open_flag,
@@ -662,7 +615,7 @@ int tune2fs_main(int argc, char **argv)
662 /* For e2label emulation */ 615 /* For e2label emulation */
663 printf("%.*s\n", (int) sizeof(sb->s_volume_name), 616 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
664 sb->s_volume_name); 617 sb->s_volume_name);
665 exit(0); 618 return 0;
666 } 619 }
667 retval = ext2fs_check_if_mounted(device_name, &mount_flags); 620 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
668 if (retval) 621 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)
29 exit(1); 29 exit(1);
30} 30}
31 31
32void check_plausibility(const char *device) 32void check_plausibility(const char *device, int force)
33{ 33{
34 int val; 34 int val;
35#ifdef CONFIG_LFS 35#ifdef CONFIG_LFS
@@ -39,7 +39,8 @@ void check_plausibility(const char *device)
39 struct stat s; 39 struct stat s;
40 val = stat(device, &s); 40 val = stat(device, &s);
41#endif 41#endif
42 42 if (force)
43 return;
43 if(val == -1) 44 if(val == -1)
44 bb_perror_msg_and_die("Could not stat %s", device); 45 bb_perror_msg_and_die("Could not stat %s", device);
45 if (!S_ISBLK(s.st_mode)) { 46 if (!S_ISBLK(s.st_mode)) {
@@ -205,3 +206,50 @@ void print_check_message(ext2_filsys fs)
205 fs->super->s_max_mnt_count, 206 fs->super->s_max_mnt_count,
206 (double)fs->super->s_checkinterval / (3600 * 24)); 207 (double)fs->super->s_checkinterval / (3600 * 24));
207} 208}
209
210void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force)
211{
212 errcode_t retval;
213 ext2_filsys jfs;
214 io_manager io_ptr;
215
216 check_plausibility(journal_device, force);
217 check_mount(journal_device, force, "journal");
218 io_ptr = unix_io_manager;
219 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
220 EXT2_FLAG_JOURNAL_DEV_OK, 0,
221 fs->blocksize, io_ptr, &jfs);
222 if (retval)
223 bb_error_msg_and_die("Could not journal device %s", journal_device);
224 if(!quiet)
225 printf("Adding journal to device %s: ", journal_device);
226 fflush(stdout);
227 retval = ext2fs_add_journal_device(fs, jfs);
228 if(retval)
229 bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device);
230 if(!quiet)
231 puts("done");
232 ext2fs_close(jfs);
233}
234
235void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet)
236{
237 unsigned long journal_blocks;
238 errcode_t retval;
239
240 journal_blocks = figure_journal_size(journal_size, fs);
241 if (!journal_blocks) {
242 fs->super->s_feature_compat &=
243 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
244 return;
245 }
246 if(!quiet)
247 printf("Creating journal (%ld blocks): ", journal_blocks);
248 fflush(stdout);
249 retval = ext2fs_add_journal_inode(fs, journal_blocks,
250 journal_flags);
251 if(retval)
252 bb_error_msg_and_die("Could not create journal");
253 if(!quiet)
254 puts("done");
255}
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 @@
11 */ 11 */
12 12
13extern void proceed_question(void); 13extern void proceed_question(void);
14extern void check_plausibility(const char *device); 14extern void check_plausibility(const char *device, int force);
15extern void parse_journal_opts(char **, int *, int *, const char *opts); 15extern void parse_journal_opts(char **, int *, int *, const char *opts);
16extern void check_mount(const char *device, int force, const char *type); 16extern void check_mount(const char *device, int force, const char *type);
17extern int figure_journal_size(int size, ext2_filsys fs); 17extern int figure_journal_size(int size, ext2_filsys fs);
18extern void print_check_message(ext2_filsys fs); 18extern void print_check_message(ext2_filsys fs);
19extern void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force);
20extern void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet);
21