2 * tune2fs.c - Change the file system parameters on an ext2 file system
4 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
11 * This file may be redistributed under the terms of the GNU Public
19 * 93/10/31 - Added the -c option to change the maximal mount counts
20 * 93/12/14 - Added -l flag to list contents of superblock
21 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23 * 93/12/29 - Added the -e option to change errors behavior
24 * 94/02/27 - Ported to use the ext2fs library
25 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
42 #include <sys/types.h>
45 #include "ext2fs/ext2_fs.h"
46 #include "ext2fs/ext2fs.h"
47 #include "uuid/uuid.h"
49 #include "ext2fs/jfs_user.h"
51 #include "blkid/blkid.h"
53 static char * device_name = NULL;
54 static char * new_label, *new_last_mounted, *new_UUID;
55 static char * io_options;
56 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
57 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
58 static time_t last_check_time;
59 static int print_label;
60 static int max_mount_count, mount_count, mount_flags;
61 static unsigned long interval, reserved_ratio, reserved_blocks;
62 static unsigned long resgid, resuid;
63 static unsigned short errors;
65 static char *features_cmd;
66 static char *mntopts_cmd;
68 static int journal_size, journal_flags;
69 static char *journal_device = NULL;
71 static const char *please_fsck = "Please run e2fsck on the filesystem\n";
73 static __u32 ok_features[3] = {
74 EXT3_FEATURE_COMPAT_HAS_JOURNAL | EXT2_FEATURE_COMPAT_DIR_INDEX,
75 EXT2_FEATURE_INCOMPAT_FILETYPE,
76 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
80 * Remove an external journal from the filesystem
82 static void remove_journal_device(ext2_filsys fs)
87 journal_superblock_t *jsb;
90 int commit_remove_journal = 0;
94 commit_remove_journal = 1; /* force removal even if error */
96 uuid_unparse(fs->super->s_journal_uuid, buf);
97 journal_path = blkid_get_devname(NULL, "UUID", buf);
101 ext2fs_find_block_device(fs->super->s_journal_dev);
106 io_ptr = unix_io_manager;
107 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
108 EXT2_FLAG_JOURNAL_DEV_OK, 0,
109 fs->blocksize, io_ptr, &jfs);
111 bb_error_msg("Failed to open external journal");
112 goto no_valid_journal;
114 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
115 bb_error_msg("%s is not a journal device", journal_path);
116 goto no_valid_journal;
119 /* Get the journal superblock */
120 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
121 bb_error_msg("Failed to read journal superblock");
122 goto no_valid_journal;
125 jsb = (journal_superblock_t *) buf;
126 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
127 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
128 bb_error_msg("Journal superblock not found!");
129 goto no_valid_journal;
132 /* Find the filesystem UUID */
133 nr_users = ntohl(jsb->s_nr_users);
134 for (i=0; i < nr_users; i++) {
135 if (memcmp(fs->super->s_uuid,
136 &jsb->s_users[i*16], 16) == 0)
140 bb_error_msg("Filesystem's UUID not found on journal device");
141 commit_remove_journal = 1;
142 goto no_valid_journal;
145 for (i=0; i < nr_users; i++)
146 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
147 jsb->s_nr_users = htonl(nr_users);
149 /* Write back the journal superblock */
150 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
151 bb_error_msg("Failed to write journal superblock");
152 goto no_valid_journal;
155 commit_remove_journal = 1;
158 if (commit_remove_journal == 0)
159 bb_error_msg_and_die("Journal NOT removed");
160 fs->super->s_journal_dev = 0;
161 uuid_clear(fs->super->s_journal_uuid);
162 ext2fs_mark_super_dirty(fs);
163 puts("Journal removed");
167 /* Helper function for remove_journal_inode */
168 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
169 int blockcnt EXT2FS_ATTR((unused)),
170 void *private EXT2FS_ATTR((unused)))
176 ext2fs_unmark_block_bitmap(fs->block_map,block);
177 group = ext2fs_group_of_blk(fs, block);
178 fs->group_desc[group].bg_free_blocks_count++;
179 fs->super->s_free_blocks_count++;
184 * Remove the journal inode from the filesystem
186 static void remove_journal_inode(ext2_filsys fs)
188 struct ext2_inode inode;
190 ino_t ino = fs->super->s_journal_inum;
191 char *msg = "to read";
192 char *s = "journal inode";
194 retval = ext2fs_read_inode(fs, ino, &inode);
196 goto REMOVE_JOURNAL_INODE_ERROR;
197 if (ino == EXT2_JOURNAL_INO) {
198 retval = ext2fs_read_bitmaps(fs);
200 msg = "to read bitmaps";
202 goto REMOVE_JOURNAL_INODE_ERROR;
204 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
205 release_blocks_proc, NULL);
208 goto REMOVE_JOURNAL_INODE_ERROR;
210 memset(&inode, 0, sizeof(inode));
211 ext2fs_mark_bb_dirty(fs);
212 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
214 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
215 retval = ext2fs_write_inode(fs, ino, &inode);
218 REMOVE_JOURNAL_INODE_ERROR:
219 bb_error_msg_and_die("Failed %s %s", msg, s);
221 fs->super->s_journal_inum = 0;
222 ext2fs_mark_super_dirty(fs);
226 * Update the default mount options
228 static void update_mntopts(ext2_filsys fs, char *mntopts)
230 struct ext2_super_block *sb= fs->super;
232 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
233 bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
234 ext2fs_mark_super_dirty(fs);
238 * Update the feature set as provided by the user.
240 static void update_feature_set(ext2_filsys fs, char *features)
242 int sparse, old_sparse, filetype, old_filetype;
243 int journal, old_journal, dxdir, old_dxdir;
244 struct ext2_super_block *sb= fs->super;
245 __u32 old_compat, old_incompat, old_ro_compat;
247 old_compat = sb->s_feature_compat;
248 old_ro_compat = sb->s_feature_ro_compat;
249 old_incompat = sb->s_feature_incompat;
251 old_sparse = sb->s_feature_ro_compat &
252 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
253 old_filetype = sb->s_feature_incompat &
254 EXT2_FEATURE_INCOMPAT_FILETYPE;
255 old_journal = sb->s_feature_compat &
256 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
257 old_dxdir = sb->s_feature_compat &
258 EXT2_FEATURE_COMPAT_DIR_INDEX;
259 if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
260 bb_error_msg_and_die("Invalid filesystem option set: %s", features);
261 sparse = sb->s_feature_ro_compat &
262 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
263 filetype = sb->s_feature_incompat &
264 EXT2_FEATURE_INCOMPAT_FILETYPE;
265 journal = sb->s_feature_compat &
266 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
267 dxdir = sb->s_feature_compat &
268 EXT2_FEATURE_COMPAT_DIR_INDEX;
269 if (old_journal && !journal) {
270 if ((mount_flags & EXT2_MF_MOUNTED) &&
271 !(mount_flags & EXT2_MF_READONLY)) {
272 bb_error_msg_and_die(
273 "The has_journal flag may only be "
274 "cleared when the filesystem is\n"
275 "unmounted or mounted "
278 if (sb->s_feature_incompat &
279 EXT3_FEATURE_INCOMPAT_RECOVER) {
280 bb_error_msg_and_die(
281 "The needs_recovery flag is set. "
282 "%s before clearing the has_journal flag.",
285 if (sb->s_journal_inum) {
286 remove_journal_inode(fs);
288 if (sb->s_journal_dev) {
289 remove_journal_device(fs);
292 if (journal && !old_journal) {
294 * If adding a journal flag, let the create journal
295 * code below handle creating setting the flag and
296 * creating the journal. We supply a default size if
301 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
303 if (dxdir && !old_dxdir) {
304 if (!sb->s_def_hash_version)
305 sb->s_def_hash_version = EXT2_HASH_TEA;
306 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
307 uuid_generate((unsigned char *) sb->s_hash_seed);
310 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
311 (sb->s_feature_compat || sb->s_feature_ro_compat ||
312 sb->s_feature_incompat))
313 ext2fs_update_dynamic_rev(fs);
314 if ((sparse != old_sparse) ||
315 (filetype != old_filetype)) {
316 sb->s_state &= ~EXT2_VALID_FS;
317 printf("\n%s\n", please_fsck);
319 if ((old_compat != sb->s_feature_compat) ||
320 (old_ro_compat != sb->s_feature_ro_compat) ||
321 (old_incompat != sb->s_feature_incompat))
322 ext2fs_mark_super_dirty(fs);
326 * Add a journal to the filesystem.
328 static void add_journal(ext2_filsys fs)
330 if (fs->super->s_feature_compat &
331 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
332 bb_error_msg_and_die("The filesystem already has a journal");
334 if (journal_device) {
335 make_journal_device(journal_device, fs, 0, 0);
336 } else if (journal_size) {
337 make_journal_blocks(fs, journal_size, journal_flags, 0);
339 * If the filesystem wasn't mounted, we need to force
340 * the block group descriptors out.
342 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
343 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
345 print_check_message(fs);
352 static char * x_blkid_get_devname(const char *token)
356 if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
357 bb_error_msg_and_die("Unable to resolve '%s'", token);
361 #ifdef CONFIG_E2LABEL
362 static void parse_e2label_options(int argc, char ** argv)
364 if ((argc < 2) || (argc > 3))
366 io_options = strchr(argv[1], '?');
369 device_name = x_blkid_get_devname(argv[1]);
371 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
378 #define parse_e2label_options(x,y)
381 static time_t parse_time(char *str)
385 if (strcmp(str, "now") == 0) {
388 memset(&ts, 0, sizeof(ts));
390 strptime(str, "%Y%m%d%H%M%S", &ts);
392 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
393 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
396 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
397 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
398 ts.tm_min > 59 || ts.tm_sec > 61)
401 if (ts.tm_mday == 0) {
402 bb_error_msg_and_die("Couldn't parse date/time specifier: %s", str);
404 return (mktime(&ts));
407 static void parse_tune2fs_options(int argc, char **argv)
412 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
413 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
417 if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) {
418 goto MOUNTS_COUNT_ERROR;
420 if (max_mount_count == 0)
421 max_mount_count = -1;
423 open_flag = EXT2_FLAG_RW;
426 if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) {
428 bb_error_msg_and_die("bad mounts count - %s", optarg);
431 open_flag = EXT2_FLAG_RW;
434 if (strcmp (optarg, "continue") == 0)
435 errors = EXT2_ERRORS_CONTINUE;
436 else if (strcmp (optarg, "remount-ro") == 0)
437 errors = EXT2_ERRORS_RO;
438 else if (strcmp (optarg, "panic") == 0)
439 errors = EXT2_ERRORS_PANIC;
441 bb_error_msg_and_die("bad error behavior - %s", optarg);
444 open_flag = EXT2_FLAG_RW;
446 case 'f': /* Force */
450 if (safe_strtoul(optarg, &resgid))
451 resgid = bb_xgetgrnam(optarg);
453 open_flag = EXT2_FLAG_RW;
456 interval = strtoul (optarg, &tmp, 0);
469 case 'M': /* months! */
470 interval *= 86400 * 30;
474 case 'W': /* weeks */
475 interval *= 86400 * 7;
479 if (*tmp || interval > (365 * 86400)) {
480 bb_error_msg_and_die("bad interval - %s", optarg);
483 open_flag = EXT2_FLAG_RW;
488 open_flag = EXT2_FLAG_RW;
491 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
492 open_flag = EXT2_FLAG_RW;
500 open_flag = EXT2_FLAG_RW |
501 EXT2_FLAG_JOURNAL_DEV_OK;
504 if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) {
505 bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
508 open_flag = EXT2_FLAG_RW;
511 new_last_mounted = optarg;
513 open_flag = EXT2_FLAG_RW;
517 bb_error_msg_and_die("-o may only be specified once");
519 mntopts_cmd = optarg;
520 open_flag = EXT2_FLAG_RW;
525 bb_error_msg_and_die("-O may only be specified once");
527 features_cmd = optarg;
528 open_flag = EXT2_FLAG_RW;
531 if(safe_strtoul(optarg, &reserved_blocks)) {
532 bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
535 open_flag = EXT2_FLAG_RW;
538 s_flag = atoi(optarg);
539 open_flag = EXT2_FLAG_RW;
543 last_check_time = parse_time(optarg);
544 open_flag = EXT2_FLAG_RW;
547 if (safe_strtoul(optarg, &resuid))
548 resuid = bb_xgetpwnam(optarg);
550 open_flag = EXT2_FLAG_RW;
555 open_flag = EXT2_FLAG_RW |
556 EXT2_FLAG_JOURNAL_DEV_OK;
561 if (optind < argc - 1 || optind == argc)
563 if (!open_flag && !l_flag)
565 io_options = strchr(argv[optind], '?');
568 device_name = x_blkid_get_devname(argv[optind]);
572 static attribute_noreturn void do_findfs(int argc, char **argv)
575 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
577 device_name = x_blkid_get_devname(argv[1]);
582 #define do_findfs(x, y)
585 static void clean_up(void)
587 if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
588 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
591 int tune2fs_main(int argc, char **argv)
595 struct ext2_super_block *sb;
598 if (ENABLE_FEATURE_CLEAN_UP)
601 if (ENABLE_FINDFS && (bb_applet_name[0] == 'f')) /* findfs */
602 do_findfs(argc, argv); /* no return */
603 else if (ENABLE_E2LABEL && (bb_applet_name[0] == 'e')) /* e2label */
604 parse_e2label_options(argc, argv);
606 parse_tune2fs_options(argc, argv); /* tune2fs */
608 io_ptr = unix_io_manager;
609 retval = ext2fs_open2(device_name, io_options, open_flag,
612 bb_error_msg_and_die("No valid superblock on %s", device_name);
615 /* For e2label emulation */
616 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
620 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
622 bb_error_msg_and_die("Could not determine if %s is mounted", device_name);
623 /* Normally we only need to write out the superblock */
624 fs->flags |= EXT2_FLAG_SUPER_ONLY;
627 sb->s_max_mnt_count = max_mount_count;
628 ext2fs_mark_super_dirty(fs);
629 printf("Setting maximal mount count to %d\n", max_mount_count);
632 sb->s_mnt_count = mount_count;
633 ext2fs_mark_super_dirty(fs);
634 printf("Setting current mount count to %d\n", mount_count);
637 sb->s_errors = errors;
638 ext2fs_mark_super_dirty(fs);
639 printf("Setting error behavior to %d\n", errors);
642 sb->s_def_resgid = resgid;
643 ext2fs_mark_super_dirty(fs);
644 printf("Setting reserved blocks gid to %lu\n", resgid);
647 sb->s_checkinterval = interval;
648 ext2fs_mark_super_dirty(fs);
649 printf("Setting interval between check %lu seconds\n", interval);
652 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
654 ext2fs_mark_super_dirty(fs);
655 printf("Setting reserved blocks percentage to %lu (%u blocks)\n",
656 reserved_ratio, sb->s_r_blocks_count);
659 if (reserved_blocks >= sb->s_blocks_count/2)
660 bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
661 sb->s_r_blocks_count = reserved_blocks;
662 ext2fs_mark_super_dirty(fs);
663 printf("Setting reserved blocks count to %lu\n", reserved_blocks);
666 if (sb->s_feature_ro_compat &
667 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
668 bb_error_msg("\nThe filesystem already has sparse superblocks\n");
670 sb->s_feature_ro_compat |=
671 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
672 sb->s_state &= ~EXT2_VALID_FS;
673 ext2fs_mark_super_dirty(fs);
674 printf("\nSparse superblock flag set. %s", please_fsck);
678 if (!(sb->s_feature_ro_compat &
679 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
680 bb_error_msg("\nThe filesystem already has sparse superblocks disabled\n");
682 sb->s_feature_ro_compat &=
683 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
684 sb->s_state &= ~EXT2_VALID_FS;
685 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
686 ext2fs_mark_super_dirty(fs);
687 printf("\nSparse superblock flag cleared. %s", please_fsck);
691 sb->s_lastcheck = last_check_time;
692 ext2fs_mark_super_dirty(fs);
693 printf("Setting time filesystem last checked to %s\n",
694 ctime(&last_check_time));
697 sb->s_def_resuid = resuid;
698 ext2fs_mark_super_dirty(fs);
699 printf("Setting reserved blocks uid to %lu\n", resuid);
702 if (strlen(new_label) > sizeof(sb->s_volume_name))
703 bb_error_msg("Warning: label too long, truncating\n");
704 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
705 strncpy(sb->s_volume_name, new_label,
706 sizeof(sb->s_volume_name));
707 ext2fs_mark_super_dirty(fs);
710 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
711 strncpy(sb->s_last_mounted, new_last_mounted,
712 sizeof(sb->s_last_mounted));
713 ext2fs_mark_super_dirty(fs);
716 update_mntopts(fs, mntopts_cmd);
718 update_feature_set(fs, features_cmd);
719 if (journal_size || journal_device)
723 if ((strcasecmp(new_UUID, "null") == 0) ||
724 (strcasecmp(new_UUID, "clear") == 0)) {
725 uuid_clear(sb->s_uuid);
726 } else if (strcasecmp(new_UUID, "time") == 0) {
727 uuid_generate_time(sb->s_uuid);
728 } else if (strcasecmp(new_UUID, "random") == 0) {
729 uuid_generate(sb->s_uuid);
730 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
731 bb_error_msg_and_die("Invalid UUID format");
733 ext2fs_mark_super_dirty(fs);
738 return (ext2fs_close (fs) ? 1 : 0);