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>
44 #include "ext2fs/ext2_fs.h"
45 #include "ext2fs/ext2fs.h"
46 #include "et/com_err.h"
47 #include "uuid/uuid.h"
51 #include "get_device_by_label.h"
53 #include "../version.h"
54 #include "nls-enable.h"
56 const char * program_name = "tune2fs";
58 char * new_label, *new_last_mounted, *new_UUID;
59 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
60 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag;
61 static int print_label;
62 static int max_mount_count, mount_count, mount_flags;
63 static unsigned long interval, reserved_ratio, reserved_blocks;
64 static unsigned long resgid, resuid;
65 static unsigned short errors;
67 static char *features_cmd;
69 int journal_size, journal_flags;
72 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
74 static void usage(void)
77 _("Usage: %s [-c max-mounts-count] [-e errors-behavior] "
79 "\t[-i interval[d|m|w]] [-j] [-J journal-options]\n"
80 "\t[-l] [-s sparse-flag] [-m reserved-blocks-percent]\n"
81 "\t[-r reserved-blocks-count] [-u user] [-C mount-count]\n"
82 "\t[-L volume-label] [-M last-mounted-dir] [-U UUID]\n"
83 "\t[-O [^]feature[,...]] device\n"), program_name);
87 static __u32 ok_features[3] = {
88 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
89 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
90 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
94 * Remove an external journal from the filesystem
96 static void remove_journal_device(ext2_filsys fs)
101 journal_superblock_t *jsb;
104 int commit_remove_journal = 0;
108 commit_remove_journal = 1; /* force removal even if error */
110 uuid_unparse(fs->super->s_journal_uuid, buf);
111 journal_device = get_spec_by_uuid(buf);
113 if (!journal_device) {
115 ext2fs_find_block_device(fs->super->s_journal_dev);
120 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
121 EXT2_FLAG_JOURNAL_DEV_OK, 0,
122 fs->blocksize, unix_io_manager, &jfs);
124 com_err(program_name, retval,
125 _("while trying to open external journal"));
126 goto no_valid_journal;
128 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
129 fprintf(stderr, _("%s is not a journal device.\n"),
131 goto no_valid_journal;
134 /* Get the journal superblock */
135 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
136 com_err(program_name, retval,
137 _("while reading journal superblock"));
138 goto no_valid_journal;
141 jsb = (journal_superblock_t *) buf;
142 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
143 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
144 fprintf(stderr, _("Journal superblock not found!\n"));
145 goto no_valid_journal;
148 /* Find the filesystem UUID */
149 nr_users = ntohl(jsb->s_nr_users);
150 for (i=0; i < nr_users; i++) {
151 if (memcmp(fs->super->s_uuid,
152 &jsb->s_users[i*16], 16) == 0)
157 _("Filesystem's UUID not found on journal device.\n"));
158 commit_remove_journal = 1;
159 goto no_valid_journal;
162 for (i=0; i < nr_users; i++)
163 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
164 jsb->s_nr_users = htonl(nr_users);
166 /* Write back the journal superblock */
167 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
168 com_err(program_name, retval,
169 "while writing journal superblock.");
170 goto no_valid_journal;
173 commit_remove_journal = 1;
176 if (commit_remove_journal == 0) {
177 fprintf(stderr, _("Journal NOT removed\n"));
180 fs->super->s_journal_dev = 0;
181 memset(fs->super->s_journal_uuid, 0,
182 sizeof(fs->super->s_journal_uuid));
183 ext2fs_mark_super_dirty(fs);
184 printf(_("Journal removed\n"));
185 free(journal_device);
188 /* Helper function for remove_journal_inode */
189 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
190 int blockcnt, void *private)
196 ext2fs_unmark_block_bitmap(fs->block_map,block);
197 group = ext2fs_group_of_blk(fs, block);
198 fs->group_desc[group].bg_free_blocks_count++;
199 fs->super->s_free_blocks_count++;
204 * Remove the journal inode from the filesystem
206 static void remove_journal_inode(ext2_filsys fs)
208 struct ext2_inode inode;
210 ino_t ino = fs->super->s_journal_inum;
213 retval = ext2fs_read_inode(fs, ino, &inode);
215 com_err(program_name, retval,
216 _("while reading journal inode"));
219 if (ino == EXT2_JOURNAL_INO) {
220 retval = ext2fs_read_bitmaps(fs);
222 com_err(program_name, retval,
223 _("while reading bitmaps"));
226 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
227 release_blocks_proc, NULL);
229 com_err(program_name, retval,
230 _("while clearing journal inode"));
233 memset(&inode, 0, sizeof(inode));
234 ext2fs_mark_bb_dirty(fs);
235 group = ext2fs_group_of_ino(fs, ino);
236 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
238 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
239 retval = ext2fs_write_inode(fs, ino, &inode);
241 com_err(program_name, retval,
242 _("while writing journal inode"));
245 fs->super->s_journal_inum = 0;
246 ext2fs_mark_super_dirty(fs);
250 * Update the feature set as provided by the user.
252 static void update_feature_set(ext2_filsys fs, char *features)
254 int sparse, old_sparse, filetype, old_filetype;
255 int journal, old_journal;
256 struct ext2_inode inode;
257 struct ext2_super_block *sb= fs->super;
260 old_sparse = sb->s_feature_ro_compat &
261 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
262 old_filetype = sb->s_feature_incompat &
263 EXT2_FEATURE_INCOMPAT_FILETYPE;
264 old_journal = sb->s_feature_compat &
265 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
266 if (e2p_edit_feature(features, &sb->s_feature_compat,
268 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
272 sparse = sb->s_feature_ro_compat &
273 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
274 filetype = sb->s_feature_incompat &
275 EXT2_FEATURE_INCOMPAT_FILETYPE;
276 journal = sb->s_feature_compat &
277 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
278 if (old_journal && !journal) {
279 if ((mount_flags & EXT2_MF_MOUNTED) &&
280 !(mount_flags & EXT2_MF_READONLY)) {
282 _("The has_journal flag may only be "
283 "cleared when the filesystem is\n"
284 "unmounted or mounted "
288 if (sb->s_feature_incompat &
289 EXT3_FEATURE_INCOMPAT_RECOVER) {
291 _("The needs_recovery flag is set. "
292 "Please run e2fsck before clearing\n"
293 "the has_journal flag.\n"));
296 if (sb->s_journal_inum) {
297 remove_journal_inode(fs);
299 if (sb->s_journal_dev) {
300 remove_journal_device(fs);
303 if (journal && !old_journal) {
305 * If adding a journal flag, let the create journal
306 * code below handle creating setting the flag and
307 * creating the journal. We supply a default size if
312 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
315 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
316 (sb->s_feature_compat || sb->s_feature_ro_compat ||
317 sb->s_feature_incompat))
318 ext2fs_update_dynamic_rev(fs);
319 if ((sparse != old_sparse) ||
320 (filetype != old_filetype)) {
321 sb->s_state &= ~EXT2_VALID_FS;
322 printf("\n%s\n", _(please_fsck));
324 ext2fs_mark_super_dirty(fs);
328 * Add a journal to the filesystem.
330 static void add_journal(ext2_filsys fs)
332 unsigned long journal_blocks;
336 if (fs->super->s_feature_compat &
337 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
338 fprintf(stderr, _("The filesystem already has a journal.\n"));
341 if (journal_device) {
342 check_plausibility(journal_device);
343 check_mount(journal_device, 0, _("journal"));
344 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
345 EXT2_FLAG_JOURNAL_DEV_OK, 0,
346 fs->blocksize, unix_io_manager, &jfs);
348 com_err(program_name, retval,
349 _("\n\twhile trying to open journal on %s\n"),
353 printf(_("Creating journal on device %s: "),
357 retval = ext2fs_add_journal_device(fs, jfs);
360 com_err (program_name, retval,
361 _("while adding filesystem to journal on %s"),
366 } else if (journal_size) {
367 printf(_("Creating journal inode: "));
369 journal_blocks = figure_journal_size(journal_size, fs);
371 retval = ext2fs_add_journal_inode(fs, journal_blocks,
374 fprintf(stderr, "\n");
375 com_err(program_name, retval,
376 _("\n\twhile trying to create journal file"));
381 * If the filesystem wasn't mounted, we need to force
382 * the block group descriptors out.
384 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
385 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
387 print_check_message(fs);
392 free(journal_device);
397 * Given argv[0], return the program name.
399 static char *get_progname(char *argv_zero)
403 cp = strrchr(argv_zero, '/');
411 static void parse_e2label_options(int argc, char ** argv)
413 if ((argc < 2) || (argc > 3)) {
414 fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
417 device_name = argv[1];
419 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
427 static void parse_tune2fs_options(int argc, char **argv)
434 printf(_("tune2fs %s, %s for EXT2 FS %s, %s\n"),
435 E2FSPROGS_VERSION, E2FSPROGS_DATE,
436 EXT2FS_VERSION, EXT2FS_DATE);
437 while ((c = getopt (argc, argv, "c:e:fg:i:jlm:r:s:u:C:J:L:M:O:U:")) != EOF)
441 max_mount_count = strtol (optarg, &tmp, 0);
442 if (*tmp || max_mount_count > 16000) {
443 com_err (program_name, 0,
444 _("bad mounts count - %s"),
448 if (max_mount_count == 0)
449 max_mount_count = -1;
451 open_flag = EXT2_FLAG_RW;
454 mount_count = strtoul (optarg, &tmp, 0);
455 if (*tmp || mount_count > 16000) {
456 com_err (program_name, 0,
457 _("bad mounts count - %s"),
462 open_flag = EXT2_FLAG_RW;
465 if (strcmp (optarg, "continue") == 0)
466 errors = EXT2_ERRORS_CONTINUE;
467 else if (strcmp (optarg, "remount-ro") == 0)
468 errors = EXT2_ERRORS_RO;
469 else if (strcmp (optarg, "panic") == 0)
470 errors = EXT2_ERRORS_PANIC;
472 com_err (program_name, 0,
473 _("bad error behavior - %s"),
478 open_flag = EXT2_FLAG_RW;
480 case 'f': /* Force */
484 resgid = strtoul (optarg, &tmp, 0);
486 gr = getgrnam (optarg);
495 com_err (program_name, 0,
496 _("bad gid/group name - %s"),
501 open_flag = EXT2_FLAG_RW;
504 interval = strtoul (optarg, &tmp, 0);
517 case 'M': /* months! */
518 interval *= 86400 * 30;
522 case 'W': /* weeks */
523 interval *= 86400 * 7;
527 if (*tmp || interval > (365 * 86400)) {
528 com_err (program_name, 0,
529 _("bad interval - %s"), optarg);
533 open_flag = EXT2_FLAG_RW;
538 open_flag = EXT2_FLAG_RW;
541 parse_journal_opts(optarg);
542 open_flag = EXT2_FLAG_RW;
550 open_flag = EXT2_FLAG_RW |
551 EXT2_FLAG_JOURNAL_DEV_OK;
554 reserved_ratio = strtoul (optarg, &tmp, 0);
555 if (*tmp || reserved_ratio > 50) {
556 com_err (program_name, 0,
557 _("bad reserved block ratio - %s"),
562 open_flag = EXT2_FLAG_RW;
565 new_last_mounted = optarg;
567 open_flag = EXT2_FLAG_RW;
571 com_err (program_name, 0,
572 _("-O may only be specified once"));
575 features_cmd = optarg;
576 open_flag = EXT2_FLAG_RW;
579 reserved_blocks = strtoul (optarg, &tmp, 0);
581 com_err (program_name, 0,
582 _("bad reserved blocks count - %s"),
587 open_flag = EXT2_FLAG_RW;
590 s_flag = atoi(optarg);
591 open_flag = EXT2_FLAG_RW;
594 resuid = strtoul (optarg, &tmp, 0);
596 pw = getpwnam (optarg);
605 com_err (program_name, 0,
606 _("bad uid/user name - %s"),
611 open_flag = EXT2_FLAG_RW;
616 open_flag = EXT2_FLAG_RW |
617 EXT2_FLAG_JOURNAL_DEV_OK;
622 if (optind < argc - 1 || optind == argc)
624 if (!open_flag && !l_flag)
626 device_name = argv[optind];
631 int main (int argc, char ** argv)
635 struct ext2_super_block *sb;
638 setlocale(LC_MESSAGES, "");
639 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
640 textdomain(NLS_CAT_NAME);
643 program_name = *argv;
644 initialize_ext2_error_table();
646 if (strcmp(get_progname(argv[0]), "e2label") == 0)
647 parse_e2label_options(argc, argv);
649 parse_tune2fs_options(argc, argv);
651 retval = ext2fs_open (device_name, open_flag, 0, 0,
652 unix_io_manager, &fs);
654 com_err (program_name, retval, _("while trying to open %s"),
657 _("Couldn't find valid filesystem superblock.\n"));
662 /* For e2label emulation */
663 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
667 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
669 com_err("ext2fs_check_if_mount", retval,
670 _("while determining whether %s is mounted."),
674 /* Normally we only need to write out the superblock */
675 fs->flags |= EXT2_FLAG_SUPER_ONLY;
678 sb->s_max_mnt_count = max_mount_count;
679 ext2fs_mark_super_dirty(fs);
680 printf (_("Setting maximal mount count to %d\n"),
684 sb->s_mnt_count = mount_count;
685 ext2fs_mark_super_dirty(fs);
686 printf (_("Setting current mount count to %d\n"), mount_count);
689 sb->s_errors = errors;
690 ext2fs_mark_super_dirty(fs);
691 printf (_("Setting error behavior to %d\n"), errors);
694 sb->s_def_resgid = resgid;
695 ext2fs_mark_super_dirty(fs);
696 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
699 sb->s_checkinterval = interval;
700 ext2fs_mark_super_dirty(fs);
701 printf (_("Setting interval between check %lu seconds\n"), interval);
704 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
706 ext2fs_mark_super_dirty(fs);
707 printf (_("Setting reserved blocks percentage to %lu (%u blocks)\n"),
708 reserved_ratio, sb->s_r_blocks_count);
711 if (reserved_blocks >= sb->s_blocks_count) {
712 com_err (program_name, 0,
713 _("reserved blocks count is too big (%ul)"),
717 sb->s_r_blocks_count = reserved_blocks;
718 ext2fs_mark_super_dirty(fs);
719 printf (_("Setting reserved blocks count to %lu\n"),
723 if (sb->s_feature_ro_compat &
724 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
725 fprintf(stderr, _("\nThe filesystem already"
726 " has sparse superblocks.\n"));
728 sb->s_feature_ro_compat |=
729 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
730 sb->s_state &= ~EXT2_VALID_FS;
731 ext2fs_mark_super_dirty(fs);
732 printf(_("\nSparse superblock flag set. %s"),
737 if (!(sb->s_feature_ro_compat &
738 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
739 fprintf(stderr, _("\nThe filesystem already"
740 " has sparse superblocks disabled.\n"));
742 sb->s_feature_ro_compat &=
743 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
744 sb->s_state &= ~EXT2_VALID_FS;
745 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
746 ext2fs_mark_super_dirty(fs);
747 printf(_("\nSparse superblock flag cleared. %s"),
752 sb->s_def_resuid = resuid;
753 ext2fs_mark_super_dirty(fs);
754 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
757 if (strlen(new_label) > sizeof(sb->s_volume_name))
758 fprintf(stderr, _("Warning: label too "
759 "long, truncating.\n"));
760 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
761 strncpy(sb->s_volume_name, new_label,
762 sizeof(sb->s_volume_name));
763 ext2fs_mark_super_dirty(fs);
766 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
767 strncpy(sb->s_last_mounted, new_last_mounted,
768 sizeof(sb->s_last_mounted));
769 ext2fs_mark_super_dirty(fs);
772 update_feature_set(fs, features_cmd);
773 if (journal_size || journal_device)
777 if ((strcasecmp(new_UUID, "null") == 0) ||
778 (strcasecmp(new_UUID, "clear") == 0)) {
779 uuid_clear(sb->s_uuid);
780 } else if (strcasecmp(new_UUID, "time") == 0) {
781 uuid_generate_time(sb->s_uuid);
782 } else if (strcasecmp(new_UUID, "random") == 0) {
783 uuid_generate(sb->s_uuid);
784 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
785 com_err(program_name, 0, _("Invalid UUID format\n"));
788 ext2fs_mark_super_dirty(fs);