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)
28 #include <sys/types.h>
38 #include "ext2fs/ext2_fs.h"
39 #include "ext2fs/ext2fs.h"
40 #include "uuid/uuid.h"
42 #include "ext2fs/kernel-jbd.h"
44 #include "blkid/blkid.h"
50 static char * device_name = NULL;
51 static char * new_label, *new_last_mounted, *new_UUID;
52 static char * io_options;
53 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
54 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
55 static time_t last_check_time;
56 static int print_label;
57 static int max_mount_count, mount_count, mount_flags;
58 static unsigned long interval, reserved_ratio, reserved_blocks;
59 static unsigned long resgid, resuid;
60 static unsigned short errors;
62 static char *features_cmd;
63 static char *mntopts_cmd;
65 static int journal_size, journal_flags;
66 static char *journal_device = NULL;
68 static const char *please_fsck = "Please run e2fsck on the filesystem\n";
70 static __u32 ok_features[3] = {
71 EXT3_FEATURE_COMPAT_HAS_JOURNAL | EXT2_FEATURE_COMPAT_DIR_INDEX,
72 EXT2_FEATURE_INCOMPAT_FILETYPE,
73 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
77 * Remove an external journal from the filesystem
79 static void remove_journal_device(ext2_filsys fs)
84 journal_superblock_t *jsb;
87 int commit_remove_journal = 0;
91 commit_remove_journal = 1; /* force removal even if error */
93 uuid_unparse(fs->super->s_journal_uuid, buf);
94 journal_path = blkid_get_devname(NULL, "UUID", buf);
98 ext2fs_find_block_device(fs->super->s_journal_dev);
103 io_ptr = unix_io_manager;
104 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
105 EXT2_FLAG_JOURNAL_DEV_OK, 0,
106 fs->blocksize, io_ptr, &jfs);
108 bb_error_msg("Failed to open external journal");
109 goto no_valid_journal;
111 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
112 bb_error_msg("%s is not a journal device", journal_path);
113 goto no_valid_journal;
116 /* Get the journal superblock */
117 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
118 bb_error_msg("Failed to read journal superblock");
119 goto no_valid_journal;
122 jsb = (journal_superblock_t *) buf;
123 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
124 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
125 bb_error_msg("Journal superblock not found!");
126 goto no_valid_journal;
129 /* Find the filesystem UUID */
130 nr_users = ntohl(jsb->s_nr_users);
131 for (i=0; i < nr_users; i++) {
132 if (memcmp(fs->super->s_uuid,
133 &jsb->s_users[i*16], 16) == 0)
137 bb_error_msg("Filesystem's UUID not found on journal device");
138 commit_remove_journal = 1;
139 goto no_valid_journal;
142 for (i=0; i < nr_users; i++)
143 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
144 jsb->s_nr_users = htonl(nr_users);
146 /* Write back the journal superblock */
147 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
148 bb_error_msg("Failed to write journal superblock");
149 goto no_valid_journal;
152 commit_remove_journal = 1;
155 if (commit_remove_journal == 0)
156 bb_error_msg_and_die("Journal NOT removed");
157 fs->super->s_journal_dev = 0;
158 uuid_clear(fs->super->s_journal_uuid);
159 ext2fs_mark_super_dirty(fs);
160 puts("Journal removed");
164 /* Helper function for remove_journal_inode */
165 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
166 int blockcnt EXT2FS_ATTR((unused)),
167 void *private EXT2FS_ATTR((unused)))
173 ext2fs_unmark_block_bitmap(fs->block_map,block);
174 group = ext2fs_group_of_blk(fs, block);
175 fs->group_desc[group].bg_free_blocks_count++;
176 fs->super->s_free_blocks_count++;
181 * Remove the journal inode from the filesystem
183 static void remove_journal_inode(ext2_filsys fs)
185 struct ext2_inode inode;
187 ino_t ino = fs->super->s_journal_inum;
188 char *msg = "to read";
189 char *s = "journal inode";
191 retval = ext2fs_read_inode(fs, ino, &inode);
193 goto REMOVE_JOURNAL_INODE_ERROR;
194 if (ino == EXT2_JOURNAL_INO) {
195 retval = ext2fs_read_bitmaps(fs);
197 msg = "to read bitmaps";
199 goto REMOVE_JOURNAL_INODE_ERROR;
201 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
202 release_blocks_proc, NULL);
205 goto REMOVE_JOURNAL_INODE_ERROR;
207 memset(&inode, 0, sizeof(inode));
208 ext2fs_mark_bb_dirty(fs);
209 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
211 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
212 retval = ext2fs_write_inode(fs, ino, &inode);
215 REMOVE_JOURNAL_INODE_ERROR:
216 bb_error_msg_and_die("Failed %s %s", msg, s);
218 fs->super->s_journal_inum = 0;
219 ext2fs_mark_super_dirty(fs);
223 * Update the default mount options
225 static void update_mntopts(ext2_filsys fs, char *mntopts)
227 struct ext2_super_block *sb= fs->super;
229 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
230 bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
231 ext2fs_mark_super_dirty(fs);
235 * Update the feature set as provided by the user.
237 static void update_feature_set(ext2_filsys fs, char *features)
239 int sparse, old_sparse, filetype, old_filetype;
240 int journal, old_journal, dxdir, old_dxdir;
241 struct ext2_super_block *sb= fs->super;
242 __u32 old_compat, old_incompat, old_ro_compat;
244 old_compat = sb->s_feature_compat;
245 old_ro_compat = sb->s_feature_ro_compat;
246 old_incompat = sb->s_feature_incompat;
248 old_sparse = sb->s_feature_ro_compat &
249 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
250 old_filetype = sb->s_feature_incompat &
251 EXT2_FEATURE_INCOMPAT_FILETYPE;
252 old_journal = sb->s_feature_compat &
253 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
254 old_dxdir = sb->s_feature_compat &
255 EXT2_FEATURE_COMPAT_DIR_INDEX;
256 if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
257 bb_error_msg_and_die("Invalid filesystem option set: %s", features);
258 sparse = sb->s_feature_ro_compat &
259 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
260 filetype = sb->s_feature_incompat &
261 EXT2_FEATURE_INCOMPAT_FILETYPE;
262 journal = sb->s_feature_compat &
263 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
264 dxdir = sb->s_feature_compat &
265 EXT2_FEATURE_COMPAT_DIR_INDEX;
266 if (old_journal && !journal) {
267 if ((mount_flags & EXT2_MF_MOUNTED) &&
268 !(mount_flags & EXT2_MF_READONLY)) {
269 bb_error_msg_and_die(
270 "The has_journal flag may only be "
271 "cleared when the filesystem is\n"
272 "unmounted or mounted "
275 if (sb->s_feature_incompat &
276 EXT3_FEATURE_INCOMPAT_RECOVER) {
277 bb_error_msg_and_die(
278 "The needs_recovery flag is set. "
279 "%s before clearing the has_journal flag.",
282 if (sb->s_journal_inum) {
283 remove_journal_inode(fs);
285 if (sb->s_journal_dev) {
286 remove_journal_device(fs);
289 if (journal && !old_journal) {
291 * If adding a journal flag, let the create journal
292 * code below handle creating setting the flag and
293 * creating the journal. We supply a default size if
298 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
300 if (dxdir && !old_dxdir) {
301 if (!sb->s_def_hash_version)
302 sb->s_def_hash_version = EXT2_HASH_TEA;
303 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
304 uuid_generate((unsigned char *) sb->s_hash_seed);
307 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
308 (sb->s_feature_compat || sb->s_feature_ro_compat ||
309 sb->s_feature_incompat))
310 ext2fs_update_dynamic_rev(fs);
311 if ((sparse != old_sparse) ||
312 (filetype != old_filetype)) {
313 sb->s_state &= ~EXT2_VALID_FS;
314 printf("\n%s\n", please_fsck);
316 if ((old_compat != sb->s_feature_compat) ||
317 (old_ro_compat != sb->s_feature_ro_compat) ||
318 (old_incompat != sb->s_feature_incompat))
319 ext2fs_mark_super_dirty(fs);
323 * Add a journal to the filesystem.
325 static void add_journal(ext2_filsys fs)
327 if (fs->super->s_feature_compat &
328 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
329 bb_error_msg_and_die("The filesystem already has a journal");
331 if (journal_device) {
332 make_journal_device(journal_device, fs, 0, 0);
333 } else if (journal_size) {
334 make_journal_blocks(fs, journal_size, journal_flags, 0);
336 * If the filesystem wasn't mounted, we need to force
337 * the block group descriptors out.
339 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
340 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
342 print_check_message(fs);
349 static char * x_blkid_get_devname(const char *token)
353 if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
354 bb_error_msg_and_die("Unable to resolve '%s'", token);
358 #ifdef CONFIG_E2LABEL
359 static void parse_e2label_options(int argc, char ** argv)
361 if ((argc < 2) || (argc > 3))
363 io_options = strchr(argv[1], '?');
366 device_name = x_blkid_get_devname(argv[1]);
368 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
375 #define parse_e2label_options(x,y)
378 static time_t parse_time(char *str)
382 if (strcmp(str, "now") == 0) {
385 memset(&ts, 0, sizeof(ts));
387 strptime(str, "%Y%m%d%H%M%S", &ts);
389 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
390 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
393 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
394 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
395 ts.tm_min > 59 || ts.tm_sec > 61)
398 if (ts.tm_mday == 0) {
399 bb_error_msg_and_die("Couldn't parse date/time specifier: %s", str);
401 return (mktime(&ts));
404 static void parse_tune2fs_options(int argc, char **argv)
409 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
410 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
414 if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) {
415 goto MOUNTS_COUNT_ERROR;
417 if (max_mount_count == 0)
418 max_mount_count = -1;
420 open_flag = EXT2_FLAG_RW;
423 if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) {
425 bb_error_msg_and_die("bad mounts count - %s", optarg);
428 open_flag = EXT2_FLAG_RW;
431 if (strcmp (optarg, "continue") == 0)
432 errors = EXT2_ERRORS_CONTINUE;
433 else if (strcmp (optarg, "remount-ro") == 0)
434 errors = EXT2_ERRORS_RO;
435 else if (strcmp (optarg, "panic") == 0)
436 errors = EXT2_ERRORS_PANIC;
438 bb_error_msg_and_die("bad error behavior - %s", optarg);
441 open_flag = EXT2_FLAG_RW;
443 case 'f': /* Force */
447 if (safe_strtoul(optarg, &resgid))
448 resgid = bb_xgetgrnam(optarg);
450 open_flag = EXT2_FLAG_RW;
453 interval = strtoul (optarg, &tmp, 0);
466 case 'M': /* months! */
467 interval *= 86400 * 30;
471 case 'W': /* weeks */
472 interval *= 86400 * 7;
476 if (*tmp || interval > (365 * 86400)) {
477 bb_error_msg_and_die("bad interval - %s", optarg);
480 open_flag = EXT2_FLAG_RW;
485 open_flag = EXT2_FLAG_RW;
488 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
489 open_flag = EXT2_FLAG_RW;
497 open_flag = EXT2_FLAG_RW |
498 EXT2_FLAG_JOURNAL_DEV_OK;
501 if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) {
502 bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
505 open_flag = EXT2_FLAG_RW;
508 new_last_mounted = optarg;
510 open_flag = EXT2_FLAG_RW;
514 bb_error_msg_and_die("-o may only be specified once");
516 mntopts_cmd = optarg;
517 open_flag = EXT2_FLAG_RW;
522 bb_error_msg_and_die("-O may only be specified once");
524 features_cmd = optarg;
525 open_flag = EXT2_FLAG_RW;
528 if(safe_strtoul(optarg, &reserved_blocks)) {
529 bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
532 open_flag = EXT2_FLAG_RW;
535 s_flag = atoi(optarg);
536 open_flag = EXT2_FLAG_RW;
540 last_check_time = parse_time(optarg);
541 open_flag = EXT2_FLAG_RW;
544 if (safe_strtoul(optarg, &resuid))
545 resuid = bb_xgetpwnam(optarg);
547 open_flag = EXT2_FLAG_RW;
552 open_flag = EXT2_FLAG_RW |
553 EXT2_FLAG_JOURNAL_DEV_OK;
558 if (optind < argc - 1 || optind == argc)
560 if (!open_flag && !l_flag)
562 io_options = strchr(argv[optind], '?');
565 device_name = x_blkid_get_devname(argv[optind]);
569 static ATTRIBUTE_NORETURN void do_findfs(int argc, char **argv)
572 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
574 device_name = x_blkid_get_devname(argv[1]);
579 #define do_findfs(x, y)
582 static void tune2fs_clean_up(void)
584 if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
585 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
588 int tune2fs_main(int argc, char **argv)
592 struct ext2_super_block *sb;
595 if (ENABLE_FEATURE_CLEAN_UP)
596 atexit(tune2fs_clean_up);
598 if (ENABLE_FINDFS && (bb_applet_name[0] == 'f')) /* findfs */
599 do_findfs(argc, argv); /* no return */
600 else if (ENABLE_E2LABEL && (bb_applet_name[0] == 'e')) /* e2label */
601 parse_e2label_options(argc, argv);
603 parse_tune2fs_options(argc, argv); /* tune2fs */
605 io_ptr = unix_io_manager;
606 retval = ext2fs_open2(device_name, io_options, open_flag,
609 bb_error_msg_and_die("No valid superblock on %s", device_name);
612 /* For e2label emulation */
613 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
617 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
619 bb_error_msg_and_die("Could not determine if %s is mounted", device_name);
620 /* Normally we only need to write out the superblock */
621 fs->flags |= EXT2_FLAG_SUPER_ONLY;
624 sb->s_max_mnt_count = max_mount_count;
625 ext2fs_mark_super_dirty(fs);
626 printf("Setting maximal mount count to %d\n", max_mount_count);
629 sb->s_mnt_count = mount_count;
630 ext2fs_mark_super_dirty(fs);
631 printf("Setting current mount count to %d\n", mount_count);
634 sb->s_errors = errors;
635 ext2fs_mark_super_dirty(fs);
636 printf("Setting error behavior to %d\n", errors);
639 sb->s_def_resgid = resgid;
640 ext2fs_mark_super_dirty(fs);
641 printf("Setting reserved blocks gid to %lu\n", resgid);
644 sb->s_checkinterval = interval;
645 ext2fs_mark_super_dirty(fs);
646 printf("Setting interval between check %lu seconds\n", interval);
649 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
651 ext2fs_mark_super_dirty(fs);
652 printf("Setting reserved blocks percentage to %lu (%u blocks)\n",
653 reserved_ratio, sb->s_r_blocks_count);
656 if (reserved_blocks >= sb->s_blocks_count/2)
657 bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
658 sb->s_r_blocks_count = reserved_blocks;
659 ext2fs_mark_super_dirty(fs);
660 printf("Setting reserved blocks count to %lu\n", reserved_blocks);
663 if (sb->s_feature_ro_compat &
664 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
665 bb_error_msg("\nThe filesystem already has sparse superblocks\n");
667 sb->s_feature_ro_compat |=
668 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
669 sb->s_state &= ~EXT2_VALID_FS;
670 ext2fs_mark_super_dirty(fs);
671 printf("\nSparse superblock flag set. %s", please_fsck);
675 if (!(sb->s_feature_ro_compat &
676 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
677 bb_error_msg("\nThe filesystem already has sparse superblocks disabled\n");
679 sb->s_feature_ro_compat &=
680 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
681 sb->s_state &= ~EXT2_VALID_FS;
682 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
683 ext2fs_mark_super_dirty(fs);
684 printf("\nSparse superblock flag cleared. %s", please_fsck);
688 sb->s_lastcheck = last_check_time;
689 ext2fs_mark_super_dirty(fs);
690 printf("Setting time filesystem last checked to %s\n",
691 ctime(&last_check_time));
694 sb->s_def_resuid = resuid;
695 ext2fs_mark_super_dirty(fs);
696 printf("Setting reserved blocks uid to %lu\n", resuid);
699 if (strlen(new_label) > sizeof(sb->s_volume_name))
700 bb_error_msg("Warning: label too long, truncating\n");
701 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
702 strncpy(sb->s_volume_name, new_label,
703 sizeof(sb->s_volume_name));
704 ext2fs_mark_super_dirty(fs);
707 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
708 strncpy(sb->s_last_mounted, new_last_mounted,
709 sizeof(sb->s_last_mounted));
710 ext2fs_mark_super_dirty(fs);
713 update_mntopts(fs, mntopts_cmd);
715 update_feature_set(fs, features_cmd);
716 if (journal_size || journal_device)
720 if ((strcasecmp(new_UUID, "null") == 0) ||
721 (strcasecmp(new_UUID, "clear") == 0)) {
722 uuid_clear(sb->s_uuid);
723 } else if (strcasecmp(new_UUID, "time") == 0) {
724 uuid_generate_time(sb->s_uuid);
725 } else if (strcasecmp(new_UUID, "random") == 0) {
726 uuid_generate(sb->s_uuid);
727 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
728 bb_error_msg_and_die("Invalid UUID format");
730 ext2fs_mark_super_dirty(fs);
735 return (ext2fs_close (fs) ? 1 : 0);