1 /* vi: set sw=4 ts=4: */
3 * tune2fs.c - Change the file system parameters on an ext2 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, 1998, 1999, 2000 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)
29 #include <sys/types.h>
39 #include "ext2fs/ext2_fs.h"
40 #include "ext2fs/ext2fs.h"
41 #include "uuid/uuid.h"
43 #include "ext2fs/kernel-jbd.h"
45 #include "blkid/blkid.h"
51 static char * device_name = NULL;
52 static char * new_label, *new_last_mounted, *new_UUID;
53 static char * io_options;
54 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
55 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
56 static time_t last_check_time;
57 static int print_label;
58 static int max_mount_count, mount_count, mount_flags;
59 static unsigned long interval, reserved_ratio, reserved_blocks;
60 static unsigned long resgid, resuid;
61 static unsigned short errors;
63 static char *features_cmd;
64 static char *mntopts_cmd;
66 static int journal_size, journal_flags;
67 static char *journal_device = NULL;
69 static const char *please_fsck = "Please run e2fsck on the filesystem\n";
71 static __u32 ok_features[3] = {
72 EXT3_FEATURE_COMPAT_HAS_JOURNAL | EXT2_FEATURE_COMPAT_DIR_INDEX,
73 EXT2_FEATURE_INCOMPAT_FILETYPE,
74 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
78 * Remove an external journal from the filesystem
80 static void remove_journal_device(ext2_filsys fs)
85 journal_superblock_t *jsb;
88 int commit_remove_journal = 0;
92 commit_remove_journal = 1; /* force removal even if error */
94 uuid_unparse(fs->super->s_journal_uuid, buf);
95 journal_path = blkid_get_devname(NULL, "UUID", buf);
99 ext2fs_find_block_device(fs->super->s_journal_dev);
104 io_ptr = unix_io_manager;
105 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
106 EXT2_FLAG_JOURNAL_DEV_OK, 0,
107 fs->blocksize, io_ptr, &jfs);
109 bb_error_msg("Failed to open external journal");
110 goto no_valid_journal;
112 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
113 bb_error_msg("%s is not a journal device", journal_path);
114 goto no_valid_journal;
117 /* Get the journal superblock */
118 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
119 bb_error_msg("Failed to read journal superblock");
120 goto no_valid_journal;
123 jsb = (journal_superblock_t *) buf;
124 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
125 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
126 bb_error_msg("Journal superblock not found!");
127 goto no_valid_journal;
130 /* Find the filesystem UUID */
131 nr_users = ntohl(jsb->s_nr_users);
132 for (i=0; i < nr_users; i++) {
133 if (memcmp(fs->super->s_uuid,
134 &jsb->s_users[i*16], 16) == 0)
138 bb_error_msg("Filesystem's UUID not found on journal device");
139 commit_remove_journal = 1;
140 goto no_valid_journal;
143 for (i=0; i < nr_users; i++)
144 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
145 jsb->s_nr_users = htonl(nr_users);
147 /* Write back the journal superblock */
148 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
149 bb_error_msg("Failed to write journal superblock");
150 goto no_valid_journal;
153 commit_remove_journal = 1;
156 if (commit_remove_journal == 0)
157 bb_error_msg_and_die("Journal NOT removed");
158 fs->super->s_journal_dev = 0;
159 uuid_clear(fs->super->s_journal_uuid);
160 ext2fs_mark_super_dirty(fs);
161 puts("Journal removed");
165 /* Helper function for remove_journal_inode */
166 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
167 int blockcnt EXT2FS_ATTR((unused)),
168 void *private EXT2FS_ATTR((unused)))
174 ext2fs_unmark_block_bitmap(fs->block_map,block);
175 group = ext2fs_group_of_blk(fs, block);
176 fs->group_desc[group].bg_free_blocks_count++;
177 fs->super->s_free_blocks_count++;
182 * Remove the journal inode from the filesystem
184 static void remove_journal_inode(ext2_filsys fs)
186 struct ext2_inode inode;
188 ino_t ino = fs->super->s_journal_inum;
189 char *msg = "to read";
190 char *s = "journal inode";
192 retval = ext2fs_read_inode(fs, ino, &inode);
194 goto REMOVE_JOURNAL_INODE_ERROR;
195 if (ino == EXT2_JOURNAL_INO) {
196 retval = ext2fs_read_bitmaps(fs);
198 msg = "to read bitmaps";
200 goto REMOVE_JOURNAL_INODE_ERROR;
202 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
203 release_blocks_proc, NULL);
206 goto REMOVE_JOURNAL_INODE_ERROR;
208 memset(&inode, 0, sizeof(inode));
209 ext2fs_mark_bb_dirty(fs);
210 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
212 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
213 retval = ext2fs_write_inode(fs, ino, &inode);
216 REMOVE_JOURNAL_INODE_ERROR:
217 bb_error_msg_and_die("Failed %s %s", msg, s);
219 fs->super->s_journal_inum = 0;
220 ext2fs_mark_super_dirty(fs);
224 * Update the default mount options
226 static void update_mntopts(ext2_filsys fs, char *mntopts)
228 struct ext2_super_block *sb= fs->super;
230 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
231 bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
232 ext2fs_mark_super_dirty(fs);
236 * Update the feature set as provided by the user.
238 static void update_feature_set(ext2_filsys fs, char *features)
240 int sparse, old_sparse, filetype, old_filetype;
241 int journal, old_journal, dxdir, old_dxdir;
242 struct ext2_super_block *sb= fs->super;
243 __u32 old_compat, old_incompat, old_ro_compat;
245 old_compat = sb->s_feature_compat;
246 old_ro_compat = sb->s_feature_ro_compat;
247 old_incompat = sb->s_feature_incompat;
249 old_sparse = sb->s_feature_ro_compat &
250 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
251 old_filetype = sb->s_feature_incompat &
252 EXT2_FEATURE_INCOMPAT_FILETYPE;
253 old_journal = sb->s_feature_compat &
254 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
255 old_dxdir = sb->s_feature_compat &
256 EXT2_FEATURE_COMPAT_DIR_INDEX;
257 if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
258 bb_error_msg_and_die("Invalid filesystem option set: %s", features);
259 sparse = sb->s_feature_ro_compat &
260 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
261 filetype = sb->s_feature_incompat &
262 EXT2_FEATURE_INCOMPAT_FILETYPE;
263 journal = sb->s_feature_compat &
264 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
265 dxdir = sb->s_feature_compat &
266 EXT2_FEATURE_COMPAT_DIR_INDEX;
267 if (old_journal && !journal) {
268 if ((mount_flags & EXT2_MF_MOUNTED) &&
269 !(mount_flags & EXT2_MF_READONLY)) {
270 bb_error_msg_and_die(
271 "The has_journal flag may only be "
272 "cleared when the filesystem is\n"
273 "unmounted or mounted "
276 if (sb->s_feature_incompat &
277 EXT3_FEATURE_INCOMPAT_RECOVER) {
278 bb_error_msg_and_die(
279 "The needs_recovery flag is set. "
280 "%s before clearing the has_journal flag.",
283 if (sb->s_journal_inum) {
284 remove_journal_inode(fs);
286 if (sb->s_journal_dev) {
287 remove_journal_device(fs);
290 if (journal && !old_journal) {
292 * If adding a journal flag, let the create journal
293 * code below handle creating setting the flag and
294 * creating the journal. We supply a default size if
299 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
301 if (dxdir && !old_dxdir) {
302 if (!sb->s_def_hash_version)
303 sb->s_def_hash_version = EXT2_HASH_TEA;
304 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
305 uuid_generate((unsigned char *) sb->s_hash_seed);
308 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
309 (sb->s_feature_compat || sb->s_feature_ro_compat ||
310 sb->s_feature_incompat))
311 ext2fs_update_dynamic_rev(fs);
312 if ((sparse != old_sparse) ||
313 (filetype != old_filetype)) {
314 sb->s_state &= ~EXT2_VALID_FS;
315 printf("\n%s\n", please_fsck);
317 if ((old_compat != sb->s_feature_compat) ||
318 (old_ro_compat != sb->s_feature_ro_compat) ||
319 (old_incompat != sb->s_feature_incompat))
320 ext2fs_mark_super_dirty(fs);
324 * Add a journal to the filesystem.
326 static void add_journal(ext2_filsys fs)
328 if (fs->super->s_feature_compat &
329 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
330 bb_error_msg_and_die("The filesystem already has a journal");
332 if (journal_device) {
333 make_journal_device(journal_device, fs, 0, 0);
334 } else if (journal_size) {
335 make_journal_blocks(fs, journal_size, journal_flags, 0);
337 * If the filesystem wasn't mounted, we need to force
338 * the block group descriptors out.
340 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
341 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
343 print_check_message(fs);
350 static char * x_blkid_get_devname(const char *token)
354 if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
355 bb_error_msg_and_die("Unable to resolve '%s'", token);
359 #ifdef CONFIG_E2LABEL
360 static void parse_e2label_options(int argc, char ** argv)
362 if ((argc < 2) || (argc > 3))
364 io_options = strchr(argv[1], '?');
367 device_name = x_blkid_get_devname(argv[1]);
369 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
376 #define parse_e2label_options(x,y)
379 static time_t parse_time(char *str)
383 if (strcmp(str, "now") == 0) {
386 memset(&ts, 0, sizeof(ts));
388 strptime(str, "%Y%m%d%H%M%S", &ts);
390 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
391 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
394 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
395 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
396 ts.tm_min > 59 || ts.tm_sec > 61)
399 if (ts.tm_mday == 0) {
400 bb_error_msg_and_die("Cannot parse date/time specifier: %s", str);
402 return (mktime(&ts));
405 static void parse_tune2fs_options(int argc, char **argv)
410 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
411 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
415 if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) {
416 goto MOUNTS_COUNT_ERROR;
418 if (max_mount_count == 0)
419 max_mount_count = -1;
421 open_flag = EXT2_FLAG_RW;
424 if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) {
426 bb_error_msg_and_die("bad mounts count - %s", optarg);
429 open_flag = EXT2_FLAG_RW;
432 if (strcmp (optarg, "continue") == 0)
433 errors = EXT2_ERRORS_CONTINUE;
434 else if (strcmp (optarg, "remount-ro") == 0)
435 errors = EXT2_ERRORS_RO;
436 else if (strcmp (optarg, "panic") == 0)
437 errors = EXT2_ERRORS_PANIC;
439 bb_error_msg_and_die("bad error behavior - %s", optarg);
442 open_flag = EXT2_FLAG_RW;
444 case 'f': /* Force */
448 if (safe_strtoul(optarg, &resgid))
449 resgid = bb_xgetgrnam(optarg);
451 open_flag = EXT2_FLAG_RW;
454 interval = strtoul (optarg, &tmp, 0);
467 case 'M': /* months! */
468 interval *= 86400 * 30;
472 case 'W': /* weeks */
473 interval *= 86400 * 7;
477 if (*tmp || interval > (365 * 86400)) {
478 bb_error_msg_and_die("bad interval - %s", optarg);
481 open_flag = EXT2_FLAG_RW;
486 open_flag = EXT2_FLAG_RW;
489 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
490 open_flag = EXT2_FLAG_RW;
498 open_flag = EXT2_FLAG_RW |
499 EXT2_FLAG_JOURNAL_DEV_OK;
502 if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) {
503 bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
506 open_flag = EXT2_FLAG_RW;
509 new_last_mounted = optarg;
511 open_flag = EXT2_FLAG_RW;
515 bb_error_msg_and_die("-o may only be specified once");
517 mntopts_cmd = optarg;
518 open_flag = EXT2_FLAG_RW;
523 bb_error_msg_and_die("-O may only be specified once");
525 features_cmd = optarg;
526 open_flag = EXT2_FLAG_RW;
529 if(safe_strtoul(optarg, &reserved_blocks)) {
530 bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
533 open_flag = EXT2_FLAG_RW;
536 s_flag = atoi(optarg);
537 open_flag = EXT2_FLAG_RW;
541 last_check_time = parse_time(optarg);
542 open_flag = EXT2_FLAG_RW;
545 if (safe_strtoul(optarg, &resuid))
546 resuid = bb_xgetpwnam(optarg);
548 open_flag = EXT2_FLAG_RW;
553 open_flag = EXT2_FLAG_RW |
554 EXT2_FLAG_JOURNAL_DEV_OK;
559 if (optind < argc - 1 || optind == argc)
561 if (!open_flag && !l_flag)
563 io_options = strchr(argv[optind], '?');
566 device_name = x_blkid_get_devname(argv[optind]);
570 static ATTRIBUTE_NORETURN void do_findfs(int argc, char **argv)
573 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
575 device_name = x_blkid_get_devname(argv[1]);
580 #define do_findfs(x, y)
583 static void tune2fs_clean_up(void)
585 if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
586 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
589 int tune2fs_main(int argc, char **argv)
593 struct ext2_super_block *sb;
596 if (ENABLE_FEATURE_CLEAN_UP)
597 atexit(tune2fs_clean_up);
599 if (ENABLE_FINDFS && (bb_applet_name[0] == 'f')) /* findfs */
600 do_findfs(argc, argv); /* no return */
601 else if (ENABLE_E2LABEL && (bb_applet_name[0] == 'e')) /* e2label */
602 parse_e2label_options(argc, argv);
604 parse_tune2fs_options(argc, argv); /* tune2fs */
606 io_ptr = unix_io_manager;
607 retval = ext2fs_open2(device_name, io_options, open_flag,
610 bb_error_msg_and_die("No valid superblock on %s", device_name);
613 /* For e2label emulation */
614 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
618 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
620 bb_error_msg_and_die("Could not determine if %s is mounted", device_name);
621 /* Normally we only need to write out the superblock */
622 fs->flags |= EXT2_FLAG_SUPER_ONLY;
625 sb->s_max_mnt_count = max_mount_count;
626 ext2fs_mark_super_dirty(fs);
627 printf("Setting maximal mount count to %d\n", max_mount_count);
630 sb->s_mnt_count = mount_count;
631 ext2fs_mark_super_dirty(fs);
632 printf("Setting current mount count to %d\n", mount_count);
635 sb->s_errors = errors;
636 ext2fs_mark_super_dirty(fs);
637 printf("Setting error behavior to %d\n", errors);
640 sb->s_def_resgid = resgid;
641 ext2fs_mark_super_dirty(fs);
642 printf("Setting reserved blocks gid to %lu\n", resgid);
645 sb->s_checkinterval = interval;
646 ext2fs_mark_super_dirty(fs);
647 printf("Setting interval between check %lu seconds\n", interval);
650 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
652 ext2fs_mark_super_dirty(fs);
653 printf("Setting reserved blocks percentage to %lu (%u blocks)\n",
654 reserved_ratio, sb->s_r_blocks_count);
657 if (reserved_blocks >= sb->s_blocks_count/2)
658 bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
659 sb->s_r_blocks_count = reserved_blocks;
660 ext2fs_mark_super_dirty(fs);
661 printf("Setting reserved blocks count to %lu\n", reserved_blocks);
664 if (sb->s_feature_ro_compat &
665 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
666 bb_error_msg("\nThe filesystem already has sparse superblocks");
668 sb->s_feature_ro_compat |=
669 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
670 sb->s_state &= ~EXT2_VALID_FS;
671 ext2fs_mark_super_dirty(fs);
672 printf("\nSparse superblock flag set. %s", please_fsck);
676 if (!(sb->s_feature_ro_compat &
677 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
678 bb_error_msg("\nThe filesystem already has sparse superblocks disabled");
680 sb->s_feature_ro_compat &=
681 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
682 sb->s_state &= ~EXT2_VALID_FS;
683 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
684 ext2fs_mark_super_dirty(fs);
685 printf("\nSparse superblock flag cleared. %s", please_fsck);
689 sb->s_lastcheck = last_check_time;
690 ext2fs_mark_super_dirty(fs);
691 printf("Setting time filesystem last checked to %s\n",
692 ctime(&last_check_time));
695 sb->s_def_resuid = resuid;
696 ext2fs_mark_super_dirty(fs);
697 printf("Setting reserved blocks uid to %lu\n", resuid);
700 if (strlen(new_label) > sizeof(sb->s_volume_name))
701 bb_error_msg("Warning: label too long, truncating");
702 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
703 safe_strncpy(sb->s_volume_name, new_label,
704 sizeof(sb->s_volume_name));
705 ext2fs_mark_super_dirty(fs);
708 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
709 safe_strncpy(sb->s_last_mounted, new_last_mounted,
710 sizeof(sb->s_last_mounted));
711 ext2fs_mark_super_dirty(fs);
714 update_mntopts(fs, mntopts_cmd);
716 update_feature_set(fs, features_cmd);
717 if (journal_size || journal_device)
721 if ((strcasecmp(new_UUID, "null") == 0) ||
722 (strcasecmp(new_UUID, "clear") == 0)) {
723 uuid_clear(sb->s_uuid);
724 } else if (strcasecmp(new_UUID, "time") == 0) {
725 uuid_generate_time(sb->s_uuid);
726 } else if (strcasecmp(new_UUID, "random") == 0) {
727 uuid_generate(sb->s_uuid);
728 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
729 bb_error_msg_and_die("Invalid UUID format");
731 ext2fs_mark_super_dirty(fs);
736 return (ext2fs_close (fs) ? 1 : 0);