rezize2fs: fix memory leak when fixing up the orphan file inode
[e2fsprogs.git] / misc / tune2fs.c
blob2548a7669ae3c33443106f81fc3217e8c96149f5
1 /*
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.
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the GNU Public
12 * License.
13 * %End-Header%
17 * History:
18 * 93/06/01 - Creation
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 #include "config.h"
30 #include <fcntl.h>
31 #include <grp.h>
32 #ifdef HAVE_GETOPT_H
33 #include <getopt.h>
34 #else
35 extern char *optarg;
36 extern int optind;
37 #endif
38 #include <pwd.h>
39 #include <stdio.h>
40 #ifdef HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43 #ifdef HAVE_STRINGS_H
44 #include <strings.h> /* for strcasecmp() */
45 #else
46 #define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
47 #define _DEFAULT_SOURCE /* since glibc 2.20 _BSD_SOURCE is deprecated */
48 #endif
49 #include <string.h>
50 #include <time.h>
51 #include <unistd.h>
52 #include <sys/types.h>
53 #include <libgen.h>
54 #include <limits.h> /* for PATH_MAX */
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
57 #endif
59 #ifndef PATH_MAX
60 #define PATH_MAX 4096
61 #endif
63 #include "ext2fs/ext2_fs.h"
64 #include "ext2fs/ext2fs.h"
65 #include "ext2fs/kernel-jbd.h"
66 #include "et/com_err.h"
67 #include "support/plausible.h"
68 #include "support/quotaio.h"
69 #include "support/devname.h"
70 #include "uuid/uuid.h"
71 #include "e2p/e2p.h"
72 #include "util.h"
73 #include "blkid/blkid.h"
75 #include "../version.h"
76 #include "support/nls-enable.h"
78 #define QOPT_ENABLE (1)
79 #define QOPT_DISABLE (-1)
81 #ifndef FS_IOC_SETFSLABEL
82 #define FSLABEL_MAX 256
83 #define FS_IOC_SETFSLABEL _IOW(0x94, 50, char[FSLABEL_MAX])
84 #endif
86 #ifndef FS_IOC_GETFSLABEL
87 #define FS_IOC_GETFSLABEL _IOR(0x94, 49, char[FSLABEL_MAX])
88 #endif
90 struct fsuuid {
91 __u32 fsu_len;
92 __u32 fsu_flags;
93 __u8 fsu_uuid[];
96 #ifndef EXT4_IOC_GETFSUUID
97 #define EXT4_IOC_GETFSUUID _IOR('f', 44, struct fsuuid)
98 #endif
100 #ifndef EXT4_IOC_SETFSUUID
101 #define EXT4_IOC_SETFSUUID _IOW('f', 44, struct fsuuid)
102 #endif
104 extern int ask_yn(const char *string, int def);
106 const char *program_name = "tune2fs";
107 char *device_name;
108 char *new_label, *new_last_mounted, *requested_uuid;
109 char *io_options;
110 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
111 static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
112 static int I_flag;
113 static int clear_mmp;
114 static time_t last_check_time;
115 static int print_label;
116 static int max_mount_count, mount_count, mount_flags;
117 static unsigned long interval;
118 static blk64_t reserved_blocks;
119 static double reserved_ratio;
120 static unsigned long resgid, resuid;
121 static unsigned short errors;
122 static int open_flag;
123 static char *features_cmd;
124 static char *mntopts_cmd;
125 static int stride, stripe_width;
126 static int stride_set, stripe_width_set;
127 static char *extended_cmd;
128 static unsigned long new_inode_size;
129 static char *ext_mount_opts;
130 static int quota_enable[MAXQUOTAS];
131 static int rewrite_checksums;
132 static int feature_64bit;
133 static int fsck_requested;
134 static char *undo_file;
135 int enabling_casefold;
137 int journal_size, journal_fc_size, journal_flags;
138 char *journal_device;
139 static blk64_t journal_location = ~0LL;
140 static e2_blkcnt_t orphan_file_blocks;
142 static struct list_head blk_move_list;
144 struct blk_move {
145 struct list_head list;
146 blk64_t old_loc;
147 blk64_t new_loc;
150 errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs);
152 static const char *fsck_explain = N_("\nThis operation requires a freshly checked filesystem.\n");
154 static const char *please_fsck = N_("Please run e2fsck -f on the filesystem.\n");
155 static const char *please_dir_fsck =
156 N_("Please run e2fsck -fD on the filesystem.\n");
158 #ifdef CONFIG_BUILD_FINDFS
159 void do_findfs(int argc, char **argv);
160 #endif
162 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
163 int journal_enable_debug = -1;
164 #endif
166 static void usage(void)
168 fprintf(stderr,
169 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] [-f] "
170 "[-g group]\n"
171 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
172 "\t[-m reserved_blocks_percent] [-o [^]mount_options[,...]]\n"
173 "\t[-r reserved_blocks_count] [-u user] [-C mount_count]\n"
174 "\t[-L volume_label] [-M last_mounted_dir]\n"
175 "\t[-O [^]feature[,...]] [-Q quota_options]\n"
176 "\t[-E extended-option[,...]] [-T last_check_time] "
177 "[-U UUID]\n\t[-I new_inode_size] [-z undo_file] device\n"),
178 program_name);
179 exit(1);
182 static __u32 ok_features[3] = {
183 /* Compat */
184 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
185 EXT2_FEATURE_COMPAT_DIR_INDEX |
186 EXT4_FEATURE_COMPAT_FAST_COMMIT |
187 EXT4_FEATURE_COMPAT_STABLE_INODES |
188 EXT4_FEATURE_COMPAT_ORPHAN_FILE,
189 /* Incompat */
190 EXT2_FEATURE_INCOMPAT_FILETYPE |
191 EXT3_FEATURE_INCOMPAT_EXTENTS |
192 EXT4_FEATURE_INCOMPAT_FLEX_BG |
193 EXT4_FEATURE_INCOMPAT_EA_INODE|
194 EXT4_FEATURE_INCOMPAT_MMP |
195 EXT4_FEATURE_INCOMPAT_64BIT |
196 EXT4_FEATURE_INCOMPAT_ENCRYPT |
197 EXT4_FEATURE_INCOMPAT_CSUM_SEED |
198 EXT4_FEATURE_INCOMPAT_LARGEDIR |
199 EXT4_FEATURE_INCOMPAT_CASEFOLD,
200 /* R/O compat */
201 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
202 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
203 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
204 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
205 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
206 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
207 EXT4_FEATURE_RO_COMPAT_QUOTA |
208 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
209 EXT4_FEATURE_RO_COMPAT_READONLY |
210 EXT4_FEATURE_RO_COMPAT_PROJECT |
211 EXT4_FEATURE_RO_COMPAT_VERITY
214 static __u32 clear_ok_features[3] = {
215 /* Compat */
216 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
217 EXT2_FEATURE_COMPAT_RESIZE_INODE |
218 EXT2_FEATURE_COMPAT_DIR_INDEX |
219 EXT4_FEATURE_COMPAT_FAST_COMMIT |
220 EXT4_FEATURE_COMPAT_ORPHAN_FILE,
221 /* Incompat */
222 EXT2_FEATURE_INCOMPAT_FILETYPE |
223 EXT4_FEATURE_INCOMPAT_FLEX_BG |
224 EXT4_FEATURE_INCOMPAT_MMP |
225 EXT4_FEATURE_INCOMPAT_64BIT |
226 EXT4_FEATURE_INCOMPAT_CSUM_SEED |
227 EXT4_FEATURE_INCOMPAT_CASEFOLD,
228 /* R/O compat */
229 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
230 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
231 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
232 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
233 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
234 EXT4_FEATURE_RO_COMPAT_QUOTA |
235 EXT4_FEATURE_RO_COMPAT_PROJECT |
236 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
237 EXT4_FEATURE_RO_COMPAT_READONLY
241 * Try to get journal super block if any
243 static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
245 int retval;
246 journal_superblock_t *jsb;
248 if (!ext2fs_has_feature_journal_dev(jfs->super)) {
249 return EXT2_ET_UNSUPP_FEATURE;
252 /* Get the journal superblock */
253 if ((retval = io_channel_read_blk64(jfs->io,
254 ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
255 com_err(program_name, retval, "%s",
256 _("while reading journal superblock"));
257 return retval;
260 jsb = (journal_superblock_t *) buf;
261 if ((jsb->s_header.h_magic != (unsigned)ntohl(JBD2_MAGIC_NUMBER)) ||
262 (jsb->s_header.h_blocktype != (unsigned)ntohl(JBD2_SUPERBLOCK_V2))) {
263 fputs(_("Journal superblock not found!\n"), stderr);
264 return EXT2_ET_BAD_MAGIC;
267 return 0;
270 static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JBD2_USERS_SIZE],
271 int nr_users)
273 int i;
274 for (i = 0; i < nr_users; i++) {
275 if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
276 return &s_users[i * UUID_SIZE];
279 return NULL;
283 * Remove an external journal from the filesystem
285 static int remove_journal_device(ext2_filsys fs)
287 char *journal_path;
288 ext2_filsys jfs;
289 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
290 journal_superblock_t *jsb;
291 int i, nr_users;
292 errcode_t retval;
293 int commit_remove_journal = 0;
294 io_manager io_ptr;
296 if (f_flag)
297 commit_remove_journal = 1; /* force removal even if error */
299 uuid_unparse(fs->super->s_journal_uuid, buf);
300 journal_path = blkid_get_devname(NULL, "UUID", buf);
302 if (!journal_path) {
303 journal_path =
304 ext2fs_find_block_device(fs->super->s_journal_dev);
305 if (!journal_path)
306 goto no_valid_journal;
309 #ifdef CONFIG_TESTIO_DEBUG
310 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
311 io_ptr = test_io_manager;
312 test_io_backing_manager = unix_io_manager;
313 } else
314 #endif
315 io_ptr = unix_io_manager;
316 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
317 EXT2_FLAG_JOURNAL_DEV_OK, 0,
318 fs->blocksize, io_ptr, &jfs);
319 if (retval) {
320 com_err(program_name, retval, "%s",
321 _("while trying to open external journal"));
322 goto no_valid_journal;
325 if ((retval = get_journal_sb(jfs, buf))) {
326 if (retval == EXT2_ET_UNSUPP_FEATURE)
327 fprintf(stderr, _("%s is not a journal device.\n"),
328 journal_path);
329 goto no_valid_journal;
332 jsb = (journal_superblock_t *) buf;
333 /* Find the filesystem UUID */
334 nr_users = ntohl(jsb->s_nr_users);
335 if (nr_users > JBD2_USERS_MAX) {
336 fprintf(stderr, _("Journal superblock is corrupted, nr_users\n"
337 "is too high (%d).\n"), nr_users);
338 commit_remove_journal = 1;
339 goto no_valid_journal;
342 if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
343 fputs(_("Filesystem's UUID not found on journal device.\n"),
344 stderr);
345 commit_remove_journal = 1;
346 goto no_valid_journal;
348 nr_users--;
349 for (i = 0; i < nr_users; i++)
350 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
351 jsb->s_nr_users = htonl(nr_users);
353 /* Write back the journal superblock */
354 retval = io_channel_write_blk64(jfs->io,
355 ext2fs_journal_sb_start(fs->blocksize),
356 -SUPERBLOCK_SIZE, buf);
357 if (retval) {
358 com_err(program_name, retval,
359 "while writing journal superblock.");
360 goto no_valid_journal;
363 commit_remove_journal = 1;
365 no_valid_journal:
366 if (commit_remove_journal == 0) {
367 fputs(_("Cannot locate journal device. It was NOT removed\n"
368 "Use -f option to remove missing journal device.\n"),
369 stderr);
370 return 1;
372 fs->super->s_journal_dev = 0;
373 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
374 uuid_clear(fs->super->s_journal_uuid);
375 ext2fs_mark_super_dirty(fs);
376 fputs(_("Journal removed\n"), stdout);
377 free(journal_path);
379 return 0;
382 /* Helper function for remove_journal_inode */
383 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
384 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
385 blk64_t ref_block EXT2FS_ATTR((unused)),
386 int ref_offset EXT2FS_ATTR((unused)),
387 void *private EXT2FS_ATTR((unused)))
389 blk64_t block;
390 int group;
392 block = *blocknr;
393 ext2fs_unmark_block_bitmap2(fs->block_map, block);
394 group = ext2fs_group_of_blk2(fs, block);
395 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
396 ext2fs_group_desc_csum_set(fs, group);
397 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
398 return 0;
402 * Remove the journal inode from the filesystem
404 static errcode_t remove_journal_inode(ext2_filsys fs)
406 struct ext2_inode inode;
407 errcode_t retval;
408 ext2_ino_t ino = fs->super->s_journal_inum;
410 retval = ext2fs_read_inode(fs, ino, &inode);
411 if (retval) {
412 com_err(program_name, retval, "%s",
413 _("while reading journal inode"));
414 return retval;
416 if (ino == EXT2_JOURNAL_INO) {
417 retval = ext2fs_read_bitmaps(fs);
418 if (retval) {
419 com_err(program_name, retval, "%s",
420 _("while reading bitmaps"));
421 return retval;
423 retval = ext2fs_block_iterate3(fs, ino,
424 BLOCK_FLAG_READ_ONLY, NULL,
425 release_blocks_proc, NULL);
426 if (retval) {
427 com_err(program_name, retval, "%s",
428 _("while clearing journal inode"));
429 return retval;
431 fs->super->s_overhead_clusters -=
432 EXT2FS_NUM_B2C(fs, EXT2_I_SIZE(&inode) / fs->blocksize);
433 memset(&inode, 0, sizeof(inode));
434 ext2fs_mark_bb_dirty(fs);
435 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
436 } else
437 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
438 retval = ext2fs_write_inode(fs, ino, &inode);
439 if (retval) {
440 com_err(program_name, retval, "%s",
441 _("while writing journal inode"));
442 return retval;
444 fs->super->s_journal_inum = 0;
445 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
446 ext2fs_mark_super_dirty(fs);
448 return 0;
452 * Update the default mount options
454 static int update_mntopts(ext2_filsys fs, char *mntopts)
456 struct ext2_super_block *sb = fs->super;
458 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
459 fprintf(stderr, _("Invalid mount option set: %s\n"),
460 mntopts);
461 return 1;
463 ext2fs_mark_super_dirty(fs);
465 return 0;
468 static int check_fsck_needed(ext2_filsys fs, const char *prompt)
470 /* Refuse to modify anything but a freshly checked valid filesystem. */
471 if (!(fs->super->s_state & EXT2_VALID_FS) ||
472 (fs->super->s_state & EXT2_ERROR_FS) ||
473 (ext2fs_get_tstamp(fs->super, s_lastcheck) <
474 ext2fs_get_tstamp(fs->super, s_mtime))) {
475 puts(_(fsck_explain));
476 puts(_(please_fsck));
477 if (mount_flags & EXT2_MF_READONLY)
478 printf("%s", _("(and reboot afterwards!)\n"));
479 return 1;
482 /* Give the admin a few seconds to bail out of a dangerous op. */
483 if (!getenv("TUNE2FS_FORCE_PROMPT") && (!isatty(0) || !isatty(1)))
484 return 0;
486 puts(prompt);
487 proceed_question(5);
489 return 0;
492 static void request_dir_fsck_afterwards(ext2_filsys fs)
494 static int requested;
496 if (requested++)
497 return;
498 fsck_requested++;
499 fs->super->s_state &= ~EXT2_VALID_FS;
500 puts(_(fsck_explain));
501 puts(_(please_dir_fsck));
502 if (mount_flags & EXT2_MF_READONLY)
503 printf("%s", _("(and reboot afterwards!)\n"));
506 static void request_fsck_afterwards(ext2_filsys fs)
508 static int requested = 0;
510 if (requested++)
511 return;
512 fsck_requested++;
513 fs->super->s_state &= ~EXT2_VALID_FS;
514 printf("\n%s\n", _(please_fsck));
515 if (mount_flags & EXT2_MF_READONLY)
516 printf("%s", _("(and reboot afterwards!)\n"));
519 static void convert_64bit(ext2_filsys fs, int direction)
522 * Is resize2fs going to demand a fsck run? Might as well tell the
523 * user now.
525 if (!fsck_requested &&
526 ((fs->super->s_state & EXT2_ERROR_FS) ||
527 !(fs->super->s_state & EXT2_VALID_FS) ||
528 ext2fs_get_tstamp(fs->super, s_lastcheck) <
529 ext2fs_get_tstamp(fs->super, s_mtime)))
530 request_fsck_afterwards(fs);
531 if (fsck_requested)
532 fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
533 direction > 0 ? "-b" : "-s", fs->device_name);
534 else
535 fprintf(stderr, _("Please run `resize2fs %s %s"),
536 direction > 0 ? "-b" : "-s", fs->device_name);
538 if (undo_file)
539 fprintf(stderr, _(" -z \"%s\""), undo_file);
540 if (direction > 0)
541 fprintf(stderr, _("' to enable 64-bit mode.\n"));
542 else
543 fprintf(stderr, _("' to disable 64-bit mode.\n"));
547 * Rewrite directory blocks with checksums
549 struct rewrite_dir_context {
550 char *buf;
551 errcode_t errcode;
552 ext2_ino_t dir;
553 int is_htree:1;
554 int clear_htree:1;
557 static int rewrite_dir_block(ext2_filsys fs,
558 blk64_t *blocknr,
559 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
560 blk64_t ref_block EXT2FS_ATTR((unused)),
561 int ref_offset EXT2FS_ATTR((unused)),
562 void *priv_data)
564 struct ext2_dx_countlimit *dcl = NULL;
565 struct rewrite_dir_context *ctx = priv_data;
566 int dcl_offset, changed = 0;
568 ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
569 ctx->dir);
570 if (ctx->errcode)
571 return BLOCK_ABORT;
574 * if htree node... Note that if we are clearing htree structures from
575 * the directory, we treat the htree internal block as an ordinary leaf.
576 * The code below will do the right thing and make space for checksum
577 * there.
579 if (ctx->is_htree && !ctx->clear_htree)
580 ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
581 &dcl, &dcl_offset);
582 if (dcl) {
583 if (!ext2fs_has_feature_metadata_csum(fs->super)) {
584 /* Ensure limit is the max size */
585 int max_entries = (fs->blocksize - dcl_offset) /
586 sizeof(struct ext2_dx_entry);
587 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
588 changed = 1;
589 dcl->limit = ext2fs_cpu_to_le16(max_entries);
591 } else {
592 /* If htree block is full then rebuild the dir */
593 if (ext2fs_le16_to_cpu(dcl->count) ==
594 ext2fs_le16_to_cpu(dcl->limit)) {
595 request_dir_fsck_afterwards(fs);
596 return 0;
599 * Ensure dcl->limit is small enough to leave room for
600 * the checksum tail.
602 int max_entries = (fs->blocksize - (dcl_offset +
603 sizeof(struct ext2_dx_tail))) /
604 sizeof(struct ext2_dx_entry);
605 if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
606 dcl->limit = ext2fs_cpu_to_le16(max_entries);
607 /* Always rewrite checksum */
608 changed = 1;
610 } else {
611 unsigned int rec_len, name_size;
612 char *top = ctx->buf + fs->blocksize;
613 struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
614 struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
616 /* Find last and penultimate dirent */
617 while ((char *)de < top) {
618 penultimate_de = last_de;
619 last_de = de;
620 ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
621 if (!ctx->errcode && !rec_len)
622 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
623 if (ctx->errcode)
624 return BLOCK_ABORT;
625 de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
627 ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
628 if (ctx->errcode)
629 return BLOCK_ABORT;
630 name_size = ext2fs_dirent_name_len(last_de);
632 if (!ext2fs_has_feature_metadata_csum(fs->super)) {
633 if (!penultimate_de)
634 return 0;
635 if (last_de->inode ||
636 name_size ||
637 rec_len != sizeof(struct ext2_dir_entry_tail))
638 return 0;
640 * The last dirent is unused and the right length to
641 * have stored a checksum. Erase it.
643 ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
644 &rec_len);
645 if (!rec_len)
646 ctx->errcode = EXT2_ET_DIR_CORRUPTED;
647 if (ctx->errcode)
648 return BLOCK_ABORT;
649 ext2fs_set_rec_len(fs, rec_len +
650 sizeof(struct ext2_dir_entry_tail),
651 penultimate_de);
652 changed = 1;
653 } else {
654 unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
655 struct ext2_dir_entry_tail *t;
658 * If the last dirent looks like the tail, just update
659 * the checksum.
661 if (!last_de->inode &&
662 rec_len == csum_size) {
663 t = (struct ext2_dir_entry_tail *)last_de;
664 t->det_reserved_name_len =
665 EXT2_DIR_NAME_LEN_CSUM;
666 changed = 1;
667 goto out;
669 if (name_size & 3)
670 name_size = (name_size & ~3) + 4;
671 /* If there's not enough space for the tail, e2fsck */
672 if (rec_len <= (8 + name_size + csum_size)) {
673 request_dir_fsck_afterwards(fs);
674 return 0;
676 /* Shorten that last de and insert the tail */
677 ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
678 t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
679 ext2fs_initialize_dirent_tail(fs, t);
681 /* Always update checksum */
682 changed = 1;
686 out:
687 if (!changed)
688 return 0;
690 ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
691 0, ctx->dir);
692 if (ctx->errcode)
693 return BLOCK_ABORT;
695 return 0;
698 static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
699 struct ext2_inode *inode)
701 errcode_t retval;
702 struct rewrite_dir_context ctx;
704 retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
705 if (retval)
706 return retval;
708 ctx.is_htree = !!(inode->i_flags & EXT2_INDEX_FL);
709 ctx.clear_htree = !ext2fs_has_feature_dir_index(fs->super);
710 ctx.dir = dir;
711 ctx.errcode = 0;
712 retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
713 BLOCK_FLAG_DATA_ONLY,
714 0, rewrite_dir_block, &ctx);
716 ext2fs_free_mem(&ctx.buf);
717 if (retval)
718 return retval;
720 if (ctx.is_htree && ctx.clear_htree) {
721 inode->i_flags &= ~EXT2_INDEX_FL;
722 retval = ext2fs_write_inode(fs, dir, inode);
723 if (retval)
724 return retval;
727 return ctx.errcode;
731 * Context information that does not change across rewrite_one_inode()
732 * invocations.
734 struct rewrite_context {
735 ext2_filsys fs;
736 struct ext2_inode *zero_inode;
737 char *ea_buf;
738 int inode_size;
741 #define fatal_err(code, args...) \
742 do { \
743 com_err(__func__, code, args); \
744 exit(1); \
745 } while (0);
747 static void update_ea_inode_hash(struct rewrite_context *ctx, ext2_ino_t ino,
748 struct ext2_inode *inode)
750 errcode_t retval;
751 ext2_file_t file;
752 __u32 hash;
754 retval = ext2fs_file_open(ctx->fs, ino, 0, &file);
755 if (retval)
756 fatal_err(retval, "open ea_inode");
757 retval = ext2fs_file_read(file, ctx->ea_buf, inode->i_size,
758 NULL);
759 if (retval)
760 fatal_err(retval, "read ea_inode");
761 retval = ext2fs_file_close(file);
762 if (retval)
763 fatal_err(retval, "close ea_inode");
765 hash = ext2fs_crc32c_le(ctx->fs->csum_seed,
766 (unsigned char *) ctx->ea_buf, inode->i_size);
767 ext2fs_set_ea_inode_hash(inode, hash);
770 static int update_xattr_entry_hashes(ext2_filsys fs,
771 struct ext2_ext_attr_entry *entry,
772 struct ext2_ext_attr_entry *end)
774 int modified = 0;
775 errcode_t retval;
777 while (entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry)) {
778 if (entry->e_value_inum) {
779 retval = ext2fs_ext_attr_hash_entry2(fs, entry, NULL,
780 &entry->e_hash);
781 if (retval)
782 fatal_err(retval, "hash ea_inode entry");
783 modified = 1;
785 entry = EXT2_EXT_ATTR_NEXT(entry);
787 return modified;
790 static void update_inline_xattr_hashes(struct rewrite_context *ctx,
791 struct ext2_inode_large *inode)
793 struct ext2_ext_attr_entry *start, *end;
794 __u32 *ea_magic;
796 if (inode->i_extra_isize == 0)
797 return;
799 if (inode->i_extra_isize & 3 ||
800 inode->i_extra_isize > ctx->inode_size - EXT2_GOOD_OLD_INODE_SIZE)
801 fatal_err(EXT2_ET_INODE_CORRUPTED, "bad i_extra_isize")
803 ea_magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
804 inode->i_extra_isize);
805 if (*ea_magic != EXT2_EXT_ATTR_MAGIC)
806 return;
808 start = (struct ext2_ext_attr_entry *)(ea_magic + 1);
809 end = (struct ext2_ext_attr_entry *)((char *)inode + ctx->inode_size);
811 update_xattr_entry_hashes(ctx->fs, start, end);
814 static void update_block_xattr_hashes(struct rewrite_context *ctx,
815 char *block_buf)
817 struct ext2_ext_attr_header *header;
818 struct ext2_ext_attr_entry *start, *end;
820 header = (struct ext2_ext_attr_header *)block_buf;
821 if (header->h_magic != EXT2_EXT_ATTR_MAGIC)
822 return;
824 start = (struct ext2_ext_attr_entry *)(header+1);
825 end = (struct ext2_ext_attr_entry *)(block_buf + ctx->fs->blocksize);
827 if (update_xattr_entry_hashes(ctx->fs, start, end))
828 ext2fs_ext_attr_block_rehash(header, end);
831 static void rewrite_one_inode(struct rewrite_context *ctx, ext2_ino_t ino,
832 struct ext2_inode *inode)
834 blk64_t file_acl_block;
835 errcode_t retval;
837 if (!ext2fs_test_inode_bitmap2(ctx->fs->inode_map, ino)) {
838 if (!memcmp(inode, ctx->zero_inode, ctx->inode_size))
839 return;
840 memset(inode, 0, ctx->inode_size);
843 if (inode->i_flags & EXT4_EA_INODE_FL)
844 update_ea_inode_hash(ctx, ino, inode);
846 if (ctx->inode_size != EXT2_GOOD_OLD_INODE_SIZE)
847 update_inline_xattr_hashes(ctx,
848 (struct ext2_inode_large *)inode);
850 retval = ext2fs_write_inode_full(ctx->fs, ino, inode, ctx->inode_size);
851 if (retval)
852 fatal_err(retval, "while writing inode");
854 retval = ext2fs_fix_extents_checksums(ctx->fs, ino, inode);
855 if (retval)
856 fatal_err(retval, "while rewriting extents");
858 if (LINUX_S_ISDIR(inode->i_mode) &&
859 ext2fs_inode_has_valid_blocks2(ctx->fs, inode)) {
860 retval = rewrite_directory(ctx->fs, ino, inode);
861 if (retval)
862 fatal_err(retval, "while rewriting directories");
865 file_acl_block = ext2fs_file_acl_block(ctx->fs, inode);
866 if (!file_acl_block)
867 return;
869 retval = ext2fs_read_ext_attr3(ctx->fs, file_acl_block, ctx->ea_buf,
870 ino);
871 if (retval)
872 fatal_err(retval, "while rewriting extended attribute");
874 update_block_xattr_hashes(ctx, ctx->ea_buf);
875 retval = ext2fs_write_ext_attr3(ctx->fs, file_acl_block, ctx->ea_buf,
876 ino);
877 if (retval)
878 fatal_err(retval, "while rewriting extended attribute");
881 #define REWRITE_EA_FL 0x01 /* Rewrite EA inodes */
882 #define REWRITE_DIR_FL 0x02 /* Rewrite directories */
883 #define REWRITE_NONDIR_FL 0x04 /* Rewrite other inodes */
884 #define REWRITE_ALL (REWRITE_EA_FL | REWRITE_DIR_FL | REWRITE_NONDIR_FL)
886 static void rewrite_inodes_pass(struct rewrite_context *ctx, unsigned int flags)
888 ext2_inode_scan scan;
889 errcode_t retval;
890 ext2_ino_t ino;
891 struct ext2_inode *inode;
892 int rewrite;
894 retval = ext2fs_get_mem(ctx->inode_size, &inode);
895 if (retval)
896 fatal_err(retval, "while allocating memory");
898 retval = ext2fs_open_inode_scan(ctx->fs, 0, &scan);
899 if (retval)
900 fatal_err(retval, "while opening inode scan");
902 do {
903 retval = ext2fs_get_next_inode_full(scan, &ino, inode,
904 ctx->inode_size);
905 if (retval)
906 fatal_err(retval, "while getting next inode");
907 if (!ino)
908 break;
910 rewrite = 0;
911 if (inode->i_flags & EXT4_EA_INODE_FL) {
912 if (flags & REWRITE_EA_FL)
913 rewrite = 1;
914 } else if (LINUX_S_ISDIR(inode->i_mode)) {
915 if (flags & REWRITE_DIR_FL)
916 rewrite = 1;
917 } else {
918 if (flags & REWRITE_NONDIR_FL)
919 rewrite = 1;
921 if (rewrite)
922 rewrite_one_inode(ctx, ino, inode);
923 } while (ino);
924 ext2fs_close_inode_scan(scan);
925 ext2fs_free_mem(&inode);
929 * Forcibly rewrite checksums in inodes specified by 'flags'
931 static void rewrite_inodes(ext2_filsys fs, unsigned int flags)
933 struct rewrite_context ctx = {
934 .fs = fs,
935 .inode_size = EXT2_INODE_SIZE(fs->super),
937 errcode_t retval;
939 if (fs->super->s_creator_os == EXT2_OS_HURD)
940 return;
942 retval = ext2fs_get_memzero(ctx.inode_size, &ctx.zero_inode);
943 if (retval)
944 fatal_err(retval, "while allocating memory");
946 retval = ext2fs_get_mem(64 * 1024, &ctx.ea_buf);
947 if (retval)
948 fatal_err(retval, "while allocating memory");
951 * Extended attribute inodes have a lookup hash that needs to be
952 * recalculated with the new csum_seed. Other inodes referencing xattr
953 * inodes need this value to be up to date. That's why we do two passes:
955 * pass 1: update xattr inodes to update their lookup hash as well as
956 * other checksums.
958 * pass 2: go over other inodes to update their checksums.
960 if (ext2fs_has_feature_ea_inode(fs->super) && (flags & REWRITE_EA_FL))
961 rewrite_inodes_pass(&ctx, REWRITE_EA_FL);
962 flags &= ~REWRITE_EA_FL;
963 rewrite_inodes_pass(&ctx, flags);
965 ext2fs_free_mem(&ctx.zero_inode);
966 ext2fs_free_mem(&ctx.ea_buf);
969 static errcode_t rewrite_metadata_checksums(ext2_filsys fs, unsigned int flags)
971 errcode_t retval;
972 dgrp_t i;
974 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
975 ext2fs_init_csum_seed(fs);
976 for (i = 0; i < fs->group_desc_count; i++)
977 ext2fs_group_desc_csum_set(fs, i);
978 retval = ext2fs_read_bitmaps(fs);
979 if (retval)
980 fatal_err(retval, "while reading bitmaps");
981 rewrite_inodes(fs, flags);
982 ext2fs_mark_ib_dirty(fs);
983 ext2fs_mark_bb_dirty(fs);
984 retval = ext2fs_mmp_update2(fs, 1);
985 if (retval)
986 return retval;
987 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
988 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
989 if (ext2fs_has_feature_metadata_csum(fs->super))
990 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
991 else
992 fs->super->s_checksum_type = 0;
993 ext2fs_mark_super_dirty(fs);
994 return 0;
997 static void enable_uninit_bg(ext2_filsys fs)
999 struct ext2_group_desc *gd;
1000 dgrp_t i;
1002 for (i = 0; i < fs->group_desc_count; i++) {
1003 gd = ext2fs_group_desc(fs, fs->group_desc, i);
1004 gd->bg_itable_unused = 0;
1005 gd->bg_flags = EXT2_BG_INODE_ZEROED;
1006 ext2fs_group_desc_csum_set(fs, i);
1008 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1011 static errcode_t zero_empty_inodes(ext2_filsys fs)
1013 int length = EXT2_INODE_SIZE(fs->super);
1014 struct ext2_inode *inode = NULL;
1015 ext2_inode_scan scan;
1016 errcode_t retval;
1017 ext2_ino_t ino;
1019 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1020 if (retval)
1021 goto out;
1023 retval = ext2fs_get_mem(length, &inode);
1024 if (retval)
1025 goto out;
1027 do {
1028 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
1029 if (retval)
1030 goto out;
1031 if (!ino)
1032 break;
1033 if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
1034 memset(inode, 0, length);
1035 retval = ext2fs_write_inode_full(fs, ino, inode,
1036 length);
1037 if (retval)
1038 goto out;
1040 } while (1);
1042 out:
1043 ext2fs_free_mem(&inode);
1044 ext2fs_close_inode_scan(scan);
1045 return retval;
1048 static int has_casefold_inode(ext2_filsys fs)
1050 int length = EXT2_INODE_SIZE(fs->super);
1051 struct ext2_inode *inode = NULL;
1052 ext2_inode_scan scan;
1053 errcode_t retval;
1054 ext2_ino_t ino;
1055 int found_casefold = 0;
1057 retval = ext2fs_get_mem(length, &inode);
1058 if (retval)
1059 fatal_err(retval, "while allocating memory");
1061 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1062 if (retval)
1063 fatal_err(retval, "while opening inode scan");
1065 do {
1066 retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
1067 if (retval)
1068 fatal_err(retval, "while getting next inode");
1069 if (!ino)
1070 break;
1072 if(inode->i_flags & EXT4_CASEFOLD_FL) {
1073 found_casefold = 1;
1074 break;
1076 } while(1);
1078 ext2fs_free_mem(&inode);
1079 ext2fs_close_inode_scan(scan);
1080 return found_casefold;
1083 static errcode_t disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
1085 struct ext2_group_desc *gd;
1086 dgrp_t i;
1087 errcode_t retval;
1088 blk64_t b, c, d;
1090 /* Load bitmaps to ensure that the uninit ones get written out */
1091 fs->super->s_feature_ro_compat |= csum_feature_flag;
1092 retval = ext2fs_read_bitmaps(fs);
1093 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
1094 if (retval) {
1095 com_err("disable_uninit_bg", retval,
1096 "while reading bitmaps");
1097 request_fsck_afterwards(fs);
1098 return retval;
1100 ext2fs_mark_ib_dirty(fs);
1101 ext2fs_mark_bb_dirty(fs);
1103 /* If we're only turning off uninit_bg, zero the inodes */
1104 if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
1105 retval = zero_empty_inodes(fs);
1106 if (retval) {
1107 com_err("disable_uninit_bg", retval,
1108 "while zeroing unused inodes");
1109 request_fsck_afterwards(fs);
1110 return retval;
1114 /* The bbitmap is zeroed; we must mark group metadata blocks in use */
1115 for (i = 0; i < fs->group_desc_count; i++) {
1116 b = ext2fs_block_bitmap_loc(fs, i);
1117 ext2fs_mark_block_bitmap2(fs->block_map, b);
1118 b = ext2fs_inode_bitmap_loc(fs, i);
1119 ext2fs_mark_block_bitmap2(fs->block_map, b);
1121 retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
1122 if (retval == 0 && b)
1123 ext2fs_mark_block_bitmap2(fs->block_map, b);
1124 if (retval == 0 && c)
1125 ext2fs_mark_block_bitmap2(fs->block_map, c);
1126 if (retval == 0 && d)
1127 ext2fs_mark_block_bitmap2(fs->block_map, d);
1128 if (retval) {
1129 com_err("disable_uninit_bg", retval,
1130 "while initializing block bitmaps");
1131 request_fsck_afterwards(fs);
1134 gd = ext2fs_group_desc(fs, fs->group_desc, i);
1135 gd->bg_itable_unused = 0;
1136 gd->bg_flags = 0;
1137 ext2fs_group_desc_csum_set(fs, i);
1139 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1140 ext2fs_mark_super_dirty(fs);
1142 return 0;
1145 static void
1146 try_confirm_csum_seed_support(void)
1148 if (access("/sys/fs/ext4/features/metadata_csum_seed", R_OK))
1149 fputs(_("WARNING: Could not confirm kernel support for "
1150 "metadata_csum_seed.\n This requires Linux >= "
1151 "v4.4.\n"), stderr);
1155 * Update the feature set as provided by the user.
1157 static int update_feature_set(ext2_filsys fs, char *features)
1159 struct ext2_super_block *sb = fs->super;
1160 __u32 old_features[3];
1161 int type_err;
1162 unsigned int mask_err;
1163 errcode_t err;
1164 enum quota_type qtype;
1166 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
1167 ((&sb->s_feature_compat)[(type)] & (mask)))
1168 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
1169 !((&sb->s_feature_compat)[(type)] & (mask)))
1170 #define FEATURE_CHANGED(type, mask) ((mask) & \
1171 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
1173 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
1174 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
1175 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
1177 if (e2p_edit_feature2(features, &sb->s_feature_compat,
1178 ok_features, clear_ok_features,
1179 &type_err, &mask_err)) {
1180 if (!mask_err)
1181 fprintf(stderr,
1182 _("Invalid filesystem option set: %s\n"),
1183 features);
1184 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
1185 fprintf(stderr, _("Clearing filesystem feature '%s' "
1186 "not supported.\n"),
1187 e2p_feature2string(type_err &
1188 E2P_FEATURE_TYPE_MASK,
1189 mask_err));
1190 else
1191 fprintf(stderr, _("Setting filesystem feature '%s' "
1192 "not supported.\n"),
1193 e2p_feature2string(type_err, mask_err));
1194 return 1;
1197 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1198 if ((mount_flags & EXT2_MF_MOUNTED) &&
1199 !(mount_flags & EXT2_MF_READONLY)) {
1200 fputs(_("The has_journal feature may only be "
1201 "cleared when the filesystem is\n"
1202 "unmounted or mounted "
1203 "read-only.\n"), stderr);
1204 return 1;
1206 if (ext2fs_has_feature_journal_needs_recovery(sb) &&
1207 f_flag < 2) {
1208 fputs(_("The needs_recovery flag is set. "
1209 "Please run e2fsck before clearing\n"
1210 "the has_journal flag.\n"), stderr);
1211 return 1;
1213 if (sb->s_journal_inum) {
1214 if (remove_journal_inode(fs))
1215 return 1;
1217 if (sb->s_journal_dev) {
1218 if (remove_journal_device(fs))
1219 return 1;
1223 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT4_FEATURE_COMPAT_ORPHAN_FILE)) {
1224 ext2_ino_t ino;
1226 if (mount_flags & EXT2_MF_MOUNTED) {
1227 fputs(_("The orphan_file feature may only be cleared "
1228 "when the filesystem is unmounted.\n"), stderr);
1229 return 1;
1231 if (ext2fs_has_feature_orphan_present(sb) && f_flag < 2) {
1232 fputs(_("The orphan_present feature is set. Please "
1233 "run e2fsck before clearing orphan_file "
1234 "feature.\n"),
1235 stderr);
1236 return 1;
1238 err = ext2fs_read_bitmaps(fs);
1239 if (err) {
1240 com_err(program_name, err, "%s",
1241 _("while loading bitmaps"));
1242 return 1;
1244 err = ext2fs_truncate_orphan_file(fs);
1245 if (err) {
1246 com_err(program_name, err,
1247 _("\n\twhile trying to delete orphan file\n"));
1248 return 1;
1250 ino = sb->s_orphan_file_inum;
1251 sb->s_orphan_file_inum = 0;
1252 ext2fs_inode_alloc_stats2(fs, ino, -1, 0);
1253 ext2fs_clear_feature_orphan_file(sb);
1254 ext2fs_clear_feature_orphan_present(sb);
1255 ext2fs_mark_super_dirty(fs);
1258 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT4_FEATURE_COMPAT_ORPHAN_FILE)) {
1259 if (!ext2fs_has_feature_journal(sb)) {
1260 fputs(_("orphan_file feature can be set only for "
1261 "filesystems with journal.\n"), stderr);
1262 return 1;
1265 * If adding an orphan file, let the create orphan file
1266 * code below handle setting the flag and creating it.
1267 * We supply a default size if necessary.
1269 orphan_file_blocks = ext2fs_default_orphan_file_blocks(fs);
1270 ext2fs_set_feature_orphan_file(sb);
1273 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1274 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1275 if (ext2fs_has_feature_meta_bg(sb)) {
1276 fputs(_("Setting filesystem feature 'sparse_super' "
1277 "not supported\nfor filesystems with "
1278 "the meta_bg feature enabled.\n"),
1279 stderr);
1280 return 1;
1284 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1285 int error;
1287 if ((mount_flags & EXT2_MF_MOUNTED) ||
1288 (mount_flags & EXT2_MF_READONLY)) {
1289 fputs(_("The multiple mount protection feature can't\n"
1290 "be set if the filesystem is mounted or\n"
1291 "read-only.\n"), stderr);
1292 return 1;
1295 error = ext2fs_mmp_init(fs);
1296 if (error) {
1297 fputs(_("\nError while enabling multiple mount "
1298 "protection feature."), stderr);
1299 return 1;
1303 * We want to update group desc with the new free blocks count
1305 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1307 printf(_("Multiple mount protection has been enabled "
1308 "with update interval %ds.\n"),
1309 sb->s_mmp_update_interval);
1312 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1313 int error;
1315 if (mount_flags & EXT2_MF_READONLY) {
1316 fputs(_("The multiple mount protection feature cannot\n"
1317 "be disabled if the filesystem is readonly.\n"),
1318 stderr);
1319 return 1;
1322 error = ext2fs_read_bitmaps(fs);
1323 if (error) {
1324 fputs(_("Error while reading bitmaps\n"), stderr);
1325 return 1;
1328 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1329 if (error) {
1330 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1332 if (error == EXT2_ET_MMP_MAGIC_INVALID)
1333 printf(_("Magic number in MMP block does not "
1334 "match. expected: %x, actual: %x\n"),
1335 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1336 else
1337 com_err(program_name, error, "%s",
1338 _("while reading MMP block."));
1339 goto mmp_error;
1342 /* We need to force out the group descriptors as well */
1343 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1344 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1345 mmp_error:
1346 sb->s_mmp_block = 0;
1347 sb->s_mmp_update_interval = 0;
1350 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1352 * If adding a journal flag, let the create journal
1353 * code below handle setting the flag and creating the
1354 * journal. We supply a default size if necessary.
1356 if (!journal_size)
1357 journal_size = -1;
1358 ext2fs_clear_feature_journal(sb);
1361 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1362 if (!sb->s_def_hash_version)
1363 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1364 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1365 uuid_generate((unsigned char *) sb->s_hash_seed);
1368 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1369 ext2fs_has_feature_metadata_csum(sb)) {
1370 if (check_fsck_needed(fs,
1371 _("Disabling directory index on filesystem with "
1372 "checksums could take some time.")))
1373 return 1;
1374 if (mount_flags & EXT2_MF_MOUNTED) {
1375 fputs(_("Cannot disable dir_index on a mounted "
1376 "filesystem!\n"), stderr);
1377 exit(1);
1380 * Clearing dir_index on checksummed filesystem requires
1381 * rewriting all directories to update checksums.
1383 rewrite_checksums |= REWRITE_DIR_FL;
1386 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1387 if (ext2fs_check_desc(fs)) {
1388 fputs(_("Clearing the flex_bg flag would "
1389 "cause the the filesystem to be\n"
1390 "inconsistent.\n"), stderr);
1391 return 1;
1395 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1396 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1397 if ((mount_flags & EXT2_MF_MOUNTED) &&
1398 !(mount_flags & EXT2_MF_READONLY)) {
1399 fputs(_("The huge_file feature may only be "
1400 "cleared when the filesystem is\n"
1401 "unmounted or mounted "
1402 "read-only.\n"), stderr);
1403 return 1;
1407 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1408 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1409 if (check_fsck_needed(fs,
1410 _("Enabling checksums could take some time.")))
1411 return 1;
1412 if (mount_flags & EXT2_MF_MOUNTED) {
1413 fputs(_("Cannot enable metadata_csum on a mounted "
1414 "filesystem!\n"), stderr);
1415 return 1;
1417 if (!ext2fs_has_feature_extents(fs->super))
1418 printf("%s",
1419 _("Extents are not enabled. The file extent "
1420 "tree can be checksummed, whereas block maps "
1421 "cannot. Not enabling extents reduces the "
1422 "coverage of metadata checksumming. "
1423 "Re-run with -O extent to rectify.\n"));
1424 if (!ext2fs_has_feature_64bit(fs->super))
1425 printf("%s",
1426 _("64-bit filesystem support is not enabled. "
1427 "The larger fields afforded by this feature "
1428 "enable full-strength checksumming. "
1429 "Run resize2fs -b to rectify.\n"));
1430 rewrite_checksums = REWRITE_ALL;
1431 /* metadata_csum supersedes uninit_bg */
1432 ext2fs_clear_feature_gdt_csum(fs->super);
1434 /* if uninit_bg was previously off, rewrite group desc */
1435 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1436 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1437 enable_uninit_bg(fs);
1440 * Since metadata_csum supersedes uninit_bg, pretend like
1441 * uninit_bg has been off all along.
1443 old_features[E2P_FEATURE_RO_INCOMPAT] &=
1444 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1447 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1448 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1449 __u32 test_features[3];
1451 if (check_fsck_needed(fs,
1452 _("Disabling checksums could take some time.")))
1453 return 1;
1454 if (mount_flags & EXT2_MF_MOUNTED) {
1455 fputs(_("Cannot disable metadata_csum on a mounted "
1456 "filesystem!\n"), stderr);
1457 return 1;
1459 rewrite_checksums = REWRITE_ALL;
1461 /* Enable uninit_bg unless the user expressly turned it off */
1462 memcpy(test_features, old_features, sizeof(test_features));
1463 test_features[E2P_FEATURE_RO_INCOMPAT] |=
1464 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1465 e2p_edit_feature2(features, test_features, ok_features,
1466 clear_ok_features, NULL, NULL);
1467 if (test_features[E2P_FEATURE_RO_INCOMPAT] &
1468 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
1469 ext2fs_set_feature_gdt_csum(fs->super);
1472 * If we're turning off metadata_csum and not turning on
1473 * uninit_bg, rewrite group desc.
1475 if (!ext2fs_has_feature_gdt_csum(fs->super)) {
1476 err = disable_uninit_bg(fs,
1477 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1478 if (err)
1479 return 1;
1480 } else
1482 * metadata_csum previously provided uninit_bg, so if
1483 * we're also setting the uninit_bg feature bit,
1484 * pretend like it was previously enabled. Checksums
1485 * will be rewritten with crc16 later.
1487 old_features[E2P_FEATURE_RO_INCOMPAT] |=
1488 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1489 fs->super->s_checksum_seed = 0;
1490 ext2fs_clear_feature_csum_seed(fs->super);
1493 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1494 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1495 if (mount_flags & EXT2_MF_MOUNTED) {
1496 fputs(_("Cannot enable uninit_bg on a mounted "
1497 "filesystem!\n"), stderr);
1498 return 1;
1501 /* Do not enable uninit_bg when metadata_csum enabled */
1502 if (ext2fs_has_feature_metadata_csum(fs->super))
1503 ext2fs_clear_feature_gdt_csum(fs->super);
1504 else
1505 enable_uninit_bg(fs);
1508 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1509 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1510 if (mount_flags & EXT2_MF_MOUNTED) {
1511 fputs(_("Cannot disable uninit_bg on a mounted "
1512 "filesystem!\n"), stderr);
1513 return 1;
1516 err = disable_uninit_bg(fs,
1517 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1518 if (err)
1519 return 1;
1523 * We don't actually toggle 64bit; resize2fs does that. But this
1524 * must come after the metadata_csum feature_on so that it won't
1525 * complain about the lack of 64bit.
1527 if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1528 EXT4_FEATURE_INCOMPAT_64BIT)) {
1529 if (mount_flags & EXT2_MF_MOUNTED) {
1530 fprintf(stderr, _("Cannot enable 64-bit mode "
1531 "while mounted!\n"));
1532 return 1;
1534 ext2fs_clear_feature_64bit(sb);
1535 feature_64bit = 1;
1537 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1538 EXT4_FEATURE_INCOMPAT_64BIT)) {
1539 if (mount_flags & EXT2_MF_MOUNTED) {
1540 fprintf(stderr, _("Cannot disable 64-bit mode "
1541 "while mounted!\n"));
1542 return 1;
1544 ext2fs_set_feature_64bit(sb);
1545 feature_64bit = -1;
1548 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1549 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1551 * Set the Q_flag here and handle the quota options in the code
1552 * below.
1554 if (!Q_flag) {
1555 Q_flag = 1;
1556 /* Enable usr/grp quota by default */
1557 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
1558 if (qtype != PRJQUOTA)
1559 quota_enable[qtype] = QOPT_ENABLE;
1560 else
1561 quota_enable[qtype] = QOPT_DISABLE;
1564 ext2fs_clear_feature_quota(sb);
1567 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1568 EXT4_FEATURE_RO_COMPAT_PROJECT)) {
1569 if (fs->super->s_inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
1570 fprintf(stderr, _("Cannot enable project feature; "
1571 "inode size too small.\n"));
1572 return 1;
1574 Q_flag = 1;
1575 quota_enable[PRJQUOTA] = QOPT_ENABLE;
1578 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1579 EXT4_FEATURE_RO_COMPAT_PROJECT)) {
1580 Q_flag = 1;
1581 quota_enable[PRJQUOTA] = QOPT_DISABLE;
1584 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1585 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1587 * Set the Q_flag here and handle the quota options in the code
1588 * below.
1590 if (Q_flag)
1591 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1592 "arguments.\n"), stderr);
1593 Q_flag = 1;
1594 /* Disable all quota by default */
1595 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1596 quota_enable[qtype] = QOPT_DISABLE;
1599 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_ENCRYPT)) {
1600 fs->super->s_encrypt_algos[0] =
1601 EXT4_ENCRYPTION_MODE_AES_256_XTS;
1602 fs->super->s_encrypt_algos[1] =
1603 EXT4_ENCRYPTION_MODE_AES_256_CTS;
1606 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_CASEFOLD)) {
1607 if (mount_flags & EXT2_MF_MOUNTED) {
1608 fputs(_("The casefold feature may only be enabled when "
1609 "the filesystem is unmounted.\n"), stderr);
1610 return 1;
1612 fs->super->s_encoding = EXT4_ENC_UTF8_12_1;
1613 fs->super->s_encoding_flags = e2p_get_encoding_flags(EXT4_ENC_UTF8_12_1);
1614 enabling_casefold = 1;
1617 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_CASEFOLD)) {
1618 if (mount_flags & EXT2_MF_MOUNTED) {
1619 fputs(_("The casefold feature may only be disabled when "
1620 "the filesystem is unmounted.\n"), stderr);
1621 return 1;
1623 if (has_casefold_inode(fs)) {
1624 fputs(_("The casefold feature can't be cleared when "
1625 "there are inodes with +F flag.\n"), stderr);
1626 return 1;
1628 fs->super->s_encoding = 0;
1629 fs->super->s_encoding_flags = 0;
1630 enabling_casefold = 0;
1633 if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1634 EXT4_FEATURE_INCOMPAT_CSUM_SEED)) {
1635 if (!ext2fs_has_feature_metadata_csum(sb)) {
1636 fputs(_("Setting feature 'metadata_csum_seed' "
1637 "is only supported\non filesystems with "
1638 "the metadata_csum feature enabled.\n"),
1639 stderr);
1640 return 1;
1642 try_confirm_csum_seed_support();
1643 fs->super->s_checksum_seed = fs->csum_seed;
1646 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1647 EXT4_FEATURE_INCOMPAT_CSUM_SEED)) {
1648 __le32 uuid_seed;
1650 uuid_seed = ext2fs_crc32c_le(~0, fs->super->s_uuid,
1651 sizeof(fs->super->s_uuid));
1652 if (fs->super->s_checksum_seed != uuid_seed) {
1653 if (mount_flags & (EXT2_MF_BUSY|EXT2_MF_MOUNTED)) {
1654 fputs(_("UUID has changed since enabling "
1655 "metadata_csum. Filesystem must be unmounted "
1656 "\nto safely rewrite all metadata to match the new UUID.\n"),
1657 stderr);
1658 return 1;
1660 if (check_fsck_needed(fs, _("Recalculating checksums "
1661 "could take some time.")))
1662 return 1;
1663 rewrite_checksums = REWRITE_ALL;
1667 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1668 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1669 sb->s_feature_incompat))
1670 ext2fs_update_dynamic_rev(fs);
1672 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1673 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1674 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1675 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1676 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1677 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1678 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1679 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1680 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1681 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1682 request_fsck_afterwards(fs);
1684 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1685 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1686 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1687 ext2fs_mark_super_dirty(fs);
1689 return 0;
1693 * Add a journal to the filesystem.
1695 static int add_journal(ext2_filsys fs)
1697 struct ext2fs_journal_params jparams;
1698 errcode_t retval;
1699 ext2_filsys jfs;
1700 io_manager io_ptr;
1702 if (ext2fs_has_feature_journal(fs->super)) {
1703 fputs(_("The filesystem already has a journal.\n"), stderr);
1704 goto err;
1706 if (journal_device) {
1707 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1708 NULL))
1709 proceed_question(-1);
1710 check_mount(journal_device, 0, _("journal"));
1711 #ifdef CONFIG_TESTIO_DEBUG
1712 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1713 io_ptr = test_io_manager;
1714 test_io_backing_manager = unix_io_manager;
1715 } else
1716 #endif
1717 io_ptr = unix_io_manager;
1718 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1719 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1720 fs->blocksize, io_ptr, &jfs);
1721 if (retval) {
1722 com_err(program_name, retval,
1723 _("\n\twhile trying to open journal on %s\n"),
1724 journal_device);
1725 goto err;
1727 printf(_("Creating journal on device %s: "),
1728 journal_device);
1729 fflush(stdout);
1731 retval = ext2fs_add_journal_device(fs, jfs);
1732 ext2fs_close_free(&jfs);
1733 if (retval) {
1734 com_err(program_name, retval,
1735 _("while adding filesystem to journal on %s"),
1736 journal_device);
1737 goto err;
1739 fputs(_("done\n"), stdout);
1740 } else if (journal_size) {
1741 fputs(_("Creating journal inode: "), stdout);
1742 fflush(stdout);
1743 figure_journal_size(&jparams, journal_size, journal_fc_size, fs);
1745 if (journal_location_string)
1746 journal_location =
1747 parse_num_blocks2(journal_location_string,
1748 fs->super->s_log_block_size);
1749 retval = ext2fs_add_journal_inode3(fs, &jparams,
1750 journal_location,
1751 journal_flags);
1752 if (retval) {
1753 fprintf(stderr, "\n");
1754 com_err(program_name, retval, "%s",
1755 _("\n\twhile trying to create journal file"));
1756 return retval;
1758 fs->super->s_overhead_clusters += EXT2FS_NUM_B2C(fs,
1759 jparams.num_journal_blocks + jparams.num_fc_blocks);
1760 ext2fs_mark_super_dirty(fs);
1761 fputs(_("done\n"), stdout);
1764 * If the filesystem wasn't mounted, we need to force
1765 * the block group descriptors out.
1767 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1768 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1770 print_check_message(fs->super->s_max_mnt_count,
1771 fs->super->s_checkinterval);
1772 return 0;
1774 err:
1775 free(journal_device);
1776 return 1;
1779 static int handle_quota_options(ext2_filsys fs)
1781 errcode_t retval;
1782 quota_ctx_t qctx;
1783 ext2_ino_t qf_ino;
1784 enum quota_type qtype;
1785 unsigned int qtype_bits = 0;
1786 int need_dirty = 0;
1788 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++)
1789 if (quota_enable[qtype] != 0)
1790 break;
1791 if (qtype == MAXQUOTAS)
1792 /* Nothing to do. */
1793 return 0;
1795 if (quota_enable[PRJQUOTA] == QOPT_ENABLE &&
1796 fs->super->s_inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
1797 fprintf(stderr, _("Cannot enable project quota; "
1798 "inode size too small.\n"));
1799 return 1;
1802 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
1803 if (quota_enable[qtype] == QOPT_ENABLE)
1804 qtype_bits |= 1 << qtype;
1807 retval = quota_init_context(&qctx, fs, qtype_bits);
1808 if (retval) {
1809 com_err(program_name, retval,
1810 _("while initializing quota context in support library"));
1811 return 1;
1814 if (qtype_bits)
1815 quota_compute_usage(qctx);
1817 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
1818 if (quota_enable[qtype] == QOPT_ENABLE &&
1819 *quota_sb_inump(fs->super, qtype) == 0) {
1820 if ((qf_ino = quota_file_exists(fs, qtype)) > 0) {
1821 retval = quota_read_all_dquots(qctx, qf_ino,
1822 qtype,
1823 QREAD_LIMITS);
1824 if (retval) {
1825 com_err(program_name, retval,
1826 _("while updating quota limits (%d)"),
1827 qtype);
1828 quota_errout:
1829 quota_release_context(&qctx);
1830 return 1;
1833 retval = quota_write_inode(qctx, 1 << qtype);
1834 if (retval) {
1835 com_err(program_name, retval,
1836 _("while writing quota file (%d)"),
1837 qtype);
1838 goto quota_errout;
1840 /* Enable Quota feature if one of quota enabled */
1841 if (!ext2fs_has_feature_quota(fs->super)) {
1842 ext2fs_set_feature_quota(fs->super);
1843 need_dirty = 1;
1845 if (qtype == PRJQUOTA &&
1846 !ext2fs_has_feature_project(fs->super)) {
1847 ext2fs_set_feature_project(fs->super);
1848 need_dirty = 1;
1850 } else if (quota_enable[qtype] == QOPT_DISABLE) {
1851 retval = quota_remove_inode(fs, qtype);
1852 if (retval) {
1853 com_err(program_name, retval,
1854 _("while removing quota file (%d)"),
1855 qtype);
1856 goto quota_errout;
1858 if (qtype == PRJQUOTA) {
1859 ext2fs_clear_feature_project(fs->super);
1860 need_dirty = 1;
1865 quota_release_context(&qctx);
1866 /* Clear Quota feature if all quota types disabled. */
1867 if (!qtype_bits) {
1868 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++)
1869 if (*quota_sb_inump(fs->super, qtype))
1870 break;
1871 if (qtype == MAXQUOTAS) {
1872 ext2fs_clear_feature_quota(fs->super);
1873 need_dirty = 1;
1877 if (need_dirty)
1878 ext2fs_mark_super_dirty(fs);
1879 return 0;
1882 static int option_handle_function(char *token)
1884 if (strncmp(token, "usr", 3) == 0) {
1885 quota_enable[USRQUOTA] = QOPT_ENABLE;
1886 } else if (strncmp(token, "^usr", 4) == 0) {
1887 quota_enable[USRQUOTA] = QOPT_DISABLE;
1888 } else if (strncmp(token, "grp", 3) == 0) {
1889 quota_enable[GRPQUOTA] = QOPT_ENABLE;
1890 } else if (strncmp(token, "^grp", 4) == 0) {
1891 quota_enable[GRPQUOTA] = QOPT_DISABLE;
1892 } else if (strncmp(token, "prj", 3) == 0) {
1893 quota_enable[PRJQUOTA] = QOPT_ENABLE;
1894 } else if (strncmp(token, "^prj", 4) == 0) {
1895 quota_enable[PRJQUOTA] = QOPT_DISABLE;
1896 } else {
1897 fputs(_("\nBad quota options specified.\n\n"
1898 "Following valid quota options are available "
1899 "(pass by separating with comma):\n"
1900 "\t[^]usr[quota]\n"
1901 "\t[^]grp[quota]\n"
1902 "\t[^]prj[quota]\n"
1903 "\n\n"), stderr);
1904 return 1;
1906 return 0;
1909 static void parse_e2label_options(int argc, char ** argv)
1911 if ((argc < 2) || (argc > 3)) {
1912 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1913 exit(1);
1915 io_options = strchr(argv[1], '?');
1916 if (io_options)
1917 *io_options++ = 0;
1918 device_name = get_devname(NULL, argv[1], NULL);
1919 if (!device_name) {
1920 com_err("e2label", 0, _("Unable to resolve '%s'"),
1921 argv[1]);
1922 exit(1);
1924 open_flag = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SUPER_ONLY;
1925 if (argc == 3) {
1926 open_flag |= EXT2_FLAG_RW;
1927 L_flag = 1;
1928 new_label = argv[2];
1929 } else
1930 print_label++;
1933 static time_t parse_time(char *str)
1935 struct tm ts;
1937 if (strcmp(str, "now") == 0) {
1938 return (time(0));
1940 memset(&ts, 0, sizeof(ts));
1941 #ifdef HAVE_STRPTIME
1942 strptime(str, "%Y%m%d%H%M%S", &ts);
1943 #else
1944 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1945 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1946 ts.tm_year -= 1900;
1947 ts.tm_mon -= 1;
1948 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1949 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1950 ts.tm_min > 59 || ts.tm_sec > 61)
1951 ts.tm_mday = 0;
1952 #endif
1953 if (ts.tm_mday == 0) {
1954 com_err(program_name, 0,
1955 _("Couldn't parse date/time specifier: %s"),
1956 str);
1957 usage();
1959 ts.tm_isdst = -1;
1960 return (mktime(&ts));
1963 static void parse_tune2fs_options(int argc, char **argv)
1965 int c;
1966 char *tmp;
1967 struct group *gr;
1968 struct passwd *pw;
1969 int ret;
1970 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:z:Q:";
1972 open_flag = 0;
1973 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1974 while ((c = getopt(argc, argv, optstring)) != EOF)
1975 switch (c) {
1976 case 'c':
1977 open_flag = EXT2_FLAG_RW;
1978 c_flag = 1;
1979 if (strcmp(optarg, "random") == 0) {
1980 max_mount_count = 65536;
1981 break;
1983 max_mount_count = strtol(optarg, &tmp, 0);
1984 if (*tmp || max_mount_count > 16000 ||
1985 max_mount_count < -16000) {
1986 com_err(program_name, 0,
1987 _("bad mounts count - %s"),
1988 optarg);
1989 usage();
1991 if (max_mount_count == 0)
1992 max_mount_count = -1;
1993 break;
1994 case 'C':
1995 mount_count = strtoul(optarg, &tmp, 0);
1996 if (*tmp || mount_count > 16000) {
1997 com_err(program_name, 0,
1998 _("bad mounts count - %s"),
1999 optarg);
2000 usage();
2002 C_flag = 1;
2003 open_flag = EXT2_FLAG_RW;
2004 break;
2005 case 'e':
2006 if (strcmp(optarg, "continue") == 0)
2007 errors = EXT2_ERRORS_CONTINUE;
2008 else if (strcmp(optarg, "remount-ro") == 0)
2009 errors = EXT2_ERRORS_RO;
2010 else if (strcmp(optarg, "panic") == 0)
2011 errors = EXT2_ERRORS_PANIC;
2012 else {
2013 com_err(program_name, 0,
2014 _("bad error behavior - %s"),
2015 optarg);
2016 usage();
2018 e_flag = 1;
2019 open_flag = EXT2_FLAG_RW;
2020 break;
2021 case 'E':
2022 extended_cmd = optarg;
2023 open_flag |= EXT2_FLAG_RW;
2024 break;
2025 case 'f': /* Force */
2026 f_flag++;
2027 break;
2028 case 'g':
2029 resgid = strtoul(optarg, &tmp, 0);
2030 if (*tmp) {
2031 gr = getgrnam(optarg);
2032 if (gr == NULL)
2033 tmp = optarg;
2034 else {
2035 resgid = gr->gr_gid;
2036 *tmp = 0;
2039 if (*tmp) {
2040 com_err(program_name, 0,
2041 _("bad gid/group name - %s"),
2042 optarg);
2043 usage();
2045 g_flag = 1;
2046 open_flag = EXT2_FLAG_RW;
2047 break;
2048 case 'i':
2049 interval = strtoul(optarg, &tmp, 0);
2050 switch (*tmp) {
2051 case 's':
2052 tmp++;
2053 break;
2054 case '\0':
2055 case 'd':
2056 case 'D': /* days */
2057 interval *= 86400;
2058 if (*tmp != '\0')
2059 tmp++;
2060 break;
2061 case 'm':
2062 case 'M': /* months! */
2063 interval *= 86400 * 30;
2064 tmp++;
2065 break;
2066 case 'w':
2067 case 'W': /* weeks */
2068 interval *= 86400 * 7;
2069 tmp++;
2070 break;
2072 if (*tmp) {
2073 com_err(program_name, 0,
2074 _("bad interval - %s"), optarg);
2075 usage();
2077 i_flag = 1;
2078 open_flag = EXT2_FLAG_RW;
2079 break;
2080 case 'j':
2081 if (!journal_size)
2082 journal_size = -1;
2083 open_flag = EXT2_FLAG_RW;
2084 break;
2085 case 'J':
2086 parse_journal_opts(optarg);
2087 open_flag = EXT2_FLAG_RW;
2088 break;
2089 case 'l':
2090 l_flag = 1;
2091 break;
2092 case 'L':
2093 new_label = optarg;
2094 L_flag = 1;
2095 open_flag |= EXT2_FLAG_RW |
2096 EXT2_FLAG_JOURNAL_DEV_OK;
2097 break;
2098 case 'm':
2099 reserved_ratio = strtod(optarg, &tmp);
2100 if (*tmp || reserved_ratio > 50 ||
2101 reserved_ratio < 0) {
2102 com_err(program_name, 0,
2103 _("bad reserved block ratio - %s"),
2104 optarg);
2105 usage();
2107 m_flag = 1;
2108 open_flag = EXT2_FLAG_RW;
2109 break;
2110 case 'M':
2111 new_last_mounted = optarg;
2112 M_flag = 1;
2113 open_flag = EXT2_FLAG_RW;
2114 break;
2115 case 'o':
2116 if (mntopts_cmd) {
2117 com_err(program_name, 0, "%s",
2118 _("-o may only be specified once"));
2119 usage();
2121 mntopts_cmd = optarg;
2122 open_flag = EXT2_FLAG_RW;
2123 break;
2124 case 'O':
2125 if (features_cmd) {
2126 com_err(program_name, 0, "%s",
2127 _("-O may only be specified once"));
2128 usage();
2130 features_cmd = optarg;
2131 open_flag = EXT2_FLAG_RW;
2132 break;
2133 case 'Q':
2134 Q_flag = 1;
2135 ret = parse_quota_opts(optarg, option_handle_function);
2136 if (ret)
2137 exit(1);
2138 open_flag = EXT2_FLAG_RW;
2139 break;
2140 case 'r':
2141 reserved_blocks = strtoul(optarg, &tmp, 0);
2142 if (*tmp) {
2143 com_err(program_name, 0,
2144 _("bad reserved blocks count - %s"),
2145 optarg);
2146 usage();
2148 r_flag = 1;
2149 open_flag = EXT2_FLAG_RW;
2150 break;
2151 case 's': /* Deprecated */
2152 s_flag = atoi(optarg);
2153 open_flag = EXT2_FLAG_RW;
2154 break;
2155 case 'T':
2156 T_flag = 1;
2157 last_check_time = parse_time(optarg);
2158 open_flag = EXT2_FLAG_RW;
2159 break;
2160 case 'u':
2161 resuid = strtoul(optarg, &tmp, 0);
2162 if (*tmp) {
2163 pw = getpwnam(optarg);
2164 if (pw == NULL)
2165 tmp = optarg;
2166 else {
2167 resuid = pw->pw_uid;
2168 *tmp = 0;
2171 if (*tmp) {
2172 com_err(program_name, 0,
2173 _("bad uid/user name - %s"),
2174 optarg);
2175 usage();
2177 u_flag = 1;
2178 open_flag = EXT2_FLAG_RW;
2179 break;
2180 case 'U':
2181 requested_uuid = optarg;
2182 U_flag = 1;
2183 open_flag = EXT2_FLAG_RW |
2184 EXT2_FLAG_JOURNAL_DEV_OK;
2185 break;
2186 case 'I':
2187 new_inode_size = strtoul(optarg, &tmp, 0);
2188 if (*tmp) {
2189 com_err(program_name, 0,
2190 _("bad inode size - %s"),
2191 optarg);
2192 usage();
2194 if (!((new_inode_size &
2195 (new_inode_size - 1)) == 0)) {
2196 com_err(program_name, 0,
2197 _("Inode size must be a "
2198 "power of two- %s"),
2199 optarg);
2200 usage();
2202 open_flag = EXT2_FLAG_RW;
2203 I_flag = 1;
2204 break;
2205 case 'z':
2206 undo_file = optarg;
2207 break;
2208 default:
2209 usage();
2211 if (optind < argc - 1 || optind == argc)
2212 usage();
2213 if (!open_flag && !l_flag)
2214 usage();
2215 io_options = strchr(argv[optind], '?');
2216 if (io_options)
2217 *io_options++ = 0;
2218 device_name = get_devname(NULL, argv[optind], NULL);
2219 if (!device_name) {
2220 com_err(program_name, 0, _("Unable to resolve '%s'"),
2221 argv[optind]);
2222 exit(1);
2226 #ifdef CONFIG_BUILD_FINDFS
2227 void do_findfs(int argc, char **argv)
2229 char *dev;
2231 if ((argc != 2) ||
2232 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
2233 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
2234 exit(2);
2236 dev = blkid_get_devname(NULL, argv[1], NULL);
2237 if (!dev) {
2238 com_err("findfs", 0, _("Unable to resolve '%s'"),
2239 argv[1]);
2240 exit(1);
2242 puts(dev);
2243 exit(0);
2245 #endif
2247 static int parse_extended_opts(ext2_filsys fs, const char *opts)
2249 struct ext2_super_block *sb = fs->super;
2250 char *buf, *token, *next, *p, *arg;
2251 int len, hash_alg;
2252 int r_usage = 0;
2253 int encoding = 0;
2254 char *encoding_flags = NULL;
2256 len = strlen(opts);
2257 buf = malloc(len+1);
2258 if (!buf) {
2259 fprintf(stderr, "%s",
2260 _("Couldn't allocate memory to parse options!\n"));
2261 return 1;
2263 strcpy(buf, opts);
2264 for (token = buf; token && *token; token = next) {
2265 p = strchr(token, ',');
2266 next = 0;
2267 if (p) {
2268 *p = 0;
2269 next = p+1;
2271 arg = strchr(token, '=');
2272 if (arg) {
2273 *arg = 0;
2274 arg++;
2276 if (strcmp(token, "clear-mmp") == 0 ||
2277 strcmp(token, "clear_mmp") == 0) {
2278 clear_mmp = 1;
2279 } else if (strcmp(token, "mmp_update_interval") == 0) {
2280 unsigned long intv;
2281 if (!arg) {
2282 r_usage++;
2283 continue;
2285 intv = strtoul(arg, &p, 0);
2286 if (*p) {
2287 fprintf(stderr,
2288 _("Invalid mmp_update_interval: %s\n"),
2289 arg);
2290 r_usage++;
2291 continue;
2293 if (intv == 0) {
2294 intv = EXT4_MMP_UPDATE_INTERVAL;
2295 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
2296 fprintf(stderr,
2297 _("mmp_update_interval too big: %lu\n"),
2298 intv);
2299 r_usage++;
2300 continue;
2302 printf(P_("Setting multiple mount protection update "
2303 "interval to %lu second\n",
2304 "Setting multiple mount protection update "
2305 "interval to %lu seconds\n", intv),
2306 intv);
2307 sb->s_mmp_update_interval = intv;
2308 ext2fs_mark_super_dirty(fs);
2309 } else if (!strcmp(token, "force_fsck")) {
2310 sb->s_state |= EXT2_ERROR_FS;
2311 printf(_("Setting filesystem error flag to force fsck.\n"));
2312 ext2fs_mark_super_dirty(fs);
2313 } else if (!strcmp(token, "test_fs")) {
2314 sb->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2315 printf("Setting test filesystem flag\n");
2316 ext2fs_mark_super_dirty(fs);
2317 } else if (!strcmp(token, "^test_fs")) {
2318 sb->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
2319 printf("Clearing test filesystem flag\n");
2320 ext2fs_mark_super_dirty(fs);
2321 } else if (strcmp(token, "stride") == 0) {
2322 if (!arg) {
2323 r_usage++;
2324 continue;
2326 stride = strtoul(arg, &p, 0);
2327 if (*p) {
2328 fprintf(stderr,
2329 _("Invalid RAID stride: %s\n"),
2330 arg);
2331 r_usage++;
2332 continue;
2334 stride_set = 1;
2335 } else if (strcmp(token, "stripe-width") == 0 ||
2336 strcmp(token, "stripe_width") == 0) {
2337 if (!arg) {
2338 r_usage++;
2339 continue;
2341 stripe_width = strtoul(arg, &p, 0);
2342 if (*p) {
2343 fprintf(stderr,
2344 _("Invalid RAID stripe-width: %s\n"),
2345 arg);
2346 r_usage++;
2347 continue;
2349 stripe_width_set = 1;
2350 } else if (strcmp(token, "hash_alg") == 0 ||
2351 strcmp(token, "hash-alg") == 0) {
2352 if (!arg) {
2353 r_usage++;
2354 continue;
2356 hash_alg = e2p_string2hash(arg);
2357 if (hash_alg < 0) {
2358 fprintf(stderr,
2359 _("Invalid hash algorithm: %s\n"),
2360 arg);
2361 r_usage++;
2362 continue;
2364 sb->s_def_hash_version = hash_alg;
2365 printf(_("Setting default hash algorithm "
2366 "to %s (%d)\n"),
2367 arg, hash_alg);
2368 ext2fs_mark_super_dirty(fs);
2369 } else if (!strcmp(token, "mount_opts")) {
2370 if (!arg) {
2371 r_usage++;
2372 continue;
2374 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
2375 fprintf(stderr,
2376 "Extended mount options too long\n");
2377 continue;
2379 ext_mount_opts = strdup(arg);
2380 } else if (!strcmp(token, "encoding")) {
2381 if (!arg) {
2382 r_usage++;
2383 continue;
2385 if (mount_flags & EXT2_MF_MOUNTED) {
2386 fputs(_("The casefold feature may only be enabled when "
2387 "the filesystem is unmounted.\n"), stderr);
2388 r_usage++;
2389 continue;
2391 if (ext2fs_has_feature_casefold(sb) && !enabling_casefold) {
2392 fprintf(stderr, _("Cannot alter existing encoding\n"));
2393 r_usage++;
2394 continue;
2396 encoding = e2p_str2encoding(arg);
2397 if (encoding < 0) {
2398 fprintf(stderr, _("Invalid encoding: %s\n"), arg);
2399 r_usage++;
2400 continue;
2402 enabling_casefold = 1;
2403 sb->s_encoding = encoding;
2404 printf(_("Setting encoding to '%s'\n"), arg);
2405 sb->s_encoding_flags =
2406 e2p_get_encoding_flags(sb->s_encoding);
2407 } else if (!strcmp(token, "encoding_flags")) {
2408 if (!arg) {
2409 r_usage++;
2410 continue;
2412 encoding_flags = arg;
2413 } else if (!strcmp(token, "orphan_file_size")) {
2414 if (!arg) {
2415 r_usage++;
2416 continue;
2418 orphan_file_blocks = parse_num_blocks2(arg,
2419 fs->super->s_log_block_size);
2421 if (orphan_file_blocks < 1) {
2422 fprintf(stderr,
2423 _("Invalid size of orphan file %s\n"),
2424 arg);
2425 r_usage++;
2426 continue;
2428 } else
2429 r_usage++;
2432 if (encoding > 0 && !r_usage) {
2433 sb->s_encoding_flags =
2434 e2p_get_encoding_flags(sb->s_encoding);
2436 if (encoding_flags &&
2437 e2p_str2encoding_flags(sb->s_encoding, encoding_flags,
2438 &sb->s_encoding_flags)) {
2439 fprintf(stderr, _("error: Invalid encoding flag: %s\n"),
2440 encoding_flags);
2441 r_usage++;
2442 } else if (encoding_flags)
2443 printf(_("Setting encoding_flags to '%s'\n"),
2444 encoding_flags);
2445 ext2fs_set_feature_casefold(sb);
2446 ext2fs_mark_super_dirty(fs);
2447 } else if (encoding_flags && !r_usage) {
2448 fprintf(stderr, _("error: An encoding must be explicitly "
2449 "specified when passing encoding-flags\n"));
2450 r_usage++;
2452 if (r_usage) {
2453 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
2454 "Extended options are separated by commas, "
2455 "and may take an argument which\n"
2456 "\tis set off by an equals ('=') sign.\n\n"
2457 "Valid extended options are:\n"
2458 "\tclear_mmp\n"
2459 "\thash_alg=<hash algorithm>\n"
2460 "\tmount_opts=<extended default mount options>\n"
2461 "\tmmp_update_interval=<mmp update interval in seconds>\n"
2462 "\tstride=<RAID per-disk chunk size in blocks>\n"
2463 "\tstripe_width=<RAID stride*data disks in blocks>\n"
2464 "\tforce_fsck\n"
2465 "\ttest_fs\n"
2466 "\t^test_fs\n"
2467 "\tencoding=<encoding>\n"
2468 "\tencoding_flags=<flags>\n"));
2469 free(buf);
2470 return 1;
2472 free(buf);
2474 return 0;
2478 * Fill in the block bitmap bmap with the information regarding the
2479 * blocks to be moved
2481 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
2482 ext2fs_block_bitmap bmap)
2484 dgrp_t i;
2485 int retval;
2486 ext2_badblocks_list bb_list = 0;
2487 blk64_t j, needed_blocks = 0;
2488 blk64_t start_blk, end_blk;
2490 retval = ext2fs_read_bb_inode(fs, &bb_list);
2491 if (retval)
2492 return retval;
2494 for (i = 0; i < fs->group_desc_count; i++) {
2495 start_blk = ext2fs_inode_table_loc(fs, i) +
2496 fs->inode_blocks_per_group;
2498 end_blk = ext2fs_inode_table_loc(fs, i) +
2499 new_ino_blks_per_grp;
2501 for (j = start_blk; j < end_blk; j++) {
2502 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
2504 * IF the block is a bad block we fail
2506 if (ext2fs_badblocks_list_test(bb_list, j)) {
2507 ext2fs_badblocks_list_free(bb_list);
2508 return ENOSPC;
2511 ext2fs_mark_block_bitmap2(bmap, j);
2512 } else {
2514 * We are going to use this block for
2515 * inode table. So mark them used.
2517 ext2fs_mark_block_bitmap2(fs->block_map, j);
2520 needed_blocks += end_blk - start_blk;
2523 ext2fs_badblocks_list_free(bb_list);
2524 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
2525 return ENOSPC;
2527 return 0;
2530 static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
2532 dgrp_t group;
2533 group = ext2fs_group_of_blk2(fs, blk);
2534 if (ext2fs_block_bitmap_loc(fs, group) == blk)
2535 return 1;
2536 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
2537 return 1;
2538 return 0;
2541 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
2543 blk64_t start_blk, end_blk;
2544 start_blk = fs->super->s_first_data_block +
2545 EXT2_GROUPS_TO_BLOCKS(fs->super, group);
2547 * We cannot get new block beyond end_blk for for the last block group
2548 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
2550 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
2551 if (blk >= start_blk && blk <= end_blk)
2552 return 1;
2553 return 0;
2556 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
2559 char *buf;
2560 dgrp_t group = 0;
2561 errcode_t retval;
2562 int meta_data = 0;
2563 blk64_t blk, new_blk, goal;
2564 struct blk_move *bmv;
2566 retval = ext2fs_get_mem(fs->blocksize, &buf);
2567 if (retval)
2568 return retval;
2570 for (new_blk = blk = fs->super->s_first_data_block;
2571 blk < ext2fs_blocks_count(fs->super); blk++) {
2572 if (!ext2fs_test_block_bitmap2(bmap, blk))
2573 continue;
2575 if (ext2fs_is_meta_block(fs, blk)) {
2577 * If the block is mapping a fs meta data block
2578 * like group desc/block bitmap/inode bitmap. We
2579 * should find a block in the same group and fix
2580 * the respective fs metadata pointers. Otherwise
2581 * fail
2583 group = ext2fs_group_of_blk2(fs, blk);
2584 goal = ext2fs_group_first_block2(fs, group);
2585 meta_data = 1;
2587 } else {
2588 goal = new_blk;
2590 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
2591 if (retval)
2592 goto err_out;
2594 /* new fs meta data block should be in the same group */
2595 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
2596 retval = ENOSPC;
2597 goto err_out;
2600 /* Mark this block as allocated */
2601 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
2603 /* Add it to block move list */
2604 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
2605 if (retval)
2606 goto err_out;
2608 bmv->old_loc = blk;
2609 bmv->new_loc = new_blk;
2611 list_add(&(bmv->list), &blk_move_list);
2613 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2614 if (retval)
2615 goto err_out;
2617 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2618 if (retval)
2619 goto err_out;
2622 err_out:
2623 ext2fs_free_mem(&buf);
2624 return retval;
2627 static blk64_t translate_block(blk64_t blk)
2629 struct list_head *entry;
2630 struct blk_move *bmv;
2632 list_for_each(entry, &blk_move_list) {
2633 bmv = list_entry(entry, struct blk_move, list);
2634 if (bmv->old_loc == blk)
2635 return bmv->new_loc;
2638 return 0;
2641 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2642 blk64_t *block_nr,
2643 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2644 blk64_t ref_block EXT2FS_ATTR((unused)),
2645 int ref_offset EXT2FS_ATTR((unused)),
2646 void *priv_data)
2648 int ret = 0;
2649 blk64_t new_blk;
2650 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2652 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2653 return 0;
2654 new_blk = translate_block(*block_nr);
2655 if (new_blk) {
2656 *block_nr = new_blk;
2658 * This will force the ext2fs_write_inode in the iterator
2660 ret |= BLOCK_CHANGED;
2663 return ret;
2666 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2668 errcode_t retval = 0;
2669 ext2_ino_t ino;
2670 blk64_t blk;
2671 char *block_buf = 0;
2672 struct ext2_inode inode;
2673 ext2_inode_scan scan = NULL;
2675 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2676 if (retval)
2677 return retval;
2679 retval = ext2fs_open_inode_scan(fs, 0, &scan);
2680 if (retval)
2681 goto err_out;
2683 while (1) {
2684 retval = ext2fs_get_next_inode(scan, &ino, &inode);
2685 if (retval)
2686 goto err_out;
2688 if (!ino)
2689 break;
2691 if (inode.i_links_count == 0)
2692 continue; /* inode not in use */
2694 /* FIXME!!
2695 * If we end up modifying the journal inode
2696 * the sb->s_jnl_blocks will differ. But a
2697 * subsequent e2fsck fixes that.
2698 * Do we need to fix this ??
2701 if (ext2fs_file_acl_block(fs, &inode) &&
2702 ext2fs_test_block_bitmap2(bmap,
2703 ext2fs_file_acl_block(fs, &inode))) {
2704 blk = translate_block(ext2fs_file_acl_block(fs,
2705 &inode));
2706 if (!blk)
2707 continue;
2709 ext2fs_file_acl_block_set(fs, &inode, blk);
2712 * Write the inode to disk so that inode table
2713 * resizing can work
2715 retval = ext2fs_write_inode(fs, ino, &inode);
2716 if (retval)
2717 goto err_out;
2720 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2721 continue;
2723 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2724 process_block, bmap);
2725 if (retval)
2726 goto err_out;
2730 err_out:
2731 ext2fs_free_mem(&block_buf);
2732 ext2fs_close_inode_scan(scan);
2734 return retval;
2738 * We need to scan for inode and block bitmaps that may need to be
2739 * moved. This can take place if the filesystem was formatted for
2740 * RAID arrays using the mke2fs's extended option "stride".
2742 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2744 dgrp_t i;
2745 blk64_t blk, new_blk;
2747 for (i = 0; i < fs->group_desc_count; i++) {
2748 blk = ext2fs_block_bitmap_loc(fs, i);
2749 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2750 new_blk = translate_block(blk);
2751 if (!new_blk)
2752 continue;
2753 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2756 blk = ext2fs_inode_bitmap_loc(fs, i);
2757 if (ext2fs_test_block_bitmap2(bmap, blk)) {
2758 new_blk = translate_block(blk);
2759 if (!new_blk)
2760 continue;
2761 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2764 return 0;
2767 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2769 dgrp_t i;
2770 blk64_t blk;
2771 errcode_t retval;
2772 int new_ino_blks_per_grp;
2773 unsigned int j;
2774 char *old_itable = NULL, *new_itable = NULL;
2775 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2776 unsigned long old_ino_size;
2777 int old_itable_size, new_itable_size;
2779 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2780 old_ino_size = EXT2_INODE_SIZE(fs->super);
2782 new_ino_blks_per_grp = ext2fs_div_ceil(
2783 EXT2_INODES_PER_GROUP(fs->super) *
2784 new_ino_size,
2785 fs->blocksize);
2787 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2789 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2790 if (retval)
2791 return retval;
2793 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2794 if (retval)
2795 goto err_out;
2797 tmp_old_itable = old_itable;
2798 tmp_new_itable = new_itable;
2800 for (i = 0; i < fs->group_desc_count; i++) {
2801 blk = ext2fs_inode_table_loc(fs, i);
2802 retval = io_channel_read_blk64(fs->io, blk,
2803 fs->inode_blocks_per_group, old_itable);
2804 if (retval)
2805 goto err_out;
2807 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2808 memcpy(new_itable, old_itable, old_ino_size);
2810 memset(new_itable+old_ino_size, 0,
2811 new_ino_size - old_ino_size);
2813 new_itable += new_ino_size;
2814 old_itable += old_ino_size;
2817 /* reset the pointer */
2818 old_itable = tmp_old_itable;
2819 new_itable = tmp_new_itable;
2821 retval = io_channel_write_blk64(fs->io, blk,
2822 new_ino_blks_per_grp, new_itable);
2823 if (retval)
2824 goto err_out;
2827 /* Update the meta data */
2828 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2829 ext2fs_free_inode_cache(fs->icache);
2830 fs->icache = 0;
2831 fs->super->s_inode_size = new_ino_size;
2833 err_out:
2834 if (old_itable)
2835 ext2fs_free_mem(&old_itable);
2837 if (new_itable)
2838 ext2fs_free_mem(&new_itable);
2840 return retval;
2844 #define list_for_each_safe(pos, pnext, head) \
2845 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2846 pos = pnext, pnext = pos->next)
2848 static void free_blk_move_list(void)
2850 struct list_head *entry, *tmp;
2851 struct blk_move *bmv;
2853 list_for_each_safe(entry, tmp, &blk_move_list) {
2854 bmv = list_entry(entry, struct blk_move, list);
2855 list_del(entry);
2856 ext2fs_free_mem(&bmv);
2858 return;
2861 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2863 errcode_t retval;
2864 int new_ino_blks_per_grp;
2865 ext2fs_block_bitmap bmap;
2867 ext2fs_update_dynamic_rev(fs);
2868 retval = ext2fs_read_inode_bitmap(fs);
2869 if (retval) {
2870 fputs(_("Failed to read inode bitmap\n"), stderr);
2871 return retval;
2873 retval = ext2fs_read_block_bitmap(fs);
2874 if (retval) {
2875 fputs(_("Failed to read block bitmap\n"), stderr);
2876 return retval;
2878 INIT_LIST_HEAD(&blk_move_list);
2881 new_ino_blks_per_grp = ext2fs_div_ceil(
2882 EXT2_INODES_PER_GROUP(fs->super)*
2883 new_size,
2884 fs->blocksize);
2886 /* We may change the file system.
2887 * Mark the file system as invalid so that
2888 * the user is prompted to run fsck.
2890 fs->super->s_state &= ~EXT2_VALID_FS;
2892 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2893 &bmap);
2894 if (retval) {
2895 fputs(_("Failed to allocate block bitmap when "
2896 "increasing inode size\n"), stderr);
2897 return retval;
2899 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2900 if (retval) {
2901 fputs(_("Not enough space to increase inode size \n"), stderr);
2902 goto err_out;
2904 retval = move_block(fs, bmap);
2905 if (retval) {
2906 fputs(_("Failed to relocate blocks during inode resize \n"),
2907 stderr);
2908 goto err_out;
2910 retval = inode_scan_and_fix(fs, bmap);
2911 if (retval)
2912 goto err_out_undo;
2914 retval = group_desc_scan_and_fix(fs, bmap);
2915 if (retval)
2916 goto err_out_undo;
2918 retval = expand_inode_table(fs, new_size);
2919 if (retval)
2920 goto err_out_undo;
2922 ext2fs_calculate_summary_stats(fs, 1 /* super only */);
2924 fs->super->s_state |= EXT2_VALID_FS;
2925 /* mark super block and block bitmap as dirty */
2926 ext2fs_mark_super_dirty(fs);
2927 ext2fs_mark_bb_dirty(fs);
2929 err_out:
2930 free_blk_move_list();
2931 ext2fs_free_block_bitmap(bmap);
2933 return retval;
2935 err_out_undo:
2936 free_blk_move_list();
2937 ext2fs_free_block_bitmap(bmap);
2938 fputs(_("Error in resizing the inode size.\n"
2939 "Run e2undo to undo the "
2940 "file system changes. \n"), stderr);
2942 return retval;
2945 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2947 errcode_t retval = 0;
2948 const char *tdb_dir;
2949 char *tdb_file = NULL;
2950 char *dev_name, *tmp_name;
2952 /* (re)open a specific undo file */
2953 if (undo_file && undo_file[0] != 0) {
2954 retval = set_undo_io_backing_manager(*io_ptr);
2955 if (retval)
2956 goto err;
2957 *io_ptr = undo_io_manager;
2958 retval = set_undo_io_backup_file(undo_file);
2959 if (retval)
2960 goto err;
2961 printf(_("Overwriting existing filesystem; this can be undone "
2962 "using the command:\n"
2963 " e2undo %s %s\n\n"),
2964 undo_file, name);
2965 return retval;
2969 * Configuration via a conf file would be
2970 * nice
2972 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2973 if (!tdb_dir)
2974 tdb_dir = "/var/lib/e2fsprogs";
2976 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2977 access(tdb_dir, W_OK))
2978 return 0;
2980 tmp_name = strdup(name);
2981 if (!tmp_name)
2982 goto errout;
2983 dev_name = basename(tmp_name);
2984 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2985 if (!tdb_file) {
2986 free(tmp_name);
2987 goto errout;
2989 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2990 free(tmp_name);
2992 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2993 retval = errno;
2994 com_err(program_name, retval,
2995 _("while trying to delete %s"), tdb_file);
2996 goto errout;
2999 retval = set_undo_io_backing_manager(*io_ptr);
3000 if (retval)
3001 goto errout;
3002 *io_ptr = undo_io_manager;
3003 retval = set_undo_io_backup_file(tdb_file);
3004 if (retval)
3005 goto errout;
3006 printf(_("Overwriting existing filesystem; this can be undone "
3007 "using the command:\n"
3008 " e2undo %s %s\n\n"),
3009 tdb_file, name);
3011 free(tdb_file);
3012 return 0;
3013 errout:
3014 free(tdb_file);
3015 err:
3016 com_err("tune2fs", retval, "while trying to setup undo file\n");
3017 return retval;
3020 static int
3021 fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
3023 int retval, nr_users, start;
3024 journal_superblock_t *jsb;
3025 ext2_filsys jfs;
3026 __u8 *j_uuid;
3027 char *journal_path;
3028 char uuid[UUID_STR_SIZE];
3029 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
3031 if (!ext2fs_has_feature_journal(sb) || uuid_is_null(sb->s_journal_uuid))
3032 return 0;
3034 uuid_unparse(sb->s_journal_uuid, uuid);
3035 journal_path = blkid_get_devname(NULL, "UUID", uuid);
3036 if (!journal_path)
3037 return 0;
3039 retval = ext2fs_open2(journal_path, io_options,
3040 EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
3041 0, 0, unix_io_manager, &jfs);
3042 if (retval) {
3043 com_err(program_name, retval,
3044 _("while trying to open %s"),
3045 journal_path);
3046 return retval;
3049 retval = get_journal_sb(jfs, buf);
3050 if (retval != 0) {
3051 if (retval == EXT2_ET_UNSUPP_FEATURE)
3052 fprintf(stderr, _("%s is not a journal device.\n"),
3053 journal_path);
3054 return retval;
3057 jsb = (journal_superblock_t *) buf;
3058 /* Find the filesystem UUID */
3059 nr_users = ntohl(jsb->s_nr_users);
3060 if (nr_users > JBD2_USERS_MAX) {
3061 fprintf(stderr, _("Journal superblock is corrupted, nr_users\n"
3062 "is too high (%d).\n"), nr_users);
3063 return EXT2_ET_CORRUPT_JOURNAL_SB;
3066 j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
3067 if (j_uuid == NULL) {
3068 fputs(_("Filesystem's UUID not found on journal device.\n"),
3069 stderr);
3070 return EXT2_ET_LOAD_EXT_JOURNAL;
3073 memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
3075 start = ext2fs_journal_sb_start(jfs->blocksize);
3076 /* Write back the journal superblock */
3077 retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
3078 if (retval != 0) {
3079 com_err(program_name, retval,
3080 "while writing journal superblock.");
3081 return retval;
3084 ext2fs_close(jfs);
3086 return 0;
3090 * Use FS_IOC_SETFSLABEL or FS_IOC_GETFSLABEL to set/get file system label
3091 * Return: 0 on success
3092 * 1 on error
3093 * -1 when the old method should be used
3095 static int handle_fslabel(int setlabel)
3097 #ifdef __linux__
3098 errcode_t ret;
3099 int mnt_flags, fd;
3100 char label[FSLABEL_MAX];
3101 unsigned int maxlen = FSLABEL_MAX - 1;
3102 char mntpt[PATH_MAX + 1];
3104 ret = ext2fs_check_mount_point(device_name, &mnt_flags,
3105 mntpt, sizeof(mntpt));
3106 if (ret)
3107 return -1;
3109 if (!(mnt_flags & EXT2_MF_MOUNTED) ||
3110 (setlabel && (mnt_flags & EXT2_MF_READONLY)))
3111 return -1;
3113 if (!mntpt[0])
3114 return -1;
3116 fd = open(mntpt, O_RDONLY);
3117 if (fd < 0)
3118 return -1;
3120 /* Get fs label */
3121 if (!setlabel) {
3122 if (ioctl(fd, FS_IOC_GETFSLABEL, &label)) {
3123 close(fd);
3124 if (errno == ENOTTY)
3125 return -1;
3126 com_err(mntpt, errno, _("while trying to get fs label"));
3127 return 1;
3129 close(fd);
3130 printf("%.*s\n", EXT2_LEN_STR(label));
3131 return 0;
3134 /* If it's extN file system, truncate the label to appropriate size */
3135 if (mnt_flags & EXT2_MF_EXTFS)
3136 maxlen = EXT2_LABEL_LEN;
3137 if (strlen(new_label) > maxlen) {
3138 fputs(_("Warning: label too long, truncating.\n"),
3139 stderr);
3140 new_label[maxlen] = '\0';
3143 /* Set fs label */
3144 if (ioctl(fd, FS_IOC_SETFSLABEL, new_label)) {
3145 close(fd);
3146 if (errno == ENOTTY)
3147 return -1;
3148 com_err(mntpt, errno, _("while trying to set fs label"));
3149 return 1;
3151 close(fd);
3152 return 0;
3153 #else
3154 return -1;
3155 #endif
3158 #ifndef BUILD_AS_LIB
3159 int main(int argc, char **argv)
3160 #else
3161 int tune2fs_main(int argc, char **argv)
3162 #endif /* BUILD_AS_LIB */
3164 errcode_t retval;
3165 ext2_filsys fs;
3166 struct ext2_super_block *sb;
3167 io_manager io_ptr, io_ptr_orig = NULL;
3168 int rc = 0;
3169 char default_undo_file[1] = { 0 };
3170 char mntpt[PATH_MAX + 1] = { 0 };
3171 int fd = -1;
3172 struct fsuuid *fsuuid = NULL;
3174 #ifdef ENABLE_NLS
3175 setlocale(LC_MESSAGES, "");
3176 setlocale(LC_CTYPE, "");
3177 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
3178 textdomain(NLS_CAT_NAME);
3179 set_com_err_gettext(gettext);
3180 #endif
3181 if (argc && *argv)
3182 program_name = *argv;
3183 else
3184 usage();
3185 add_error_table(&et_ext2_error_table);
3187 #ifdef CONFIG_BUILD_FINDFS
3188 if (strcmp(get_progname(argv[0]), "findfs") == 0)
3189 do_findfs(argc, argv);
3190 #endif
3191 if (strcmp(get_progname(argv[0]), "e2label") == 0)
3192 parse_e2label_options(argc, argv);
3193 else
3194 parse_tune2fs_options(argc, argv);
3196 #ifdef CONFIG_TESTIO_DEBUG
3197 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
3198 io_ptr = test_io_manager;
3199 test_io_backing_manager = unix_io_manager;
3200 } else
3201 #endif
3202 io_ptr = unix_io_manager;
3205 * Try the get/set fs label using ioctls before we even attempt
3206 * to open the file system.
3208 if (L_flag || print_label) {
3209 rc = handle_fslabel(L_flag);
3210 if (rc != -1) {
3211 #ifndef BUILD_AS_LIB
3212 exit(rc);
3213 #endif
3214 return rc;
3216 rc = 0;
3219 retry_open:
3220 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
3221 open_flag |= EXT2_FLAG_SKIP_MMP;
3223 open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_THREADS |
3224 EXT2_FLAG_JOURNAL_DEV_OK;
3226 /* keep the filesystem struct around to dump MMP data */
3227 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
3229 retval = ext2fs_open2(device_name, io_options, open_flag,
3230 0, 0, io_ptr, &fs);
3231 if (retval) {
3232 com_err(program_name, retval,
3233 _("while trying to open %s"),
3234 device_name);
3235 if (retval == EXT2_ET_MMP_FSCK_ON ||
3236 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
3237 dump_mmp_msg(fs->mmp_buf,
3238 _("If you are sure the filesystem "
3239 "is not in use on any node, run:\n"
3240 "'tune2fs -f -E clear_mmp {device}'\n"));
3241 else if (retval == EXT2_ET_MMP_FAILED)
3242 dump_mmp_msg(fs->mmp_buf, NULL);
3243 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
3244 fprintf(stderr,
3245 _("MMP block magic is bad. Try to fix it by "
3246 "running:\n'e2fsck -f %s'\n"), device_name);
3247 else if (retval == EXT2_ET_BAD_MAGIC)
3248 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
3249 else if (retval != EXT2_ET_MMP_FAILED)
3250 fprintf(stderr, "%s",
3251 _("Couldn't find valid filesystem superblock.\n"));
3253 ext2fs_free(fs);
3254 exit(1);
3256 if (ext2fs_has_feature_journal_dev(fs->super)) {
3257 fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
3258 ext2fs_free(fs);
3259 exit(1);
3261 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
3263 if (I_flag) {
3265 * Check the inode size is right so we can issue an
3266 * error message and bail before setting up the tdb
3267 * file.
3269 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
3270 fprintf(stderr, _("The inode size is already %lu\n"),
3271 new_inode_size);
3272 rc = 1;
3273 goto closefs;
3275 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
3276 fprintf(stderr, "%s",
3277 _("Shrinking inode size is not supported\n"));
3278 rc = 1;
3279 goto closefs;
3281 if (new_inode_size > fs->blocksize) {
3282 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
3283 new_inode_size, fs->blocksize);
3284 rc = 1;
3285 goto closefs;
3287 rc = check_fsck_needed(fs,
3288 _("Resizing inodes could take some time."));
3289 if (rc)
3290 goto closefs;
3292 * If inode resize is requested use the
3293 * Undo I/O manager
3295 undo_file = default_undo_file;
3298 /* Set up an undo file */
3299 if (undo_file && io_ptr_orig == NULL) {
3300 io_ptr_orig = io_ptr;
3301 retval = tune2fs_setup_tdb(device_name, &io_ptr);
3302 if (retval) {
3303 rc = 1;
3304 goto closefs;
3306 if (io_ptr != io_ptr_orig) {
3307 ext2fs_close_free(&fs);
3308 goto retry_open;
3312 sb = fs->super;
3313 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3315 if (print_label) {
3316 /* For e2label emulation */
3317 printf("%.*s\n", EXT2_LEN_STR(sb->s_volume_name));
3318 remove_error_table(&et_ext2_error_table);
3319 goto closefs;
3322 retval = ext2fs_check_mount_point(device_name, &mount_flags,
3323 mntpt, sizeof(mntpt));
3324 if (retval) {
3325 com_err("ext2fs_check_mount_point", retval,
3326 _("while determining whether %s is mounted."),
3327 device_name);
3328 rc = 1;
3329 goto closefs;
3332 #ifdef NO_RECOVERY
3333 /* Warn if file system needs recovery and it is opened for writing. */
3334 if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & EXT2_MF_MOUNTED) &&
3335 (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
3336 (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER)) {
3337 fprintf(stderr,
3338 _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
3339 "\te2fsck -E journal_only %s\n\n"
3340 "then rerun this command. Otherwise, any changes made may be overwritten\n"
3341 "by journal recovery.\n"), device_name);
3343 #else
3344 /* Recover the journal if possible. */
3345 if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & (EXT2_MF_BUSY | EXT2_MF_MOUNTED)) &&
3346 ext2fs_has_feature_journal_needs_recovery(fs->super)) {
3347 printf(_("Recovering journal.\n"));
3348 retval = ext2fs_run_ext3_journal(&fs);
3349 if (retval) {
3350 com_err("tune2fs", retval,
3351 "while recovering journal.\n");
3352 printf(_("Please run e2fsck -fy %s.\n"), device_name);
3353 if (!fs)
3354 exit(1);
3355 rc = 1;
3356 goto closefs;
3358 sb = fs->super;
3360 #endif
3362 /* Normally we only need to write out the superblock */
3363 fs->flags |= EXT2_FLAG_SUPER_ONLY;
3365 if (c_flag) {
3366 if (max_mount_count == 65536)
3367 max_mount_count = EXT2_DFL_MAX_MNT_COUNT +
3368 (random() % EXT2_DFL_MAX_MNT_COUNT);
3369 sb->s_max_mnt_count = max_mount_count;
3370 ext2fs_mark_super_dirty(fs);
3371 printf(_("Setting maximal mount count to %d\n"),
3372 max_mount_count);
3374 if (C_flag) {
3375 sb->s_mnt_count = mount_count;
3376 ext2fs_mark_super_dirty(fs);
3377 printf(_("Setting current mount count to %d\n"), mount_count);
3379 if (e_flag) {
3380 sb->s_errors = errors;
3381 ext2fs_mark_super_dirty(fs);
3382 printf(_("Setting error behavior to %d\n"), errors);
3384 if (g_flag) {
3385 if (sb->s_def_resgid != resgid) {
3386 sb->s_def_resgid = resgid;
3387 ext2fs_mark_super_dirty(fs);
3388 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
3389 } else {
3390 printf(_("Reserved blocks gid already set to %lu\n"), resgid);
3393 if (i_flag) {
3394 if ((unsigned long long)interval >= (1ULL << 32)) {
3395 com_err(program_name, 0,
3396 _("interval between checks is too big (%lu)"),
3397 interval);
3398 rc = 1;
3399 goto closefs;
3401 sb->s_checkinterval = interval;
3402 ext2fs_mark_super_dirty(fs);
3403 printf(_("Setting interval between checks to %lu seconds\n"),
3404 interval);
3406 if (m_flag) {
3407 ext2fs_r_blocks_count_set(sb, reserved_ratio *
3408 ext2fs_blocks_count(sb) / 100.0);
3409 ext2fs_mark_super_dirty(fs);
3410 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
3411 reserved_ratio,
3412 (unsigned long long) ext2fs_r_blocks_count(sb));
3414 if (r_flag) {
3415 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
3416 com_err(program_name, 0,
3417 _("reserved blocks count is too big (%llu)"),
3418 (unsigned long long) reserved_blocks);
3419 rc = 1;
3420 goto closefs;
3422 ext2fs_r_blocks_count_set(sb, reserved_blocks);
3423 ext2fs_mark_super_dirty(fs);
3424 printf(_("Setting reserved blocks count to %llu\n"),
3425 (unsigned long long) reserved_blocks);
3427 if (s_flag == 1) {
3428 if (ext2fs_has_feature_sparse_super(sb)) {
3429 fputs(_("\nThe filesystem already has sparse "
3430 "superblocks.\n"), stderr);
3431 } else if (ext2fs_has_feature_meta_bg(sb)) {
3432 fputs(_("\nSetting the sparse superblock flag not "
3433 "supported\nfor filesystems with "
3434 "the meta_bg feature enabled.\n"),
3435 stderr);
3436 rc = 1;
3437 goto closefs;
3438 } else {
3439 ext2fs_set_feature_sparse_super(sb);
3440 sb->s_state &= ~EXT2_VALID_FS;
3441 ext2fs_mark_super_dirty(fs);
3442 printf(_("\nSparse superblock flag set. %s"),
3443 _(please_fsck));
3446 if (s_flag == 0) {
3447 fputs(_("\nClearing the sparse superblock flag not supported.\n"),
3448 stderr);
3449 rc = 1;
3450 goto closefs;
3452 if (T_flag) {
3453 ext2fs_set_tstamp(sb, s_lastcheck, last_check_time);
3454 ext2fs_mark_super_dirty(fs);
3455 printf(_("Setting time filesystem last checked to %s\n"),
3456 ctime(&last_check_time));
3458 if (u_flag) {
3459 sb->s_def_resuid = resuid;
3460 ext2fs_mark_super_dirty(fs);
3461 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
3463 if (L_flag) {
3464 if (strlen(new_label) > sizeof(sb->s_volume_name))
3465 fputs(_("Warning: label too long, truncating.\n"),
3466 stderr);
3467 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
3468 strncpy((char *)sb->s_volume_name, new_label,
3469 sizeof(sb->s_volume_name));
3470 ext2fs_mark_super_dirty(fs);
3472 if (M_flag) {
3473 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
3474 strncpy((char *)sb->s_last_mounted, new_last_mounted,
3475 sizeof(sb->s_last_mounted));
3476 ext2fs_mark_super_dirty(fs);
3478 if (mntopts_cmd) {
3479 rc = update_mntopts(fs, mntopts_cmd);
3480 if (rc)
3481 goto closefs;
3483 if (features_cmd) {
3484 rc = update_feature_set(fs, features_cmd);
3485 if (rc)
3486 goto closefs;
3488 if (extended_cmd) {
3489 rc = parse_extended_opts(fs, extended_cmd);
3490 if (rc)
3491 goto closefs;
3492 if (clear_mmp && !f_flag) {
3493 fputs(_("Error in using clear_mmp. "
3494 "It must be used with -f\n"),
3495 stderr);
3496 rc = 1;
3497 goto closefs;
3500 if (clear_mmp) {
3501 rc = ext2fs_mmp_clear(fs);
3502 goto closefs;
3504 if (journal_size || journal_device) {
3505 rc = add_journal(fs);
3506 if (rc)
3507 goto closefs;
3509 if (orphan_file_blocks) {
3510 errcode_t err;
3512 err = ext2fs_read_bitmaps(fs);
3513 if (err) {
3514 com_err(program_name, err, "%s",
3515 _("while loading bitmaps"));
3516 rc = 1;
3517 goto closefs;
3519 err = ext2fs_create_orphan_file(fs, orphan_file_blocks);
3520 if (err) {
3521 com_err(program_name, err, "%s",
3522 _("while creating orphan file"));
3523 rc = 1;
3524 goto closefs;
3528 if (Q_flag) {
3529 if (mount_flags & (EXT2_MF_BUSY | EXT2_MF_MOUNTED)) {
3530 fputs(_("The quota feature may only be changed when "
3531 "the filesystem is unmounted and not in use.\n"), stderr);
3532 rc = 1;
3533 goto closefs;
3535 rc = handle_quota_options(fs);
3536 if (rc)
3537 goto closefs;
3540 if (U_flag) {
3541 int set_csum = 0;
3542 dgrp_t i;
3543 char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
3544 __u8 old_uuid[UUID_SIZE];
3545 uuid_t new_uuid;
3546 errcode_t ret = -1;
3548 if (ext2fs_has_feature_stable_inodes(fs->super)) {
3549 fputs(_("Cannot change the UUID of this filesystem "
3550 "because it has the stable_inodes feature "
3551 "flag.\n"), stderr);
3552 exit(1);
3555 if (!ext2fs_has_feature_csum_seed(fs->super) &&
3556 (ext2fs_has_feature_metadata_csum(fs->super) ||
3557 ext2fs_has_feature_ea_inode(fs->super))) {
3558 rc = check_fsck_needed(fs,
3559 _("Setting the UUID on this "
3560 "filesystem could take some time."));
3561 if (rc)
3562 goto closefs;
3563 rewrite_checksums = REWRITE_ALL;
3566 if (ext2fs_has_group_desc_csum(fs)) {
3568 * Changing the UUID on a metadata_csum FS requires
3569 * rewriting all metadata, which can race with a
3570 * mounted fs. Don't allow that unless we're saving
3571 * the checksum seed.
3573 if ((mount_flags & EXT2_MF_MOUNTED) &&
3574 !ext2fs_has_feature_csum_seed(fs->super) &&
3575 ext2fs_has_feature_metadata_csum(fs->super)) {
3576 fputs(_("The UUID may only be "
3577 "changed when the filesystem is "
3578 "unmounted.\n"), stderr);
3579 fputs(_("If you only use kernels newer than "
3580 "v4.4, run 'tune2fs -O "
3581 "metadata_csum_seed' and re-run this "
3582 "command.\n"), stderr);
3583 try_confirm_csum_seed_support();
3584 rc = 1;
3585 goto closefs;
3589 * Determine if the block group checksums are
3590 * correct so we know whether or not to set
3591 * them later on.
3593 for (i = 0; i < fs->group_desc_count; i++)
3594 if (!ext2fs_group_desc_csum_verify(fs, i))
3595 break;
3596 if (i >= fs->group_desc_count)
3597 set_csum = 1;
3600 #ifdef __linux__
3601 if ((mount_flags & EXT2_MF_MOUNTED) &&
3602 !(mount_flags & EXT2_MF_READONLY) && mntpt[0]) {
3603 fd = open(mntpt, O_RDONLY);
3604 if (fd >= 0)
3605 fsuuid = malloc(sizeof(*fsuuid) + UUID_SIZE);
3606 if (fsuuid) {
3607 fsuuid->fsu_len = UUID_SIZE;
3608 fsuuid->fsu_flags = 0;
3609 ret = ioctl(fd, EXT4_IOC_GETFSUUID, fsuuid);
3610 if (ret || fsuuid->fsu_len != UUID_SIZE) {
3611 free(fsuuid);
3612 fsuuid = NULL;
3616 #endif
3618 memcpy(old_uuid, fsuuid ? fsuuid->fsu_uuid : sb->s_uuid,
3619 UUID_SIZE);
3620 if ((strcasecmp(requested_uuid, "null") == 0) ||
3621 (strcasecmp(requested_uuid, "clear") == 0)) {
3622 uuid_clear(new_uuid);
3623 } else if (strcasecmp(requested_uuid, "time") == 0) {
3624 uuid_generate_time(new_uuid);
3625 } else if (strcasecmp(requested_uuid, "random") == 0) {
3626 uuid_generate(new_uuid);
3627 } else if (uuid_parse(requested_uuid, new_uuid)) {
3628 com_err(program_name, 0, "%s",
3629 _("Invalid UUID format\n"));
3630 rc = 1;
3631 goto closefs;
3634 ret = -1;
3635 #ifdef __linux__
3636 if (fsuuid) {
3637 fsuuid->fsu_len = UUID_SIZE;
3638 fsuuid->fsu_flags = 0;
3639 memcpy(&fsuuid->fsu_uuid, new_uuid, UUID_SIZE);
3640 ret = ioctl(fd, EXT4_IOC_SETFSUUID, fsuuid);
3642 #endif
3644 * If we can't set the UUID via the ioctl, fall
3645 * back to directly modifying the superblock
3647 if (ret) {
3648 memcpy(sb->s_uuid, new_uuid, UUID_SIZE);
3649 ext2fs_init_csum_seed(fs);
3650 if (set_csum) {
3651 for (i = 0; i < fs->group_desc_count; i++)
3652 ext2fs_group_desc_csum_set(fs, i);
3653 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
3655 ext2fs_mark_super_dirty(fs);
3658 /* If this is a journal dev, we need to copy UUID into jsb */
3659 if (!(rc = get_journal_sb(fs, buf))) {
3660 journal_superblock_t *jsb;
3662 jsb = (journal_superblock_t *) buf;
3663 fputs(_("Need to update journal superblock.\n"), stdout);
3664 memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
3666 /* Writeback the journal superblock */
3667 if ((rc = io_channel_write_blk64(fs->io,
3668 ext2fs_journal_sb_start(fs->blocksize),
3669 -SUPERBLOCK_SIZE, buf)))
3670 goto closefs;
3671 } else if (rc != EXT2_ET_UNSUPP_FEATURE)
3672 goto closefs;
3673 else {
3674 rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
3676 if ((rc = fs_update_journal_user(sb, old_uuid)))
3677 goto closefs;
3681 if (I_flag) {
3682 if (mount_flags & (EXT2_MF_BUSY | EXT2_MF_MOUNTED)) {
3683 fputs(_("The inode size may only be "
3684 "changed when the filesystem is "
3685 "unmounted and not in use.\n"), stderr);
3686 rc = 1;
3687 goto closefs;
3689 if (ext2fs_has_feature_flex_bg(fs->super)) {
3690 fputs(_("Changing the inode size not supported for "
3691 "filesystems with the flex_bg\n"
3692 "feature enabled.\n"),
3693 stderr);
3694 rc = 1;
3695 goto closefs;
3698 * We want to update group descriptor also
3699 * with the new free inode count
3701 if (rewrite_checksums)
3702 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3703 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
3704 retval = resize_inode(fs, new_inode_size);
3705 if (rewrite_checksums)
3706 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
3707 if (retval == 0) {
3708 printf(_("Setting inode size %lu\n"),
3709 new_inode_size);
3710 rewrite_checksums = REWRITE_ALL;
3711 } else {
3712 printf("%s", _("Failed to change inode size\n"));
3713 rc = 1;
3714 goto closefs;
3718 if (rewrite_checksums) {
3719 retval = rewrite_metadata_checksums(fs, rewrite_checksums);
3720 if (retval != 0) {
3721 printf("Failed to rewrite metadata checksums\n");
3722 rc = 1;
3723 goto closefs;
3727 if (l_flag)
3728 list_super(sb);
3729 if (stride_set) {
3730 sb->s_raid_stride = stride;
3731 ext2fs_mark_super_dirty(fs);
3732 printf(_("Setting stride size to %d\n"), stride);
3734 if (stripe_width_set) {
3735 sb->s_raid_stripe_width = stripe_width;
3736 ext2fs_mark_super_dirty(fs);
3737 printf(_("Setting stripe width to %d\n"), stripe_width);
3739 if (ext_mount_opts) {
3740 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
3741 sizeof(fs->super->s_mount_opts));
3742 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
3743 ext2fs_mark_super_dirty(fs);
3744 printf(_("Setting extended default mount options to '%s'\n"),
3745 ext_mount_opts);
3746 free(ext_mount_opts);
3749 free(device_name);
3750 remove_error_table(&et_ext2_error_table);
3752 closefs:
3753 if (fd >= 0)
3754 close(fd);
3755 if (fsuuid)
3756 free(fsuuid);
3757 if (rc) {
3758 ext2fs_mmp_stop(fs);
3759 #ifndef BUILD_AS_LIB
3760 exit(1);
3761 #endif
3764 if (feature_64bit)
3765 convert_64bit(fs, feature_64bit);
3767 retval = ext2fs_close_free(&fs);
3768 if (retval) {
3769 com_err("tune2fs", retval,
3770 _("while writing out and closing file system"));
3771 rc = 1;
3774 return rc;