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() */
44 #include <sys/types.h>
48 #include "ext2fs/ext2_fs.h"
49 #include "ext2fs/ext2fs.h"
50 #include "et/com_err.h"
51 #include "uuid/uuid.h"
55 #include "blkid/blkid.h"
57 #include "../version.h"
58 #include "nls-enable.h"
60 const char * program_name
= "tune2fs";
62 char * new_label
, *new_last_mounted
, *new_UUID
;
64 static int c_flag
, C_flag
, e_flag
, f_flag
, g_flag
, i_flag
, l_flag
, L_flag
;
65 static int m_flag
, M_flag
, r_flag
, s_flag
= -1, u_flag
, U_flag
, T_flag
;
67 static time_t last_check_time
;
68 static int print_label
;
69 static int max_mount_count
, mount_count
, mount_flags
;
70 static unsigned long interval
, reserved_blocks
;
71 static double reserved_ratio
;
72 static unsigned long resgid
, resuid
;
73 static unsigned short errors
;
75 static char *features_cmd
;
76 static char *mntopts_cmd
;
77 static int stride
, stripe_width
;
78 static int stride_set
, stripe_width_set
;
79 static char *extended_cmd
;
80 static unsigned long new_inode_size
;
82 int journal_size
, journal_flags
;
85 static struct list_head blk_move_list
;
88 struct list_head list
;
94 static const char *please_fsck
= N_("Please run e2fsck on the filesystem.\n");
96 void do_findfs(int argc
, char **argv
);
98 static void usage(void)
101 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
103 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
104 "\t[-m reserved_blocks_percent] "
105 "[-o [^]mount_options[,...]] \n"
106 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
107 "[-L volume_label]\n"
108 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
109 "\t[-E extended-option[,...]] [-T last_check_time] "
110 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name
);
114 static __u32 ok_features
[3] = {
116 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
117 EXT2_FEATURE_COMPAT_DIR_INDEX
,
119 EXT2_FEATURE_INCOMPAT_FILETYPE
|
120 EXT3_FEATURE_INCOMPAT_EXTENTS
|
121 EXT4_FEATURE_INCOMPAT_FLEX_BG
,
123 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
124 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
125 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
126 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
127 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
|
128 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
131 static __u32 clear_ok_features
[3] = {
133 EXT3_FEATURE_COMPAT_HAS_JOURNAL
|
134 EXT2_FEATURE_COMPAT_RESIZE_INODE
|
135 EXT2_FEATURE_COMPAT_DIR_INDEX
,
137 EXT2_FEATURE_INCOMPAT_FILETYPE
|
138 EXT4_FEATURE_INCOMPAT_FLEX_BG
,
140 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
|
141 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
|
142 EXT4_FEATURE_RO_COMPAT_DIR_NLINK
|
143 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE
|
144 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
148 * Remove an external journal from the filesystem
150 static void remove_journal_device(ext2_filsys fs
)
155 journal_superblock_t
*jsb
;
158 int commit_remove_journal
= 0;
162 commit_remove_journal
= 1; /* force removal even if error */
164 uuid_unparse(fs
->super
->s_journal_uuid
, buf
);
165 journal_path
= blkid_get_devname(NULL
, "UUID", buf
);
169 ext2fs_find_block_device(fs
->super
->s_journal_dev
);
174 #ifdef CONFIG_TESTIO_DEBUG
175 io_ptr
= test_io_manager
;
176 test_io_backing_manager
= unix_io_manager
;
178 io_ptr
= unix_io_manager
;
180 retval
= ext2fs_open(journal_path
, EXT2_FLAG_RW
|
181 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
182 fs
->blocksize
, io_ptr
, &jfs
);
184 com_err(program_name
, retval
,
185 _("while trying to open external journal"));
186 goto no_valid_journal
;
188 if (!(jfs
->super
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)) {
189 fprintf(stderr
, _("%s is not a journal device.\n"),
191 goto no_valid_journal
;
194 /* Get the journal superblock */
195 if ((retval
= io_channel_read_blk(jfs
->io
, 1, -1024, buf
))) {
196 com_err(program_name
, retval
,
197 _("while reading journal superblock"));
198 goto no_valid_journal
;
201 jsb
= (journal_superblock_t
*) buf
;
202 if ((jsb
->s_header
.h_magic
!= (unsigned) ntohl(JFS_MAGIC_NUMBER
)) ||
203 (jsb
->s_header
.h_blocktype
!= (unsigned) ntohl(JFS_SUPERBLOCK_V2
))) {
204 fputs(_("Journal superblock not found!\n"), stderr
);
205 goto no_valid_journal
;
208 /* Find the filesystem UUID */
209 nr_users
= ntohl(jsb
->s_nr_users
);
210 for (i
=0; i
< nr_users
; i
++) {
211 if (memcmp(fs
->super
->s_uuid
,
212 &jsb
->s_users
[i
*16], 16) == 0)
216 fputs(_("Filesystem's UUID not found on journal device.\n"),
218 commit_remove_journal
= 1;
219 goto no_valid_journal
;
222 for (i
=0; i
< nr_users
; i
++)
223 memcpy(&jsb
->s_users
[i
*16], &jsb
->s_users
[(i
+1)*16], 16);
224 jsb
->s_nr_users
= htonl(nr_users
);
226 /* Write back the journal superblock */
227 if ((retval
= io_channel_write_blk(jfs
->io
, 1, -1024, buf
))) {
228 com_err(program_name
, retval
,
229 "while writing journal superblock.");
230 goto no_valid_journal
;
233 commit_remove_journal
= 1;
236 if (commit_remove_journal
== 0) {
237 fputs(_("Journal NOT removed\n"), stderr
);
240 fs
->super
->s_journal_dev
= 0;
241 uuid_clear(fs
->super
->s_journal_uuid
);
242 ext2fs_mark_super_dirty(fs
);
243 fputs(_("Journal removed\n"), stdout
);
247 /* Helper function for remove_journal_inode */
248 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
249 int blockcnt
EXT2FS_ATTR((unused
)),
250 void *private EXT2FS_ATTR((unused
)))
256 ext2fs_unmark_block_bitmap(fs
->block_map
,block
);
257 group
= ext2fs_group_of_blk(fs
, block
);
258 fs
->group_desc
[group
].bg_free_blocks_count
++;
259 ext2fs_group_desc_csum_set(fs
, group
);
260 fs
->super
->s_free_blocks_count
++;
265 * Remove the journal inode from the filesystem
267 static void remove_journal_inode(ext2_filsys fs
)
269 struct ext2_inode inode
;
271 ino_t ino
= fs
->super
->s_journal_inum
;
273 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
275 com_err(program_name
, retval
,
276 _("while reading journal inode"));
279 if (ino
== EXT2_JOURNAL_INO
) {
280 retval
= ext2fs_read_bitmaps(fs
);
282 com_err(program_name
, retval
,
283 _("while reading bitmaps"));
286 retval
= ext2fs_block_iterate(fs
, ino
,
287 BLOCK_FLAG_READ_ONLY
, NULL
,
288 release_blocks_proc
, NULL
);
290 com_err(program_name
, retval
,
291 _("while clearing journal inode"));
294 memset(&inode
, 0, sizeof(inode
));
295 ext2fs_mark_bb_dirty(fs
);
296 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
298 inode
.i_flags
&= ~EXT2_IMMUTABLE_FL
;
299 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
301 com_err(program_name
, retval
,
302 _("while writing journal inode"));
305 fs
->super
->s_journal_inum
= 0;
306 ext2fs_mark_super_dirty(fs
);
310 * Update the default mount options
312 static void update_mntopts(ext2_filsys fs
, char *mntopts
)
314 struct ext2_super_block
*sb
= fs
->super
;
316 if (e2p_edit_mntopts(mntopts
, &sb
->s_default_mount_opts
, ~0)) {
317 fprintf(stderr
, _("Invalid mount option set: %s\n"),
321 ext2fs_mark_super_dirty(fs
);
325 * Update the feature set as provided by the user.
327 static void update_feature_set(ext2_filsys fs
, char *features
)
329 struct ext2_super_block
*sb
= fs
->super
;
330 __u32 old_features
[3];
332 unsigned int mask_err
;
334 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
335 ((&sb->s_feature_compat)[(type)] & (mask)))
336 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
337 !((&sb->s_feature_compat)[(type)] & (mask)))
338 #define FEATURE_CHANGED(type, mask) ((mask) & \
339 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
341 old_features
[E2P_FEATURE_COMPAT
] = sb
->s_feature_compat
;
342 old_features
[E2P_FEATURE_INCOMPAT
] = sb
->s_feature_incompat
;
343 old_features
[E2P_FEATURE_RO_INCOMPAT
] = sb
->s_feature_ro_compat
;
345 if (e2p_edit_feature2(features
, &sb
->s_feature_compat
,
346 ok_features
, clear_ok_features
,
347 &type_err
, &mask_err
)) {
350 _("Invalid filesystem option set: %s\n"),
352 else if (type_err
& E2P_FEATURE_NEGATE_FLAG
)
353 fprintf(stderr
, _("Clearing filesystem feature '%s' "
355 e2p_feature2string(type_err
&
356 E2P_FEATURE_TYPE_MASK
,
359 fprintf(stderr
, _("Setting filesystem feature '%s' "
361 e2p_feature2string(type_err
, mask_err
));
365 if (FEATURE_OFF(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
366 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
367 !(mount_flags
& EXT2_MF_READONLY
)) {
368 fputs(_("The has_journal feature may only be "
369 "cleared when the filesystem is\n"
370 "unmounted or mounted "
371 "read-only.\n"), stderr
);
374 if (sb
->s_feature_incompat
&
375 EXT3_FEATURE_INCOMPAT_RECOVER
) {
376 fputs(_("The needs_recovery flag is set. "
377 "Please run e2fsck before clearing\n"
378 "the has_journal flag.\n"), stderr
);
381 if (sb
->s_journal_inum
) {
382 remove_journal_inode(fs
);
384 if (sb
->s_journal_dev
) {
385 remove_journal_device(fs
);
389 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
391 * If adding a journal flag, let the create journal
392 * code below handle setting the flag and creating the
393 * journal. We supply a default size if necessary.
397 sb
->s_feature_compat
&= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
400 if (FEATURE_ON(E2P_FEATURE_COMPAT
, EXT2_FEATURE_COMPAT_DIR_INDEX
)) {
401 if (!sb
->s_def_hash_version
)
402 sb
->s_def_hash_version
= EXT2_HASH_HALF_MD4
;
403 if (uuid_is_null((unsigned char *) sb
->s_hash_seed
))
404 uuid_generate((unsigned char *) sb
->s_hash_seed
);
407 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT
, EXT4_FEATURE_INCOMPAT_FLEX_BG
)) {
408 if (ext2fs_check_desc(fs
)) {
409 fputs(_("Clearing the flex_bg flag would "
410 "cause the the filesystem to be\n"
411 "inconsistent.\n"), stderr
);
416 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
417 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
)) {
418 if ((mount_flags
& EXT2_MF_MOUNTED
) &&
419 !(mount_flags
& EXT2_MF_READONLY
)) {
420 fputs(_("The huge_file feature may only be "
421 "cleared when the filesystem is\n"
422 "unmounted or mounted "
423 "read-only.\n"), stderr
);
428 if (sb
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
429 (sb
->s_feature_compat
|| sb
->s_feature_ro_compat
||
430 sb
->s_feature_incompat
))
431 ext2fs_update_dynamic_rev(fs
);
433 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT
,
434 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
) ||
435 FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT
,
436 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) ||
437 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
438 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) ||
439 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT
,
440 EXT2_FEATURE_INCOMPAT_FILETYPE
) ||
441 FEATURE_CHANGED(E2P_FEATURE_COMPAT
,
442 EXT2_FEATURE_COMPAT_RESIZE_INODE
) ||
443 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT
,
444 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
)) {
445 sb
->s_state
&= ~EXT2_VALID_FS
;
446 printf("\n%s\n", _(please_fsck
));
447 if (mount_flags
& EXT2_MF_READONLY
)
448 printf(_("(and reboot afterwards!)\n"));
451 if ((old_features
[E2P_FEATURE_COMPAT
] != sb
->s_feature_compat
) ||
452 (old_features
[E2P_FEATURE_INCOMPAT
] != sb
->s_feature_incompat
) ||
453 (old_features
[E2P_FEATURE_RO_INCOMPAT
] != sb
->s_feature_ro_compat
))
454 ext2fs_mark_super_dirty(fs
);
458 * Add a journal to the filesystem.
460 static void add_journal(ext2_filsys fs
)
462 unsigned long journal_blocks
;
467 if (fs
->super
->s_feature_compat
&
468 EXT3_FEATURE_COMPAT_HAS_JOURNAL
) {
469 fputs(_("The filesystem already has a journal.\n"), stderr
);
472 if (journal_device
) {
473 check_plausibility(journal_device
);
474 check_mount(journal_device
, 0, _("journal"));
475 #ifdef CONFIG_TESTIO_DEBUG
476 io_ptr
= test_io_manager
;
477 test_io_backing_manager
= unix_io_manager
;
479 io_ptr
= unix_io_manager
;
481 retval
= ext2fs_open(journal_device
, EXT2_FLAG_RW
|
482 EXT2_FLAG_JOURNAL_DEV_OK
, 0,
483 fs
->blocksize
, io_ptr
, &jfs
);
485 com_err(program_name
, retval
,
486 _("\n\twhile trying to open journal on %s\n"),
490 printf(_("Creating journal on device %s: "),
494 retval
= ext2fs_add_journal_device(fs
, jfs
);
497 com_err (program_name
, retval
,
498 _("while adding filesystem to journal on %s"),
502 fputs(_("done\n"), stdout
);
503 } else if (journal_size
) {
504 fputs(_("Creating journal inode: "), stdout
);
506 journal_blocks
= figure_journal_size(journal_size
, fs
);
508 retval
= ext2fs_add_journal_inode(fs
, journal_blocks
,
511 fprintf(stderr
, "\n");
512 com_err(program_name
, retval
,
513 _("\n\twhile trying to create journal file"));
516 fputs(_("done\n"), stdout
);
518 * If the filesystem wasn't mounted, we need to force
519 * the block group descriptors out.
521 if ((mount_flags
& EXT2_MF_MOUNTED
) == 0)
522 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
524 print_check_message(fs
);
529 free(journal_device
);
534 static void parse_e2label_options(int argc
, char ** argv
)
536 if ((argc
< 2) || (argc
> 3)) {
537 fputs(_("Usage: e2label device [newlabel]\n"), stderr
);
540 io_options
= strchr(argv
[1], '?');
543 device_name
= blkid_get_devname(NULL
, argv
[1], NULL
);
545 com_err("e2label", 0, _("Unable to resolve '%s'"),
549 open_flag
= EXT2_FLAG_JOURNAL_DEV_OK
;
551 open_flag
|= EXT2_FLAG_RW
;
558 static time_t parse_time(char *str
)
562 if (strcmp(str
, "now") == 0) {
565 memset(&ts
, 0, sizeof(ts
));
567 strptime(str
, "%Y%m%d%H%M%S", &ts
);
569 sscanf(str
, "%4d%2d%2d%2d%2d%2d", &ts
.tm_year
, &ts
.tm_mon
,
570 &ts
.tm_mday
, &ts
.tm_hour
, &ts
.tm_min
, &ts
.tm_sec
);
573 if (ts
.tm_year
< 0 || ts
.tm_mon
< 0 || ts
.tm_mon
> 11 ||
574 ts
.tm_mday
< 0 || ts
.tm_mday
> 31 || ts
.tm_hour
> 23 ||
575 ts
.tm_min
> 59 || ts
.tm_sec
> 61)
578 if (ts
.tm_mday
== 0) {
579 com_err(program_name
, 0,
580 _("Couldn't parse date/time specifier: %s"),
585 return (mktime(&ts
));
588 static void parse_tune2fs_options(int argc
, char **argv
)
597 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
598 while ((c
= getopt(argc
, argv
, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:")) != EOF
)
602 max_mount_count
= strtol (optarg
, &tmp
, 0);
603 if (*tmp
|| max_mount_count
> 16000) {
604 com_err (program_name
, 0,
605 _("bad mounts count - %s"),
609 if (max_mount_count
== 0)
610 max_mount_count
= -1;
612 open_flag
= EXT2_FLAG_RW
;
615 mount_count
= strtoul (optarg
, &tmp
, 0);
616 if (*tmp
|| mount_count
> 16000) {
617 com_err (program_name
, 0,
618 _("bad mounts count - %s"),
623 open_flag
= EXT2_FLAG_RW
;
626 if (strcmp (optarg
, "continue") == 0)
627 errors
= EXT2_ERRORS_CONTINUE
;
628 else if (strcmp (optarg
, "remount-ro") == 0)
629 errors
= EXT2_ERRORS_RO
;
630 else if (strcmp (optarg
, "panic") == 0)
631 errors
= EXT2_ERRORS_PANIC
;
633 com_err (program_name
, 0,
634 _("bad error behavior - %s"),
639 open_flag
= EXT2_FLAG_RW
;
642 extended_cmd
= optarg
;
643 open_flag
|= EXT2_FLAG_RW
;
645 case 'f': /* Force */
649 resgid
= strtoul (optarg
, &tmp
, 0);
651 gr
= getgrnam (optarg
);
660 com_err (program_name
, 0,
661 _("bad gid/group name - %s"),
666 open_flag
= EXT2_FLAG_RW
;
669 interval
= strtoul (optarg
, &tmp
, 0);
682 case 'M': /* months! */
683 interval
*= 86400 * 30;
687 case 'W': /* weeks */
688 interval
*= 86400 * 7;
693 com_err (program_name
, 0,
694 _("bad interval - %s"), optarg
);
698 open_flag
= EXT2_FLAG_RW
;
703 open_flag
= EXT2_FLAG_RW
;
706 parse_journal_opts(optarg
);
707 open_flag
= EXT2_FLAG_RW
;
715 open_flag
|= EXT2_FLAG_RW
|
716 EXT2_FLAG_JOURNAL_DEV_OK
;
719 reserved_ratio
= strtod(optarg
, &tmp
);
720 if (*tmp
|| reserved_ratio
> 50) {
721 com_err (program_name
, 0,
722 _("bad reserved block ratio - %s"),
727 open_flag
= EXT2_FLAG_RW
;
730 new_last_mounted
= optarg
;
732 open_flag
= EXT2_FLAG_RW
;
736 com_err (program_name
, 0,
737 _("-o may only be specified once"));
740 mntopts_cmd
= optarg
;
741 open_flag
= EXT2_FLAG_RW
;
746 com_err (program_name
, 0,
747 _("-O may only be specified once"));
750 features_cmd
= optarg
;
751 open_flag
= EXT2_FLAG_RW
;
754 reserved_blocks
= strtoul (optarg
, &tmp
, 0);
756 com_err (program_name
, 0,
757 _("bad reserved blocks count - %s"),
762 open_flag
= EXT2_FLAG_RW
;
764 case 's': /* Deprecated */
765 s_flag
= atoi(optarg
);
766 open_flag
= EXT2_FLAG_RW
;
770 last_check_time
= parse_time(optarg
);
771 open_flag
= EXT2_FLAG_RW
;
774 resuid
= strtoul (optarg
, &tmp
, 0);
776 pw
= getpwnam (optarg
);
785 com_err (program_name
, 0,
786 _("bad uid/user name - %s"),
791 open_flag
= EXT2_FLAG_RW
;
796 open_flag
= EXT2_FLAG_RW
|
797 EXT2_FLAG_JOURNAL_DEV_OK
;
800 new_inode_size
= strtoul (optarg
, &tmp
, 0);
802 com_err (program_name
, 0,
803 _("bad inode size - %s"),
807 if (!((new_inode_size
&
808 (new_inode_size
- 1)) == 0)) {
809 com_err (program_name
, 0,
810 _("Inode size must be a "
815 open_flag
= EXT2_FLAG_RW
;
821 if (optind
< argc
- 1 || optind
== argc
)
823 if (!open_flag
&& !l_flag
)
825 io_options
= strchr(argv
[optind
], '?');
828 device_name
= blkid_get_devname(NULL
, argv
[optind
], NULL
);
830 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
836 void do_findfs(int argc
, char **argv
)
841 (strncmp(argv
[1], "LABEL=", 6) && strncmp(argv
[1], "UUID=", 5))) {
842 fprintf(stderr
, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
845 dev
= blkid_get_devname(NULL
, argv
[1], NULL
);
847 com_err("findfs", 0, _("Unable to resolve '%s'"),
855 static void parse_extended_opts(ext2_filsys fs
, const char *opts
)
857 char *buf
, *token
, *next
, *p
, *arg
;
865 _("Couldn't allocate memory to parse options!\n"));
869 for (token
= buf
; token
&& *token
; token
= next
) {
870 p
= strchr(token
, ',');
876 arg
= strchr(token
, '=');
881 if (!strcmp(token
, "test_fs")) {
882 fs
->super
->s_flags
|= EXT2_FLAGS_TEST_FILESYS
;
883 printf("Setting test filesystem flag\n");
884 ext2fs_mark_super_dirty(fs
);
885 } else if (!strcmp(token
, "^test_fs")) {
886 fs
->super
->s_flags
&= ~EXT2_FLAGS_TEST_FILESYS
;
887 printf("Clearing test filesystem flag\n");
888 ext2fs_mark_super_dirty(fs
);
889 } else if (strcmp(token
, "stride") == 0) {
894 stride
= strtoul(arg
, &p
, 0);
895 if (*p
|| (stride
== 0)) {
897 _("Invalid RAID stride: %s\n"),
903 } else if (strcmp(token
, "stripe-width") == 0 ||
904 strcmp(token
, "stripe_width") == 0) {
909 stripe_width
= strtoul(arg
, &p
, 0);
910 if (*p
|| (stripe_width
== 0)) {
912 _("Invalid RAID stripe-width: %s\n"),
917 stripe_width_set
= 1;
918 } else if (strcmp(token
, "hash_alg") == 0 ||
919 strcmp(token
, "hash-alg") == 0) {
924 hash_alg
= e2p_string2hash(arg
);
927 _("Invalid hash algorithm: %s\n"),
932 fs
->super
->s_def_hash_version
= hash_alg
;
933 printf(_("Setting default hash algorithm "
936 ext2fs_mark_super_dirty(fs
);
941 fprintf(stderr
, _("\nBad options specified.\n\n"
942 "Extended options are separated by commas, "
943 "and may take an argument which\n"
944 "\tis set off by an equals ('=') sign.\n\n"
945 "Valid extended options are:\n"
946 "\tstride=<RAID per-disk chunk size in blocks>\n"
947 "\tstripe_width=<RAID stride*data disks in blocks>\n"
948 "\thash_alg=<hash algorithm>\n"
957 static int get_move_bitmap(ext2_filsys fs
, int new_ino_blks_per_grp
,
958 ext2fs_block_bitmap bmap
)
961 blk_t j
, needed_blocks
= 0;
962 blk_t start_blk
, end_blk
;
964 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
966 start_blk
= fs
->group_desc
[i
].bg_inode_table
+
967 fs
->inode_blocks_per_group
;
969 end_blk
= fs
->group_desc
[i
].bg_inode_table
+
970 new_ino_blks_per_grp
;
972 for (j
= start_blk
; j
< end_blk
; j
++) {
974 if (ext2fs_test_block_bitmap(fs
->block_map
, j
)) {
976 * What happens if the block is marked
979 ext2fs_mark_block_bitmap(bmap
, j
);
983 * We are going to use this block for
984 * inode table. So mark them used.
986 ext2fs_mark_block_bitmap(fs
->block_map
, j
);
991 if (needed_blocks
> fs
->super
->s_free_blocks_count
) {
998 static int move_block(ext2_filsys fs
, ext2fs_block_bitmap bmap
)
1003 struct blk_move
*bmv
;
1006 retval
= ext2fs_get_mem(fs
->blocksize
, &buf
);
1010 for (blk
= fs
->super
->s_first_data_block
;
1011 blk
< fs
->super
->s_blocks_count
; blk
++) {
1013 if (!ext2fs_test_block_bitmap(bmap
, blk
))
1016 retval
= ext2fs_new_block(fs
, blk
, NULL
, &new_blk
);
1020 /* Mark this block as allocated */
1021 ext2fs_mark_block_bitmap(fs
->block_map
, new_blk
);
1023 /* Add it to block move list */
1024 retval
= ext2fs_get_mem(sizeof(struct blk_move
), &bmv
);
1029 bmv
->new_loc
= new_blk
;
1031 list_add(&(bmv
->list
), &blk_move_list
);
1033 retval
= io_channel_read_blk(fs
->io
, blk
, 1, buf
);
1037 retval
= io_channel_write_blk(fs
->io
, new_blk
, 1, buf
);
1043 ext2fs_free_mem(&buf
);
1047 static blk_t
transalate_block(blk_t blk
)
1049 struct list_head
*entry
;
1050 struct blk_move
*bmv
;
1052 list_for_each(entry
, &blk_move_list
) {
1054 bmv
= list_entry(entry
, struct blk_move
, list
);
1055 if (bmv
->old_loc
== blk
)
1056 return bmv
->new_loc
;
1062 static int process_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
1064 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1065 blk_t ref_block
EXT2FS_ATTR((unused
)),
1066 int ref_offset
EXT2FS_ATTR((unused
)),
1067 void *priv_data
EXT2FS_ATTR((unused
)))
1073 new_blk
= transalate_block(*block_nr
);
1075 *block_nr
= new_blk
;
1077 * This will force the ext2fs_write_inode in the iterator
1079 ret
|= BLOCK_CHANGED
;
1085 static int inode_scan_and_fix(ext2_filsys fs
)
1087 errcode_t retval
= 0;
1090 char *block_buf
= 0;
1091 struct ext2_inode inode
;
1092 ext2_inode_scan scan
= NULL
;
1094 retval
= ext2fs_get_mem(fs
->blocksize
* 3, &block_buf
);
1098 retval
= ext2fs_open_inode_scan(fs
, 0, &scan
);
1104 retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
1111 if (inode
.i_links_count
== 0)
1112 continue; /* inode not in use */
1115 * If we end up modifying the journal inode
1116 * the sb->s_jnl_blocks will differ. But a
1117 * subsequent e2fsck fixes that.
1118 * Do we need to fix this ??
1121 if (inode
.i_file_acl
) {
1123 blk
= transalate_block(inode
.i_file_acl
);
1127 inode
.i_file_acl
= blk
;
1130 * Write the inode to disk so that inode table
1133 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
1138 if (!ext2fs_inode_has_valid_blocks(&inode
))
1141 retval
= ext2fs_block_iterate2(fs
, ino
, 0,
1142 block_buf
, process_block
,
1150 ext2fs_free_mem(&block_buf
);
1156 static int expand_inode_table(ext2_filsys fs
, unsigned long new_ino_size
)
1161 int new_ino_blks_per_grp
;
1163 char *old_itable
= NULL
, *new_itable
= NULL
;
1164 char *tmp_old_itable
= NULL
, *tmp_new_itable
= NULL
;
1165 unsigned long old_ino_size
;
1166 int old_itable_size
, new_itable_size
;
1168 old_itable_size
= fs
->inode_blocks_per_group
* fs
->blocksize
;
1169 old_ino_size
= EXT2_INODE_SIZE(fs
->super
);
1171 new_ino_blks_per_grp
= ext2fs_div_ceil(
1172 EXT2_INODES_PER_GROUP(fs
->super
) *
1176 new_itable_size
= new_ino_blks_per_grp
* fs
->blocksize
;
1178 retval
= ext2fs_get_mem(old_itable_size
, &old_itable
);
1182 retval
= ext2fs_get_mem(new_itable_size
, &new_itable
);
1186 tmp_old_itable
= old_itable
;
1187 tmp_new_itable
= new_itable
;
1189 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1191 blk
= fs
->group_desc
[i
].bg_inode_table
;
1192 retval
= io_channel_read_blk(fs
->io
, blk
,
1193 fs
->inode_blocks_per_group
, old_itable
);
1197 for (j
= 0; j
< EXT2_INODES_PER_GROUP(fs
->super
); j
++) {
1199 memcpy(new_itable
, old_itable
, old_ino_size
);
1201 memset(new_itable
+old_ino_size
, 0,
1202 new_ino_size
- old_ino_size
);
1204 new_itable
+= new_ino_size
;
1205 old_itable
+= old_ino_size
;
1208 /* reset the pointer */
1209 old_itable
= tmp_old_itable
;
1210 new_itable
= tmp_new_itable
;
1212 retval
= io_channel_write_blk(fs
->io
, blk
,
1213 new_ino_blks_per_grp
, new_itable
);
1218 /* Update the meta data */
1219 fs
->inode_blocks_per_group
= new_ino_blks_per_grp
;
1220 fs
->super
->s_inode_size
= new_ino_size
;
1224 ext2fs_free_mem(&old_itable
);
1227 ext2fs_free_mem(&new_itable
);
1232 static errcode_t
ext2fs_calculate_summary_stats(ext2_filsys fs
)
1236 unsigned int group
= 0;
1237 unsigned int count
= 0;
1242 * First calculate the block statistics
1244 for (blk
= fs
->super
->s_first_data_block
;
1245 blk
< fs
->super
->s_blocks_count
; blk
++) {
1246 if (!ext2fs_fast_test_block_bitmap(fs
->block_map
, blk
)) {
1251 if ((count
== fs
->super
->s_blocks_per_group
) ||
1252 (blk
== fs
->super
->s_blocks_count
-1)) {
1253 fs
->group_desc
[group
++].bg_free_blocks_count
=
1259 fs
->super
->s_free_blocks_count
= total_free
;
1262 * Next, calculate the inode statistics
1269 /* Protect loop from wrap-around if s_inodes_count maxed */
1270 for (ino
= 1; ino
<= fs
->super
->s_inodes_count
&& ino
> 0; ino
++) {
1271 if (!ext2fs_fast_test_inode_bitmap(fs
->inode_map
, ino
)) {
1276 if ((count
== fs
->super
->s_inodes_per_group
) ||
1277 (ino
== fs
->super
->s_inodes_count
)) {
1278 fs
->group_desc
[group
++].bg_free_inodes_count
=
1284 fs
->super
->s_free_inodes_count
= total_free
;
1285 ext2fs_mark_super_dirty(fs
);
1289 #define list_for_each_safe(pos, pnext, head) \
1290 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1291 pos = pnext, pnext = pos->next)
1293 static void free_blk_move_list(void)
1295 struct list_head
*entry
, *tmp
;
1296 struct blk_move
*bmv
;
1298 list_for_each_safe(entry
, tmp
, &blk_move_list
) {
1300 bmv
= list_entry(entry
, struct blk_move
, list
);
1302 ext2fs_free_mem(&bmv
);
1308 static int resize_inode(ext2_filsys fs
, unsigned long new_size
)
1311 int new_ino_blks_per_grp
;
1312 ext2fs_block_bitmap bmap
;
1314 ext2fs_read_inode_bitmap(fs
);
1315 ext2fs_read_block_bitmap(fs
);
1316 INIT_LIST_HEAD(&blk_move_list
);
1319 new_ino_blks_per_grp
= ext2fs_div_ceil(
1320 EXT2_INODES_PER_GROUP(fs
->super
)*
1324 /* We may change the file system.
1325 * Mark the file system as invalid so that
1326 * the user is prompted to run fsck.
1328 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
1330 retval
= ext2fs_allocate_block_bitmap(fs
, _("blocks to be moved"),
1335 retval
= get_move_bitmap(fs
, new_ino_blks_per_grp
, bmap
);
1339 retval
= move_block(fs
, bmap
);
1343 retval
= inode_scan_and_fix(fs
);
1347 retval
= expand_inode_table(fs
, new_size
);
1351 ext2fs_calculate_summary_stats(fs
);
1353 fs
->super
->s_state
|= EXT2_VALID_FS
;
1354 /* mark super block and block bitmap as dirty */
1355 ext2fs_mark_super_dirty(fs
);
1356 ext2fs_mark_bb_dirty(fs
);
1359 free_blk_move_list();
1360 ext2fs_free_block_bitmap(bmap
);
1365 static int tune2fs_setup_tdb(const char *name
, io_manager
*io_ptr
)
1367 errcode_t retval
= 0;
1368 const char *tdb_dir
;
1369 char tdb_file
[PATH_MAX
];
1370 char *dev_name
, *tmp_name
;
1374 * Configuration via a conf file would be
1377 profile_get_string(profile
, "scratch_files",
1381 tmp_name
= strdup(name
);
1382 dev_name
= basename(tmp_name
);
1384 tdb_dir
= getenv("E2FSPROGS_UNDO_DIR");
1386 tdb_dir
="/var/lib/e2fsprogs";
1388 if (!strcmp(tdb_dir
, "none") || (tdb_dir
[0] == 0) ||
1389 access(tdb_dir
, W_OK
))
1392 sprintf(tdb_file
, "%s/tune2fs-%s.e2undo", tdb_dir
, dev_name
);
1394 if (!access(tdb_file
, F_OK
)) {
1395 if (unlink(tdb_file
) < 0) {
1397 com_err(program_name
, retval
,
1398 _("while trying to delete %s"),
1404 set_undo_io_backing_manager(*io_ptr
);
1405 *io_ptr
= undo_io_manager
;
1406 set_undo_io_backup_file(tdb_file
);
1407 printf(_("To undo the tune2fs operations please run "
1408 "the command\n e2undo %s %s\n\n"),
1414 int main (int argc
, char ** argv
)
1418 struct ext2_super_block
*sb
;
1419 io_manager io_ptr
, io_ptr_orig
= NULL
;
1422 setlocale(LC_MESSAGES
, "");
1423 setlocale(LC_CTYPE
, "");
1424 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
1425 textdomain(NLS_CAT_NAME
);
1428 program_name
= *argv
;
1429 add_error_table(&et_ext2_error_table
);
1431 if (strcmp(get_progname(argv
[0]), "findfs") == 0)
1432 do_findfs(argc
, argv
);
1433 if (strcmp(get_progname(argv
[0]), "e2label") == 0)
1434 parse_e2label_options(argc
, argv
);
1436 parse_tune2fs_options(argc
, argv
);
1438 #ifdef CONFIG_TESTIO_DEBUG
1439 io_ptr
= test_io_manager
;
1440 test_io_backing_manager
= unix_io_manager
;
1442 io_ptr
= unix_io_manager
;
1446 retval
= ext2fs_open2(device_name
, io_options
, open_flag
,
1449 com_err (program_name
, retval
, _("while trying to open %s"),
1452 _("Couldn't find valid filesystem superblock.\n"));
1456 if (I_flag
&& !io_ptr_orig
) {
1458 * Check the inode size is right so we can issue an
1459 * error message and bail before setting up the tdb
1462 if (new_inode_size
== EXT2_INODE_SIZE(fs
->super
)) {
1463 fprintf(stderr
, _("The inode size is already %d\n"),
1467 if (new_inode_size
< EXT2_INODE_SIZE(fs
->super
)) {
1468 fprintf(stderr
, _("Shrinking the inode size is "
1469 "not supported\n"));
1474 * If inode resize is requested use the
1477 io_ptr_orig
= io_ptr
;
1478 retval
= tune2fs_setup_tdb(device_name
, &io_ptr
);
1481 if (io_ptr
!= io_ptr_orig
) {
1488 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
1491 /* For e2label emulation */
1492 printf("%.*s\n", (int) sizeof(sb
->s_volume_name
),
1494 remove_error_table(&et_ext2_error_table
);
1498 retval
= ext2fs_check_if_mounted(device_name
, &mount_flags
);
1500 com_err("ext2fs_check_if_mount", retval
,
1501 _("while determining whether %s is mounted."),
1505 /* Normally we only need to write out the superblock */
1506 fs
->flags
|= EXT2_FLAG_SUPER_ONLY
;
1509 sb
->s_max_mnt_count
= max_mount_count
;
1510 ext2fs_mark_super_dirty(fs
);
1511 printf (_("Setting maximal mount count to %d\n"),
1515 sb
->s_mnt_count
= mount_count
;
1516 ext2fs_mark_super_dirty(fs
);
1517 printf (_("Setting current mount count to %d\n"), mount_count
);
1520 sb
->s_errors
= errors
;
1521 ext2fs_mark_super_dirty(fs
);
1522 printf (_("Setting error behavior to %d\n"), errors
);
1525 sb
->s_def_resgid
= resgid
;
1526 ext2fs_mark_super_dirty(fs
);
1527 printf (_("Setting reserved blocks gid to %lu\n"), resgid
);
1530 sb
->s_checkinterval
= interval
;
1531 ext2fs_mark_super_dirty(fs
);
1532 printf (_("Setting interval between checks to %lu seconds\n"), interval
);
1535 sb
->s_r_blocks_count
= (unsigned int) (reserved_ratio
*
1536 sb
->s_blocks_count
/ 100.0);
1537 ext2fs_mark_super_dirty(fs
);
1538 printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
1539 reserved_ratio
, sb
->s_r_blocks_count
);
1542 if (reserved_blocks
>= sb
->s_blocks_count
/2) {
1543 com_err (program_name
, 0,
1544 _("reserved blocks count is too big (%lu)"),
1548 sb
->s_r_blocks_count
= reserved_blocks
;
1549 ext2fs_mark_super_dirty(fs
);
1550 printf (_("Setting reserved blocks count to %lu\n"),
1554 if (sb
->s_feature_ro_compat
&
1555 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
)
1556 fputs(_("\nThe filesystem already has sparse "
1557 "superblocks.\n"), stderr
);
1559 sb
->s_feature_ro_compat
|=
1560 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
;
1561 sb
->s_state
&= ~EXT2_VALID_FS
;
1562 ext2fs_mark_super_dirty(fs
);
1563 printf(_("\nSparse superblock flag set. %s"),
1568 fputs(_("\nClearing the sparse superflag not supported.\n"),
1573 sb
->s_lastcheck
= last_check_time
;
1574 ext2fs_mark_super_dirty(fs
);
1575 printf(_("Setting time filesystem last checked to %s\n"),
1576 ctime(&last_check_time
));
1579 sb
->s_def_resuid
= resuid
;
1580 ext2fs_mark_super_dirty(fs
);
1581 printf (_("Setting reserved blocks uid to %lu\n"), resuid
);
1584 if (strlen(new_label
) > sizeof(sb
->s_volume_name
))
1585 fputs(_("Warning: label too long, truncating.\n"),
1587 memset(sb
->s_volume_name
, 0, sizeof(sb
->s_volume_name
));
1588 strncpy(sb
->s_volume_name
, new_label
,
1589 sizeof(sb
->s_volume_name
));
1590 ext2fs_mark_super_dirty(fs
);
1593 memset(sb
->s_last_mounted
, 0, sizeof(sb
->s_last_mounted
));
1594 strncpy(sb
->s_last_mounted
, new_last_mounted
,
1595 sizeof(sb
->s_last_mounted
));
1596 ext2fs_mark_super_dirty(fs
);
1599 update_mntopts(fs
, mntopts_cmd
);
1601 update_feature_set(fs
, features_cmd
);
1603 parse_extended_opts(fs
, extended_cmd
);
1604 if (journal_size
|| journal_device
)
1608 if ((strcasecmp(new_UUID
, "null") == 0) ||
1609 (strcasecmp(new_UUID
, "clear") == 0)) {
1610 uuid_clear(sb
->s_uuid
);
1611 } else if (strcasecmp(new_UUID
, "time") == 0) {
1612 uuid_generate_time(sb
->s_uuid
);
1613 } else if (strcasecmp(new_UUID
, "random") == 0) {
1614 uuid_generate(sb
->s_uuid
);
1615 } else if (uuid_parse(new_UUID
, sb
->s_uuid
)) {
1616 com_err(program_name
, 0, _("Invalid UUID format\n"));
1619 ext2fs_mark_super_dirty(fs
);
1622 if (mount_flags
& EXT2_MF_MOUNTED
) {
1623 fputs(_("The inode size may only be "
1624 "changed when the filesystem is "
1625 "unmounted.\n"), stderr
);
1629 * We want to update group descriptor also
1630 * with the new free inode count
1632 fs
->flags
&= ~EXT2_FLAG_SUPER_ONLY
;
1633 if (resize_inode(fs
, new_inode_size
)) {
1634 fputs(_("Error in resizing the inode size.\n"
1635 "Run e2undo to undo the "
1636 "file system changes. \n"), stderr
);
1638 printf (_("Setting inode size %lu\n"),
1646 sb
->s_raid_stride
= stride
;
1647 ext2fs_mark_super_dirty(fs
);
1648 printf(_("Setting stride size to %d\n"), stride
);
1650 if (stripe_width_set
) {
1651 sb
->s_raid_stripe_width
= stripe_width
;
1652 ext2fs_mark_super_dirty(fs
);
1653 printf(_("Setting stripe width to %d\n"), stripe_width
);
1655 remove_error_table(&et_ext2_error_table
);
1656 return (ext2fs_close (fs
) ? 1 : 0);