"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * tune2fs.c - Change the file system parameters on an ext2 file system |
| 4 | * |
| 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) |
| 8 | * |
| 9 | * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o. |
| 10 | * |
| 11 | * %Begin-Header% |
| 12 | * This file may be redistributed under the terms of the GNU Public |
| 13 | * License. |
| 14 | * %End-Header% |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * History: |
| 19 | * 93/06/01 - Creation |
| 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) |
| 27 | */ |
| 28 | |
"Vladimir N. Oleynik" | ab57f76 | 2005-10-12 12:11:42 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
"Vladimir N. Oleynik" | ab57f76 | 2005-10-12 12:11:42 +0000 | [diff] [blame] | 36 | #include <getopt.h> |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 37 | |
| 38 | #include "e2fsbb.h" |
| 39 | #include "ext2fs/ext2_fs.h" |
| 40 | #include "ext2fs/ext2fs.h" |
| 41 | #include "uuid/uuid.h" |
| 42 | #include "e2p/e2p.h" |
Rob Landley | 43ac888 | 2006-04-01 00:40:33 +0000 | [diff] [blame] | 43 | #include "ext2fs/kernel-jbd.h" |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 44 | #include "util.h" |
| 45 | #include "blkid/blkid.h" |
| 46 | |
"Vladimir N. Oleynik" | 1c275de | 2005-10-12 11:38:09 +0000 | [diff] [blame] | 47 | #include "busybox.h" |
| 48 | #include "grp_.h" |
| 49 | #include "pwd_.h" |
| 50 | |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 51 | static char * device_name = NULL; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 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; |
| 62 | static int open_flag; |
| 63 | static char *features_cmd; |
| 64 | static char *mntopts_cmd; |
| 65 | |
| 66 | static int journal_size, journal_flags; |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 67 | static char *journal_device = NULL; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 68 | |
| 69 | static const char *please_fsck = "Please run e2fsck on the filesystem\n"; |
| 70 | |
| 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 |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * Remove an external journal from the filesystem |
| 79 | */ |
| 80 | static void remove_journal_device(ext2_filsys fs) |
| 81 | { |
| 82 | char *journal_path; |
| 83 | ext2_filsys jfs; |
| 84 | char buf[1024]; |
| 85 | journal_superblock_t *jsb; |
| 86 | int i, nr_users; |
| 87 | errcode_t retval; |
| 88 | int commit_remove_journal = 0; |
| 89 | io_manager io_ptr; |
| 90 | |
| 91 | if (f_flag) |
| 92 | commit_remove_journal = 1; /* force removal even if error */ |
| 93 | |
| 94 | uuid_unparse(fs->super->s_journal_uuid, buf); |
| 95 | journal_path = blkid_get_devname(NULL, "UUID", buf); |
| 96 | |
| 97 | if (!journal_path) { |
| 98 | journal_path = |
| 99 | ext2fs_find_block_device(fs->super->s_journal_dev); |
| 100 | if (!journal_path) |
| 101 | return; |
| 102 | } |
| 103 | |
| 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); |
| 108 | if (retval) { |
| 109 | bb_error_msg("Failed to open external journal"); |
| 110 | goto no_valid_journal; |
| 111 | } |
| 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; |
| 115 | } |
| 116 | |
| 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; |
| 121 | } |
| 122 | |
| 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; |
| 128 | } |
| 129 | |
| 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) |
| 135 | break; |
| 136 | } |
| 137 | if (i >= nr_users) { |
| 138 | bb_error_msg("Filesystem's UUID not found on journal device"); |
| 139 | commit_remove_journal = 1; |
| 140 | goto no_valid_journal; |
| 141 | } |
| 142 | nr_users--; |
| 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); |
| 146 | |
| 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; |
| 151 | } |
| 152 | |
| 153 | commit_remove_journal = 1; |
| 154 | |
| 155 | no_valid_journal: |
| 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"); |
| 162 | free(journal_path); |
| 163 | } |
| 164 | |
| 165 | /* Helper function for remove_journal_inode */ |
| 166 | static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 167 | int blockcnt EXT2FS_ATTR((unused)), |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 168 | void *private EXT2FS_ATTR((unused))) |
| 169 | { |
| 170 | blk_t block; |
| 171 | int group; |
| 172 | |
| 173 | block = *blocknr; |
| 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++; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Remove the journal inode from the filesystem |
| 183 | */ |
| 184 | static void remove_journal_inode(ext2_filsys fs) |
| 185 | { |
| 186 | struct ext2_inode inode; |
| 187 | errcode_t retval; |
| 188 | ino_t ino = fs->super->s_journal_inum; |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 189 | char *msg = "to read"; |
| 190 | char *s = "journal inode"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 191 | |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 192 | retval = ext2fs_read_inode(fs, ino, &inode); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 193 | if (retval) |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 194 | goto REMOVE_JOURNAL_INODE_ERROR; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 195 | if (ino == EXT2_JOURNAL_INO) { |
| 196 | retval = ext2fs_read_bitmaps(fs); |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 197 | if (retval) { |
| 198 | msg = "to read bitmaps"; |
| 199 | s = ""; |
| 200 | goto REMOVE_JOURNAL_INODE_ERROR; |
| 201 | } |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 202 | retval = ext2fs_block_iterate(fs, ino, 0, NULL, |
| 203 | release_blocks_proc, NULL); |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 204 | if (retval) { |
| 205 | msg = "clearing"; |
| 206 | goto REMOVE_JOURNAL_INODE_ERROR; |
| 207 | } |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 208 | memset(&inode, 0, sizeof(inode)); |
| 209 | ext2fs_mark_bb_dirty(fs); |
| 210 | fs->flags &= ~EXT2_FLAG_SUPER_ONLY; |
| 211 | } else |
| 212 | inode.i_flags &= ~EXT2_IMMUTABLE_FL; |
| 213 | retval = ext2fs_write_inode(fs, ino, &inode); |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 214 | if (retval) { |
| 215 | msg = "writing"; |
| 216 | REMOVE_JOURNAL_INODE_ERROR: |
| 217 | bb_error_msg_and_die("Failed %s %s", msg, s); |
| 218 | } |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 219 | fs->super->s_journal_inum = 0; |
| 220 | ext2fs_mark_super_dirty(fs); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Update the default mount options |
| 225 | */ |
| 226 | static void update_mntopts(ext2_filsys fs, char *mntopts) |
| 227 | { |
| 228 | struct ext2_super_block *sb= fs->super; |
| 229 | |
| 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); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Update the feature set as provided by the user. |
| 237 | */ |
| 238 | static void update_feature_set(ext2_filsys fs, char *features) |
| 239 | { |
| 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; |
| 244 | |
| 245 | old_compat = sb->s_feature_compat; |
| 246 | old_ro_compat = sb->s_feature_ro_compat; |
| 247 | old_incompat = sb->s_feature_incompat; |
| 248 | |
| 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 " |
| 274 | "read-only"); |
| 275 | } |
| 276 | if (sb->s_feature_incompat & |
| 277 | EXT3_FEATURE_INCOMPAT_RECOVER) { |
| 278 | bb_error_msg_and_die( |
| 279 | "The needs_recovery flag is set. " |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 280 | "%s before clearing the has_journal flag.", |
| 281 | please_fsck); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 282 | } |
| 283 | if (sb->s_journal_inum) { |
| 284 | remove_journal_inode(fs); |
| 285 | } |
| 286 | if (sb->s_journal_dev) { |
| 287 | remove_journal_device(fs); |
| 288 | } |
| 289 | } |
| 290 | if (journal && !old_journal) { |
| 291 | /* |
| 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 |
| 295 | * necessary. |
| 296 | */ |
| 297 | if (!journal_size) |
| 298 | journal_size = -1; |
| 299 | sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; |
| 300 | } |
| 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); |
| 306 | } |
| 307 | |
| 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); |
| 316 | } |
| 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); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Add a journal to the filesystem. |
| 325 | */ |
| 326 | static void add_journal(ext2_filsys fs) |
| 327 | { |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 328 | if (fs->super->s_feature_compat & |
| 329 | EXT3_FEATURE_COMPAT_HAS_JOURNAL) { |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 330 | bb_error_msg_and_die("The filesystem already has a journal"); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 331 | } |
| 332 | if (journal_device) { |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 333 | make_journal_device(journal_device, fs, 0, 0); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 334 | } else if (journal_size) { |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 335 | make_journal_blocks(fs, journal_size, journal_flags, 0); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 336 | /* |
| 337 | * If the filesystem wasn't mounted, we need to force |
| 338 | * the block group descriptors out. |
| 339 | */ |
| 340 | if ((mount_flags & EXT2_MF_MOUNTED) == 0) |
| 341 | fs->flags &= ~EXT2_FLAG_SUPER_ONLY; |
| 342 | } |
| 343 | print_check_message(fs); |
| 344 | return; |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 345 | } |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 346 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 347 | /* |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 348 | * Busybox stuff |
| 349 | */ |
| 350 | static char * x_blkid_get_devname(const char *token) |
| 351 | { |
| 352 | char * dev_name; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 353 | |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 354 | if (!(dev_name = blkid_get_devname(NULL, token, NULL))) |
| 355 | bb_error_msg_and_die("Unable to resolve '%s'", token); |
| 356 | return dev_name; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 359 | #ifdef CONFIG_E2LABEL |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 360 | static void parse_e2label_options(int argc, char ** argv) |
| 361 | { |
| 362 | if ((argc < 2) || (argc > 3)) |
| 363 | bb_show_usage(); |
| 364 | io_options = strchr(argv[1], '?'); |
| 365 | if (io_options) |
| 366 | *io_options++ = 0; |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 367 | device_name = x_blkid_get_devname(argv[1]); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 368 | if (argc == 3) { |
| 369 | open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK; |
| 370 | L_flag = 1; |
| 371 | new_label = argv[2]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 372 | } else |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 373 | print_label++; |
| 374 | } |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 375 | #else |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 376 | #define parse_e2label_options(x,y) |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 377 | #endif |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 378 | |
| 379 | static time_t parse_time(char *str) |
| 380 | { |
| 381 | struct tm ts; |
| 382 | |
| 383 | if (strcmp(str, "now") == 0) { |
| 384 | return (time(0)); |
| 385 | } |
| 386 | memset(&ts, 0, sizeof(ts)); |
| 387 | #ifdef HAVE_STRPTIME |
| 388 | strptime(str, "%Y%m%d%H%M%S", &ts); |
| 389 | #else |
| 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); |
| 392 | ts.tm_year -= 1900; |
| 393 | ts.tm_mon -= 1; |
| 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) |
| 397 | ts.tm_mday = 0; |
| 398 | #endif |
| 399 | if (ts.tm_mday == 0) { |
| 400 | bb_error_msg_and_die("Couldn't parse date/time specifier: %s", str); |
| 401 | } |
| 402 | return (mktime(&ts)); |
| 403 | } |
| 404 | |
| 405 | static void parse_tune2fs_options(int argc, char **argv) |
| 406 | { |
| 407 | int c; |
| 408 | char * tmp; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 409 | |
| 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) |
| 412 | switch (c) |
| 413 | { |
| 414 | case 'c': |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 415 | if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) { |
| 416 | goto MOUNTS_COUNT_ERROR; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 417 | } |
| 418 | if (max_mount_count == 0) |
| 419 | max_mount_count = -1; |
| 420 | c_flag = 1; |
| 421 | open_flag = EXT2_FLAG_RW; |
| 422 | break; |
| 423 | case 'C': |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 424 | if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) { |
| 425 | MOUNTS_COUNT_ERROR: |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 426 | bb_error_msg_and_die("bad mounts count - %s", optarg); |
| 427 | } |
| 428 | C_flag = 1; |
| 429 | open_flag = EXT2_FLAG_RW; |
| 430 | break; |
| 431 | case 'e': |
| 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; |
| 438 | else { |
| 439 | bb_error_msg_and_die("bad error behavior - %s", optarg); |
| 440 | } |
| 441 | e_flag = 1; |
| 442 | open_flag = EXT2_FLAG_RW; |
| 443 | break; |
| 444 | case 'f': /* Force */ |
| 445 | f_flag = 1; |
| 446 | break; |
| 447 | case 'g': |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 448 | if (safe_strtoul(optarg, &resgid)) |
| 449 | resgid = bb_xgetgrnam(optarg); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 450 | g_flag = 1; |
| 451 | open_flag = EXT2_FLAG_RW; |
| 452 | break; |
| 453 | case 'i': |
| 454 | interval = strtoul (optarg, &tmp, 0); |
| 455 | switch (*tmp) { |
| 456 | case 's': |
| 457 | tmp++; |
| 458 | break; |
| 459 | case '\0': |
| 460 | case 'd': |
| 461 | case 'D': /* days */ |
| 462 | interval *= 86400; |
| 463 | if (*tmp != '\0') |
| 464 | tmp++; |
| 465 | break; |
| 466 | case 'm': |
| 467 | case 'M': /* months! */ |
| 468 | interval *= 86400 * 30; |
| 469 | tmp++; |
| 470 | break; |
| 471 | case 'w': |
| 472 | case 'W': /* weeks */ |
| 473 | interval *= 86400 * 7; |
| 474 | tmp++; |
| 475 | break; |
| 476 | } |
| 477 | if (*tmp || interval > (365 * 86400)) { |
| 478 | bb_error_msg_and_die("bad interval - %s", optarg); |
| 479 | } |
| 480 | i_flag = 1; |
| 481 | open_flag = EXT2_FLAG_RW; |
| 482 | break; |
| 483 | case 'j': |
| 484 | if (!journal_size) |
| 485 | journal_size = -1; |
| 486 | open_flag = EXT2_FLAG_RW; |
| 487 | break; |
| 488 | case 'J': |
| 489 | parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg); |
| 490 | open_flag = EXT2_FLAG_RW; |
| 491 | break; |
| 492 | case 'l': |
| 493 | l_flag = 1; |
| 494 | break; |
| 495 | case 'L': |
| 496 | new_label = optarg; |
| 497 | L_flag = 1; |
| 498 | open_flag = EXT2_FLAG_RW | |
| 499 | EXT2_FLAG_JOURNAL_DEV_OK; |
| 500 | break; |
| 501 | case 'm': |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 502 | if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) { |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 503 | bb_error_msg_and_die("bad reserved block ratio - %s", optarg); |
| 504 | } |
| 505 | m_flag = 1; |
| 506 | open_flag = EXT2_FLAG_RW; |
| 507 | break; |
| 508 | case 'M': |
| 509 | new_last_mounted = optarg; |
| 510 | M_flag = 1; |
| 511 | open_flag = EXT2_FLAG_RW; |
| 512 | break; |
| 513 | case 'o': |
| 514 | if (mntopts_cmd) { |
| 515 | bb_error_msg_and_die("-o may only be specified once"); |
| 516 | } |
| 517 | mntopts_cmd = optarg; |
| 518 | open_flag = EXT2_FLAG_RW; |
| 519 | break; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 520 | |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 521 | case 'O': |
| 522 | if (features_cmd) { |
| 523 | bb_error_msg_and_die("-O may only be specified once"); |
| 524 | } |
| 525 | features_cmd = optarg; |
| 526 | open_flag = EXT2_FLAG_RW; |
| 527 | break; |
| 528 | case 'r': |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 529 | if(safe_strtoul(optarg, &reserved_blocks)) { |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 530 | bb_error_msg_and_die("bad reserved blocks count - %s", optarg); |
| 531 | } |
| 532 | r_flag = 1; |
| 533 | open_flag = EXT2_FLAG_RW; |
| 534 | break; |
| 535 | case 's': |
| 536 | s_flag = atoi(optarg); |
| 537 | open_flag = EXT2_FLAG_RW; |
| 538 | break; |
| 539 | case 'T': |
| 540 | T_flag = 1; |
| 541 | last_check_time = parse_time(optarg); |
| 542 | open_flag = EXT2_FLAG_RW; |
| 543 | break; |
| 544 | case 'u': |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 545 | if (safe_strtoul(optarg, &resuid)) |
| 546 | resuid = bb_xgetpwnam(optarg); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 547 | u_flag = 1; |
| 548 | open_flag = EXT2_FLAG_RW; |
| 549 | break; |
| 550 | case 'U': |
| 551 | new_UUID = optarg; |
| 552 | U_flag = 1; |
| 553 | open_flag = EXT2_FLAG_RW | |
| 554 | EXT2_FLAG_JOURNAL_DEV_OK; |
| 555 | break; |
| 556 | default: |
| 557 | bb_show_usage(); |
| 558 | } |
| 559 | if (optind < argc - 1 || optind == argc) |
| 560 | bb_show_usage(); |
| 561 | if (!open_flag && !l_flag) |
| 562 | bb_show_usage(); |
| 563 | io_options = strchr(argv[optind], '?'); |
| 564 | if (io_options) |
| 565 | *io_options++ = 0; |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 566 | device_name = x_blkid_get_devname(argv[optind]); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 569 | #ifdef CONFIG_FINDFS |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 570 | static ATTRIBUTE_NORETURN void do_findfs(int argc, char **argv) |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 571 | { |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 572 | if ((argc != 2) || |
| 573 | (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) |
| 574 | bb_show_usage(); |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 575 | device_name = x_blkid_get_devname(argv[1]); |
| 576 | puts(device_name); |
Mike Frysinger | 0f8a638 | 2005-09-24 06:07:34 +0000 | [diff] [blame] | 577 | exit(0); |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 578 | } |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 579 | #else |
| 580 | #define do_findfs(x, y) |
Mike Frysinger | a1c6a57 | 2005-09-24 05:55:03 +0000 | [diff] [blame] | 581 | #endif |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 582 | |
Bernhard Reutner-Fischer | 1f6dc6d | 2006-03-30 13:54:30 +0000 | [diff] [blame] | 583 | static void tune2fs_clean_up(void) |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 584 | { |
| 585 | if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name); |
| 586 | if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device); |
| 587 | } |
| 588 | |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 589 | int tune2fs_main(int argc, char **argv) |
| 590 | { |
| 591 | errcode_t retval; |
| 592 | ext2_filsys fs; |
| 593 | struct ext2_super_block *sb; |
| 594 | io_manager io_ptr; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 595 | |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 596 | if (ENABLE_FEATURE_CLEAN_UP) |
Bernhard Reutner-Fischer | 1f6dc6d | 2006-03-30 13:54:30 +0000 | [diff] [blame] | 597 | atexit(tune2fs_clean_up); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 598 | |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 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 */ |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 602 | parse_e2label_options(argc, argv); |
| 603 | else |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 604 | parse_tune2fs_options(argc, argv); /* tune2fs */ |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 605 | |
| 606 | io_ptr = unix_io_manager; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 607 | retval = ext2fs_open2(device_name, io_options, open_flag, |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 608 | 0, 0, io_ptr, &fs); |
| 609 | if (retval) |
| 610 | bb_error_msg_and_die("No valid superblock on %s", device_name); |
| 611 | sb = fs->super; |
| 612 | if (print_label) { |
| 613 | /* For e2label emulation */ |
| 614 | printf("%.*s\n", (int) sizeof(sb->s_volume_name), |
| 615 | sb->s_volume_name); |
Mike Frysinger | 7f782da | 2005-10-02 08:10:31 +0000 | [diff] [blame] | 616 | return 0; |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 617 | } |
| 618 | retval = ext2fs_check_if_mounted(device_name, &mount_flags); |
| 619 | if (retval) |
| 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; |
| 623 | |
| 624 | if (c_flag) { |
| 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); |
| 628 | } |
| 629 | if (C_flag) { |
| 630 | sb->s_mnt_count = mount_count; |
| 631 | ext2fs_mark_super_dirty(fs); |
| 632 | printf("Setting current mount count to %d\n", mount_count); |
| 633 | } |
| 634 | if (e_flag) { |
| 635 | sb->s_errors = errors; |
| 636 | ext2fs_mark_super_dirty(fs); |
| 637 | printf("Setting error behavior to %d\n", errors); |
| 638 | } |
| 639 | if (g_flag) { |
| 640 | sb->s_def_resgid = resgid; |
| 641 | ext2fs_mark_super_dirty(fs); |
| 642 | printf("Setting reserved blocks gid to %lu\n", resgid); |
| 643 | } |
| 644 | if (i_flag) { |
| 645 | sb->s_checkinterval = interval; |
| 646 | ext2fs_mark_super_dirty(fs); |
| 647 | printf("Setting interval between check %lu seconds\n", interval); |
| 648 | } |
| 649 | if (m_flag) { |
| 650 | sb->s_r_blocks_count = (sb->s_blocks_count / 100) |
| 651 | * reserved_ratio; |
| 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); |
| 655 | } |
| 656 | if (r_flag) { |
| 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); |
| 662 | } |
| 663 | if (s_flag == 1) { |
| 664 | if (sb->s_feature_ro_compat & |
| 665 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) |
| 666 | bb_error_msg("\nThe filesystem already has sparse superblocks\n"); |
| 667 | else { |
| 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); |
| 673 | } |
| 674 | } |
| 675 | if (s_flag == 0) { |
| 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\n"); |
| 679 | else { |
| 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); |
| 686 | } |
| 687 | } |
| 688 | if (T_flag) { |
| 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)); |
| 693 | } |
| 694 | if (u_flag) { |
| 695 | sb->s_def_resuid = resuid; |
| 696 | ext2fs_mark_super_dirty(fs); |
| 697 | printf("Setting reserved blocks uid to %lu\n", resuid); |
| 698 | } |
| 699 | if (L_flag) { |
| 700 | if (strlen(new_label) > sizeof(sb->s_volume_name)) |
| 701 | bb_error_msg("Warning: label too long, truncating\n"); |
| 702 | memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name)); |
Rob Landley | 768945b | 2006-06-25 00:34:52 +0000 | [diff] [blame] | 703 | safe_strncpy(sb->s_volume_name, new_label, |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 704 | sizeof(sb->s_volume_name)); |
| 705 | ext2fs_mark_super_dirty(fs); |
| 706 | } |
| 707 | if (M_flag) { |
| 708 | memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted)); |
Rob Landley | 768945b | 2006-06-25 00:34:52 +0000 | [diff] [blame] | 709 | safe_strncpy(sb->s_last_mounted, new_last_mounted, |
Mike Frysinger | d5624dc | 2005-06-11 00:40:20 +0000 | [diff] [blame] | 710 | sizeof(sb->s_last_mounted)); |
| 711 | ext2fs_mark_super_dirty(fs); |
| 712 | } |
| 713 | if (mntopts_cmd) |
| 714 | update_mntopts(fs, mntopts_cmd); |
| 715 | if (features_cmd) |
| 716 | update_feature_set(fs, features_cmd); |
| 717 | if (journal_size || journal_device) |
| 718 | add_journal(fs); |
| 719 | |
| 720 | if (U_flag) { |
| 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"); |
| 730 | } |
| 731 | ext2fs_mark_super_dirty(fs); |
| 732 | } |
| 733 | |
| 734 | if (l_flag) |
| 735 | list_super (sb); |
| 736 | return (ext2fs_close (fs) ? 1 : 0); |
| 737 | } |