2 * tune2fs.c - Change the file system parameters on
3 * an unmounted second extended file system
5 * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
6 * Laboratoire MASI, Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
9 * Copyright 1995, 1996, 1997 by Theodore Ts'o.
12 * This file may be redistributed under the terms of the GNU Public
20 * 93/10/31 - Added the -c option to change the maximal mount counts
21 * 93/12/14 - Added -l flag to list contents of superblock
22 * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
23 * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
24 * 93/12/29 - Added the -e option to change errors behavior
25 * 94/02/27 - Ported to use the ext2fs library
26 * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
43 #include <sys/types.h>
45 #include <linux/ext2_fs.h>
47 #include "ext2fs/ext2fs.h"
48 #include "et/com_err.h"
49 #include "uuid/uuid.h"
54 #include "../version.h"
55 #include "nls-enable.h"
57 const char * program_name = "tune2fs";
59 char * new_label, *new_last_mounted, *new_UUID;
60 static int c_flag, C_flag, e_flag, g_flag, i_flag, l_flag, L_flag;
61 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag;
62 static int print_label;
63 static int max_mount_count, mount_count, mount_flags;
64 static unsigned long interval, reserved_ratio, reserved_blocks;
65 static unsigned long resgid, resuid;
66 static unsigned short errors;
68 static char *features_cmd;
70 int journal_size, journal_flags;
73 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
75 static void usage(void)
78 _("Usage: %s [-c max-mounts-count] [-e errors-behavior] "
80 "\t[-i interval[d|m|w]] [-j] [-J journal-options]\n"
81 "\t[-l] [-s sparse-flag] [-m reserved-blocks-percent]\n"
82 "\t[-r reserved-blocks-count] [-u user] [-C mount-count]\n"
83 "\t[-L volume-label] [-M last-mounted-dir] [-U UUID]\n"
84 "\t[-O [^]feature[,...]] device\n"), program_name);
88 static __u32 ok_features[3] = {
89 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
90 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
91 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
95 * Remove an external journal from the filesystem
97 static void remove_journal_device(ext2_filsys fs)
102 journal_superblock_t *jsb;
106 journal_device = ext2fs_find_block_device(fs->super->s_journal_dev);
110 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
111 EXT2_FLAG_JOURNAL_DEV_OK, 0,
112 fs->blocksize, unix_io_manager, &jfs);
114 com_err(program_name, retval,
115 _("while trying to open external journal"));
118 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
119 fprintf(stderr, "%s is not a journal device.\n",
124 /* Get the journal superblock */
125 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
126 com_err(program_name, retval,
127 _("while reading journal superblock"));
131 jsb = (journal_superblock_t *) buf;
132 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
133 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
134 fprintf(stderr, _("Journal superblock not found!\n"));
138 /* Find the filesystem UUID */
139 nr_users = ntohl(jsb->s_nr_users);
140 for (i=0; i < nr_users; i++) {
141 if (memcmp(fs->super->s_uuid,
142 &jsb->s_users[i*16], 16) == 0)
146 fprintf(stderr, "Filesystem's UUID not found on journal device.\n");
150 for (i=0; i < nr_users; i++)
151 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
152 jsb->s_nr_users = htonl(nr_users);
154 /* Write back the journal superblock */
155 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
156 com_err(program_name, retval,
157 "while writing journal superblock.");
161 fs->super->s_journal_dev = 0;
162 memset(fs->super->s_journal_uuid, 0,
163 sizeof(fs->super->s_journal_uuid));
164 ext2fs_mark_super_dirty(fs);
169 * Update the feature set as provided by the user.
171 static void update_feature_set(ext2_filsys fs, char *features)
173 int sparse, old_sparse, filetype, old_filetype;
174 int journal, old_journal;
175 struct ext2_inode inode;
176 struct ext2_super_block *sb= fs->super;
179 old_sparse = sb->s_feature_ro_compat &
180 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
181 old_filetype = sb->s_feature_incompat &
182 EXT2_FEATURE_INCOMPAT_FILETYPE;
183 old_journal = sb->s_feature_compat &
184 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
185 if (e2p_edit_feature(features, &sb->s_feature_compat,
187 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
191 sparse = sb->s_feature_ro_compat &
192 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
193 filetype = sb->s_feature_incompat &
194 EXT2_FEATURE_INCOMPAT_FILETYPE;
195 journal = sb->s_feature_compat &
196 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
197 if (old_journal && !journal) {
198 if ((mount_flags & EXT2_MF_MOUNTED) &&
199 !(mount_flags & EXT2_MF_READONLY)) {
201 _("The HAS_JOURNAL flag may only be "
202 "cleared when the filesystem is\n"
203 "unmounted or mounted "
207 if (sb->s_feature_incompat &
208 EXT3_FEATURE_INCOMPAT_RECOVER) {
210 _("The NEEDS_RECOVERY flag is set. "
211 "Please run e2fsck before clearing\n"
212 "the HAS_JOURNAL flag.\n"));
216 * Remove the immutable flag on the journal inode
218 if (sb->s_journal_inum) {
219 retval = ext2fs_read_inode(fs, sb->s_journal_inum,
222 com_err(program_name, retval,
223 "while reading journal inode");
226 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
227 retval = ext2fs_write_inode(fs, sb->s_journal_inum,
230 com_err(program_name, retval,
231 "while write journal inode");
235 if (sb->s_journal_dev)
236 remove_journal_device(fs);
238 if (journal && !old_journal) {
240 * If adding a journal flag, let the create journal
241 * code below handle creating setting the flag and
242 * creating the journal. We supply a default size if
247 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
248 journal = old_journal;
251 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
252 (sb->s_feature_compat || sb->s_feature_ro_compat ||
253 sb->s_feature_incompat))
254 ext2fs_update_dynamic_rev(fs);
255 if ((sparse != old_sparse) ||
256 (filetype != old_filetype) ||
257 (journal != old_journal)) {
258 sb->s_state &= ~EXT2_VALID_FS;
259 printf("\n%s\n", _(please_fsck));
261 ext2fs_mark_super_dirty(fs);
265 * Add a journal to the filesystem.
267 static void add_journal(ext2_filsys fs)
269 unsigned long journal_blocks;
273 if (fs->super->s_feature_compat &
274 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
275 fprintf(stderr, _("The filesystem already has a journal.\n"));
278 if (journal_device) {
279 check_plausibility(journal_device);
280 check_mount(journal_device, 0, _("journal"));
281 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
282 EXT2_FLAG_JOURNAL_DEV_OK, 0,
283 fs->blocksize, unix_io_manager, &jfs);
285 com_err(program_name, retval,
286 _("\n\twhile trying to open journal on %s\n"),
290 printf(_("Creating journal on device %s: "),
294 retval = ext2fs_add_journal_device(fs, jfs);
296 com_err (program_name, retval,
297 _("while trying to create journal on device %s"),
303 } else if (journal_size) {
304 printf(_("Creating journal inode: "));
306 journal_blocks = figure_journal_size(journal_size, fs);
308 retval = ext2fs_add_journal_inode(fs, journal_blocks,
312 com_err(program_name, retval,
313 _("\n\twhile trying to create journal file"));
318 * If the filesystem wasn't mounted, we need to force
319 * the block group descriptors out.
321 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
322 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
327 * Given argv[0], return the program name.
329 static char *get_progname(char *argv_zero)
333 cp = strrchr(argv_zero, '/');
341 static void parse_e2label_options(int argc, char ** argv)
343 if ((argc < 2) || (argc > 3)) {
344 fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
347 device_name = argv[1];
349 open_flag = EXT2_FLAG_RW;
357 static void parse_tune2fs_options(int argc, char **argv)
364 fprintf (stderr, _("tune2fs %s, %s for EXT2 FS %s, %s\n"),
365 E2FSPROGS_VERSION, E2FSPROGS_DATE,
366 EXT2FS_VERSION, EXT2FS_DATE);
367 while ((c = getopt (argc, argv, "c:e:g:i:jlm:r:s:u:C:J:L:M:O:U:")) != EOF)
371 max_mount_count = strtol (optarg, &tmp, 0);
372 if (*tmp || max_mount_count > 16000) {
373 com_err (program_name, 0,
374 _("bad mounts count - %s"),
379 open_flag = EXT2_FLAG_RW;
382 mount_count = strtoul (optarg, &tmp, 0);
383 if (*tmp || mount_count > 16000) {
384 com_err (program_name, 0,
385 _("bad mounts count - %s"),
390 open_flag = EXT2_FLAG_RW;
393 if (strcmp (optarg, "continue") == 0)
394 errors = EXT2_ERRORS_CONTINUE;
395 else if (strcmp (optarg, "remount-ro") == 0)
396 errors = EXT2_ERRORS_RO;
397 else if (strcmp (optarg, "panic") == 0)
398 errors = EXT2_ERRORS_PANIC;
400 com_err (program_name, 0,
401 _("bad error behavior - %s"),
406 open_flag = EXT2_FLAG_RW;
409 resgid = strtoul (optarg, &tmp, 0);
411 gr = getgrnam (optarg);
420 com_err (program_name, 0,
421 _("bad gid/group name - %s"),
426 open_flag = EXT2_FLAG_RW;
429 interval = strtoul (optarg, &tmp, 0);
442 case 'M': /* months! */
443 interval *= 86400 * 30;
447 case 'W': /* weeks */
448 interval *= 86400 * 7;
452 if (*tmp || interval > (365 * 86400)) {
453 com_err (program_name, 0,
454 _("bad interval - %s"), optarg);
458 open_flag = EXT2_FLAG_RW;
465 parse_journal_opts(optarg);
466 open_flag = EXT2_FLAG_RW;
474 open_flag = EXT2_FLAG_RW;
477 reserved_ratio = strtoul (optarg, &tmp, 0);
478 if (*tmp || reserved_ratio > 50) {
479 com_err (program_name, 0,
480 _("bad reserved block ratio - %s"),
485 open_flag = EXT2_FLAG_RW;
488 new_last_mounted = optarg;
490 open_flag = EXT2_FLAG_RW;
493 features_cmd = optarg;
494 open_flag = EXT2_FLAG_RW;
497 reserved_blocks = strtoul (optarg, &tmp, 0);
499 com_err (program_name, 0,
500 _("bad reserved blocks count - %s"),
505 open_flag = EXT2_FLAG_RW;
508 s_flag = atoi(optarg);
509 open_flag = EXT2_FLAG_RW;
512 resuid = strtoul (optarg, &tmp, 0);
514 pw = getpwnam (optarg);
523 com_err (program_name, 0,
524 _("bad uid/user name - %s"),
529 open_flag = EXT2_FLAG_RW;
534 open_flag = EXT2_FLAG_RW;
539 if (optind < argc - 1 || optind == argc)
541 if (!open_flag && !l_flag)
543 device_name = argv[optind];
548 int main (int argc, char ** argv)
552 struct ext2_super_block *sb;
555 setlocale(LC_MESSAGES, "");
556 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
557 textdomain(NLS_CAT_NAME);
560 program_name = *argv;
561 initialize_ext2_error_table();
563 if (strcmp(get_progname(argv[0]), "e2label") == 0)
564 parse_e2label_options(argc, argv);
566 parse_tune2fs_options(argc, argv);
568 retval = ext2fs_open (device_name, open_flag, 0, 0,
569 unix_io_manager, &fs);
571 com_err (program_name, retval, _("while trying to open %s"),
573 printf(_("Couldn't find valid filesystem superblock.\n"));
578 /* For e2label emulation */
579 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
583 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
585 com_err("ext2fs_check_if_mount", retval,
586 _("while determining whether %s is mounted."),
590 /* Normally we only need to write out the superblock */
591 fs->flags |= EXT2_FLAG_SUPER_ONLY;
594 sb->s_max_mnt_count = max_mount_count;
595 ext2fs_mark_super_dirty(fs);
596 printf (_("Setting maximal mount count to %d\n"),
600 sb->s_mnt_count = mount_count;
601 ext2fs_mark_super_dirty(fs);
602 printf (_("Setting current mount count to %d\n"), mount_count);
605 sb->s_errors = errors;
606 ext2fs_mark_super_dirty(fs);
607 printf (_("Setting error behavior to %d\n"), errors);
610 sb->s_def_resgid = resgid;
611 ext2fs_mark_super_dirty(fs);
612 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
615 sb->s_checkinterval = interval;
616 ext2fs_mark_super_dirty(fs);
617 printf (_("Setting interval between check %lu seconds\n"), interval);
620 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
622 ext2fs_mark_super_dirty(fs);
623 printf (_("Setting reserved blocks percentage to %lu (%u blocks)\n"),
624 reserved_ratio, sb->s_r_blocks_count);
627 if (reserved_blocks >= sb->s_blocks_count) {
628 com_err (program_name, 0,
629 _("reserved blocks count is too big (%ul)"),
633 sb->s_r_blocks_count = reserved_blocks;
634 ext2fs_mark_super_dirty(fs);
635 printf (_("Setting reserved blocks count to %lu\n"),
639 if (sb->s_feature_ro_compat &
640 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
641 fprintf(stderr, _("\nThe filesystem already"
642 " has sparse superblocks.\n"));
644 sb->s_feature_ro_compat |=
645 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
646 sb->s_state &= ~EXT2_VALID_FS;
647 ext2fs_mark_super_dirty(fs);
648 printf(_("\nSparse superblock flag set. %s"),
653 if (!(sb->s_feature_ro_compat &
654 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
655 fprintf(stderr, _("\nThe filesystem already"
656 " has sparse superblocks disabled.\n"));
658 sb->s_feature_ro_compat &=
659 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
660 sb->s_state &= ~EXT2_VALID_FS;
661 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
662 ext2fs_mark_super_dirty(fs);
663 printf(_("\nSparse superblock flag cleared. %s"),
668 sb->s_def_resuid = resuid;
669 ext2fs_mark_super_dirty(fs);
670 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
673 if (strlen(new_label) > sizeof(sb->s_volume_name))
674 fprintf(stderr, _("Warning: label too "
675 "long, truncating.\n"));
676 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
677 strncpy(sb->s_volume_name, new_label,
678 sizeof(sb->s_volume_name));
679 ext2fs_mark_super_dirty(fs);
682 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
683 strncpy(sb->s_last_mounted, new_last_mounted,
684 sizeof(sb->s_last_mounted));
685 ext2fs_mark_super_dirty(fs);
688 update_feature_set(fs, features_cmd);
689 if (journal_size || journal_device)
693 if (strcasecmp(new_UUID, "null") == 0) {
694 uuid_clear(sb->s_uuid);
695 } else if (strcasecmp(new_UUID, "time") == 0) {
696 uuid_generate_time(sb->s_uuid);
697 } else if (strcasecmp(new_UUID, "random") == 0) {
698 uuid_generate(sb->s_uuid);
699 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
700 com_err(program_name, 0, _("Invalid UUID format\n"));
703 ext2fs_mark_super_dirty(fs);