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)
28 #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
29 #define _BSD_SOURCE /* for inclusion of strcasecmp() */
46 #include <sys/types.h>
50 #include "ext2fs/ext2_fs.h"
51 #include "ext2fs/ext2fs.h"
52 #include "et/com_err.h"
53 #include "uuid/uuid.h"
57 #include "blkid/blkid.h"
59 #include "../version.h"
60 #include "nls-enable.h"
62 const char * program_name
= "tune2fs";
64 char * new_label
, *new_last_mounted
, *new_UUID
;
66 static int c_flag
, C_flag
, e_flag
, f_flag
, g_flag
, i_flag
, l_flag
, L_flag
;
67 static int m_flag
, M_flag
, r_flag
, s_flag
= -1, u_flag
, U_flag
, T_flag
;
69 static time_t last_check_time
;
70 static int print_label
;
71 static int max_mount_count
, mount_count
, mount_flags
;
72 static unsigned long interval
, reserved_blocks
;
73 static double reserved_ratio
;
74 static unsigned long resgid
, resuid
;
75 static unsigned short errors
;
77 static char *features_cmd
;
78 static char *mntopts_cmd
;
79 static int stride
, stripe_width
;
80 static int stride_set
, stripe_width_set
;
81 static char *extended_cmd
;
82 static unsigned long new_inode_size
;
84 int journal_size
, journal_flags
;
87 static struct list_head blk_move_list
;
90 struct list_head list
;
96 static const char *please_fsck
= N_("Please run e2fsck on the filesystem.\n");
98 void do_findfs(int argc
, char **argv
);
100 static void usage(void)
103 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
105 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
106 "\t[-m reserved_blocks_percent] "
107 "[-o [^]mount_options[,...]] \n"
108 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
109 "[-L volume_label]\n"
110 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
111 "\t[-E extended-option[,...]] [-T last_check_time] "
112 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name
);
116 static __u32 ok_features
[3] = {
118 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
119 EXT2_FEATURE_COMPAT_DIR_INDEX
,
121 EXT2_FEATURE_INCOMPAT_FILETYPE
|
122 EXT3_FEATURE_INCOMPAT_EXTENTS
|
123 EXT4_FEATURE_INCOMPAT_FLEX_BG
,
125 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
126 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
127 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
128 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
129 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
|
130 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
133 static __u32 clear_ok_features
[3] = {
135 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
136 EXT2_FEATURE_COMPAT_RESIZE_INODE
|
137 EXT2_FEATURE_COMPAT_DIR_INDEX
,
139 EXT2_FEATURE_INCOMPAT_FILETYPE
|
140 EXT4_FEATURE_INCOMPAT_FLEX_BG
,
142 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
143 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
144 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
145 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
146 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
150 * Remove an external journal from the filesystem
152 static void remove_journal_device(ext2_filsys fs
)
157 journal_superblock_t
*jsb
;
160 int commit_remove_journal
= 0;
164 commit_remove_journal
= 1; /* force removal even if error */
166 uuid_unparse(fs
->super
->s_journal_uuid
, buf
);
167 journal_path
= blkid_get_devname(NULL
, "UUID", buf
);
171 ext2fs_find_block_device(fs
->super
->s_journal_dev
);
176 #ifdef CONFIG_TESTIO_DEBUG
177 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
178 io_ptr
= test_io_manager
;
179 test_io_backing_manager
= unix_io_manager
;
182 io_ptr
= unix_io_manager
;
183 retval
= ext2fs_open(journal_path
, EXT2_FLAG_RW
|
184 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
185 fs
->blocksize
, io_ptr
, &jfs
);
187 com_err(program_name
, retval
,
188 _("while trying to open external journal"));
189 goto no_valid_journal
;
191 if (!(jfs
->super
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)) {
192 fprintf(stderr
, _("%s is not a journal device.\n"),
194 goto no_valid_journal
;
197 /* Get the journal superblock */
198 if ((retval
= io_channel_read_blk(jfs
->io
, 1, -1024, buf
))) {
199 com_err(program_name
, retval
,
200 _("while reading journal superblock"));
201 goto no_valid_journal
;
204 jsb
= (journal_superblock_t
*) buf
;
205 if ((jsb
->s_header
.h_magic
!= (unsigned) ntohl(JFS_MAGIC_NUMBER
)) ||
206 (jsb
->s_header
.h_blocktype
!= (unsigned) ntohl(JFS_SUPERBLOCK_V2
))) {
207 fputs(_("Journal superblock not found!\n"), stderr
);
208 goto no_valid_journal
;
211 /* Find the filesystem UUID */
212 nr_users
= ntohl(jsb
->s_nr_users
);
213 for (i
=0; i
< nr_users
; i
++) {
214 if (memcmp(fs
->super
->s_uuid
,
215 &jsb
->s_users
[i
*16], 16) == 0)
219 fputs(_("Filesystem's UUID not found on journal device.\n"),
221 commit_remove_journal
= 1;
222 goto no_valid_journal
;
225 for (i
=0; i
< nr_users
; i
++)
226 memcpy(&jsb
->s_users
[i
*16], &jsb
->s_users
[(i
+1)*16], 16);
227 jsb
->s_nr_users
= htonl(nr_users
);
229 /* Write back the journal superblock */
230 if ((retval
= io_channel_write_blk(jfs
->io
, 1, -1024, buf
))) {
231 com_err(program_name
, retval
,
232 "while writing journal superblock.");
233 goto no_valid_journal
;
236 commit_remove_journal
= 1;
239 if (commit_remove_journal
== 0) {
240 fputs(_("Journal NOT removed\n"), stderr
);
243 fs
->super
->s_journal_dev
= 0;
244 uuid_clear(fs
->super
->s_journal_uuid
);
245 ext2fs_mark_super_dirty(fs
);
246 fputs(_("Journal removed\n"), stdout
);
250 /* Helper function for remove_journal_inode */
251 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
252 int blockcnt
EXT2FS_ATTR((unused
)),
253 void *private EXT2FS_ATTR((unused
)))
259 ext2fs_unmark_block_bitmap(fs
->block_map
,block
);
260 group
= ext2fs_group_of_blk(fs
, block
);
261 fs
->group_desc
[group
].bg_free_blocks_count
++;
262 ext2fs_group_desc_csum_set(fs
, group
);
263 fs
->super
->s_free_blocks_count
++;
268 * Remove the journal inode from the filesystem
270 static void remove_journal_inode(ext2_filsys fs
)
272 struct ext2_inode inode
;
274 ino_t ino
= fs
->super
->s_journal_inum
;
276 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
278 com_err(program_name
, retval
,
279 _("while reading journal inode"));
282 if (ino
== EXT2_JOURNAL_INO
) {
283 retval
= ext2fs_read_bitmaps(fs
);
285 com_err(program_name
, retval
,
286 _("while reading bitmaps"));
289 retval
= ext2fs_block_iterate(fs
, ino
,
290 BLOCK_FLAG_READ_ONLY
, NULL
,
291 release_blocks_proc
, NULL
);
293 com_err(program_name
, retval
,
294 _("while clearing journal inode"));
297 memset(&inode
, 0, sizeof(inode
));
298 ext2fs_mark_bb_dirty(fs
);
299 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
301 inode
.i_flags
&= ~EXT2_IMMUTABLE_FL
;
302 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
304 com_err(program_name
, retval
,
305 _("while writing journal inode"));
308 fs
->super
->s_journal_inum
= 0;
309 ext2fs_mark_super_dirty(fs
);
313 * Update the default mount options
315 static void update_mntopts(ext2_filsys fs
, char *mntopts
)
317 struct ext2_super_block
*sb
= fs
->super
;
319 if (e2p_edit_mntopts(mntopts
, &sb
->s_default_mount_opts
, ~0)) {
320 fprintf(stderr
, _("Invalid mount option set: %s\n"),
324 ext2fs_mark_super_dirty(fs
);
328 * Update the feature set as provided by the user.
330 static void update_feature_set(ext2_filsys fs
, char *features
)
332 struct ext2_super_block
*sb
= fs
->super
;
333 __u32 old_features
[3];
335 unsigned int mask_err
;
337 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
338 ((&sb->s_feature_compat)[(type)] & (mask)))
339 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
340 !((&sb->s_feature_compat)[(type)] & (mask)))
341 #define FEATURE_CHANGED(type, mask) ((mask) & \
342 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
344 old_features
[E2P_FEATURE_COMPAT
] = sb
->s_feature_compat
;
345 old_features
[E2P_FEATURE_INCOMPAT
] = sb
->s_feature_incompat
;
346 old_features
[E2P_FEATURE_RO_INCOMPAT
] = sb
->s_feature_ro_compat
;
348 if (e2p_edit_feature2(features
, &sb
->s_feature_compat
,
349 ok_features
, clear_ok_features
,
350 &type_err
, &mask_err
)) {
353 _("Invalid filesystem option set: %s\n"),
355 else if (type_err
& E2P_FEATURE_NEGATE_FLAG
)
356 fprintf(stderr
, _("Clearing filesystem feature '%s' "
358 e2p_feature2string(type_err
&
359 E2P_FEATURE_TYPE_MASK
,
362 fprintf(stderr
, _("Setting filesystem feature '%s' "
364 e2p_feature2string(type_err
, mask_err
));
368 if (FEATURE_OFF(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
369 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
370 !(mount_flags
& EXT2_MF_READONLY
)) {
371 fputs(_("The has_journal feature may only be "
372 "cleared when the filesystem is\n"
373 "unmounted or mounted "
374 "read-only.\n"), stderr
);
377 if (sb
->s_feature_incompat
&
378 EXT3_FEATURE_INCOMPAT_RECOVER
) {
379 fputs(_("The needs_recovery flag is set. "
380 "Please run e2fsck before clearing\n"
381 "the has_journal flag.\n"), stderr
);
384 if (sb
->s_journal_inum
) {
385 remove_journal_inode(fs
);
387 if (sb
->s_journal_dev
) {
388 remove_journal_device(fs
);
392 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
394 * If adding a journal flag, let the create journal
395 * code below handle setting the flag and creating the
396 * journal. We supply a default size if necessary.
400 sb
->s_feature_compat
&= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
403 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT2_FEATURE_COMPAT_DIR_INDEX
)) {
404 if (!sb
->s_def_hash_version
)
405 sb
->s_def_hash_version
= EXT2_HASH_HALF_MD4
;
406 if (uuid_is_null((unsigned char *) sb
->s_hash_seed
))
407 uuid_generate((unsigned char *) sb
->s_hash_seed
);
410 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT
, EXT4_FEATURE_INCOMPAT_FLEX_BG
)) {
411 if (ext2fs_check_desc(fs
)) {
412 fputs(_("Clearing the flex_bg flag would "
413 "cause the the filesystem to be\n"
414 "inconsistent.\n"), stderr
);
419 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
420 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
)) {
421 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
422 !(mount_flags
& EXT2_MF_READONLY
)) {
423 fputs(_("The huge_file feature may only be "
424 "cleared when the filesystem is\n"
425 "unmounted or mounted "
426 "read-only.\n"), stderr
);
431 if (sb
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
432 (sb
->s_feature_compat
|| sb
->s_feature_ro_compat
||
433 sb
->s_feature_incompat
))
434 ext2fs_update_dynamic_rev(fs
);
436 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT
,
437 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
) ||
438 FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT
,
439 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) ||
440 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
441 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) ||
442 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT
,
443 EXT2_FEATURE_INCOMPAT_FILETYPE
) ||
444 FEATURE_CHANGED(E2P_FEATURE_COMPAT
,
445 EXT2_FEATURE_COMPAT_RESIZE_INODE
) ||
446 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
447 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
)) {
448 sb
->s_state
&= ~EXT2_VALID_FS
;
449 printf("\n%s\n", _(please_fsck
));
450 if (mount_flags
& EXT2_MF_READONLY
)
451 printf(_("(and reboot afterwards!)\n"));
454 if ((old_features
[E2P_FEATURE_COMPAT
] != sb
->s_feature_compat
) ||
455 (old_features
[E2P_FEATURE_INCOMPAT
] != sb
->s_feature_incompat
) ||
456 (old_features
[E2P_FEATURE_RO_INCOMPAT
] != sb
->s_feature_ro_compat
))
457 ext2fs_mark_super_dirty(fs
);
461 * Add a journal to the filesystem.
463 static void add_journal(ext2_filsys fs
)
465 unsigned long journal_blocks
;
470 if (fs
->super
->s_feature_compat
&
471 EXT3_FEATURE_COMPAT_HAS_JOURNAL
) {
472 fputs(_("The filesystem already has a journal.\n"), stderr
);
475 if (journal_device
) {
476 check_plausibility(journal_device
);
477 check_mount(journal_device
, 0, _("journal"));
478 #ifdef CONFIG_TESTIO_DEBUG
479 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
480 io_ptr
= test_io_manager
;
481 test_io_backing_manager
= unix_io_manager
;
484 io_ptr
= unix_io_manager
;
485 retval
= ext2fs_open(journal_device
, EXT2_FLAG_RW
|
486 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
487 fs
->blocksize
, io_ptr
, &jfs
);
489 com_err(program_name
, retval
,
490 _("\n\twhile trying to open journal on %s\n"),
494 printf(_("Creating journal on device %s: "),
498 retval
= ext2fs_add_journal_device(fs
, jfs
);
501 com_err (program_name
, retval
,
502 _("while adding filesystem to journal on %s"),
506 fputs(_("done\n"), stdout
);
507 } else if (journal_size
) {
508 fputs(_("Creating journal inode: "), stdout
);
510 journal_blocks
= figure_journal_size(journal_size
, fs
);
512 retval
= ext2fs_add_journal_inode(fs
, journal_blocks
,
515 fprintf(stderr
, "\n");
516 com_err(program_name
, retval
,
517 _("\n\twhile trying to create journal file"));
520 fputs(_("done\n"), stdout
);
522 * If the filesystem wasn't mounted, we need to force
523 * the block group descriptors out.
525 if ((mount_flags
& EXT2_MF_MOUNTED
) == 0)
526 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
528 print_check_message(fs
);
533 free(journal_device
);
538 static void parse_e2label_options(int argc
, char ** argv
)
540 if ((argc
< 2) || (argc
> 3)) {
541 fputs(_("Usage: e2label device [newlabel]\n"), stderr
);
544 io_options
= strchr(argv
[1], '?');
547 device_name
= blkid_get_devname(NULL
, argv
[1], NULL
);
549 com_err("e2label", 0, _("Unable to resolve '%s'"),
553 open_flag
= EXT2_FLAG_JOURNAL_DEV_OK
;
555 open_flag
|= EXT2_FLAG_RW
;
562 static time_t parse_time(char *str
)
566 if (strcmp(str
, "now") == 0) {
569 memset(&ts
, 0, sizeof(ts
));
571 strptime(str
, "%Y%m%d%H%M%S", &ts
);
573 sscanf(str
, "%4d%2d%2d%2d%2d%2d", &ts
.tm_year
, &ts
.tm_mon
,
574 &ts
.tm_mday
, &ts
.tm_hour
, &ts
.tm_min
, &ts
.tm_sec
);
577 if (ts
.tm_year
< 0 || ts
.tm_mon
< 0 || ts
.tm_mon
> 11 ||
578 ts
.tm_mday
< 0 || ts
.tm_mday
> 31 || ts
.tm_hour
> 23 ||
579 ts
.tm_min
> 59 || ts
.tm_sec
> 61)
582 if (ts
.tm_mday
== 0) {
583 com_err(program_name
, 0,
584 _("Couldn't parse date/time specifier: %s"),
589 return (mktime(&ts
));
592 static void parse_tune2fs_options(int argc
, char **argv
)
601 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
602 while ((c
= getopt(argc
, argv
, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:")) != EOF
)
606 max_mount_count
= strtol (optarg
, &tmp
, 0);
607 if (*tmp
|| max_mount_count
> 16000) {
608 com_err (program_name
, 0,
609 _("bad mounts count - %s"),
613 if (max_mount_count
== 0)
614 max_mount_count
= -1;
616 open_flag
= EXT2_FLAG_RW
;
619 mount_count
= strtoul (optarg
, &tmp
, 0);
620 if (*tmp
|| mount_count
> 16000) {
621 com_err (program_name
, 0,
622 _("bad mounts count - %s"),
627 open_flag
= EXT2_FLAG_RW
;
630 if (strcmp (optarg
, "continue") == 0)
631 errors
= EXT2_ERRORS_CONTINUE
;
632 else if (strcmp (optarg
, "remount-ro") == 0)
633 errors
= EXT2_ERRORS_RO
;
634 else if (strcmp (optarg
, "panic") == 0)
635 errors
= EXT2_ERRORS_PANIC
;
637 com_err (program_name
, 0,
638 _("bad error behavior - %s"),
643 open_flag
= EXT2_FLAG_RW
;
646 extended_cmd
= optarg
;
647 open_flag
|= EXT2_FLAG_RW
;
649 case 'f': /* Force */
653 resgid
= strtoul (optarg
, &tmp
, 0);
655 gr
= getgrnam (optarg
);
664 com_err (program_name
, 0,
665 _("bad gid/group name - %s"),
670 open_flag
= EXT2_FLAG_RW
;
673 interval
= strtoul (optarg
, &tmp
, 0);
686 case 'M': /* months! */
687 interval
*= 86400 * 30;
691 case 'W': /* weeks */
692 interval
*= 86400 * 7;
697 com_err (program_name
, 0,
698 _("bad interval - %s"), optarg
);
702 open_flag
= EXT2_FLAG_RW
;
707 open_flag
= EXT2_FLAG_RW
;
710 parse_journal_opts(optarg
);
711 open_flag
= EXT2_FLAG_RW
;
719 open_flag
|= EXT2_FLAG_RW
|
720 EXT2_FLAG_JOURNAL_DEV_OK
;
723 reserved_ratio
= strtod(optarg
, &tmp
);
724 if (*tmp
|| reserved_ratio
> 50) {
725 com_err (program_name
, 0,
726 _("bad reserved block ratio - %s"),
731 open_flag
= EXT2_FLAG_RW
;
734 new_last_mounted
= optarg
;
736 open_flag
= EXT2_FLAG_RW
;
740 com_err (program_name
, 0,
741 _("-o may only be specified once"));
744 mntopts_cmd
= optarg
;
745 open_flag
= EXT2_FLAG_RW
;
750 com_err (program_name
, 0,
751 _("-O may only be specified once"));
754 features_cmd
= optarg
;
755 open_flag
= EXT2_FLAG_RW
;
758 reserved_blocks
= strtoul (optarg
, &tmp
, 0);
760 com_err (program_name
, 0,
761 _("bad reserved blocks count - %s"),
766 open_flag
= EXT2_FLAG_RW
;
768 case 's': /* Deprecated */
769 s_flag
= atoi(optarg
);
770 open_flag
= EXT2_FLAG_RW
;
774 last_check_time
= parse_time(optarg
);
775 open_flag
= EXT2_FLAG_RW
;
778 resuid
= strtoul (optarg
, &tmp
, 0);
780 pw
= getpwnam (optarg
);
789 com_err (program_name
, 0,
790 _("bad uid/user name - %s"),
795 open_flag
= EXT2_FLAG_RW
;
800 open_flag
= EXT2_FLAG_RW
|
801 EXT2_FLAG_JOURNAL_DEV_OK
;
804 new_inode_size
= strtoul (optarg
, &tmp
, 0);
806 com_err (program_name
, 0,
807 _("bad inode size - %s"),
811 if (!((new_inode_size
&
812 (new_inode_size
- 1)) == 0)) {
813 com_err (program_name
, 0,
814 _("Inode size must be a "
819 open_flag
= EXT2_FLAG_RW
;
825 if (optind
< argc
- 1 || optind
== argc
)
827 if (!open_flag
&& !l_flag
)
829 io_options
= strchr(argv
[optind
], '?');
832 device_name
= blkid_get_devname(NULL
, argv
[optind
], NULL
);
834 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
840 void do_findfs(int argc
, char **argv
)
845 (strncmp(argv
[1], "LABEL=", 6) && strncmp(argv
[1], "UUID=", 5))) {
846 fprintf(stderr
, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
849 dev
= blkid_get_devname(NULL
, argv
[1], NULL
);
851 com_err("findfs", 0, _("Unable to resolve '%s'"),
859 static void parse_extended_opts(ext2_filsys fs
, const char *opts
)
861 char *buf
, *token
, *next
, *p
, *arg
;
869 _("Couldn't allocate memory to parse options!\n"));
873 for (token
= buf
; token
&& *token
; token
= next
) {
874 p
= strchr(token
, ',');
880 arg
= strchr(token
, '=');
885 if (!strcmp(token
, "test_fs")) {
886 fs
->super
->s_flags
|= EXT2_FLAGS_TEST_FILESYS
;
887 printf("Setting test filesystem flag\n");
888 ext2fs_mark_super_dirty(fs
);
889 } else if (!strcmp(token
, "^test_fs")) {
890 fs
->super
->s_flags
&= ~EXT2_FLAGS_TEST_FILESYS
;
891 printf("Clearing test filesystem flag\n");
892 ext2fs_mark_super_dirty(fs
);
893 } else if (strcmp(token
, "stride") == 0) {
898 stride
= strtoul(arg
, &p
, 0);
899 if (*p
|| (stride
== 0)) {
901 _("Invalid RAID stride: %s\n"),
907 } else if (strcmp(token
, "stripe-width") == 0 ||
908 strcmp(token
, "stripe_width") == 0) {
913 stripe_width
= strtoul(arg
, &p
, 0);
914 if (*p
|| (stripe_width
== 0)) {
916 _("Invalid RAID stripe-width: %s\n"),
921 stripe_width_set
= 1;
922 } else if (strcmp(token
, "hash_alg") == 0 ||
923 strcmp(token
, "hash-alg") == 0) {
928 hash_alg
= e2p_string2hash(arg
);
931 _("Invalid hash algorithm: %s\n"),
936 fs
->super
->s_def_hash_version
= hash_alg
;
937 printf(_("Setting default hash algorithm "
940 ext2fs_mark_super_dirty(fs
);
945 fprintf(stderr
, _("\nBad options specified.\n\n"
946 "Extended options are separated by commas, "
947 "and may take an argument which\n"
948 "\tis set off by an equals ('=') sign.\n\n"
949 "Valid extended options are:\n"
950 "\tstride=<RAID per-disk chunk size in blocks>\n"
951 "\tstripe_width=<RAID stride*data disks in blocks>\n"
952 "\thash_alg=<hash algorithm>\n"
961 static int get_move_bitmap(ext2_filsys fs
, int new_ino_blks_per_grp
,
962 ext2fs_block_bitmap bmap
)
965 blk_t j
, needed_blocks
= 0;
966 blk_t start_blk
, end_blk
;
968 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
970 start_blk
= fs
->group_desc
[i
].bg_inode_table
+
971 fs
->inode_blocks_per_group
;
973 end_blk
= fs
->group_desc
[i
].bg_inode_table
+
974 new_ino_blks_per_grp
;
976 for (j
= start_blk
; j
< end_blk
; j
++) {
978 if (ext2fs_test_block_bitmap(fs
->block_map
, j
)) {
980 * What happens if the block is marked
983 ext2fs_mark_block_bitmap(bmap
, j
);
987 * We are going to use this block for
988 * inode table. So mark them used.
990 ext2fs_mark_block_bitmap(fs
->block_map
, j
);
995 if (needed_blocks
> fs
->super
->s_free_blocks_count
) {
1002 static int move_block(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1007 struct blk_move
*bmv
;
1010 retval
= ext2fs_get_mem(fs
->blocksize
, &buf
);
1014 for (new_blk
= blk
= fs
->super
->s_first_data_block
;
1015 blk
< fs
->super
->s_blocks_count
; blk
++) {
1017 if (!ext2fs_test_block_bitmap(bmap
, blk
))
1020 retval
= ext2fs_new_block(fs
, new_blk
, NULL
, &new_blk
);
1024 /* Mark this block as allocated */
1025 ext2fs_mark_block_bitmap(fs
->block_map
, new_blk
);
1027 /* Add it to block move list */
1028 retval
= ext2fs_get_mem(sizeof(struct blk_move
), &bmv
);
1033 bmv
->new_loc
= new_blk
;
1035 list_add(&(bmv
->list
), &blk_move_list
);
1037 retval
= io_channel_read_blk(fs
->io
, blk
, 1, buf
);
1041 retval
= io_channel_write_blk(fs
->io
, new_blk
, 1, buf
);
1047 ext2fs_free_mem(&buf
);
1051 static blk_t
transalate_block(blk_t blk
)
1053 struct list_head
*entry
;
1054 struct blk_move
*bmv
;
1056 list_for_each(entry
, &blk_move_list
) {
1058 bmv
= list_entry(entry
, struct blk_move
, list
);
1059 if (bmv
->old_loc
== blk
)
1060 return bmv
->new_loc
;
1066 static int process_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
1068 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1069 blk_t ref_block
EXT2FS_ATTR((unused
)),
1070 int ref_offset
EXT2FS_ATTR((unused
)),
1075 ext2fs_block_bitmap bmap
= (ext2fs_block_bitmap
) priv_data
;
1077 if (!ext2fs_test_block_bitmap(bmap
, *block_nr
))
1079 new_blk
= transalate_block(*block_nr
);
1081 *block_nr
= new_blk
;
1083 * This will force the ext2fs_write_inode in the iterator
1085 ret
|= BLOCK_CHANGED
;
1091 static int inode_scan_and_fix(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1093 errcode_t retval
= 0;
1096 char *block_buf
= 0;
1097 struct ext2_inode inode
;
1098 ext2_inode_scan scan
= NULL
;
1100 retval
= ext2fs_get_mem(fs
->blocksize
* 3, &block_buf
);
1104 retval
= ext2fs_open_inode_scan(fs
, 0, &scan
);
1110 retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
1117 if (inode
.i_links_count
== 0)
1118 continue; /* inode not in use */
1121 * If we end up modifying the journal inode
1122 * the sb->s_jnl_blocks will differ. But a
1123 * subsequent e2fsck fixes that.
1124 * Do we need to fix this ??
1127 if (inode
.i_file_acl
&&
1128 ext2fs_test_block_bitmap(bmap
, inode
.i_file_acl
)) {
1129 blk
= transalate_block(inode
.i_file_acl
);
1133 inode
.i_file_acl
= blk
;
1136 * Write the inode to disk so that inode table
1139 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
1144 if (!ext2fs_inode_has_valid_blocks(&inode
))
1147 retval
= ext2fs_block_iterate2(fs
, ino
, 0, block_buf
,
1148 process_block
, bmap
);
1155 ext2fs_free_mem(&block_buf
);
1161 static int expand_inode_table(ext2_filsys fs
, unsigned long new_ino_size
)
1166 int new_ino_blks_per_grp
;
1168 char *old_itable
= NULL
, *new_itable
= NULL
;
1169 char *tmp_old_itable
= NULL
, *tmp_new_itable
= NULL
;
1170 unsigned long old_ino_size
;
1171 int old_itable_size
, new_itable_size
;
1173 old_itable_size
= fs
->inode_blocks_per_group
* fs
->blocksize
;
1174 old_ino_size
= EXT2_INODE_SIZE(fs
->super
);
1176 new_ino_blks_per_grp
= ext2fs_div_ceil(
1177 EXT2_INODES_PER_GROUP(fs
->super
) *
1181 new_itable_size
= new_ino_blks_per_grp
* fs
->blocksize
;
1183 retval
= ext2fs_get_mem(old_itable_size
, &old_itable
);
1187 retval
= ext2fs_get_mem(new_itable_size
, &new_itable
);
1191 tmp_old_itable
= old_itable
;
1192 tmp_new_itable
= new_itable
;
1194 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1196 blk
= fs
->group_desc
[i
].bg_inode_table
;
1197 retval
= io_channel_read_blk(fs
->io
, blk
,
1198 fs
->inode_blocks_per_group
, old_itable
);
1202 for (j
= 0; j
< EXT2_INODES_PER_GROUP(fs
->super
); j
++) {
1204 memcpy(new_itable
, old_itable
, old_ino_size
);
1206 memset(new_itable
+old_ino_size
, 0,
1207 new_ino_size
- old_ino_size
);
1209 new_itable
+= new_ino_size
;
1210 old_itable
+= old_ino_size
;
1213 /* reset the pointer */
1214 old_itable
= tmp_old_itable
;
1215 new_itable
= tmp_new_itable
;
1217 retval
= io_channel_write_blk(fs
->io
, blk
,
1218 new_ino_blks_per_grp
, new_itable
);
1223 /* Update the meta data */
1224 fs
->inode_blocks_per_group
= new_ino_blks_per_grp
;
1225 fs
->super
->s_inode_size
= new_ino_size
;
1229 ext2fs_free_mem(&old_itable
);
1232 ext2fs_free_mem(&new_itable
);
1237 static errcode_t
ext2fs_calculate_summary_stats(ext2_filsys fs
)
1241 unsigned int group
= 0;
1242 unsigned int count
= 0;
1247 * First calculate the block statistics
1249 for (blk
= fs
->super
->s_first_data_block
;
1250 blk
< fs
->super
->s_blocks_count
; blk
++) {
1251 if (!ext2fs_fast_test_block_bitmap(fs
->block_map
, blk
)) {
1256 if ((count
== fs
->super
->s_blocks_per_group
) ||
1257 (blk
== fs
->super
->s_blocks_count
-1)) {
1258 fs
->group_desc
[group
++].bg_free_blocks_count
=
1264 fs
->super
->s_free_blocks_count
= total_free
;
1267 * Next, calculate the inode statistics
1274 /* Protect loop from wrap-around if s_inodes_count maxed */
1275 for (ino
= 1; ino
<= fs
->super
->s_inodes_count
&& ino
> 0; ino
++) {
1276 if (!ext2fs_fast_test_inode_bitmap(fs
->inode_map
, ino
)) {
1281 if ((count
== fs
->super
->s_inodes_per_group
) ||
1282 (ino
== fs
->super
->s_inodes_count
)) {
1283 fs
->group_desc
[group
++].bg_free_inodes_count
=
1289 fs
->super
->s_free_inodes_count
= total_free
;
1290 ext2fs_mark_super_dirty(fs
);
1294 #define list_for_each_safe(pos, pnext, head) \
1295 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1296 pos = pnext, pnext = pos->next)
1298 static void free_blk_move_list(void)
1300 struct list_head
*entry
, *tmp
;
1301 struct blk_move
*bmv
;
1303 list_for_each_safe(entry
, tmp
, &blk_move_list
) {
1305 bmv
= list_entry(entry
, struct blk_move
, list
);
1307 ext2fs_free_mem(&bmv
);
1313 static int resize_inode(ext2_filsys fs
, unsigned long new_size
)
1316 int new_ino_blks_per_grp
;
1317 ext2fs_block_bitmap bmap
;
1319 ext2fs_read_inode_bitmap(fs
);
1320 ext2fs_read_block_bitmap(fs
);
1321 INIT_LIST_HEAD(&blk_move_list
);
1324 new_ino_blks_per_grp
= ext2fs_div_ceil(
1325 EXT2_INODES_PER_GROUP(fs
->super
)*
1329 /* We may change the file system.
1330 * Mark the file system as invalid so that
1331 * the user is prompted to run fsck.
1333 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
1335 retval
= ext2fs_allocate_block_bitmap(fs
, _("blocks to be moved"),
1340 retval
= get_move_bitmap(fs
, new_ino_blks_per_grp
, bmap
);
1344 retval
= move_block(fs
, bmap
);
1348 retval
= inode_scan_and_fix(fs
, bmap
);
1352 retval
= expand_inode_table(fs
, new_size
);
1356 ext2fs_calculate_summary_stats(fs
);
1358 fs
->super
->s_state
|= EXT2_VALID_FS
;
1359 /* mark super block and block bitmap as dirty */
1360 ext2fs_mark_super_dirty(fs
);
1361 ext2fs_mark_bb_dirty(fs
);
1364 free_blk_move_list();
1365 ext2fs_free_block_bitmap(bmap
);
1370 static int tune2fs_setup_tdb(const char *name
, io_manager
*io_ptr
)
1372 errcode_t retval
= 0;
1373 const char *tdb_dir
;
1374 char tdb_file
[PATH_MAX
];
1375 char *dev_name
, *tmp_name
;
1379 * Configuration via a conf file would be
1382 profile_get_string(profile
, "scratch_files",
1386 tmp_name
= strdup(name
);
1387 dev_name
= basename(tmp_name
);
1389 tdb_dir
= getenv("E2FSPROGS_UNDO_DIR");
1391 tdb_dir
="/var/lib/e2fsprogs";
1393 if (!strcmp(tdb_dir
, "none") || (tdb_dir
[0] == 0) ||
1394 access(tdb_dir
, W_OK
))
1397 sprintf(tdb_file
, "%s/tune2fs-%s.e2undo", tdb_dir
, dev_name
);
1399 if (!access(tdb_file
, F_OK
)) {
1400 if (unlink(tdb_file
) < 0) {
1402 com_err(program_name
, retval
,
1403 _("while trying to delete %s"),
1409 set_undo_io_backing_manager(*io_ptr
);
1410 *io_ptr
= undo_io_manager
;
1411 set_undo_io_backup_file(tdb_file
);
1412 printf(_("To undo the tune2fs operations please run "
1413 "the command\n e2undo %s %s\n\n"),
1419 int main (int argc
, char ** argv
)
1423 struct ext2_super_block
*sb
;
1424 io_manager io_ptr
, io_ptr_orig
= NULL
;
1427 setlocale(LC_MESSAGES
, "");
1428 setlocale(LC_CTYPE
, "");
1429 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
1430 textdomain(NLS_CAT_NAME
);
1433 program_name
= *argv
;
1434 add_error_table(&et_ext2_error_table
);
1436 if (strcmp(get_progname(argv
[0]), "findfs") == 0)
1437 do_findfs(argc
, argv
);
1438 if (strcmp(get_progname(argv
[0]), "e2label") == 0)
1439 parse_e2label_options(argc
, argv
);
1441 parse_tune2fs_options(argc
, argv
);
1443 #ifdef CONFIG_TESTIO_DEBUG
1444 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1445 io_ptr
= test_io_manager
;
1446 test_io_backing_manager
= unix_io_manager
;
1449 io_ptr
= unix_io_manager
;
1452 retval
= ext2fs_open2(device_name
, io_options
, open_flag
,
1455 com_err (program_name
, retval
, _("while trying to open %s"),
1458 _("Couldn't find valid filesystem superblock.\n"));
1462 if (I_flag
&& !io_ptr_orig
) {
1464 * Check the inode size is right so we can issue an
1465 * error message and bail before setting up the tdb
1468 if (new_inode_size
== EXT2_INODE_SIZE(fs
->super
)) {
1469 fprintf(stderr
, _("The inode size is already %d\n"),
1473 if (new_inode_size
< EXT2_INODE_SIZE(fs
->super
)) {
1474 fprintf(stderr
, _("Shrinking the inode size is "
1475 "not supported\n"));
1480 * If inode resize is requested use the
1483 io_ptr_orig
= io_ptr
;
1484 retval
= tune2fs_setup_tdb(device_name
, &io_ptr
);
1487 if (io_ptr
!= io_ptr_orig
) {
1494 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
1497 /* For e2label emulation */
1498 printf("%.*s\n", (int) sizeof(sb
->s_volume_name
),
1500 remove_error_table(&et_ext2_error_table
);
1504 retval
= ext2fs_check_if_mounted(device_name
, &mount_flags
);
1506 com_err("ext2fs_check_if_mount", retval
,
1507 _("while determining whether %s is mounted."),
1511 /* Normally we only need to write out the superblock */
1512 fs
->flags
|= EXT2_FLAG_SUPER_ONLY
;
1515 sb
->s_max_mnt_count
= max_mount_count
;
1516 ext2fs_mark_super_dirty(fs
);
1517 printf (_("Setting maximal mount count to %d\n"),
1521 sb
->s_mnt_count
= mount_count
;
1522 ext2fs_mark_super_dirty(fs
);
1523 printf (_("Setting current mount count to %d\n"), mount_count
);
1526 sb
->s_errors
= errors
;
1527 ext2fs_mark_super_dirty(fs
);
1528 printf (_("Setting error behavior to %d\n"), errors
);
1531 sb
->s_def_resgid
= resgid
;
1532 ext2fs_mark_super_dirty(fs
);
1533 printf (_("Setting reserved blocks gid to %lu\n"), resgid
);
1536 sb
->s_checkinterval
= interval
;
1537 ext2fs_mark_super_dirty(fs
);
1538 printf (_("Setting interval between checks to %lu seconds\n"), interval
);
1541 sb
->s_r_blocks_count
= (unsigned int) (reserved_ratio
*
1542 sb
->s_blocks_count
/ 100.0);
1543 ext2fs_mark_super_dirty(fs
);
1544 printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
1545 reserved_ratio
, sb
->s_r_blocks_count
);
1548 if (reserved_blocks
>= sb
->s_blocks_count
/2) {
1549 com_err (program_name
, 0,
1550 _("reserved blocks count is too big (%lu)"),
1554 sb
->s_r_blocks_count
= reserved_blocks
;
1555 ext2fs_mark_super_dirty(fs
);
1556 printf (_("Setting reserved blocks count to %lu\n"),
1560 if (sb
->s_feature_ro_compat
&
1561 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
)
1562 fputs(_("\nThe filesystem already has sparse "
1563 "superblocks.\n"), stderr
);
1565 sb
->s_feature_ro_compat
|=
1566 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
;
1567 sb
->s_state
&= ~EXT2_VALID_FS
;
1568 ext2fs_mark_super_dirty(fs
);
1569 printf(_("\nSparse superblock flag set. %s"),
1574 fputs(_("\nClearing the sparse superflag not supported.\n"),
1579 sb
->s_lastcheck
= last_check_time
;
1580 ext2fs_mark_super_dirty(fs
);
1581 printf(_("Setting time filesystem last checked to %s\n"),
1582 ctime(&last_check_time
));
1585 sb
->s_def_resuid
= resuid
;
1586 ext2fs_mark_super_dirty(fs
);
1587 printf (_("Setting reserved blocks uid to %lu\n"), resuid
);
1590 if (strlen(new_label
) > sizeof(sb
->s_volume_name
))
1591 fputs(_("Warning: label too long, truncating.\n"),
1593 memset(sb
->s_volume_name
, 0, sizeof(sb
->s_volume_name
));
1594 strncpy(sb
->s_volume_name
, new_label
,
1595 sizeof(sb
->s_volume_name
));
1596 ext2fs_mark_super_dirty(fs
);
1599 memset(sb
->s_last_mounted
, 0, sizeof(sb
->s_last_mounted
));
1600 strncpy(sb
->s_last_mounted
, new_last_mounted
,
1601 sizeof(sb
->s_last_mounted
));
1602 ext2fs_mark_super_dirty(fs
);
1605 update_mntopts(fs
, mntopts_cmd
);
1607 update_feature_set(fs
, features_cmd
);
1609 parse_extended_opts(fs
, extended_cmd
);
1610 if (journal_size
|| journal_device
)
1617 if (sb
->s_feature_ro_compat
&
1618 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) {
1620 * Determine if the block group checksums are
1621 * correct so we know whether or not to set
1624 for (i
= 0; i
< fs
->group_desc_count
; i
++)
1625 if (!ext2fs_group_desc_csum_verify(fs
, i
))
1627 if (i
>= fs
->group_desc_count
)
1630 if ((strcasecmp(new_UUID
, "null") == 0) ||
1631 (strcasecmp(new_UUID
, "clear") == 0)) {
1632 uuid_clear(sb
->s_uuid
);
1633 } else if (strcasecmp(new_UUID
, "time") == 0) {
1634 uuid_generate_time(sb
->s_uuid
);
1635 } else if (strcasecmp(new_UUID
, "random") == 0) {
1636 uuid_generate(sb
->s_uuid
);
1637 } else if (uuid_parse(new_UUID
, sb
->s_uuid
)) {
1638 com_err(program_name
, 0, _("Invalid UUID format\n"));
1642 for (i
= 0; i
< fs
->group_desc_count
; i
++)
1643 ext2fs_group_desc_csum_set(fs
, i
);
1644 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
1646 ext2fs_mark_super_dirty(fs
);
1649 if (mount_flags
& EXT2_MF_MOUNTED
) {
1650 fputs(_("The inode size may only be "
1651 "changed when the filesystem is "
1652 "unmounted.\n"), stderr
);
1656 * We want to update group descriptor also
1657 * with the new free inode count
1659 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
1660 if (resize_inode(fs
, new_inode_size
)) {
1661 fputs(_("Error in resizing the inode size.\n"
1662 "Run e2undo to undo the "
1663 "file system changes. \n"), stderr
);
1665 printf (_("Setting inode size %lu\n"),
1673 sb
->s_raid_stride
= stride
;
1674 ext2fs_mark_super_dirty(fs
);
1675 printf(_("Setting stride size to %d\n"), stride
);
1677 if (stripe_width_set
) {
1678 sb
->s_raid_stripe_width
= stripe_width
;
1679 ext2fs_mark_super_dirty(fs
);
1680 printf(_("Setting stripe width to %d\n"), stripe_width
);
1682 remove_error_table(&et_ext2_error_table
);
1683 return (ext2fs_close (fs
) ? 1 : 0);