blob: d22deede2d89ea26de76a7cd29071882a25ad8d0 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerd5624dc2005-06-11 00:40:20 +00002/*
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"ab57f762005-10-12 12:11:42 +000029#include <sys/types.h>
Mike Frysingerd5624dc2005-06-11 00:40:20 +000030#include <fcntl.h>
Mike Frysingerd5624dc2005-06-11 00:40:20 +000031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
"Vladimir N. Oleynik"ab57f762005-10-12 12:11:42 +000036#include <getopt.h>
Mike Frysingerd5624dc2005-06-11 00:40:20 +000037
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 Landley43ac8882006-04-01 00:40:33 +000043#include "ext2fs/kernel-jbd.h"
Mike Frysingerd5624dc2005-06-11 00:40:20 +000044#include "util.h"
45#include "blkid/blkid.h"
46
"Vladimir N. Oleynik"1c275de2005-10-12 11:38:09 +000047#include "busybox.h"
"Vladimir N. Oleynik"1c275de2005-10-12 11:38:09 +000048
Mike Frysinger7f782da2005-10-02 08:10:31 +000049static char * device_name = NULL;
Mike Frysingerd5624dc2005-06-11 00:40:20 +000050static char * new_label, *new_last_mounted, *new_UUID;
51static char * io_options;
52static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
53static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
54static time_t last_check_time;
55static int print_label;
56static int max_mount_count, mount_count, mount_flags;
57static unsigned long interval, reserved_ratio, reserved_blocks;
58static unsigned long resgid, resuid;
59static unsigned short errors;
60static int open_flag;
61static char *features_cmd;
62static char *mntopts_cmd;
63
64static int journal_size, journal_flags;
Mike Frysinger7f782da2005-10-02 08:10:31 +000065static char *journal_device = NULL;
Mike Frysingerd5624dc2005-06-11 00:40:20 +000066
67static const char *please_fsck = "Please run e2fsck on the filesystem\n";
68
69static __u32 ok_features[3] = {
70 EXT3_FEATURE_COMPAT_HAS_JOURNAL | EXT2_FEATURE_COMPAT_DIR_INDEX,
71 EXT2_FEATURE_INCOMPAT_FILETYPE,
72 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
73};
74
75/*
76 * Remove an external journal from the filesystem
77 */
78static void remove_journal_device(ext2_filsys fs)
79{
80 char *journal_path;
81 ext2_filsys jfs;
82 char buf[1024];
83 journal_superblock_t *jsb;
84 int i, nr_users;
85 errcode_t retval;
86 int commit_remove_journal = 0;
87 io_manager io_ptr;
88
89 if (f_flag)
90 commit_remove_journal = 1; /* force removal even if error */
91
92 uuid_unparse(fs->super->s_journal_uuid, buf);
93 journal_path = blkid_get_devname(NULL, "UUID", buf);
94
95 if (!journal_path) {
96 journal_path =
97 ext2fs_find_block_device(fs->super->s_journal_dev);
98 if (!journal_path)
99 return;
100 }
101
102 io_ptr = unix_io_manager;
103 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
104 EXT2_FLAG_JOURNAL_DEV_OK, 0,
105 fs->blocksize, io_ptr, &jfs);
106 if (retval) {
107 bb_error_msg("Failed to open external journal");
108 goto no_valid_journal;
109 }
110 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
111 bb_error_msg("%s is not a journal device", journal_path);
112 goto no_valid_journal;
113 }
114
115 /* Get the journal superblock */
116 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
117 bb_error_msg("Failed to read journal superblock");
118 goto no_valid_journal;
119 }
120
121 jsb = (journal_superblock_t *) buf;
122 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
123 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
124 bb_error_msg("Journal superblock not found!");
125 goto no_valid_journal;
126 }
127
128 /* Find the filesystem UUID */
129 nr_users = ntohl(jsb->s_nr_users);
130 for (i=0; i < nr_users; i++) {
131 if (memcmp(fs->super->s_uuid,
132 &jsb->s_users[i*16], 16) == 0)
133 break;
134 }
135 if (i >= nr_users) {
136 bb_error_msg("Filesystem's UUID not found on journal device");
137 commit_remove_journal = 1;
138 goto no_valid_journal;
139 }
140 nr_users--;
141 for (i=0; i < nr_users; i++)
142 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
143 jsb->s_nr_users = htonl(nr_users);
144
145 /* Write back the journal superblock */
146 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
147 bb_error_msg("Failed to write journal superblock");
148 goto no_valid_journal;
149 }
150
151 commit_remove_journal = 1;
152
153no_valid_journal:
154 if (commit_remove_journal == 0)
155 bb_error_msg_and_die("Journal NOT removed");
156 fs->super->s_journal_dev = 0;
157 uuid_clear(fs->super->s_journal_uuid);
158 ext2fs_mark_super_dirty(fs);
159 puts("Journal removed");
160 free(journal_path);
161}
162
163/* Helper function for remove_journal_inode */
164static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000165 int blockcnt EXT2FS_ATTR((unused)),
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000166 void *private EXT2FS_ATTR((unused)))
167{
168 blk_t block;
169 int group;
170
171 block = *blocknr;
172 ext2fs_unmark_block_bitmap(fs->block_map,block);
173 group = ext2fs_group_of_blk(fs, block);
174 fs->group_desc[group].bg_free_blocks_count++;
175 fs->super->s_free_blocks_count++;
176 return 0;
177}
178
179/*
180 * Remove the journal inode from the filesystem
181 */
182static void remove_journal_inode(ext2_filsys fs)
183{
184 struct ext2_inode inode;
185 errcode_t retval;
186 ino_t ino = fs->super->s_journal_inum;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000187 char *msg = "to read";
188 char *s = "journal inode";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000189
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000190 retval = ext2fs_read_inode(fs, ino, &inode);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000191 if (retval)
Mike Frysinger7f782da2005-10-02 08:10:31 +0000192 goto REMOVE_JOURNAL_INODE_ERROR;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000193 if (ino == EXT2_JOURNAL_INO) {
194 retval = ext2fs_read_bitmaps(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000195 if (retval) {
196 msg = "to read bitmaps";
197 s = "";
198 goto REMOVE_JOURNAL_INODE_ERROR;
199 }
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000200 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
201 release_blocks_proc, NULL);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000202 if (retval) {
203 msg = "clearing";
204 goto REMOVE_JOURNAL_INODE_ERROR;
205 }
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000206 memset(&inode, 0, sizeof(inode));
207 ext2fs_mark_bb_dirty(fs);
208 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
209 } else
210 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
211 retval = ext2fs_write_inode(fs, ino, &inode);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000212 if (retval) {
213 msg = "writing";
214REMOVE_JOURNAL_INODE_ERROR:
215 bb_error_msg_and_die("Failed %s %s", msg, s);
216 }
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000217 fs->super->s_journal_inum = 0;
218 ext2fs_mark_super_dirty(fs);
219}
220
221/*
222 * Update the default mount options
223 */
224static void update_mntopts(ext2_filsys fs, char *mntopts)
225{
226 struct ext2_super_block *sb= fs->super;
227
228 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
229 bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
230 ext2fs_mark_super_dirty(fs);
231}
232
233/*
234 * Update the feature set as provided by the user.
235 */
236static void update_feature_set(ext2_filsys fs, char *features)
237{
238 int sparse, old_sparse, filetype, old_filetype;
239 int journal, old_journal, dxdir, old_dxdir;
240 struct ext2_super_block *sb= fs->super;
241 __u32 old_compat, old_incompat, old_ro_compat;
242
243 old_compat = sb->s_feature_compat;
244 old_ro_compat = sb->s_feature_ro_compat;
245 old_incompat = sb->s_feature_incompat;
246
247 old_sparse = sb->s_feature_ro_compat &
248 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
249 old_filetype = sb->s_feature_incompat &
250 EXT2_FEATURE_INCOMPAT_FILETYPE;
251 old_journal = sb->s_feature_compat &
252 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
253 old_dxdir = sb->s_feature_compat &
254 EXT2_FEATURE_COMPAT_DIR_INDEX;
255 if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
256 bb_error_msg_and_die("Invalid filesystem option set: %s", features);
257 sparse = sb->s_feature_ro_compat &
258 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
259 filetype = sb->s_feature_incompat &
260 EXT2_FEATURE_INCOMPAT_FILETYPE;
261 journal = sb->s_feature_compat &
262 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
263 dxdir = sb->s_feature_compat &
264 EXT2_FEATURE_COMPAT_DIR_INDEX;
265 if (old_journal && !journal) {
266 if ((mount_flags & EXT2_MF_MOUNTED) &&
267 !(mount_flags & EXT2_MF_READONLY)) {
268 bb_error_msg_and_die(
269 "The has_journal flag may only be "
270 "cleared when the filesystem is\n"
271 "unmounted or mounted "
272 "read-only");
273 }
274 if (sb->s_feature_incompat &
275 EXT3_FEATURE_INCOMPAT_RECOVER) {
276 bb_error_msg_and_die(
277 "The needs_recovery flag is set. "
Mike Frysinger7f782da2005-10-02 08:10:31 +0000278 "%s before clearing the has_journal flag.",
279 please_fsck);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000280 }
281 if (sb->s_journal_inum) {
282 remove_journal_inode(fs);
283 }
284 if (sb->s_journal_dev) {
285 remove_journal_device(fs);
286 }
287 }
288 if (journal && !old_journal) {
289 /*
290 * If adding a journal flag, let the create journal
291 * code below handle creating setting the flag and
292 * creating the journal. We supply a default size if
293 * necessary.
294 */
295 if (!journal_size)
296 journal_size = -1;
297 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
298 }
299 if (dxdir && !old_dxdir) {
300 if (!sb->s_def_hash_version)
301 sb->s_def_hash_version = EXT2_HASH_TEA;
302 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
303 uuid_generate((unsigned char *) sb->s_hash_seed);
304 }
305
306 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
307 (sb->s_feature_compat || sb->s_feature_ro_compat ||
308 sb->s_feature_incompat))
309 ext2fs_update_dynamic_rev(fs);
310 if ((sparse != old_sparse) ||
311 (filetype != old_filetype)) {
312 sb->s_state &= ~EXT2_VALID_FS;
313 printf("\n%s\n", please_fsck);
314 }
315 if ((old_compat != sb->s_feature_compat) ||
316 (old_ro_compat != sb->s_feature_ro_compat) ||
317 (old_incompat != sb->s_feature_incompat))
318 ext2fs_mark_super_dirty(fs);
319}
320
321/*
322 * Add a journal to the filesystem.
323 */
324static void add_journal(ext2_filsys fs)
325{
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000326 if (fs->super->s_feature_compat &
327 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000328 bb_error_msg_and_die("The filesystem already has a journal");
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000329 }
330 if (journal_device) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000331 make_journal_device(journal_device, fs, 0, 0);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000332 } else if (journal_size) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000333 make_journal_blocks(fs, journal_size, journal_flags, 0);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000334 /*
335 * If the filesystem wasn't mounted, we need to force
336 * the block group descriptors out.
337 */
338 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
339 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
340 }
341 print_check_message(fs);
342 return;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000343}
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000344
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000345/*
Mike Frysinger7f782da2005-10-02 08:10:31 +0000346 * Busybox stuff
347 */
348static char * x_blkid_get_devname(const char *token)
349{
350 char * dev_name;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000351
Mike Frysinger7f782da2005-10-02 08:10:31 +0000352 if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
353 bb_error_msg_and_die("Unable to resolve '%s'", token);
354 return dev_name;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000355}
356
Mike Frysingera1c6a572005-09-24 05:55:03 +0000357#ifdef CONFIG_E2LABEL
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000358static void parse_e2label_options(int argc, char ** argv)
359{
360 if ((argc < 2) || (argc > 3))
361 bb_show_usage();
362 io_options = strchr(argv[1], '?');
363 if (io_options)
364 *io_options++ = 0;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000365 device_name = x_blkid_get_devname(argv[1]);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000366 if (argc == 3) {
367 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
368 L_flag = 1;
369 new_label = argv[2];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000370 } else
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000371 print_label++;
372}
Mike Frysinger7f782da2005-10-02 08:10:31 +0000373#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000374#define parse_e2label_options(x,y)
Mike Frysingera1c6a572005-09-24 05:55:03 +0000375#endif
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000376
377static time_t parse_time(char *str)
378{
379 struct tm ts;
380
381 if (strcmp(str, "now") == 0) {
382 return (time(0));
383 }
384 memset(&ts, 0, sizeof(ts));
385#ifdef HAVE_STRPTIME
386 strptime(str, "%Y%m%d%H%M%S", &ts);
387#else
388 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
389 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
390 ts.tm_year -= 1900;
391 ts.tm_mon -= 1;
392 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
393 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
394 ts.tm_min > 59 || ts.tm_sec > 61)
395 ts.tm_mday = 0;
396#endif
397 if (ts.tm_mday == 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000398 bb_error_msg_and_die("Cannot parse date/time specifier: %s", str);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000399 }
400 return (mktime(&ts));
401}
402
403static void parse_tune2fs_options(int argc, char **argv)
404{
405 int c;
406 char * tmp;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000407
408 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
409 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
410 switch (c)
411 {
412 case 'c':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000413 if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) {
414 goto MOUNTS_COUNT_ERROR;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000415 }
416 if (max_mount_count == 0)
417 max_mount_count = -1;
418 c_flag = 1;
419 open_flag = EXT2_FLAG_RW;
420 break;
421 case 'C':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000422 if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) {
423MOUNTS_COUNT_ERROR:
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000424 bb_error_msg_and_die("bad mounts count - %s", optarg);
425 }
426 C_flag = 1;
427 open_flag = EXT2_FLAG_RW;
428 break;
429 case 'e':
430 if (strcmp (optarg, "continue") == 0)
431 errors = EXT2_ERRORS_CONTINUE;
432 else if (strcmp (optarg, "remount-ro") == 0)
433 errors = EXT2_ERRORS_RO;
434 else if (strcmp (optarg, "panic") == 0)
435 errors = EXT2_ERRORS_PANIC;
436 else {
437 bb_error_msg_and_die("bad error behavior - %s", optarg);
438 }
439 e_flag = 1;
440 open_flag = EXT2_FLAG_RW;
441 break;
442 case 'f': /* Force */
443 f_flag = 1;
444 break;
445 case 'g':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000446 if (safe_strtoul(optarg, &resgid))
447 resgid = bb_xgetgrnam(optarg);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000448 g_flag = 1;
449 open_flag = EXT2_FLAG_RW;
450 break;
451 case 'i':
452 interval = strtoul (optarg, &tmp, 0);
453 switch (*tmp) {
454 case 's':
455 tmp++;
456 break;
457 case '\0':
458 case 'd':
459 case 'D': /* days */
460 interval *= 86400;
461 if (*tmp != '\0')
462 tmp++;
463 break;
464 case 'm':
465 case 'M': /* months! */
466 interval *= 86400 * 30;
467 tmp++;
468 break;
469 case 'w':
470 case 'W': /* weeks */
471 interval *= 86400 * 7;
472 tmp++;
473 break;
474 }
475 if (*tmp || interval > (365 * 86400)) {
476 bb_error_msg_and_die("bad interval - %s", optarg);
477 }
478 i_flag = 1;
479 open_flag = EXT2_FLAG_RW;
480 break;
481 case 'j':
482 if (!journal_size)
483 journal_size = -1;
484 open_flag = EXT2_FLAG_RW;
485 break;
486 case 'J':
487 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
488 open_flag = EXT2_FLAG_RW;
489 break;
490 case 'l':
491 l_flag = 1;
492 break;
493 case 'L':
494 new_label = optarg;
495 L_flag = 1;
496 open_flag = EXT2_FLAG_RW |
497 EXT2_FLAG_JOURNAL_DEV_OK;
498 break;
499 case 'm':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000500 if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) {
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000501 bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
502 }
503 m_flag = 1;
504 open_flag = EXT2_FLAG_RW;
505 break;
506 case 'M':
507 new_last_mounted = optarg;
508 M_flag = 1;
509 open_flag = EXT2_FLAG_RW;
510 break;
511 case 'o':
512 if (mntopts_cmd) {
513 bb_error_msg_and_die("-o may only be specified once");
514 }
515 mntopts_cmd = optarg;
516 open_flag = EXT2_FLAG_RW;
517 break;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000518
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000519 case 'O':
520 if (features_cmd) {
521 bb_error_msg_and_die("-O may only be specified once");
522 }
523 features_cmd = optarg;
524 open_flag = EXT2_FLAG_RW;
525 break;
526 case 'r':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000527 if(safe_strtoul(optarg, &reserved_blocks)) {
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000528 bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
529 }
530 r_flag = 1;
531 open_flag = EXT2_FLAG_RW;
532 break;
533 case 's':
534 s_flag = atoi(optarg);
535 open_flag = EXT2_FLAG_RW;
536 break;
537 case 'T':
538 T_flag = 1;
539 last_check_time = parse_time(optarg);
540 open_flag = EXT2_FLAG_RW;
541 break;
542 case 'u':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000543 if (safe_strtoul(optarg, &resuid))
544 resuid = bb_xgetpwnam(optarg);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000545 u_flag = 1;
546 open_flag = EXT2_FLAG_RW;
547 break;
548 case 'U':
549 new_UUID = optarg;
550 U_flag = 1;
551 open_flag = EXT2_FLAG_RW |
552 EXT2_FLAG_JOURNAL_DEV_OK;
553 break;
554 default:
555 bb_show_usage();
556 }
557 if (optind < argc - 1 || optind == argc)
558 bb_show_usage();
559 if (!open_flag && !l_flag)
560 bb_show_usage();
561 io_options = strchr(argv[optind], '?');
562 if (io_options)
563 *io_options++ = 0;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000564 device_name = x_blkid_get_devname(argv[optind]);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000565}
566
Mike Frysingera1c6a572005-09-24 05:55:03 +0000567#ifdef CONFIG_FINDFS
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000568static ATTRIBUTE_NORETURN void do_findfs(int argc, char **argv)
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000569{
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000570 if ((argc != 2) ||
571 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
572 bb_show_usage();
Mike Frysinger7f782da2005-10-02 08:10:31 +0000573 device_name = x_blkid_get_devname(argv[1]);
574 puts(device_name);
Mike Frysinger0f8a6382005-09-24 06:07:34 +0000575 exit(0);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000576}
Mike Frysinger7f782da2005-10-02 08:10:31 +0000577#else
578#define do_findfs(x, y)
Mike Frysingera1c6a572005-09-24 05:55:03 +0000579#endif
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000580
Bernhard Reutner-Fischer1f6dc6d2006-03-30 13:54:30 +0000581static void tune2fs_clean_up(void)
Mike Frysinger7f782da2005-10-02 08:10:31 +0000582{
583 if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
584 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
585}
586
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000587int tune2fs_main(int argc, char **argv)
588{
589 errcode_t retval;
590 ext2_filsys fs;
591 struct ext2_super_block *sb;
592 io_manager io_ptr;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000593
Mike Frysinger7f782da2005-10-02 08:10:31 +0000594 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer1f6dc6d2006-03-30 13:54:30 +0000595 atexit(tune2fs_clean_up);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000596
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000597 if (ENABLE_FINDFS && (applet_name[0] == 'f')) /* findfs */
Mike Frysinger7f782da2005-10-02 08:10:31 +0000598 do_findfs(argc, argv); /* no return */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000599 else if (ENABLE_E2LABEL && (applet_name[0] == 'e')) /* e2label */
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000600 parse_e2label_options(argc, argv);
601 else
Mike Frysinger7f782da2005-10-02 08:10:31 +0000602 parse_tune2fs_options(argc, argv); /* tune2fs */
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000603
604 io_ptr = unix_io_manager;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000605 retval = ext2fs_open2(device_name, io_options, open_flag,
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000606 0, 0, io_ptr, &fs);
607 if (retval)
608 bb_error_msg_and_die("No valid superblock on %s", device_name);
609 sb = fs->super;
610 if (print_label) {
611 /* For e2label emulation */
612 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
613 sb->s_volume_name);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000614 return 0;
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000615 }
616 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
617 if (retval)
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000618 bb_error_msg_and_die("cannot determine if %s is mounted", device_name);
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000619 /* Normally we only need to write out the superblock */
620 fs->flags |= EXT2_FLAG_SUPER_ONLY;
621
622 if (c_flag) {
623 sb->s_max_mnt_count = max_mount_count;
624 ext2fs_mark_super_dirty(fs);
625 printf("Setting maximal mount count to %d\n", max_mount_count);
626 }
627 if (C_flag) {
628 sb->s_mnt_count = mount_count;
629 ext2fs_mark_super_dirty(fs);
630 printf("Setting current mount count to %d\n", mount_count);
631 }
632 if (e_flag) {
633 sb->s_errors = errors;
634 ext2fs_mark_super_dirty(fs);
635 printf("Setting error behavior to %d\n", errors);
636 }
637 if (g_flag) {
638 sb->s_def_resgid = resgid;
639 ext2fs_mark_super_dirty(fs);
640 printf("Setting reserved blocks gid to %lu\n", resgid);
641 }
642 if (i_flag) {
643 sb->s_checkinterval = interval;
644 ext2fs_mark_super_dirty(fs);
645 printf("Setting interval between check %lu seconds\n", interval);
646 }
647 if (m_flag) {
648 sb->s_r_blocks_count = (sb->s_blocks_count / 100)
649 * reserved_ratio;
650 ext2fs_mark_super_dirty(fs);
651 printf("Setting reserved blocks percentage to %lu (%u blocks)\n",
652 reserved_ratio, sb->s_r_blocks_count);
653 }
654 if (r_flag) {
655 if (reserved_blocks >= sb->s_blocks_count/2)
656 bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
657 sb->s_r_blocks_count = reserved_blocks;
658 ext2fs_mark_super_dirty(fs);
659 printf("Setting reserved blocks count to %lu\n", reserved_blocks);
660 }
661 if (s_flag == 1) {
662 if (sb->s_feature_ro_compat &
663 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000664 bb_error_msg("\nThe filesystem already has sparse superblocks");
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000665 else {
666 sb->s_feature_ro_compat |=
667 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
668 sb->s_state &= ~EXT2_VALID_FS;
669 ext2fs_mark_super_dirty(fs);
670 printf("\nSparse superblock flag set. %s", please_fsck);
671 }
672 }
673 if (s_flag == 0) {
674 if (!(sb->s_feature_ro_compat &
675 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000676 bb_error_msg("\nThe filesystem already has sparse superblocks disabled");
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000677 else {
678 sb->s_feature_ro_compat &=
679 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
680 sb->s_state &= ~EXT2_VALID_FS;
681 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
682 ext2fs_mark_super_dirty(fs);
683 printf("\nSparse superblock flag cleared. %s", please_fsck);
684 }
685 }
686 if (T_flag) {
687 sb->s_lastcheck = last_check_time;
688 ext2fs_mark_super_dirty(fs);
689 printf("Setting time filesystem last checked to %s\n",
690 ctime(&last_check_time));
691 }
692 if (u_flag) {
693 sb->s_def_resuid = resuid;
694 ext2fs_mark_super_dirty(fs);
695 printf("Setting reserved blocks uid to %lu\n", resuid);
696 }
697 if (L_flag) {
698 if (strlen(new_label) > sizeof(sb->s_volume_name))
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000699 bb_error_msg("Warning: label too long, truncating");
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000700 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
Rob Landley768945b2006-06-25 00:34:52 +0000701 safe_strncpy(sb->s_volume_name, new_label,
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000702 sizeof(sb->s_volume_name));
703 ext2fs_mark_super_dirty(fs);
704 }
705 if (M_flag) {
706 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
Rob Landley768945b2006-06-25 00:34:52 +0000707 safe_strncpy(sb->s_last_mounted, new_last_mounted,
Mike Frysingerd5624dc2005-06-11 00:40:20 +0000708 sizeof(sb->s_last_mounted));
709 ext2fs_mark_super_dirty(fs);
710 }
711 if (mntopts_cmd)
712 update_mntopts(fs, mntopts_cmd);
713 if (features_cmd)
714 update_feature_set(fs, features_cmd);
715 if (journal_size || journal_device)
716 add_journal(fs);
717
718 if (U_flag) {
719 if ((strcasecmp(new_UUID, "null") == 0) ||
720 (strcasecmp(new_UUID, "clear") == 0)) {
721 uuid_clear(sb->s_uuid);
722 } else if (strcasecmp(new_UUID, "time") == 0) {
723 uuid_generate_time(sb->s_uuid);
724 } else if (strcasecmp(new_UUID, "random") == 0) {
725 uuid_generate(sb->s_uuid);
726 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
727 bb_error_msg_and_die("Invalid UUID format");
728 }
729 ext2fs_mark_super_dirty(fs);
730 }
731
732 if (l_flag)
733 list_super (sb);
734 return (ext2fs_close (fs) ? 1 : 0);
735}