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;
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;
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;
192 retval = ext2fs_read_inode(fs, ino, &inode);
194 bb_error_msg_and_die("Failed to read journal inode");
195 if (ino == EXT2_JOURNAL_INO) {
196 retval = ext2fs_read_bitmaps(fs);
198 bb_error_msg_and_die("Failed to read bitmaps");
199 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
200 release_blocks_proc, NULL);
202 bb_error_msg_and_die("Failed clearing journal inode");
203 memset(&inode, 0, sizeof(inode));
204 ext2fs_mark_bb_dirty(fs);
205 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
207 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
208 retval = ext2fs_write_inode(fs, ino, &inode);
210 bb_error_msg_and_die("Failed writing journal inode");
211 fs->super->s_journal_inum = 0;
212 ext2fs_mark_super_dirty(fs);
216 * Update the default mount options
218 static void update_mntopts(ext2_filsys fs, char *mntopts)
220 struct ext2_super_block *sb= fs->super;
222 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
223 bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
224 ext2fs_mark_super_dirty(fs);
228 * Update the feature set as provided by the user.
230 static void update_feature_set(ext2_filsys fs, char *features)
232 int sparse, old_sparse, filetype, old_filetype;
233 int journal, old_journal, dxdir, old_dxdir;
234 struct ext2_super_block *sb= fs->super;
235 __u32 old_compat, old_incompat, old_ro_compat;
237 old_compat = sb->s_feature_compat;
238 old_ro_compat = sb->s_feature_ro_compat;
239 old_incompat = sb->s_feature_incompat;
241 old_sparse = sb->s_feature_ro_compat &
242 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
243 old_filetype = sb->s_feature_incompat &
244 EXT2_FEATURE_INCOMPAT_FILETYPE;
245 old_journal = sb->s_feature_compat &
246 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
247 old_dxdir = sb->s_feature_compat &
248 EXT2_FEATURE_COMPAT_DIR_INDEX;
249 if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
250 bb_error_msg_and_die("Invalid filesystem option set: %s", features);
251 sparse = sb->s_feature_ro_compat &
252 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
253 filetype = sb->s_feature_incompat &
254 EXT2_FEATURE_INCOMPAT_FILETYPE;
255 journal = sb->s_feature_compat &
256 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
257 dxdir = sb->s_feature_compat &
258 EXT2_FEATURE_COMPAT_DIR_INDEX;
259 if (old_journal && !journal) {
260 if ((mount_flags & EXT2_MF_MOUNTED) &&
261 !(mount_flags & EXT2_MF_READONLY)) {
262 bb_error_msg_and_die(
263 "The has_journal flag may only be "
264 "cleared when the filesystem is\n"
265 "unmounted or mounted "
268 if (sb->s_feature_incompat &
269 EXT3_FEATURE_INCOMPAT_RECOVER) {
270 bb_error_msg_and_die(
271 "The needs_recovery flag is set. "
272 "Please run e2fsck before clearing\n"
273 "the has_journal flag.");
275 if (sb->s_journal_inum) {
276 remove_journal_inode(fs);
278 if (sb->s_journal_dev) {
279 remove_journal_device(fs);
282 if (journal && !old_journal) {
284 * If adding a journal flag, let the create journal
285 * code below handle creating setting the flag and
286 * creating the journal. We supply a default size if
291 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
293 if (dxdir && !old_dxdir) {
294 if (!sb->s_def_hash_version)
295 sb->s_def_hash_version = EXT2_HASH_TEA;
296 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
297 uuid_generate((unsigned char *) sb->s_hash_seed);
300 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
301 (sb->s_feature_compat || sb->s_feature_ro_compat ||
302 sb->s_feature_incompat))
303 ext2fs_update_dynamic_rev(fs);
304 if ((sparse != old_sparse) ||
305 (filetype != old_filetype)) {
306 sb->s_state &= ~EXT2_VALID_FS;
307 printf("\n%s\n", please_fsck);
309 if ((old_compat != sb->s_feature_compat) ||
310 (old_ro_compat != sb->s_feature_ro_compat) ||
311 (old_incompat != sb->s_feature_incompat))
312 ext2fs_mark_super_dirty(fs);
316 * Add a journal to the filesystem.
318 static void add_journal(ext2_filsys fs)
320 unsigned long journal_blocks;
325 if (fs->super->s_feature_compat &
326 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
327 bb_error_msg("The filesystem already has a journal");
330 if (journal_device) {
331 check_plausibility(journal_device);
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);
338 bb_error_msg("Failed to open journal on %s", journal_device);
341 printf("Creating journal on device %s: ", journal_device);
344 retval = ext2fs_add_journal_device(fs, jfs);
347 bb_error_msg("Failed to add filesystem to journal on %s", journal_device);
351 } else if (journal_size) {
352 fputs("Creating journal inode: ", stdout);
354 journal_blocks = figure_journal_size(journal_size, fs);
356 retval = ext2fs_add_journal_inode(fs, journal_blocks,
359 bb_error_msg_and_die("Failed to create journal file");
363 * If the filesystem wasn't mounted, we need to force
364 * the block group descriptors out.
366 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
367 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
369 print_check_message(fs);
374 free(journal_device);
379 static void parse_e2label_options(int argc, char ** argv)
381 if ((argc < 2) || (argc > 3))
383 io_options = strchr(argv[1], '?');
386 device_name = blkid_get_devname(NULL, argv[1], NULL);
388 bb_error_msg_and_die("Unable to resolve '%s'", argv[1]);
390 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
397 static time_t parse_time(char *str)
401 if (strcmp(str, "now") == 0) {
404 memset(&ts, 0, sizeof(ts));
406 strptime(str, "%Y%m%d%H%M%S", &ts);
408 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
409 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
412 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
413 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
414 ts.tm_min > 59 || ts.tm_sec > 61)
417 if (ts.tm_mday == 0) {
418 bb_error_msg_and_die("Couldn't parse date/time specifier: %s", str);
420 return (mktime(&ts));
423 static void parse_tune2fs_options(int argc, char **argv)
430 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
431 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
435 max_mount_count = strtol (optarg, &tmp, 0);
436 if (*tmp || max_mount_count > 16000) {
437 bb_error_msg_and_die("bad mounts count - %s", optarg);
439 if (max_mount_count == 0)
440 max_mount_count = -1;
442 open_flag = EXT2_FLAG_RW;
445 mount_count = strtoul (optarg, &tmp, 0);
446 if (*tmp || mount_count > 16000) {
447 bb_error_msg_and_die("bad mounts count - %s", optarg);
450 open_flag = EXT2_FLAG_RW;
453 if (strcmp (optarg, "continue") == 0)
454 errors = EXT2_ERRORS_CONTINUE;
455 else if (strcmp (optarg, "remount-ro") == 0)
456 errors = EXT2_ERRORS_RO;
457 else if (strcmp (optarg, "panic") == 0)
458 errors = EXT2_ERRORS_PANIC;
460 bb_error_msg_and_die("bad error behavior - %s", optarg);
463 open_flag = EXT2_FLAG_RW;
465 case 'f': /* Force */
469 resgid = strtoul (optarg, &tmp, 0);
471 gr = getgrnam (optarg);
480 bb_error_msg_and_die("bad gid/group name - %s", optarg);
483 open_flag = EXT2_FLAG_RW;
486 interval = strtoul (optarg, &tmp, 0);
499 case 'M': /* months! */
500 interval *= 86400 * 30;
504 case 'W': /* weeks */
505 interval *= 86400 * 7;
509 if (*tmp || interval > (365 * 86400)) {
510 bb_error_msg_and_die("bad interval - %s", optarg);
513 open_flag = EXT2_FLAG_RW;
518 open_flag = EXT2_FLAG_RW;
521 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
522 open_flag = EXT2_FLAG_RW;
530 open_flag = EXT2_FLAG_RW |
531 EXT2_FLAG_JOURNAL_DEV_OK;
534 reserved_ratio = strtoul (optarg, &tmp, 0);
535 if (*tmp || reserved_ratio > 50) {
536 bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
539 open_flag = EXT2_FLAG_RW;
542 new_last_mounted = optarg;
544 open_flag = EXT2_FLAG_RW;
548 bb_error_msg_and_die("-o may only be specified once");
550 mntopts_cmd = optarg;
551 open_flag = EXT2_FLAG_RW;
556 bb_error_msg_and_die("-O may only be specified once");
558 features_cmd = optarg;
559 open_flag = EXT2_FLAG_RW;
562 reserved_blocks = strtoul (optarg, &tmp, 0);
564 bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
567 open_flag = EXT2_FLAG_RW;
570 s_flag = atoi(optarg);
571 open_flag = EXT2_FLAG_RW;
575 last_check_time = parse_time(optarg);
576 open_flag = EXT2_FLAG_RW;
579 resuid = strtoul (optarg, &tmp, 0);
581 pw = getpwnam (optarg);
590 bb_error_msg_and_die("bad uid/user name - %s", optarg);
593 open_flag = EXT2_FLAG_RW;
598 open_flag = EXT2_FLAG_RW |
599 EXT2_FLAG_JOURNAL_DEV_OK;
604 if (optind < argc - 1 || optind == argc)
606 if (!open_flag && !l_flag)
608 io_options = strchr(argv[optind], '?');
611 device_name = blkid_get_devname(NULL, argv[optind], NULL);
613 bb_error_msg_and_die("Unable to resolve '%s'", argv[optind]);
616 static void do_findfs(int argc, char **argv)
621 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
623 dev = blkid_get_devname(NULL, argv[1], NULL);
625 bb_error_msg_and_die("Unable to resolve '%s'", argv[1]);
630 int tune2fs_main(int argc, char **argv)
634 struct ext2_super_block *sb;
636 char *program_name = basename(argv[0]);
638 if (strcmp(program_name, "findfs") == 0)
639 do_findfs(argc, argv);
640 if (strcmp(program_name, "e2label") == 0)
641 parse_e2label_options(argc, argv);
643 parse_tune2fs_options(argc, argv);
645 io_ptr = unix_io_manager;
646 retval = ext2fs_open2(device_name, io_options, open_flag,
649 bb_error_msg_and_die("No valid superblock on %s", device_name);
652 /* For e2label emulation */
653 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
657 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
659 bb_error_msg_and_die("Could not determine if %s is mounted", device_name);
660 /* Normally we only need to write out the superblock */
661 fs->flags |= EXT2_FLAG_SUPER_ONLY;
664 sb->s_max_mnt_count = max_mount_count;
665 ext2fs_mark_super_dirty(fs);
666 printf("Setting maximal mount count to %d\n", max_mount_count);
669 sb->s_mnt_count = mount_count;
670 ext2fs_mark_super_dirty(fs);
671 printf("Setting current mount count to %d\n", mount_count);
674 sb->s_errors = errors;
675 ext2fs_mark_super_dirty(fs);
676 printf("Setting error behavior to %d\n", errors);
679 sb->s_def_resgid = resgid;
680 ext2fs_mark_super_dirty(fs);
681 printf("Setting reserved blocks gid to %lu\n", resgid);
684 sb->s_checkinterval = interval;
685 ext2fs_mark_super_dirty(fs);
686 printf("Setting interval between check %lu seconds\n", interval);
689 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
691 ext2fs_mark_super_dirty(fs);
692 printf("Setting reserved blocks percentage to %lu (%u blocks)\n",
693 reserved_ratio, sb->s_r_blocks_count);
696 if (reserved_blocks >= sb->s_blocks_count/2)
697 bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
698 sb->s_r_blocks_count = reserved_blocks;
699 ext2fs_mark_super_dirty(fs);
700 printf("Setting reserved blocks count to %lu\n", reserved_blocks);
703 if (sb->s_feature_ro_compat &
704 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
705 bb_error_msg("\nThe filesystem already has sparse superblocks\n");
707 sb->s_feature_ro_compat |=
708 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
709 sb->s_state &= ~EXT2_VALID_FS;
710 ext2fs_mark_super_dirty(fs);
711 printf("\nSparse superblock flag set. %s", please_fsck);
715 if (!(sb->s_feature_ro_compat &
716 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
717 bb_error_msg("\nThe filesystem already has sparse superblocks disabled\n");
719 sb->s_feature_ro_compat &=
720 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
721 sb->s_state &= ~EXT2_VALID_FS;
722 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
723 ext2fs_mark_super_dirty(fs);
724 printf("\nSparse superblock flag cleared. %s", please_fsck);
728 sb->s_lastcheck = last_check_time;
729 ext2fs_mark_super_dirty(fs);
730 printf("Setting time filesystem last checked to %s\n",
731 ctime(&last_check_time));
734 sb->s_def_resuid = resuid;
735 ext2fs_mark_super_dirty(fs);
736 printf("Setting reserved blocks uid to %lu\n", resuid);
739 if (strlen(new_label) > sizeof(sb->s_volume_name))
740 bb_error_msg("Warning: label too long, truncating\n");
741 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
742 strncpy(sb->s_volume_name, new_label,
743 sizeof(sb->s_volume_name));
744 ext2fs_mark_super_dirty(fs);
747 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
748 strncpy(sb->s_last_mounted, new_last_mounted,
749 sizeof(sb->s_last_mounted));
750 ext2fs_mark_super_dirty(fs);
753 update_mntopts(fs, mntopts_cmd);
755 update_feature_set(fs, features_cmd);
756 if (journal_size || journal_device)
760 if ((strcasecmp(new_UUID, "null") == 0) ||
761 (strcasecmp(new_UUID, "clear") == 0)) {
762 uuid_clear(sb->s_uuid);
763 } else if (strcasecmp(new_UUID, "time") == 0) {
764 uuid_generate_time(sb->s_uuid);
765 } else if (strcasecmp(new_UUID, "random") == 0) {
766 uuid_generate(sb->s_uuid);
767 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
768 bb_error_msg_and_die("Invalid UUID format");
770 ext2fs_mark_super_dirty(fs);
775 return (ext2fs_close (fs) ? 1 : 0);