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, T_flag;
61
static time_t last_check_time;
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]\n"
84
"\t[-O [^]feature[,...]] [-T last-check-time] [-U UUID]"
85
" device\n"), program_name);
89
static __u32 ok_features[3] = {
90
EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
91
EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
92
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
96
* Remove an external journal from the filesystem
98
static void remove_journal_device(ext2_filsys fs)
103
journal_superblock_t *jsb;
106
int commit_remove_journal = 0;
109
commit_remove_journal = 1; /* force removal even if error */
111
uuid_unparse(fs->super->s_journal_uuid, buf);
112
journal_path = get_spec_by_uuid(buf);
116
ext2fs_find_block_device(fs->super->s_journal_dev);
121
retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
122
EXT2_FLAG_JOURNAL_DEV_OK, 0,
123
fs->blocksize, unix_io_manager, &jfs);
125
com_err(program_name, retval,
126
_("while trying to open external journal"));
127
goto no_valid_journal;
129
if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
130
fprintf(stderr, _("%s is not a journal device.\n"),
132
goto no_valid_journal;
135
/* Get the journal superblock */
136
if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
137
com_err(program_name, retval,
138
_("while reading journal superblock"));
139
goto no_valid_journal;
142
jsb = (journal_superblock_t *) buf;
143
if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
144
(jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
145
fprintf(stderr, _("Journal superblock not found!\n"));
146
goto no_valid_journal;
149
/* Find the filesystem UUID */
150
nr_users = ntohl(jsb->s_nr_users);
151
for (i=0; i < nr_users; i++) {
152
if (memcmp(fs->super->s_uuid,
153
&jsb->s_users[i*16], 16) == 0)
158
_("Filesystem's UUID not found on journal device.\n"));
159
commit_remove_journal = 1;
160
goto no_valid_journal;
163
for (i=0; i < nr_users; i++)
164
memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
165
jsb->s_nr_users = htonl(nr_users);
167
/* Write back the journal superblock */
168
if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
169
com_err(program_name, retval,
170
"while writing journal superblock.");
171
goto no_valid_journal;
174
commit_remove_journal = 1;
177
if (commit_remove_journal == 0) {
178
fprintf(stderr, _("Journal NOT removed\n"));
181
fs->super->s_journal_dev = 0;
182
memset(fs->super->s_journal_uuid, 0,
183
sizeof(fs->super->s_journal_uuid));
184
ext2fs_mark_super_dirty(fs);
185
printf(_("Journal removed\n"));
189
/* Helper function for remove_journal_inode */
190
static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
191
int blockcnt, void *private)
197
ext2fs_unmark_block_bitmap(fs->block_map,block);
198
group = ext2fs_group_of_blk(fs, block);
199
fs->group_desc[group].bg_free_blocks_count++;
200
fs->super->s_free_blocks_count++;
205
* Remove the journal inode from the filesystem
207
static void remove_journal_inode(ext2_filsys fs)
209
struct ext2_inode inode;
211
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
fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
237
inode.i_flags &= ~EXT2_IMMUTABLE_FL;
238
retval = ext2fs_write_inode(fs, ino, &inode);
240
com_err(program_name, retval,
241
_("while writing journal inode"));
244
fs->super->s_journal_inum = 0;
245
ext2fs_mark_super_dirty(fs);
249
* Update the feature set as provided by the user.
251
static void update_feature_set(ext2_filsys fs, char *features)
253
int sparse, old_sparse, filetype, old_filetype;
254
int journal, old_journal;
255
struct ext2_super_block *sb= fs->super;
257
old_sparse = sb->s_feature_ro_compat &
258
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
259
old_filetype = sb->s_feature_incompat &
260
EXT2_FEATURE_INCOMPAT_FILETYPE;
261
old_journal = sb->s_feature_compat &
262
EXT3_FEATURE_COMPAT_HAS_JOURNAL;
263
if (e2p_edit_feature(features, &sb->s_feature_compat,
265
fprintf(stderr, _("Invalid filesystem option set: %s\n"),
269
sparse = sb->s_feature_ro_compat &
270
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
271
filetype = sb->s_feature_incompat &
272
EXT2_FEATURE_INCOMPAT_FILETYPE;
273
journal = sb->s_feature_compat &
274
EXT3_FEATURE_COMPAT_HAS_JOURNAL;
275
if (old_journal && !journal) {
276
if ((mount_flags & EXT2_MF_MOUNTED) &&
277
!(mount_flags & EXT2_MF_READONLY)) {
279
_("The has_journal flag may only be "
280
"cleared when the filesystem is\n"
281
"unmounted or mounted "
285
if (sb->s_feature_incompat &
286
EXT3_FEATURE_INCOMPAT_RECOVER) {
288
_("The needs_recovery flag is set. "
289
"Please run e2fsck before clearing\n"
290
"the has_journal flag.\n"));
293
if (sb->s_journal_inum) {
294
remove_journal_inode(fs);
296
if (sb->s_journal_dev) {
297
remove_journal_device(fs);
300
if (journal && !old_journal) {
302
* If adding a journal flag, let the create journal
303
* code below handle creating setting the flag and
304
* creating the journal. We supply a default size if
309
sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
312
if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
313
(sb->s_feature_compat || sb->s_feature_ro_compat ||
314
sb->s_feature_incompat))
315
ext2fs_update_dynamic_rev(fs);
316
if ((sparse != old_sparse) ||
317
(filetype != old_filetype)) {
318
sb->s_state &= ~EXT2_VALID_FS;
319
printf("\n%s\n", _(please_fsck));
321
ext2fs_mark_super_dirty(fs);
325
* Add a journal to the filesystem.
327
static void add_journal(ext2_filsys fs)
329
unsigned long journal_blocks;
333
if (fs->super->s_feature_compat &
334
EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
335
fprintf(stderr, _("The filesystem already has a journal.\n"));
338
if (journal_device) {
339
check_plausibility(journal_device);
340
check_mount(journal_device, 0, _("journal"));
341
retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
342
EXT2_FLAG_JOURNAL_DEV_OK, 0,
343
fs->blocksize, unix_io_manager, &jfs);
345
com_err(program_name, retval,
346
_("\n\twhile trying to open journal on %s\n"),
350
printf(_("Creating journal on device %s: "),
354
retval = ext2fs_add_journal_device(fs, jfs);
357
com_err (program_name, retval,
358
_("while adding filesystem to journal on %s"),
363
} else if (journal_size) {
364
printf(_("Creating journal inode: "));
366
journal_blocks = figure_journal_size(journal_size, fs);
368
retval = ext2fs_add_journal_inode(fs, journal_blocks,
371
fprintf(stderr, "\n");
372
com_err(program_name, retval,
373
_("\n\twhile trying to create journal file"));
378
* If the filesystem wasn't mounted, we need to force
379
* the block group descriptors out.
381
if ((mount_flags & EXT2_MF_MOUNTED) == 0)
382
fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
384
print_check_message(fs);
389
free(journal_device);
394
static void parse_e2label_options(int argc, char ** argv)
396
if ((argc < 2) || (argc > 3)) {
397
fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
400
device_name = argv[1];
402
open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
409
static time_t parse_time(char *str)
413
if (strcmp(str, "now") == 0) {
416
memset(&ts, 0, sizeof(ts));
417
strptime(optarg, "%Y%m%d%H%M%S", &ts);
418
if (ts.tm_mday == 0) {
419
com_err(program_name, 0,
420
_("Couldn't parse date/time specifier: %s"),
424
return (mktime(&ts));
427
static void parse_tune2fs_options(int argc, char **argv)
434
printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
435
while ((c = getopt(argc, argv, "c:e:fg:i:jlm:r:s:u:C:J:L:M:O:T:U:")) != EOF)
439
max_mount_count = strtol (optarg, &tmp, 0);
440
if (*tmp || max_mount_count > 16000) {
441
com_err (program_name, 0,
442
_("bad mounts count - %s"),
446
if (max_mount_count == 0)
447
max_mount_count = -1;
449
open_flag = EXT2_FLAG_RW;
452
mount_count = strtoul (optarg, &tmp, 0);
453
if (*tmp || mount_count > 16000) {
454
com_err (program_name, 0,
455
_("bad mounts count - %s"),
460
open_flag = EXT2_FLAG_RW;
463
if (strcmp (optarg, "continue") == 0)
464
errors = EXT2_ERRORS_CONTINUE;
465
else if (strcmp (optarg, "remount-ro") == 0)
466
errors = EXT2_ERRORS_RO;
467
else if (strcmp (optarg, "panic") == 0)
468
errors = EXT2_ERRORS_PANIC;
470
com_err (program_name, 0,
471
_("bad error behavior - %s"),
476
open_flag = EXT2_FLAG_RW;
478
case 'f': /* Force */
482
resgid = strtoul (optarg, &tmp, 0);
484
gr = getgrnam (optarg);
493
com_err (program_name, 0,
494
_("bad gid/group name - %s"),
499
open_flag = EXT2_FLAG_RW;
502
interval = strtoul (optarg, &tmp, 0);
515
case 'M': /* months! */
516
interval *= 86400 * 30;
520
case 'W': /* weeks */
521
interval *= 86400 * 7;
525
if (*tmp || interval > (365 * 86400)) {
526
com_err (program_name, 0,
527
_("bad interval - %s"), optarg);
531
open_flag = EXT2_FLAG_RW;
536
open_flag = EXT2_FLAG_RW;
539
parse_journal_opts(optarg);
540
open_flag = EXT2_FLAG_RW;
548
open_flag = EXT2_FLAG_RW |
549
EXT2_FLAG_JOURNAL_DEV_OK;
552
reserved_ratio = strtoul (optarg, &tmp, 0);
553
if (*tmp || reserved_ratio > 50) {
554
com_err (program_name, 0,
555
_("bad reserved block ratio - %s"),
560
open_flag = EXT2_FLAG_RW;
563
new_last_mounted = optarg;
565
open_flag = EXT2_FLAG_RW;
569
com_err (program_name, 0,
570
_("-O may only be specified once"));
573
features_cmd = optarg;
574
open_flag = EXT2_FLAG_RW;
577
reserved_blocks = strtoul (optarg, &tmp, 0);
579
com_err (program_name, 0,
580
_("bad reserved blocks count - %s"),
585
open_flag = EXT2_FLAG_RW;
588
s_flag = atoi(optarg);
589
open_flag = EXT2_FLAG_RW;
593
last_check_time = parse_time(optarg);
594
open_flag = EXT2_FLAG_RW;
597
resuid = strtoul (optarg, &tmp, 0);
599
pw = getpwnam (optarg);
608
com_err (program_name, 0,
609
_("bad uid/user name - %s"),
614
open_flag = EXT2_FLAG_RW;
619
open_flag = EXT2_FLAG_RW |
620
EXT2_FLAG_JOURNAL_DEV_OK;
625
if (optind < argc - 1 || optind == argc)
627
if (!open_flag && !l_flag)
629
device_name = argv[optind];
634
int main (int argc, char ** argv)
638
struct ext2_super_block *sb;
641
setlocale(LC_MESSAGES, "");
642
setlocale(LC_CTYPE, "");
643
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
644
textdomain(NLS_CAT_NAME);
647
program_name = *argv;
648
initialize_ext2_error_table();
650
if (strcmp(get_progname(argv[0]), "e2label") == 0)
651
parse_e2label_options(argc, argv);
653
parse_tune2fs_options(argc, argv);
655
retval = ext2fs_open (device_name, open_flag, 0, 0,
656
unix_io_manager, &fs);
658
com_err (program_name, retval, _("while trying to open %s"),
661
_("Couldn't find valid filesystem superblock.\n"));
666
/* For e2label emulation */
667
printf("%.*s\n", (int) sizeof(sb->s_volume_name),
671
retval = ext2fs_check_if_mounted(device_name, &mount_flags);
673
com_err("ext2fs_check_if_mount", retval,
674
_("while determining whether %s is mounted."),
678
/* Normally we only need to write out the superblock */
679
fs->flags |= EXT2_FLAG_SUPER_ONLY;
682
sb->s_max_mnt_count = max_mount_count;
683
ext2fs_mark_super_dirty(fs);
684
printf (_("Setting maximal mount count to %d\n"),
688
sb->s_mnt_count = mount_count;
689
ext2fs_mark_super_dirty(fs);
690
printf (_("Setting current mount count to %d\n"), mount_count);
693
sb->s_errors = errors;
694
ext2fs_mark_super_dirty(fs);
695
printf (_("Setting error behavior to %d\n"), errors);
698
sb->s_def_resgid = resgid;
699
ext2fs_mark_super_dirty(fs);
700
printf (_("Setting reserved blocks gid to %lu\n"), resgid);
703
sb->s_checkinterval = interval;
704
ext2fs_mark_super_dirty(fs);
705
printf (_("Setting interval between check %lu seconds\n"), interval);
708
sb->s_r_blocks_count = (sb->s_blocks_count / 100)
710
ext2fs_mark_super_dirty(fs);
711
printf (_("Setting reserved blocks percentage to %lu (%u blocks)\n"),
712
reserved_ratio, sb->s_r_blocks_count);
715
if (reserved_blocks >= sb->s_blocks_count) {
716
com_err (program_name, 0,
717
_("reserved blocks count is too big (%ul)"),
721
sb->s_r_blocks_count = reserved_blocks;
722
ext2fs_mark_super_dirty(fs);
723
printf (_("Setting reserved blocks count to %lu\n"),
727
if (sb->s_feature_ro_compat &
728
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
729
fprintf(stderr, _("\nThe filesystem already"
730
" has sparse superblocks.\n"));
732
sb->s_feature_ro_compat |=
733
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
734
sb->s_state &= ~EXT2_VALID_FS;
735
ext2fs_mark_super_dirty(fs);
736
printf(_("\nSparse superblock flag set. %s"),
741
if (!(sb->s_feature_ro_compat &
742
EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
743
fprintf(stderr, _("\nThe filesystem already"
744
" has sparse superblocks disabled.\n"));
746
sb->s_feature_ro_compat &=
747
~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
748
sb->s_state &= ~EXT2_VALID_FS;
749
fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
750
ext2fs_mark_super_dirty(fs);
751
printf(_("\nSparse superblock flag cleared. %s"),
756
sb->s_lastcheck = last_check_time;
757
ext2fs_mark_super_dirty(fs);
758
printf(_("Setting time filesystem last checked to %s\n"),
759
ctime(&last_check_time));
762
sb->s_def_resuid = resuid;
763
ext2fs_mark_super_dirty(fs);
764
printf (_("Setting reserved blocks uid to %lu\n"), resuid);
767
if (strlen(new_label) > sizeof(sb->s_volume_name))
768
fprintf(stderr, _("Warning: label too "
769
"long, truncating.\n"));
770
memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
771
strncpy(sb->s_volume_name, new_label,
772
sizeof(sb->s_volume_name));
773
ext2fs_mark_super_dirty(fs);
776
memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
777
strncpy(sb->s_last_mounted, new_last_mounted,
778
sizeof(sb->s_last_mounted));
779
ext2fs_mark_super_dirty(fs);
782
update_feature_set(fs, features_cmd);
783
if (journal_size || journal_device)
787
if ((strcasecmp(new_UUID, "null") == 0) ||
788
(strcasecmp(new_UUID, "clear") == 0)) {
789
uuid_clear(sb->s_uuid);
790
} else if (strcasecmp(new_UUID, "time") == 0) {
791
uuid_generate_time(sb->s_uuid);
792
} else if (strcasecmp(new_UUID, "random") == 0) {
793
uuid_generate(sb->s_uuid);
794
} else if (uuid_parse(new_UUID, sb->s_uuid)) {
795
com_err(program_name, 0, _("Invalid UUID format\n"));
798
ext2fs_mark_super_dirty(fs);
803
return (ext2fs_close (fs) ? 1 : 0);