blob: a893c6cc2616d9c4f29a71131d4395001420afe9 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
Theodore Ts'o4d0f2282001-04-23 20:58:03 +00002 * tune2fs.c - Change the file system parameters on an ext2 file system
Theodore Ts'o3839e651997-04-26 13:21:57 +00003 *
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)
7 *
Theodore Ts'o4d0f2282001-04-23 20:58:03 +00008 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00009 *
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the GNU Public
12 * License.
13 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000014 */
15
16/*
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)
26 */
27
Theodore Ts'oebabf2a2008-07-13 15:32:37 -040028#define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <fcntl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000030#include <grp.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000032#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000033#else
34extern char *optarg;
35extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000036#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000037#include <pwd.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000038#include <stdio.h>
Theodore Ts'of38cf3c2008-09-01 11:17:29 -040039#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000040#include <stdlib.h>
Theodore Ts'of38cf3c2008-09-01 11:17:29 -040041#endif
JP Abgralle0ed7402014-03-19 19:08:39 -070042#ifdef HAVE_STRINGS_H
43#include <strings.h> /* for strcasecmp() */
44#else
45#define _BSD_SOURCE /* for inclusion of strcasecmp() via <string.h> */
46#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000047#include <string.h>
48#include <time.h>
49#include <unistd.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000050#include <sys/types.h>
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +053051#include <libgen.h>
Theodore Ts'o36585792008-06-07 22:07:50 -040052#include <limits.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000053
Theodore Ts'o54c637d2001-05-14 11:45:38 +000054#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000055#include "ext2fs/ext2fs.h"
56#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000057#include "uuid/uuid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058#include "e2p/e2p.h"
Theodore Ts'odc2ec522001-01-18 01:51:15 +000059#include "jfs_user.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000060#include "util.h"
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050061#include "blkid/blkid.h"
JP Abgralle0ed7402014-03-19 19:08:39 -070062#include "quota/mkquota.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000063
64#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000065#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
JP Abgralle0ed7402014-03-19 19:08:39 -070067#define QOPT_ENABLE (1)
68#define QOPT_DISABLE (-1)
69
70extern int ask_yn(const char *string, int def);
71
Theodore Ts'oec43da22009-01-20 02:34:39 -050072const char *program_name = "tune2fs";
73char *device_name;
74char *new_label, *new_last_mounted, *new_UUID;
75char *io_options;
Theodore Ts'o4d0f2282001-04-23 20:58:03 +000076static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
JP Abgralle0ed7402014-03-19 19:08:39 -070077static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +053078static int I_flag;
JP Abgralle0ed7402014-03-19 19:08:39 -070079static int clear_mmp;
Theodore Ts'od4de4aa2001-12-26 08:58:01 -050080static time_t last_check_time;
Theodore Ts'o83238152001-01-09 00:16:26 +000081static int print_label;
Theodore Ts'o63985322001-01-03 17:02:13 +000082static int max_mount_count, mount_count, mount_flags;
JP Abgralle0ed7402014-03-19 19:08:39 -070083static unsigned long interval;
84static blk64_t reserved_blocks;
Andreas Dilgerce911142005-07-06 11:50:08 -050085static double reserved_ratio;
Theodore Ts'o63985322001-01-03 17:02:13 +000086static unsigned long resgid, resuid;
87static unsigned short errors;
Theodore Ts'o83238152001-01-09 00:16:26 +000088static int open_flag;
89static char *features_cmd;
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -040090static char *mntopts_cmd;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -050091static int stride, stripe_width;
92static int stride_set, stripe_width_set;
Theodore Ts'o6cb27402008-01-26 19:06:35 -050093static char *extended_cmd;
Theodore Ts'o721b3672008-06-07 11:51:33 -040094static unsigned long new_inode_size;
JP Abgralle0ed7402014-03-19 19:08:39 -070095static char *ext_mount_opts;
96static int usrquota, grpquota;
Theodore Ts'o3839e651997-04-26 13:21:57 +000097
Theodore Ts'o63985322001-01-03 17:02:13 +000098int journal_size, journal_flags;
99char *journal_device;
100
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530101static struct list_head blk_move_list;
102
103struct blk_move {
104 struct list_head list;
JP Abgralle0ed7402014-03-19 19:08:39 -0700105 blk64_t old_loc;
106 blk64_t new_loc;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530107};
108
109
Theodore Ts'o63985322001-01-03 17:02:13 +0000110static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000111
Theodore Ts'o14b596d2009-04-22 09:18:30 -0400112#ifdef CONFIG_BUILD_FINDFS
Theodore Ts'o3e699062002-10-13 23:56:28 -0400113void do_findfs(int argc, char **argv);
Theodore Ts'o14b596d2009-04-22 09:18:30 -0400114#endif
Theodore Ts'o3e699062002-10-13 23:56:28 -0400115
Theodore Ts'o818180c1998-06-27 05:11:14 +0000116static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117{
Theodore Ts'ob21e38a2001-01-01 15:26:58 +0000118 fprintf(stderr,
Theodore Ts'obb145b02005-06-20 08:35:27 -0400119 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
Theodore Ts'ob21e38a2001-01-01 15:26:58 +0000120 "[-g group]\n"
Theodore Ts'off662d52008-02-28 21:26:01 -0500121 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
122 "\t[-m reserved_blocks_percent] "
JP Abgralle0ed7402014-03-19 19:08:39 -0700123 "[-o [^]mount_options[,...]] [-p mmp_update_interval]\n"
Theodore Ts'off662d52008-02-28 21:26:01 -0500124 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
125 "[-L volume_label]\n"
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500126 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
JP Abgralle0ed7402014-03-19 19:08:39 -0700127#ifdef CONFIG_QUOTA
128 "\t[-Q quota_options]\n"
129#endif
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500130 "\t[-E extended-option[,...]] [-T last_check_time] "
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530131 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
Theodore Ts'oec43da22009-01-20 02:34:39 -0500132 exit(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133}
134
Theodore Ts'o896938d1999-10-23 01:04:50 +0000135static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500136 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400137 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'o558df542008-02-27 15:01:19 -0500138 EXT2_FEATURE_COMPAT_DIR_INDEX,
139 /* Incompat */
Theodore Ts'oa49670e2008-02-27 18:53:34 -0500140 EXT2_FEATURE_INCOMPAT_FILETYPE |
141 EXT3_FEATURE_INCOMPAT_EXTENTS |
JP Abgralle0ed7402014-03-19 19:08:39 -0700142 EXT4_FEATURE_INCOMPAT_FLEX_BG |
143 EXT4_FEATURE_INCOMPAT_MMP,
Theodore Ts'o558df542008-02-27 15:01:19 -0500144 /* R/O compat */
145 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400146 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
147 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
148 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500149 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
JP Abgralle0ed7402014-03-19 19:08:39 -0700150#ifdef CONFIG_QUOTA
151 EXT4_FEATURE_RO_COMPAT_QUOTA |
152#endif
Theodore Ts'o0f818652012-11-29 14:58:29 -0500153 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o896938d1999-10-23 01:04:50 +0000154};
155
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500156static __u32 clear_ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500157 /* Compat */
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500158 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'o037914e2008-02-26 17:31:06 -0500159 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'o558df542008-02-27 15:01:19 -0500160 EXT2_FEATURE_COMPAT_DIR_INDEX,
161 /* Incompat */
Theodore Ts'oa49670e2008-02-27 18:53:34 -0500162 EXT2_FEATURE_INCOMPAT_FILETYPE |
JP Abgralle0ed7402014-03-19 19:08:39 -0700163 EXT4_FEATURE_INCOMPAT_FLEX_BG |
164 EXT4_FEATURE_INCOMPAT_MMP,
Theodore Ts'o558df542008-02-27 15:01:19 -0500165 /* R/O compat */
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500166 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400167 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
168 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
169 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
JP Abgralle0ed7402014-03-19 19:08:39 -0700170#ifdef CONFIG_QUOTA
171 EXT4_FEATURE_RO_COMPAT_QUOTA |
172#endif
Theodore Ts'o0f818652012-11-29 14:58:29 -0500173 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000174};
175
Theodore Ts'o63985322001-01-03 17:02:13 +0000176/*
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000177 * Remove an external journal from the filesystem
178 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700179static int remove_journal_device(ext2_filsys fs)
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000180{
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500181 char *journal_path;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000182 ext2_filsys jfs;
183 char buf[1024];
184 journal_superblock_t *jsb;
185 int i, nr_users;
186 errcode_t retval;
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000187 int commit_remove_journal = 0;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400188 io_manager io_ptr;
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000189
190 if (f_flag)
191 commit_remove_journal = 1; /* force removal even if error */
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000192
Andreas Dilger2d155762001-08-17 03:48:11 -0600193 uuid_unparse(fs->super->s_journal_uuid, buf);
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500194 journal_path = blkid_get_devname(NULL, "UUID", buf);
Andreas Dilger2d155762001-08-17 03:48:11 -0600195
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500196 if (!journal_path) {
197 journal_path =
Andreas Dilger2d155762001-08-17 03:48:11 -0600198 ext2fs_find_block_device(fs->super->s_journal_dev);
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500199 if (!journal_path)
JP Abgralle0ed7402014-03-19 19:08:39 -0700200 goto no_valid_journal;
Andreas Dilger2d155762001-08-17 03:48:11 -0600201 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000202
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400203#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400204 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
205 io_ptr = test_io_manager;
206 test_io_backing_manager = unix_io_manager;
207 } else
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400208#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400209 io_ptr = unix_io_manager;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500210 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000211 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400212 fs->blocksize, io_ptr, &jfs);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000213 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700214 com_err(program_name, retval, "%s",
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000215 _("while trying to open external journal"));
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000216 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000217 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700218 if (!(jfs->super->s_feature_incompat &
219 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Theodore Ts'o7141b542001-08-15 19:17:37 -0400220 fprintf(stderr, _("%s is not a journal device.\n"),
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500221 journal_path);
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000222 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000223 }
224
225 /* Get the journal superblock */
JP Abgralle0ed7402014-03-19 19:08:39 -0700226 if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
227 com_err(program_name, retval, "%s",
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000228 _("while reading journal superblock"));
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000229 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000230 }
231
232 jsb = (journal_superblock_t *) buf;
JP Abgralle0ed7402014-03-19 19:08:39 -0700233 if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
234 (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500235 fputs(_("Journal superblock not found!\n"), stderr);
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000236 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000237 }
238
239 /* Find the filesystem UUID */
240 nr_users = ntohl(jsb->s_nr_users);
Theodore Ts'oec43da22009-01-20 02:34:39 -0500241 for (i = 0; i < nr_users; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700242 if (memcmp(fs->super->s_uuid, &jsb->s_users[i * 16], 16) == 0)
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000243 break;
244 }
245 if (i >= nr_users) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400246 fputs(_("Filesystem's UUID not found on journal device.\n"),
Theodore Ts'o54434922003-12-07 01:28:50 -0500247 stderr);
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000248 commit_remove_journal = 1;
249 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000250 }
251 nr_users--;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500252 for (i = 0; i < nr_users; i++)
JP Abgralle0ed7402014-03-19 19:08:39 -0700253 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000254 jsb->s_nr_users = htonl(nr_users);
255
256 /* Write back the journal superblock */
JP Abgralle0ed7402014-03-19 19:08:39 -0700257 if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000258 com_err(program_name, retval,
259 "while writing journal superblock.");
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000260 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000261 }
262
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000263 commit_remove_journal = 1;
264
265no_valid_journal:
266 if (commit_remove_journal == 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700267 fputs(_("Cannot locate journal device. It was NOT removed\n"
268 "Use -f option to remove missing journal device.\n"),
269 stderr);
270 return 1;
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000271 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000272 fs->super->s_journal_dev = 0;
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500273 uuid_clear(fs->super->s_journal_uuid);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000274 ext2fs_mark_super_dirty(fs);
Theodore Ts'o54434922003-12-07 01:28:50 -0500275 fputs(_("Journal removed\n"), stdout);
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500276 free(journal_path);
JP Abgralle0ed7402014-03-19 19:08:39 -0700277
278 return 0;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000279}
280
Theodore Ts'o194686b2001-07-31 12:03:23 -0400281/* Helper function for remove_journal_inode */
JP Abgralle0ed7402014-03-19 19:08:39 -0700282static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
283 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
284 blk64_t ref_block EXT2FS_ATTR((unused)),
285 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500286 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o194686b2001-07-31 12:03:23 -0400287{
JP Abgralle0ed7402014-03-19 19:08:39 -0700288 blk64_t block;
Theodore Ts'o194686b2001-07-31 12:03:23 -0400289 int group;
290
291 block = *blocknr;
JP Abgralle0ed7402014-03-19 19:08:39 -0700292 ext2fs_unmark_block_bitmap2(fs->block_map, block);
293 group = ext2fs_group_of_blk2(fs, block);
294 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500295 ext2fs_group_desc_csum_set(fs, group);
JP Abgralle0ed7402014-03-19 19:08:39 -0700296 ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
Theodore Ts'o194686b2001-07-31 12:03:23 -0400297 return 0;
298}
299
300/*
301 * Remove the journal inode from the filesystem
302 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700303static errcode_t remove_journal_inode(ext2_filsys fs)
Theodore Ts'o194686b2001-07-31 12:03:23 -0400304{
305 struct ext2_inode inode;
306 errcode_t retval;
307 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400308
Theodore Ts'o194686b2001-07-31 12:03:23 -0400309 retval = ext2fs_read_inode(fs, ino, &inode);
310 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700311 com_err(program_name, retval, "%s",
Theodore Ts'o194686b2001-07-31 12:03:23 -0400312 _("while reading journal inode"));
JP Abgralle0ed7402014-03-19 19:08:39 -0700313 return retval;
Theodore Ts'o194686b2001-07-31 12:03:23 -0400314 }
315 if (ino == EXT2_JOURNAL_INO) {
316 retval = ext2fs_read_bitmaps(fs);
317 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700318 com_err(program_name, retval, "%s",
Theodore Ts'o194686b2001-07-31 12:03:23 -0400319 _("while reading bitmaps"));
JP Abgralle0ed7402014-03-19 19:08:39 -0700320 return retval;
Theodore Ts'o194686b2001-07-31 12:03:23 -0400321 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700322 retval = ext2fs_block_iterate3(fs, ino,
323 BLOCK_FLAG_READ_ONLY, NULL,
324 release_blocks_proc, NULL);
Theodore Ts'o194686b2001-07-31 12:03:23 -0400325 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700326 com_err(program_name, retval, "%s",
Theodore Ts'o194686b2001-07-31 12:03:23 -0400327 _("while clearing journal inode"));
JP Abgralle0ed7402014-03-19 19:08:39 -0700328 return retval;
Theodore Ts'o194686b2001-07-31 12:03:23 -0400329 }
330 memset(&inode, 0, sizeof(inode));
331 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o194686b2001-07-31 12:03:23 -0400332 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
333 } else
334 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
335 retval = ext2fs_write_inode(fs, ino, &inode);
336 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700337 com_err(program_name, retval, "%s",
Theodore Ts'o194686b2001-07-31 12:03:23 -0400338 _("while writing journal inode"));
JP Abgralle0ed7402014-03-19 19:08:39 -0700339 return retval;
Theodore Ts'o194686b2001-07-31 12:03:23 -0400340 }
341 fs->super->s_journal_inum = 0;
342 ext2fs_mark_super_dirty(fs);
JP Abgralle0ed7402014-03-19 19:08:39 -0700343
344 return 0;
Theodore Ts'o194686b2001-07-31 12:03:23 -0400345}
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000346
347/*
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -0400348 * Update the default mount options
349 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700350static int update_mntopts(ext2_filsys fs, char *mntopts)
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -0400351{
Theodore Ts'oec43da22009-01-20 02:34:39 -0500352 struct ext2_super_block *sb = fs->super;
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -0400353
354 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
355 fprintf(stderr, _("Invalid mount option set: %s\n"),
356 mntopts);
JP Abgralle0ed7402014-03-19 19:08:39 -0700357 return 1;
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -0400358 }
359 ext2fs_mark_super_dirty(fs);
JP Abgralle0ed7402014-03-19 19:08:39 -0700360
361 return 0;
362}
363
364static int check_fsck_needed(ext2_filsys fs)
365{
366 if (fs->super->s_state & EXT2_VALID_FS)
367 return 0;
368 printf("\n%s\n", _(please_fsck));
369 if (mount_flags & EXT2_MF_READONLY)
370 printf(_("(and reboot afterwards!)\n"));
371 return 1;
Darrick J. Wong66457fc2013-12-14 20:51:04 -0500372}
373
Theodore Ts'o079ad632010-05-19 12:14:39 -0400374static void request_fsck_afterwards(ext2_filsys fs)
375{
376 static int requested = 0;
377
378 if (requested++)
379 return;
380 fs->super->s_state &= ~EXT2_VALID_FS;
381 printf("\n%s\n", _(please_fsck));
382 if (mount_flags & EXT2_MF_READONLY)
JP Abgralle0ed7402014-03-19 19:08:39 -0700383 printf("%s", _("(and reboot afterwards!)\n"));
Theodore Ts'o079ad632010-05-19 12:14:39 -0400384}
385
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -0400386/*
Theodore Ts'o63985322001-01-03 17:02:13 +0000387 * Update the feature set as provided by the user.
388 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700389static int update_feature_set(ext2_filsys fs, char *features)
Theodore Ts'o63985322001-01-03 17:02:13 +0000390{
Theodore Ts'oec43da22009-01-20 02:34:39 -0500391 struct ext2_super_block *sb = fs->super;
Theodore Ts'o079ad632010-05-19 12:14:39 -0400392 struct ext2_group_desc *gd;
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500393 __u32 old_features[3];
JP Abgralle0ed7402014-03-19 19:08:39 -0700394 dgrp_t i;
395 int type_err;
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500396 unsigned int mask_err;
Theodore Ts'o2eb3b202004-04-07 09:27:36 -0400397
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500398#define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
399 ((&sb->s_feature_compat)[(type)] & (mask)))
400#define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
401 !((&sb->s_feature_compat)[(type)] & (mask)))
402#define FEATURE_CHANGED(type, mask) ((mask) & \
403 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
Theodore Ts'o63985322001-01-03 17:02:13 +0000404
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500405 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
406 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
407 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
408
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500409 if (e2p_edit_feature2(features, &sb->s_feature_compat,
410 ok_features, clear_ok_features,
411 &type_err, &mask_err)) {
412 if (!mask_err)
413 fprintf(stderr,
414 _("Invalid filesystem option set: %s\n"),
415 features);
416 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
417 fprintf(stderr, _("Clearing filesystem feature '%s' "
418 "not supported.\n"),
419 e2p_feature2string(type_err &
420 E2P_FEATURE_TYPE_MASK,
421 mask_err));
422 else
423 fprintf(stderr, _("Setting filesystem feature '%s' "
424 "not supported.\n"),
425 e2p_feature2string(type_err, mask_err));
JP Abgralle0ed7402014-03-19 19:08:39 -0700426 return 1;
Theodore Ts'o63985322001-01-03 17:02:13 +0000427 }
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500428
429 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o63985322001-01-03 17:02:13 +0000430 if ((mount_flags & EXT2_MF_MOUNTED) &&
431 !(mount_flags & EXT2_MF_READONLY)) {
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400432 fputs(_("The has_journal feature may only be "
Theodore Ts'o54434922003-12-07 01:28:50 -0500433 "cleared when the filesystem is\n"
434 "unmounted or mounted "
435 "read-only.\n"), stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -0700436 return 1;
Theodore Ts'o63985322001-01-03 17:02:13 +0000437 }
438 if (sb->s_feature_incompat &
439 EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500440 fputs(_("The needs_recovery flag is set. "
441 "Please run e2fsck before clearing\n"
442 "the has_journal flag.\n"), stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -0700443 return 1;
Theodore Ts'o63985322001-01-03 17:02:13 +0000444 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000445 if (sb->s_journal_inum) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700446 if (remove_journal_inode(fs))
447 return 1;
Theodore Ts'o63985322001-01-03 17:02:13 +0000448 }
Theodore Ts'ode49f012001-07-30 16:31:30 -0400449 if (sb->s_journal_dev) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700450 if (remove_journal_device(fs))
451 return 1;
Theodore Ts'ode49f012001-07-30 16:31:30 -0400452 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000453 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700454 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
455 int error;
456
457 if ((mount_flags & EXT2_MF_MOUNTED) ||
458 (mount_flags & EXT2_MF_READONLY)) {
459 fputs(_("The multiple mount protection feature can't\n"
460 "be set if the filesystem is mounted or\n"
461 "read-only.\n"), stderr);
462 return 1;
463 }
464
465 error = ext2fs_mmp_init(fs);
466 if (error) {
467 fputs(_("\nError while enabling multiple mount "
468 "protection feature."), stderr);
469 return 1;
470 }
471
472 /*
473 * We want to update group desc with the new free blocks count
474 */
475 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
476
477 printf(_("Multiple mount protection has been enabled "
478 "with update interval %ds.\n"),
479 sb->s_mmp_update_interval);
480 }
481
482 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
483 int error;
484
485 if (mount_flags & EXT2_MF_READONLY) {
486 fputs(_("The multiple mount protection feature cannot\n"
487 "be disabled if the filesystem is readonly.\n"),
488 stderr);
489 return 1;
490 }
491
492 error = ext2fs_read_bitmaps(fs);
493 if (error) {
494 fputs(_("Error while reading bitmaps\n"), stderr);
495 return 1;
496 }
497
498 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
499 if (error) {
500 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
501
502 if (error == EXT2_ET_MMP_MAGIC_INVALID)
503 printf(_("Magic number in MMP block does not "
504 "match. expected: %x, actual: %x\n"),
505 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
506 else
507 com_err(program_name, error, "%s",
508 _("while reading MMP block."));
509 goto mmp_error;
510 }
511
512 /* We need to force out the group descriptors as well */
513 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
514 ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
515mmp_error:
516 sb->s_mmp_block = 0;
517 sb->s_mmp_update_interval = 0;
518 }
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500519
520 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o63985322001-01-03 17:02:13 +0000521 /*
522 * If adding a journal flag, let the create journal
Benno Schulenberg7a0516a2008-07-17 23:58:35 +0200523 * code below handle setting the flag and creating the
524 * journal. We supply a default size if necessary.
Theodore Ts'o63985322001-01-03 17:02:13 +0000525 */
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000526 if (!journal_size)
527 journal_size = -1;
Theodore Ts'o08dd8302001-01-14 16:25:58 +0000528 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'o63985322001-01-03 17:02:13 +0000529 }
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500530
531 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400532 if (!sb->s_def_hash_version)
Theodore Ts'od1070d92008-08-28 23:09:35 -0400533 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
Theodore Ts'o843049c2002-09-22 15:37:40 -0400534 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
535 uuid_generate((unsigned char *) sb->s_hash_seed);
536 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000537
Theodore Ts'oa49670e2008-02-27 18:53:34 -0500538 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
Jose R. Santosc2d43002007-08-13 23:32:57 -0500539 if (ext2fs_check_desc(fs)) {
540 fputs(_("Clearing the flex_bg flag would "
541 "cause the the filesystem to be\n"
542 "inconsistent.\n"), stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -0700543 return 1;
Jose R. Santosc2d43002007-08-13 23:32:57 -0500544 }
545 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000546
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400547 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
548 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
549 if ((mount_flags & EXT2_MF_MOUNTED) &&
550 !(mount_flags & EXT2_MF_READONLY)) {
551 fputs(_("The huge_file feature may only be "
552 "cleared when the filesystem is\n"
553 "unmounted or mounted "
554 "read-only.\n"), stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -0700555 return 1;
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400556 }
557 }
558
Theodore Ts'o079ad632010-05-19 12:14:39 -0400559 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
560 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700561 for (i = 0; i < fs->group_desc_count; i++) {
562 gd = ext2fs_group_desc(fs, fs->group_desc, i);
Theodore Ts'o079ad632010-05-19 12:14:39 -0400563 gd->bg_itable_unused = 0;
564 gd->bg_flags = EXT2_BG_INODE_ZEROED;
565 ext2fs_group_desc_csum_set(fs, i);
566 }
567 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
568 }
569
570 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
571 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700572 for (i = 0; i < fs->group_desc_count; i++) {
573 gd = ext2fs_group_desc(fs, fs->group_desc, i);
Theodore Ts'o079ad632010-05-19 12:14:39 -0400574 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
575 /*
576 * XXX what we really should do is zap
577 * uninitialized inode tables instead.
578 */
579 request_fsck_afterwards(fs);
580 break;
581 }
582 gd->bg_itable_unused = 0;
583 gd->bg_flags = 0;
584 gd->bg_checksum = 0;
585 }
586 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
587 }
588
JP Abgralle0ed7402014-03-19 19:08:39 -0700589 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
590 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
591 /*
592 * Set the Q_flag here and handle the quota options in the code
593 * below.
594 */
595 if (!Q_flag) {
596 Q_flag = 1;
597 /* Enable both user quota and group quota by default */
598 usrquota = QOPT_ENABLE;
599 grpquota = QOPT_ENABLE;
600 }
601 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
602 }
603
604 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
605 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
606 /*
607 * Set the Q_flag here and handle the quota options in the code
608 * below.
609 */
610 if (Q_flag)
611 fputs(_("\nWarning: '^quota' option overrides '-Q'"
612 "arguments.\n"), stderr);
613 Q_flag = 1;
614 /* Disable both user quota and group quota by default */
615 usrquota = QOPT_DISABLE;
616 grpquota = QOPT_DISABLE;
617 }
618
Theodore Ts'o63985322001-01-03 17:02:13 +0000619 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
620 (sb->s_feature_compat || sb->s_feature_ro_compat ||
621 sb->s_feature_incompat))
622 ext2fs_update_dynamic_rev(fs);
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500623
624 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
625 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400626 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
627 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500628 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
Theodore Ts'o037914e2008-02-26 17:31:06 -0500629 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
630 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
Theodore Ts'o558df542008-02-27 15:01:19 -0500631 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
632 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
Theodore Ts'o079ad632010-05-19 12:14:39 -0400633 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
634 request_fsck_afterwards(fs);
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500635
636 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
637 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
638 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
Theodore Ts'o2eb3b202004-04-07 09:27:36 -0400639 ext2fs_mark_super_dirty(fs);
JP Abgralle0ed7402014-03-19 19:08:39 -0700640
641 return 0;
Theodore Ts'o63985322001-01-03 17:02:13 +0000642}
643
644/*
645 * Add a journal to the filesystem.
646 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700647static int add_journal(ext2_filsys fs)
Theodore Ts'o63985322001-01-03 17:02:13 +0000648{
649 unsigned long journal_blocks;
650 errcode_t retval;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000651 ext2_filsys jfs;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400652 io_manager io_ptr;
Theodore Ts'o63985322001-01-03 17:02:13 +0000653
654 if (fs->super->s_feature_compat &
655 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500656 fputs(_("The filesystem already has a journal.\n"), stderr);
Andreas Dilger2d155762001-08-17 03:48:11 -0600657 goto err;
Theodore Ts'o63985322001-01-03 17:02:13 +0000658 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000659 if (journal_device) {
660 check_plausibility(journal_device);
661 check_mount(journal_device, 0, _("journal"));
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400662#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400663 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
664 io_ptr = test_io_manager;
665 test_io_backing_manager = unix_io_manager;
666 } else
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400667#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400668 io_ptr = unix_io_manager;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000669 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
670 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400671 fs->blocksize, io_ptr, &jfs);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000672 if (retval) {
673 com_err(program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +0000674 _("\n\twhile trying to open journal on %s\n"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000675 journal_device);
Andreas Dilger2d155762001-08-17 03:48:11 -0600676 goto err;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000677 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000678 printf(_("Creating journal on device %s: "),
679 journal_device);
Theodore Ts'o4055ef72001-01-14 16:11:14 +0000680 fflush(stdout);
Andreas Dilger2d155762001-08-17 03:48:11 -0600681
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000682 retval = ext2fs_add_journal_device(fs, jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -0600683 ext2fs_close(jfs);
Theodore Ts'o63985322001-01-03 17:02:13 +0000684 if (retval) {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500685 com_err(program_name, retval,
686 _("while adding filesystem to journal on %s"),
687 journal_device);
Andreas Dilger2d155762001-08-17 03:48:11 -0600688 goto err;
Theodore Ts'o63985322001-01-03 17:02:13 +0000689 }
Theodore Ts'o54434922003-12-07 01:28:50 -0500690 fputs(_("done\n"), stdout);
Theodore Ts'o63985322001-01-03 17:02:13 +0000691 } else if (journal_size) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500692 fputs(_("Creating journal inode: "), stdout);
Theodore Ts'o63985322001-01-03 17:02:13 +0000693 fflush(stdout);
Theodore Ts'o2537b6d2001-03-26 20:07:13 +0000694 journal_blocks = figure_journal_size(journal_size, fs);
695
Theodore Ts'o63985322001-01-03 17:02:13 +0000696 retval = ext2fs_add_journal_inode(fs, journal_blocks,
697 journal_flags);
698 if (retval) {
Theodore Ts'o7141b542001-08-15 19:17:37 -0400699 fprintf(stderr, "\n");
JP Abgralle0ed7402014-03-19 19:08:39 -0700700 com_err(program_name, retval, "%s",
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +0000701 _("\n\twhile trying to create journal file"));
JP Abgralle0ed7402014-03-19 19:08:39 -0700702 return retval;
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +0000703 } else
Theodore Ts'o54434922003-12-07 01:28:50 -0500704 fputs(_("done\n"), stdout);
Theodore Ts'o63985322001-01-03 17:02:13 +0000705 /*
706 * If the filesystem wasn't mounted, we need to force
707 * the block group descriptors out.
708 */
709 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
710 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
711 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700712 print_check_message(fs->super->s_max_mnt_count,
713 fs->super->s_checkinterval);
714 return 0;
Andreas Dilger2d155762001-08-17 03:48:11 -0600715
716err:
Jim Meyering45e338f2009-02-23 18:07:50 +0100717 free(journal_device);
JP Abgralle0ed7402014-03-19 19:08:39 -0700718 return 1;
Theodore Ts'o63985322001-01-03 17:02:13 +0000719}
720
JP Abgralle0ed7402014-03-19 19:08:39 -0700721static void handle_quota_options(ext2_filsys fs)
722{
723 quota_ctx_t qctx;
724 ext2_ino_t qf_ino;
725
726 if (!usrquota && !grpquota)
727 /* Nothing to do. */
728 return;
729
730 quota_init_context(&qctx, fs, -1);
731
732 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
733 quota_compute_usage(qctx);
734
735 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
736 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
737 QFMT_VFS_V1)) > 0)
738 quota_update_limits(qctx, qf_ino, USRQUOTA);
739 quota_write_inode(qctx, USRQUOTA);
740 } else if (usrquota == QOPT_DISABLE) {
741 quota_remove_inode(fs, USRQUOTA);
742 }
743
744 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
745 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
746 QFMT_VFS_V1)) > 0)
747 quota_update_limits(qctx, qf_ino, GRPQUOTA);
748 quota_write_inode(qctx, GRPQUOTA);
749 } else if (grpquota == QOPT_DISABLE) {
750 quota_remove_inode(fs, GRPQUOTA);
751 }
752
753 quota_release_context(&qctx);
754
755 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
756 fprintf(stderr, "%s", _("\nWarning: the quota feature is still "
757 "under development\n"
758 "See https://ext4.wiki.kernel.org/"
759 "index.php/Quota for more information\n\n"));
760 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
761 ext2fs_mark_super_dirty(fs);
762 } else if (!fs->super->s_usr_quota_inum &&
763 !fs->super->s_grp_quota_inum) {
764 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
765 ext2fs_mark_super_dirty(fs);
766 }
767
768 return;
769}
770
771#ifdef CONFIG_QUOTA
772static void parse_quota_opts(const char *opts)
773{
774 char *buf, *token, *next, *p;
775 int len;
776
777 len = strlen(opts);
778 buf = malloc(len+1);
779 if (!buf) {
780 fputs(_("Couldn't allocate memory to parse quota "
781 "options!\n"), stderr);
782 exit(1);
783 }
784 strcpy(buf, opts);
785 for (token = buf; token && *token; token = next) {
786 p = strchr(token, ',');
787 next = 0;
788 if (p) {
789 *p = 0;
790 next = p+1;
791 }
792
793 if (strcmp(token, "usrquota") == 0) {
794 usrquota = QOPT_ENABLE;
795 } else if (strcmp(token, "^usrquota") == 0) {
796 usrquota = QOPT_DISABLE;
797 } else if (strcmp(token, "grpquota") == 0) {
798 grpquota = QOPT_ENABLE;
799 } else if (strcmp(token, "^grpquota") == 0) {
800 grpquota = QOPT_DISABLE;
801 } else {
802 fputs(_("\nBad quota options specified.\n\n"
803 "Following valid quota options are available "
804 "(pass by separating with comma):\n"
805 "\t[^]usrquota\n"
806 "\t[^]grpquota\n"
807 "\n\n"), stderr);
808 free(buf);
809 exit(1);
810 }
811 }
812 free(buf);
813}
814#endif
Theodore Ts'o896938d1999-10-23 01:04:50 +0000815
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000816static void parse_e2label_options(int argc, char ** argv)
Theodore Ts'o83238152001-01-09 00:16:26 +0000817{
818 if ((argc < 2) || (argc > 3)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500819 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
Theodore Ts'o83238152001-01-09 00:16:26 +0000820 exit(1);
821 }
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500822 io_options = strchr(argv[1], '?');
823 if (io_options)
824 *io_options++ = 0;
Theodore Ts'o332f2c22003-03-06 12:58:33 -0500825 device_name = blkid_get_devname(NULL, argv[1], NULL);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500826 if (!device_name) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400827 com_err("e2label", 0, _("Unable to resolve '%s'"),
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500828 argv[1]);
829 exit(1);
830 }
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400831 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
Theodore Ts'o83238152001-01-09 00:16:26 +0000832 if (argc == 3) {
Theodore Ts'o0ddfd9a2008-02-09 22:22:38 -0500833 open_flag |= EXT2_FLAG_RW;
Theodore Ts'o83238152001-01-09 00:16:26 +0000834 L_flag = 1;
835 new_label = argv[2];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400836 } else
Theodore Ts'o83238152001-01-09 00:16:26 +0000837 print_label++;
838}
839
Theodore Ts'od4de4aa2001-12-26 08:58:01 -0500840static time_t parse_time(char *str)
841{
842 struct tm ts;
843
844 if (strcmp(str, "now") == 0) {
845 return (time(0));
846 }
847 memset(&ts, 0, sizeof(ts));
Theodore Ts'obc7c14e2003-05-03 16:40:09 -0400848#ifdef HAVE_STRPTIME
Theodore Ts'o690e6932004-12-21 20:40:08 -0500849 strptime(str, "%Y%m%d%H%M%S", &ts);
Theodore Ts'obc7c14e2003-05-03 16:40:09 -0400850#else
Theodore Ts'o690e6932004-12-21 20:40:08 -0500851 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
Theodore Ts'obc7c14e2003-05-03 16:40:09 -0400852 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
853 ts.tm_year -= 1900;
854 ts.tm_mon -= 1;
855 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
856 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
857 ts.tm_min > 59 || ts.tm_sec > 61)
858 ts.tm_mday = 0;
859#endif
Theodore Ts'od4de4aa2001-12-26 08:58:01 -0500860 if (ts.tm_mday == 0) {
861 com_err(program_name, 0,
862 _("Couldn't parse date/time specifier: %s"),
863 str);
864 usage();
865 }
Theodore Ts'oa2ff0f32008-03-21 09:10:09 -0400866 ts.tm_isdst = -1;
Theodore Ts'od4de4aa2001-12-26 08:58:01 -0500867 return (mktime(&ts));
868}
Theodore Ts'o83238152001-01-09 00:16:26 +0000869
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000870static void parse_tune2fs_options(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000871{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000872 int c;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500873 char *tmp;
874 struct group *gr;
875 struct passwd *pw;
JP Abgralle0ed7402014-03-19 19:08:39 -0700876 char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000877
JP Abgralle0ed7402014-03-19 19:08:39 -0700878#ifdef CONFIG_QUOTA
879 strcat(optstring, "Q:");
880#endif
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400881 open_flag = 0;
Theodore Ts'o0ddfd9a2008-02-09 22:22:38 -0500882
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400883 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
JP Abgralle0ed7402014-03-19 19:08:39 -0700884 while ((c = getopt(argc, argv, optstring)) != EOF)
Theodore Ts'oec43da22009-01-20 02:34:39 -0500885 switch (c) {
886 case 'c':
887 max_mount_count = strtol(optarg, &tmp, 0);
888 if (*tmp || max_mount_count > 16000) {
889 com_err(program_name, 0,
890 _("bad mounts count - %s"),
891 optarg);
892 usage();
893 }
894 if (max_mount_count == 0)
895 max_mount_count = -1;
896 c_flag = 1;
897 open_flag = EXT2_FLAG_RW;
898 break;
899 case 'C':
900 mount_count = strtoul(optarg, &tmp, 0);
901 if (*tmp || mount_count > 16000) {
902 com_err(program_name, 0,
903 _("bad mounts count - %s"),
904 optarg);
905 usage();
906 }
907 C_flag = 1;
908 open_flag = EXT2_FLAG_RW;
909 break;
910 case 'e':
911 if (strcmp(optarg, "continue") == 0)
912 errors = EXT2_ERRORS_CONTINUE;
913 else if (strcmp(optarg, "remount-ro") == 0)
914 errors = EXT2_ERRORS_RO;
915 else if (strcmp(optarg, "panic") == 0)
916 errors = EXT2_ERRORS_PANIC;
917 else {
918 com_err(program_name, 0,
919 _("bad error behavior - %s"),
920 optarg);
921 usage();
922 }
923 e_flag = 1;
924 open_flag = EXT2_FLAG_RW;
925 break;
926 case 'E':
927 extended_cmd = optarg;
928 open_flag |= EXT2_FLAG_RW;
929 break;
930 case 'f': /* Force */
931 f_flag = 1;
932 break;
933 case 'g':
934 resgid = strtoul(optarg, &tmp, 0);
935 if (*tmp) {
936 gr = getgrnam(optarg);
937 if (gr == NULL)
938 tmp = optarg;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000939 else {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500940 resgid = gr->gr_gid;
941 *tmp = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000942 }
Theodore Ts'oec43da22009-01-20 02:34:39 -0500943 }
944 if (*tmp) {
945 com_err(program_name, 0,
946 _("bad gid/group name - %s"),
947 optarg);
948 usage();
949 }
950 g_flag = 1;
951 open_flag = EXT2_FLAG_RW;
952 break;
953 case 'i':
954 interval = strtoul(optarg, &tmp, 0);
955 switch (*tmp) {
956 case 's':
957 tmp++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000958 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500959 case '\0':
960 case 'd':
961 case 'D': /* days */
962 interval *= 86400;
963 if (*tmp != '\0')
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000964 tmp++;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000965 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000966 case 'm':
Theodore Ts'oec43da22009-01-20 02:34:39 -0500967 case 'M': /* months! */
968 interval *= 86400 * 30;
969 tmp++;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000970 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500971 case 'w':
972 case 'W': /* weeks */
973 interval *= 86400 * 7;
974 tmp++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000975 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500976 }
977 if (*tmp) {
978 com_err(program_name, 0,
979 _("bad interval - %s"), optarg);
980 usage();
981 }
982 i_flag = 1;
983 open_flag = EXT2_FLAG_RW;
984 break;
985 case 'j':
986 if (!journal_size)
987 journal_size = -1;
988 open_flag = EXT2_FLAG_RW;
989 break;
990 case 'J':
991 parse_journal_opts(optarg);
992 open_flag = EXT2_FLAG_RW;
993 break;
994 case 'l':
995 l_flag = 1;
996 break;
997 case 'L':
998 new_label = optarg;
999 L_flag = 1;
1000 open_flag |= EXT2_FLAG_RW |
1001 EXT2_FLAG_JOURNAL_DEV_OK;
1002 break;
1003 case 'm':
1004 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o8d822452009-03-06 02:23:59 -05001005 if (*tmp || reserved_ratio > 50 ||
1006 reserved_ratio < 0) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001007 com_err(program_name, 0,
1008 _("bad reserved block ratio - %s"),
1009 optarg);
1010 usage();
1011 }
1012 m_flag = 1;
1013 open_flag = EXT2_FLAG_RW;
1014 break;
1015 case 'M':
1016 new_last_mounted = optarg;
1017 M_flag = 1;
1018 open_flag = EXT2_FLAG_RW;
1019 break;
1020 case 'o':
1021 if (mntopts_cmd) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001022 com_err(program_name, 0, "%s",
Theodore Ts'oec43da22009-01-20 02:34:39 -05001023 _("-o may only be specified once"));
1024 usage();
1025 }
1026 mntopts_cmd = optarg;
1027 open_flag = EXT2_FLAG_RW;
1028 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -05001029 case 'O':
1030 if (features_cmd) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001031 com_err(program_name, 0, "%s",
Theodore Ts'oec43da22009-01-20 02:34:39 -05001032 _("-O may only be specified once"));
1033 usage();
1034 }
1035 features_cmd = optarg;
1036 open_flag = EXT2_FLAG_RW;
1037 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07001038#ifdef CONFIG_QUOTA
1039 case 'Q':
1040 Q_flag = 1;
1041 parse_quota_opts(optarg);
1042 open_flag = EXT2_FLAG_RW;
1043 break;
1044#endif
Theodore Ts'oec43da22009-01-20 02:34:39 -05001045 case 'r':
1046 reserved_blocks = strtoul(optarg, &tmp, 0);
1047 if (*tmp) {
1048 com_err(program_name, 0,
1049 _("bad reserved blocks count - %s"),
1050 optarg);
1051 usage();
1052 }
1053 r_flag = 1;
1054 open_flag = EXT2_FLAG_RW;
1055 break;
1056 case 's': /* Deprecated */
1057 s_flag = atoi(optarg);
1058 open_flag = EXT2_FLAG_RW;
1059 break;
1060 case 'T':
1061 T_flag = 1;
1062 last_check_time = parse_time(optarg);
1063 open_flag = EXT2_FLAG_RW;
1064 break;
1065 case 'u':
1066 resuid = strtoul(optarg, &tmp, 0);
Theodore Ts'o818180c1998-06-27 05:11:14 +00001067 if (*tmp) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001068 pw = getpwnam(optarg);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001069 if (pw == NULL)
1070 tmp = optarg;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001071 else {
Theodore Ts'of3db3561997-04-26 13:34:30 +00001072 resuid = pw->pw_uid;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +00001073 *tmp = 0;
1074 }
Theodore Ts'of3db3561997-04-26 13:34:30 +00001075 }
Theodore Ts'o818180c1998-06-27 05:11:14 +00001076 if (*tmp) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001077 com_err(program_name, 0,
1078 _("bad uid/user name - %s"),
1079 optarg);
Theodore Ts'o818180c1998-06-27 05:11:14 +00001080 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +00001081 }
1082 u_flag = 1;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001083 open_flag = EXT2_FLAG_RW;
1084 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -05001085 case 'U':
1086 new_UUID = optarg;
1087 U_flag = 1;
1088 open_flag = EXT2_FLAG_RW |
1089 EXT2_FLAG_JOURNAL_DEV_OK;
1090 break;
1091 case 'I':
1092 new_inode_size = strtoul(optarg, &tmp, 0);
1093 if (*tmp) {
1094 com_err(program_name, 0,
1095 _("bad inode size - %s"),
1096 optarg);
Theodore Ts'o818180c1998-06-27 05:11:14 +00001097 usage();
Theodore Ts'oec43da22009-01-20 02:34:39 -05001098 }
1099 if (!((new_inode_size &
1100 (new_inode_size - 1)) == 0)) {
1101 com_err(program_name, 0,
1102 _("Inode size must be a "
1103 "power of two- %s"),
1104 optarg);
1105 usage();
1106 }
1107 open_flag = EXT2_FLAG_RW;
1108 I_flag = 1;
1109 break;
1110 default:
1111 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +00001112 }
1113 if (optind < argc - 1 || optind == argc)
Theodore Ts'o818180c1998-06-27 05:11:14 +00001114 usage();
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001115 if (!open_flag && !l_flag)
1116 usage();
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -05001117 io_options = strchr(argv[optind], '?');
1118 if (io_options)
1119 *io_options++ = 0;
Theodore Ts'o332f2c22003-03-06 12:58:33 -05001120 device_name = blkid_get_devname(NULL, argv[optind], NULL);
Theodore Ts'o817e49e2003-11-21 09:10:29 -05001121 if (!device_name) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001122 com_err(program_name, 0, _("Unable to resolve '%s'"),
Theodore Ts'o817e49e2003-11-21 09:10:29 -05001123 argv[optind]);
1124 exit(1);
1125 }
Theodore Ts'o118d7da2002-08-17 23:01:22 -04001126}
Theodore Ts'o83238152001-01-09 00:16:26 +00001127
Theodore Ts'o14b596d2009-04-22 09:18:30 -04001128#ifdef CONFIG_BUILD_FINDFS
Theodore Ts'o3e699062002-10-13 23:56:28 -04001129void do_findfs(int argc, char **argv)
Theodore Ts'o118d7da2002-08-17 23:01:22 -04001130{
1131 char *dev;
1132
1133 if ((argc != 2) ||
1134 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1135 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1136 exit(2);
1137 }
Theodore Ts'oed1b33e2003-03-01 19:29:01 -05001138 dev = blkid_get_devname(NULL, argv[1], NULL);
Theodore Ts'o118d7da2002-08-17 23:01:22 -04001139 if (!dev) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001140 com_err("findfs", 0, _("Unable to resolve '%s'"),
Theodore Ts'o118d7da2002-08-17 23:01:22 -04001141 argv[1]);
1142 exit(1);
1143 }
1144 puts(dev);
1145 exit(0);
1146}
Theodore Ts'o14b596d2009-04-22 09:18:30 -04001147#endif
Theodore Ts'o83238152001-01-09 00:16:26 +00001148
JP Abgralle0ed7402014-03-19 19:08:39 -07001149static int parse_extended_opts(ext2_filsys fs, const char *opts)
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001150{
1151 char *buf, *token, *next, *p, *arg;
Theodore Ts'o10ff68d2008-08-29 21:21:19 -04001152 int len, hash_alg;
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001153 int r_usage = 0;
1154
1155 len = strlen(opts);
1156 buf = malloc(len+1);
1157 if (!buf) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001158 fprintf(stderr, "%s",
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001159 _("Couldn't allocate memory to parse options!\n"));
JP Abgralle0ed7402014-03-19 19:08:39 -07001160 return 1;
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001161 }
1162 strcpy(buf, opts);
1163 for (token = buf; token && *token; token = next) {
1164 p = strchr(token, ',');
1165 next = 0;
1166 if (p) {
1167 *p = 0;
1168 next = p+1;
1169 }
1170 arg = strchr(token, '=');
1171 if (arg) {
1172 *arg = 0;
1173 arg++;
1174 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001175 if (strcmp(token, "clear-mmp") == 0 ||
1176 strcmp(token, "clear_mmp") == 0) {
1177 clear_mmp = 1;
1178 } else if (strcmp(token, "mmp_update_interval") == 0) {
1179 unsigned long intv;
1180 if (!arg) {
1181 r_usage++;
1182 continue;
1183 }
1184 intv = strtoul(arg, &p, 0);
1185 if (*p) {
1186 fprintf(stderr,
1187 _("Invalid mmp_update_interval: %s\n"),
1188 arg);
1189 r_usage++;
1190 continue;
1191 }
1192 if (intv == 0) {
1193 intv = EXT4_MMP_UPDATE_INTERVAL;
1194 } else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1195 fprintf(stderr,
1196 _("mmp_update_interval too big: %lu\n"),
1197 intv);
1198 r_usage++;
1199 continue;
1200 }
1201 printf(P_("Setting multiple mount protection update "
1202 "interval to %lu second\n",
1203 "Setting multiple mount protection update "
1204 "interval to %lu seconds\n", intv),
1205 intv);
1206 fs->super->s_mmp_update_interval = intv;
1207 ext2fs_mark_super_dirty(fs);
1208 } else if (!strcmp(token, "test_fs")) {
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001209 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1210 printf("Setting test filesystem flag\n");
1211 ext2fs_mark_super_dirty(fs);
1212 } else if (!strcmp(token, "^test_fs")) {
1213 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1214 printf("Clearing test filesystem flag\n");
1215 ext2fs_mark_super_dirty(fs);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001216 } else if (strcmp(token, "stride") == 0) {
1217 if (!arg) {
1218 r_usage++;
1219 continue;
1220 }
1221 stride = strtoul(arg, &p, 0);
JP Abgralle0ed7402014-03-19 19:08:39 -07001222 if (*p) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001223 fprintf(stderr,
JP Abgralle0ed7402014-03-19 19:08:39 -07001224 _("Invalid RAID stride: %s\n"),
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001225 arg);
1226 r_usage++;
1227 continue;
1228 }
1229 stride_set = 1;
1230 } else if (strcmp(token, "stripe-width") == 0 ||
1231 strcmp(token, "stripe_width") == 0) {
1232 if (!arg) {
1233 r_usage++;
1234 continue;
1235 }
1236 stripe_width = strtoul(arg, &p, 0);
JP Abgralle0ed7402014-03-19 19:08:39 -07001237 if (*p) {
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001238 fprintf(stderr,
1239 _("Invalid RAID stripe-width: %s\n"),
1240 arg);
1241 r_usage++;
1242 continue;
1243 }
1244 stripe_width_set = 1;
Theodore Ts'o10ff68d2008-08-29 21:21:19 -04001245 } else if (strcmp(token, "hash_alg") == 0 ||
1246 strcmp(token, "hash-alg") == 0) {
1247 if (!arg) {
1248 r_usage++;
1249 continue;
1250 }
1251 hash_alg = e2p_string2hash(arg);
1252 if (hash_alg < 0) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001253 fprintf(stderr,
Theodore Ts'o10ff68d2008-08-29 21:21:19 -04001254 _("Invalid hash algorithm: %s\n"),
1255 arg);
1256 r_usage++;
1257 continue;
1258 }
1259 fs->super->s_def_hash_version = hash_alg;
1260 printf(_("Setting default hash algorithm "
Theodore Ts'oec43da22009-01-20 02:34:39 -05001261 "to %s (%d)\n"),
Theodore Ts'o10ff68d2008-08-29 21:21:19 -04001262 arg, hash_alg);
1263 ext2fs_mark_super_dirty(fs);
JP Abgralle0ed7402014-03-19 19:08:39 -07001264 } else if (!strcmp(token, "mount_opts")) {
Theodore Ts'o9345f022010-09-18 19:38:22 -04001265 if (!arg) {
1266 r_usage++;
1267 continue;
1268 }
1269 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1270 fprintf(stderr,
1271 "Extended mount options too long\n");
1272 continue;
1273 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001274 ext_mount_opts = strdup(arg);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001275 } else
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001276 r_usage++;
1277 }
1278 if (r_usage) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001279 fprintf(stderr, "%s", _("\nBad options specified.\n\n"
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001280 "Extended options are separated by commas, "
1281 "and may take an argument which\n"
1282 "\tis set off by an equals ('=') sign.\n\n"
1283 "Valid extended options are:\n"
JP Abgralle0ed7402014-03-19 19:08:39 -07001284 "\tclear_mmp\n"
1285 "\thash_alg=<hash algorithm>\n"
1286 "\tmount_opts=<extended default mount options>\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001287 "\tstride=<RAID per-disk chunk size in blocks>\n"
Theodore Ts'o10ff68d2008-08-29 21:21:19 -04001288 "\tstripe_width=<RAID stride*data disks in blocks>\n"
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001289 "\ttest_fs\n"
1290 "\t^test_fs\n"));
1291 free(buf);
JP Abgralle0ed7402014-03-19 19:08:39 -07001292 return 1;
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001293 }
1294 free(buf);
JP Abgralle0ed7402014-03-19 19:08:39 -07001295
1296 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001297}
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001298
Theodore Ts'o31f18152009-01-20 11:49:17 -05001299/*
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301300 * Fill in the block bitmap bmap with the information regarding the
1301 * blocks to be moved
Theodore Ts'o31f18152009-01-20 11:49:17 -05001302 */
1303static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301304 ext2fs_block_bitmap bmap)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301305{
1306 dgrp_t i;
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +05301307 int retval;
1308 ext2_badblocks_list bb_list = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07001309 blk64_t j, needed_blocks = 0;
1310 blk64_t start_blk, end_blk;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301311
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +05301312 retval = ext2fs_read_bb_inode(fs, &bb_list);
1313 if (retval)
1314 return retval;
1315
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301316 for (i = 0; i < fs->group_desc_count; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001317 start_blk = ext2fs_inode_table_loc(fs, i) +
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301318 fs->inode_blocks_per_group;
1319
JP Abgralle0ed7402014-03-19 19:08:39 -07001320 end_blk = ext2fs_inode_table_loc(fs, i) +
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301321 new_ino_blks_per_grp;
1322
1323 for (j = start_blk; j < end_blk; j++) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001324 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +05301325 /*
1326 * IF the block is a bad block we fail
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301327 */
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +05301328 if (ext2fs_badblocks_list_test(bb_list, j)) {
1329 ext2fs_badblocks_list_free(bb_list);
1330 return ENOSPC;
1331 }
1332
JP Abgralle0ed7402014-03-19 19:08:39 -07001333 ext2fs_mark_block_bitmap2(bmap, j);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301334 } else {
1335 /*
1336 * We are going to use this block for
1337 * inode table. So mark them used.
1338 */
JP Abgralle0ed7402014-03-19 19:08:39 -07001339 ext2fs_mark_block_bitmap2(fs->block_map, j);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301340 }
1341 }
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301342 needed_blocks += end_blk - start_blk;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301343 }
1344
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +05301345 ext2fs_badblocks_list_free(bb_list);
JP Abgralle0ed7402014-03-19 19:08:39 -07001346 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301347 return ENOSPC;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301348
1349 return 0;
1350}
1351
JP Abgralle0ed7402014-03-19 19:08:39 -07001352static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301353{
1354 dgrp_t group;
JP Abgralle0ed7402014-03-19 19:08:39 -07001355 group = ext2fs_group_of_blk2(fs, blk);
1356 if (ext2fs_block_bitmap_loc(fs, group) == blk)
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301357 return 1;
JP Abgralle0ed7402014-03-19 19:08:39 -07001358 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301359 return 1;
1360 return 0;
1361}
1362
JP Abgralle0ed7402014-03-19 19:08:39 -07001363static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301364{
JP Abgralle0ed7402014-03-19 19:08:39 -07001365 blk64_t start_blk, end_blk;
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301366 start_blk = fs->super->s_first_data_block +
1367 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1368 /*
1369 * We cannot get new block beyond end_blk for for the last block group
1370 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1371 */
1372 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1373 if (blk >= start_blk && blk <= end_blk)
1374 return 1;
1375 return 0;
1376}
1377
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301378static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301379{
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301380
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301381 char *buf;
JP Abgralle0ed7402014-03-19 19:08:39 -07001382 dgrp_t group = 0;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301383 errcode_t retval;
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301384 int meta_data = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07001385 blk64_t blk, new_blk, goal;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301386 struct blk_move *bmv;
1387
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301388 retval = ext2fs_get_mem(fs->blocksize, &buf);
1389 if (retval)
1390 return retval;
1391
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001392 for (new_blk = blk = fs->super->s_first_data_block;
JP Abgralle0ed7402014-03-19 19:08:39 -07001393 blk < ext2fs_blocks_count(fs->super); blk++) {
1394 if (!ext2fs_test_block_bitmap2(bmap, blk))
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301395 continue;
1396
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301397 if (ext2fs_is_meta_block(fs, blk)) {
1398 /*
1399 * If the block is mapping a fs meta data block
1400 * like group desc/block bitmap/inode bitmap. We
1401 * should find a block in the same group and fix
1402 * the respective fs metadata pointers. Otherwise
1403 * fail
1404 */
JP Abgralle0ed7402014-03-19 19:08:39 -07001405 group = ext2fs_group_of_blk2(fs, blk);
1406 goal = ext2fs_group_first_block2(fs, group);
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301407 meta_data = 1;
1408
1409 } else {
1410 goal = new_blk;
1411 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001412 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301413 if (retval)
1414 goto err_out;
1415
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301416 /* new fs meta data block should be in the same group */
1417 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1418 retval = ENOSPC;
1419 goto err_out;
1420 }
1421
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301422 /* Mark this block as allocated */
JP Abgralle0ed7402014-03-19 19:08:39 -07001423 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301424
1425 /* Add it to block move list */
1426 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1427 if (retval)
1428 goto err_out;
1429
1430 bmv->old_loc = blk;
1431 bmv->new_loc = new_blk;
1432
1433 list_add(&(bmv->list), &blk_move_list);
1434
JP Abgralle0ed7402014-03-19 19:08:39 -07001435 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301436 if (retval)
1437 goto err_out;
1438
JP Abgralle0ed7402014-03-19 19:08:39 -07001439 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301440 if (retval)
1441 goto err_out;
1442 }
1443
1444err_out:
1445 ext2fs_free_mem(&buf);
1446 return retval;
1447}
1448
JP Abgralle0ed7402014-03-19 19:08:39 -07001449static blk64_t translate_block(blk64_t blk)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301450{
1451 struct list_head *entry;
1452 struct blk_move *bmv;
1453
1454 list_for_each(entry, &blk_move_list) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301455 bmv = list_entry(entry, struct blk_move, list);
1456 if (bmv->old_loc == blk)
1457 return bmv->new_loc;
1458 }
1459
1460 return 0;
1461}
1462
Theodore Ts'o721b3672008-06-07 11:51:33 -04001463static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
JP Abgralle0ed7402014-03-19 19:08:39 -07001464 blk64_t *block_nr,
Theodore Ts'o721b3672008-06-07 11:51:33 -04001465 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
JP Abgralle0ed7402014-03-19 19:08:39 -07001466 blk64_t ref_block EXT2FS_ATTR((unused)),
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301467 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001468 void *priv_data)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301469{
1470 int ret = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07001471 blk64_t new_blk;
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001472 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301473
JP Abgralle0ed7402014-03-19 19:08:39 -07001474 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001475 return 0;
Theodore Ts'oec43da22009-01-20 02:34:39 -05001476 new_blk = translate_block(*block_nr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301477 if (new_blk) {
1478 *block_nr = new_blk;
1479 /*
1480 * This will force the ext2fs_write_inode in the iterator
1481 */
1482 ret |= BLOCK_CHANGED;
1483 }
1484
1485 return ret;
1486}
1487
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001488static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301489{
1490 errcode_t retval = 0;
1491 ext2_ino_t ino;
JP Abgralle0ed7402014-03-19 19:08:39 -07001492 blk64_t blk;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301493 char *block_buf = 0;
1494 struct ext2_inode inode;
1495 ext2_inode_scan scan = NULL;
1496
1497 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1498 if (retval)
1499 return retval;
1500
1501 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1502 if (retval)
1503 goto err_out;
1504
1505 while (1) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301506 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1507 if (retval)
1508 goto err_out;
1509
1510 if (!ino)
1511 break;
1512
1513 if (inode.i_links_count == 0)
1514 continue; /* inode not in use */
1515
1516 /* FIXME!!
1517 * If we end up modifying the journal inode
1518 * the sb->s_jnl_blocks will differ. But a
1519 * subsequent e2fsck fixes that.
1520 * Do we need to fix this ??
1521 */
1522
JP Abgralle0ed7402014-03-19 19:08:39 -07001523 if (ext2fs_file_acl_block(fs, &inode) &&
1524 ext2fs_test_block_bitmap2(bmap,
1525 ext2fs_file_acl_block(fs, &inode))) {
1526 blk = translate_block(ext2fs_file_acl_block(fs,
1527 &inode));
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301528 if (!blk)
1529 continue;
1530
JP Abgralle0ed7402014-03-19 19:08:39 -07001531 ext2fs_file_acl_block_set(fs, &inode, blk);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301532
1533 /*
1534 * Write the inode to disk so that inode table
1535 * resizing can work
1536 */
1537 retval = ext2fs_write_inode(fs, ino, &inode);
1538 if (retval)
1539 goto err_out;
1540 }
1541
JP Abgralle0ed7402014-03-19 19:08:39 -07001542 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301543 continue;
1544
JP Abgralle0ed7402014-03-19 19:08:39 -07001545 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001546 process_block, bmap);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301547 if (retval)
1548 goto err_out;
1549
1550 }
1551
1552err_out:
1553 ext2fs_free_mem(&block_buf);
1554
1555 return retval;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301556}
1557
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301558/*
1559 * We need to scan for inode and block bitmaps that may need to be
1560 * moved. This can take place if the filesystem was formatted for
1561 * RAID arrays using the mke2fs's extended option "stride".
1562 */
1563static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1564{
1565 dgrp_t i;
JP Abgralle0ed7402014-03-19 19:08:39 -07001566 blk64_t blk, new_blk;
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301567
1568 for (i = 0; i < fs->group_desc_count; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001569 blk = ext2fs_block_bitmap_loc(fs, i);
1570 if (ext2fs_test_block_bitmap2(bmap, blk)) {
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301571 new_blk = translate_block(blk);
1572 if (!new_blk)
1573 continue;
JP Abgralle0ed7402014-03-19 19:08:39 -07001574 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301575 }
1576
JP Abgralle0ed7402014-03-19 19:08:39 -07001577 blk = ext2fs_inode_bitmap_loc(fs, i);
1578 if (ext2fs_test_block_bitmap2(bmap, blk)) {
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301579 new_blk = translate_block(blk);
1580 if (!new_blk)
1581 continue;
JP Abgralle0ed7402014-03-19 19:08:39 -07001582 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301583 }
1584 }
1585 return 0;
1586}
1587
Theodore Ts'o721b3672008-06-07 11:51:33 -04001588static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301589{
1590 dgrp_t i;
JP Abgralle0ed7402014-03-19 19:08:39 -07001591 blk64_t blk;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301592 errcode_t retval;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001593 int new_ino_blks_per_grp;
1594 unsigned int j;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301595 char *old_itable = NULL, *new_itable = NULL;
1596 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001597 unsigned long old_ino_size;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301598 int old_itable_size, new_itable_size;
1599
1600 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001601 old_ino_size = EXT2_INODE_SIZE(fs->super);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301602
1603 new_ino_blks_per_grp = ext2fs_div_ceil(
1604 EXT2_INODES_PER_GROUP(fs->super) *
Theodore Ts'o721b3672008-06-07 11:51:33 -04001605 new_ino_size,
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301606 fs->blocksize);
1607
1608 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1609
1610 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1611 if (retval)
1612 return retval;
1613
1614 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1615 if (retval)
1616 goto err_out;
1617
1618 tmp_old_itable = old_itable;
1619 tmp_new_itable = new_itable;
1620
1621 for (i = 0; i < fs->group_desc_count; i++) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001622 blk = ext2fs_inode_table_loc(fs, i);
1623 retval = io_channel_read_blk64(fs->io, blk,
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301624 fs->inode_blocks_per_group, old_itable);
1625 if (retval)
1626 goto err_out;
1627
1628 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
Theodore Ts'o721b3672008-06-07 11:51:33 -04001629 memcpy(new_itable, old_itable, old_ino_size);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301630
Theodore Ts'o721b3672008-06-07 11:51:33 -04001631 memset(new_itable+old_ino_size, 0,
1632 new_ino_size - old_ino_size);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301633
Theodore Ts'o721b3672008-06-07 11:51:33 -04001634 new_itable += new_ino_size;
1635 old_itable += old_ino_size;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301636 }
1637
1638 /* reset the pointer */
1639 old_itable = tmp_old_itable;
1640 new_itable = tmp_new_itable;
1641
JP Abgralle0ed7402014-03-19 19:08:39 -07001642 retval = io_channel_write_blk64(fs->io, blk,
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301643 new_ino_blks_per_grp, new_itable);
1644 if (retval)
1645 goto err_out;
1646 }
1647
1648 /* Update the meta data */
1649 fs->inode_blocks_per_group = new_ino_blks_per_grp;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001650 fs->super->s_inode_size = new_ino_size;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301651
1652err_out:
1653 if (old_itable)
1654 ext2fs_free_mem(&old_itable);
1655
1656 if (new_itable)
1657 ext2fs_free_mem(&new_itable);
1658
1659 return retval;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301660}
1661
1662static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1663{
JP Abgralle0ed7402014-03-19 19:08:39 -07001664 blk64_t blk;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301665 ext2_ino_t ino;
1666 unsigned int group = 0;
1667 unsigned int count = 0;
1668 int total_free = 0;
1669 int group_free = 0;
1670
1671 /*
1672 * First calculate the block statistics
1673 */
1674 for (blk = fs->super->s_first_data_block;
JP Abgralle0ed7402014-03-19 19:08:39 -07001675 blk < ext2fs_blocks_count(fs->super); blk++) {
1676 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301677 group_free++;
1678 total_free++;
1679 }
1680 count++;
1681 if ((count == fs->super->s_blocks_per_group) ||
JP Abgralle0ed7402014-03-19 19:08:39 -07001682 (blk == ext2fs_blocks_count(fs->super)-1)) {
1683 ext2fs_bg_free_blocks_count_set(fs, group++,
1684 group_free);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301685 count = 0;
1686 group_free = 0;
1687 }
1688 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001689 total_free = EXT2FS_C2B(fs, total_free);
1690 ext2fs_free_blocks_count_set(fs->super, total_free);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301691
1692 /*
1693 * Next, calculate the inode statistics
1694 */
1695 group_free = 0;
1696 total_free = 0;
1697 count = 0;
1698 group = 0;
1699
1700 /* Protect loop from wrap-around if s_inodes_count maxed */
1701 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001702 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301703 group_free++;
1704 total_free++;
1705 }
1706 count++;
1707 if ((count == fs->super->s_inodes_per_group) ||
1708 (ino == fs->super->s_inodes_count)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001709 ext2fs_bg_free_inodes_count_set(fs, group++,
1710 group_free);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301711 count = 0;
1712 group_free = 0;
1713 }
1714 }
1715 fs->super->s_free_inodes_count = total_free;
1716 ext2fs_mark_super_dirty(fs);
1717 return 0;
1718}
1719
1720#define list_for_each_safe(pos, pnext, head) \
1721 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1722 pos = pnext, pnext = pos->next)
1723
Theodore Ts'o721b3672008-06-07 11:51:33 -04001724static void free_blk_move_list(void)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301725{
1726 struct list_head *entry, *tmp;
1727 struct blk_move *bmv;
1728
1729 list_for_each_safe(entry, tmp, &blk_move_list) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301730 bmv = list_entry(entry, struct blk_move, list);
1731 list_del(entry);
1732 ext2fs_free_mem(&bmv);
1733 }
Theodore Ts'o31f18152009-01-20 11:49:17 -05001734 return;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301735}
Theodore Ts'o721b3672008-06-07 11:51:33 -04001736
1737static int resize_inode(ext2_filsys fs, unsigned long new_size)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301738{
1739 errcode_t retval;
1740 int new_ino_blks_per_grp;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301741 ext2fs_block_bitmap bmap;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301742
JP Abgralle0ed7402014-03-19 19:08:39 -07001743 retval = ext2fs_read_inode_bitmap(fs);
1744 if (retval) {
1745 fputs(_("Failed to read inode bitmap\n"), stderr);
1746 return retval;
1747 }
1748 retval = ext2fs_read_block_bitmap(fs);
1749 if (retval) {
1750 fputs(_("Failed to read block bitmap\n"), stderr);
1751 return retval;
1752 }
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301753 INIT_LIST_HEAD(&blk_move_list);
1754
1755
1756 new_ino_blks_per_grp = ext2fs_div_ceil(
1757 EXT2_INODES_PER_GROUP(fs->super)*
Theodore Ts'o721b3672008-06-07 11:51:33 -04001758 new_size,
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301759 fs->blocksize);
1760
1761 /* We may change the file system.
1762 * Mark the file system as invalid so that
1763 * the user is prompted to run fsck.
1764 */
1765 fs->super->s_state &= ~EXT2_VALID_FS;
1766
1767 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1768 &bmap);
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301769 if (retval) {
1770 fputs(_("Failed to allocate block bitmap when "
1771 "increasing inode size\n"), stderr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301772 return retval;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301773 }
1774 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1775 if (retval) {
1776 fputs(_("Not enough space to increase inode size \n"), stderr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301777 goto err_out;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301778 }
1779 retval = move_block(fs, bmap);
1780 if (retval) {
1781 fputs(_("Failed to relocate blocks during inode resize \n"),
1782 stderr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301783 goto err_out;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301784 }
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001785 retval = inode_scan_and_fix(fs, bmap);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301786 if (retval)
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301787 goto err_out_undo;
Theodore Ts'o31f18152009-01-20 11:49:17 -05001788
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301789 retval = group_desc_scan_and_fix(fs, bmap);
1790 if (retval)
1791 goto err_out_undo;
1792
Theodore Ts'o721b3672008-06-07 11:51:33 -04001793 retval = expand_inode_table(fs, new_size);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301794 if (retval)
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301795 goto err_out_undo;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301796
1797 ext2fs_calculate_summary_stats(fs);
1798
1799 fs->super->s_state |= EXT2_VALID_FS;
1800 /* mark super block and block bitmap as dirty */
1801 ext2fs_mark_super_dirty(fs);
1802 ext2fs_mark_bb_dirty(fs);
1803
1804err_out:
1805 free_blk_move_list();
1806 ext2fs_free_block_bitmap(bmap);
1807
1808 return retval;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301809
1810err_out_undo:
1811 free_blk_move_list();
1812 ext2fs_free_block_bitmap(bmap);
1813 fputs(_("Error in resizing the inode size.\n"
1814 "Run e2undo to undo the "
1815 "file system changes. \n"), stderr);
1816
1817 return retval;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301818}
1819
1820static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1821{
1822 errcode_t retval = 0;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001823 const char *tdb_dir;
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001824 char *tdb_file;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001825 char *dev_name, *tmp_name;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301826
1827#if 0 /* FIXME!! */
1828 /*
1829 * Configuration via a conf file would be
1830 * nice
1831 */
1832 profile_get_string(profile, "scratch_files",
1833 "directory", 0, 0,
1834 &tdb_dir);
1835#endif
1836 tmp_name = strdup(name);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001837 if (!tmp_name) {
1838 alloc_fn_fail:
JP Abgralle0ed7402014-03-19 19:08:39 -07001839 com_err(program_name, ENOMEM, "%s",
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001840 _("Couldn't allocate memory for tdb filename\n"));
1841 return ENOMEM;
1842 }
Theodore Ts'o721b3672008-06-07 11:51:33 -04001843 dev_name = basename(tmp_name);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301844
1845 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1846 if (!tdb_dir)
Theodore Ts'oec43da22009-01-20 02:34:39 -05001847 tdb_dir = "/var/lib/e2fsprogs";
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301848
1849 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1850 access(tdb_dir, W_OK))
1851 return 0;
1852
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001853 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1854 if (!tdb_file)
1855 goto alloc_fn_fail;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001856 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301857
1858 if (!access(tdb_file, F_OK)) {
1859 if (unlink(tdb_file) < 0) {
1860 retval = errno;
1861 com_err(program_name, retval,
1862 _("while trying to delete %s"),
1863 tdb_file);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001864 free(tdb_file);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301865 return retval;
1866 }
1867 }
1868
1869 set_undo_io_backing_manager(*io_ptr);
1870 *io_ptr = undo_io_manager;
1871 set_undo_io_backup_file(tdb_file);
Theodore Ts'o5bf81ba2009-01-20 01:50:07 -05001872 printf(_("To undo the tune2fs operation please run "
Benno Schulenberg577b5c42008-07-17 23:24:09 +02001873 "the command\n e2undo %s %s\n\n"),
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301874 tdb_file, name);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001875 free(tdb_file);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301876 free(tmp_name);
1877 return retval;
1878}
Theodore Ts'o83238152001-01-09 00:16:26 +00001879
JP Abgralleeaf45a2014-11-20 23:19:25 -08001880#ifndef BUILD_AS_LIB
Theodore Ts'oec43da22009-01-20 02:34:39 -05001881int main(int argc, char **argv)
JP Abgralleeaf45a2014-11-20 23:19:25 -08001882#else
1883int tune2fs_main(int argc, char **argv)
1884#endif /* BUILD_AS_LIB */
Theodore Ts'o83238152001-01-09 00:16:26 +00001885{
1886 errcode_t retval;
1887 ext2_filsys fs;
1888 struct ext2_super_block *sb;
Theodore Ts'od887f882008-07-27 16:00:48 -04001889 io_manager io_ptr, io_ptr_orig = NULL;
JP Abgralle0ed7402014-03-19 19:08:39 -07001890 int rc = 0;
Theodore Ts'o83238152001-01-09 00:16:26 +00001891
1892#ifdef ENABLE_NLS
1893 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001894 setlocale(LC_CTYPE, "");
Theodore Ts'o83238152001-01-09 00:16:26 +00001895 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1896 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -07001897 set_com_err_gettext(gettext);
Theodore Ts'o83238152001-01-09 00:16:26 +00001898#endif
1899 if (argc && *argv)
1900 program_name = *argv;
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001901 add_error_table(&et_ext2_error_table);
Theodore Ts'o83238152001-01-09 00:16:26 +00001902
Theodore Ts'o14b596d2009-04-22 09:18:30 -04001903#ifdef CONFIG_BUILD_FINDFS
Theodore Ts'o118d7da2002-08-17 23:01:22 -04001904 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1905 do_findfs(argc, argv);
Theodore Ts'o14b596d2009-04-22 09:18:30 -04001906#endif
Theodore Ts'o83238152001-01-09 00:16:26 +00001907 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1908 parse_e2label_options(argc, argv);
1909 else
1910 parse_tune2fs_options(argc, argv);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001911
Theodore Ts'o2a29f132003-05-05 12:08:47 -04001912#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001913 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1914 io_ptr = test_io_manager;
1915 test_io_backing_manager = unix_io_manager;
1916 } else
Theodore Ts'o2a29f132003-05-05 12:08:47 -04001917#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001918 io_ptr = unix_io_manager;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301919
Theodore Ts'od887f882008-07-27 16:00:48 -04001920retry_open:
JP Abgralle0ed7402014-03-19 19:08:39 -07001921 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
1922 open_flag |= EXT2_FLAG_SKIP_MMP;
1923
1924 open_flag |= EXT2_FLAG_64BITS;
1925
1926 /* keep the filesystem struct around to dump MMP data */
1927 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
1928
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001929 retval = ext2fs_open2(device_name, io_options, open_flag,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -05001930 0, 0, io_ptr, &fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001931 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001932 com_err(program_name, retval,
1933 _("while trying to open %s"),
Theodore Ts'oec43da22009-01-20 02:34:39 -05001934 device_name);
JP Abgralle0ed7402014-03-19 19:08:39 -07001935 if (retval == EXT2_ET_MMP_FSCK_ON ||
1936 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
1937 dump_mmp_msg(fs->mmp_buf,
1938 _("If you are sure the filesystem "
1939 "is not in use on any node, run:\n"
1940 "'tune2fs -f -E clear_mmp {device}'\n"));
1941 else if (retval == EXT2_ET_MMP_FAILED)
1942 dump_mmp_msg(fs->mmp_buf, NULL);
1943 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
1944 fprintf(stderr,
1945 _("MMP block magic is bad. Try to fix it by "
1946 "running:\n'e2fsck -f %s'\n"), device_name);
1947 else if (retval != EXT2_ET_MMP_FAILED)
1948 fprintf(stderr, "%s",
1949 _("Couldn't find valid filesystem superblock.\n"));
1950
1951 ext2fs_free(fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001952 exit(1);
1953 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001954 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
Theodore Ts'od887f882008-07-27 16:00:48 -04001955
1956 if (I_flag && !io_ptr_orig) {
1957 /*
1958 * Check the inode size is right so we can issue an
1959 * error message and bail before setting up the tdb
1960 * file.
1961 */
1962 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
Theodore Ts'o9266fc72009-04-22 15:13:37 -04001963 fprintf(stderr, _("The inode size is already %lu\n"),
Theodore Ts'od887f882008-07-27 16:00:48 -04001964 new_inode_size);
JP Abgralle0ed7402014-03-19 19:08:39 -07001965 rc = 1;
1966 goto closefs;
Theodore Ts'od887f882008-07-27 16:00:48 -04001967 }
1968 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001969 fprintf(stderr, "%s",
1970 _("Shrinking inode size is not supported\n"));
1971 rc = 1;
1972 goto closefs;
1973 }
1974 if (new_inode_size > fs->blocksize) {
1975 fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
1976 new_inode_size, fs->blocksize);
1977 rc = 1;
1978 goto closefs;
Akira Fujitafab0d282013-04-21 23:14:28 -04001979 }
Theodore Ts'od887f882008-07-27 16:00:48 -04001980
1981 /*
1982 * If inode resize is requested use the
1983 * Undo I/O manager
1984 */
1985 io_ptr_orig = io_ptr;
1986 retval = tune2fs_setup_tdb(device_name, &io_ptr);
JP Abgralle0ed7402014-03-19 19:08:39 -07001987 if (retval) {
1988 rc = 1;
1989 goto closefs;
1990 }
Theodore Ts'od887f882008-07-27 16:00:48 -04001991 if (io_ptr != io_ptr_orig) {
1992 ext2fs_close(fs);
1993 goto retry_open;
1994 }
1995 }
1996
Theodore Ts'o83238152001-01-09 00:16:26 +00001997 sb = fs->super;
Theodore Ts'o058ad1c2007-06-18 18:26:50 -04001998 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'o2be8fe42008-07-10 10:49:59 -04001999
Theodore Ts'o83238152001-01-09 00:16:26 +00002000 if (print_label) {
2001 /* For e2label emulation */
Theodore Ts'oc8c071a2001-01-11 16:08:23 +00002002 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2003 sb->s_volume_name);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002004 remove_error_table(&et_ext2_error_table);
JP Abgralle0ed7402014-03-19 19:08:39 -07002005 goto closefs;
Theodore Ts'o83238152001-01-09 00:16:26 +00002006 }
Theodore Ts'o2be8fe42008-07-10 10:49:59 -04002007
Theodore Ts'od06863f2004-04-12 12:37:55 -04002008 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2009 if (retval) {
2010 com_err("ext2fs_check_if_mount", retval,
2011 _("while determining whether %s is mounted."),
2012 device_name);
JP Abgralle0ed7402014-03-19 19:08:39 -07002013 rc = 1;
2014 goto closefs;
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002015 }
Theodore Ts'o63985322001-01-03 17:02:13 +00002016 /* Normally we only need to write out the superblock */
2017 fs->flags |= EXT2_FLAG_SUPER_ONLY;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002018
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002019 if (c_flag) {
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002020 sb->s_max_mnt_count = max_mount_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002021 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05002022 printf(_("Setting maximal mount count to %d\n"),
2023 max_mount_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002024 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002025 if (C_flag) {
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002026 sb->s_mnt_count = mount_count;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002027 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05002028 printf(_("Setting current mount count to %d\n"), mount_count);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002029 }
2030 if (e_flag) {
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002031 sb->s_errors = errors;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002032 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05002033 printf(_("Setting error behavior to %d\n"), errors);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002034 }
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002035 if (g_flag) {
2036 sb->s_def_resgid = resgid;
Theodore Ts'of3db3561997-04-26 13:34:30 +00002037 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05002038 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002039 }
Theodore Ts'o818180c1998-06-27 05:11:14 +00002040 if (i_flag) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002041 if (interval >= (1ULL << 32)) {
2042 com_err(program_name, 0,
2043 _("interval between checks is too big (%lu)"),
2044 interval);
2045 rc = 1;
2046 goto closefs;
2047 }
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002048 sb->s_checkinterval = interval;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002049 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05002050 printf(_("Setting interval between checks to %lu seconds\n"),
2051 interval);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002052 }
Theodore Ts'o818180c1998-06-27 05:11:14 +00002053 if (m_flag) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002054 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2055 ext2fs_blocks_count(sb) / 100.0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002056 ext2fs_mark_super_dirty(fs);
JP Abgralle0ed7402014-03-19 19:08:39 -07002057 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2058 reserved_ratio, ext2fs_r_blocks_count(sb));
Theodore Ts'o3839e651997-04-26 13:21:57 +00002059 }
Theodore Ts'o818180c1998-06-27 05:11:14 +00002060 if (r_flag) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002061 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05002062 com_err(program_name, 0,
JP Abgralle0ed7402014-03-19 19:08:39 -07002063 _("reserved blocks count is too big (%llu)"),
Theodore Ts'oec43da22009-01-20 02:34:39 -05002064 reserved_blocks);
JP Abgralle0ed7402014-03-19 19:08:39 -07002065 rc = 1;
2066 goto closefs;
Theodore Ts'of3db3561997-04-26 13:34:30 +00002067 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002068 ext2fs_r_blocks_count_set(sb, reserved_blocks);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002069 ext2fs_mark_super_dirty(fs);
JP Abgralle0ed7402014-03-19 19:08:39 -07002070 printf(_("Setting reserved blocks count to %llu\n"),
Theodore Ts'oec43da22009-01-20 02:34:39 -05002071 reserved_blocks);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002072 }
Theodore Ts'o521e3681997-04-29 17:48:10 +00002073 if (s_flag == 1) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00002074 if (sb->s_feature_ro_compat &
2075 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
Theodore Ts'o54434922003-12-07 01:28:50 -05002076 fputs(_("\nThe filesystem already has sparse "
2077 "superblocks.\n"), stderr);
Theodore Ts'o521e3681997-04-29 17:48:10 +00002078 else {
2079 sb->s_feature_ro_compat |=
2080 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002081 sb->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o521e3681997-04-29 17:48:10 +00002082 ext2fs_mark_super_dirty(fs);
Theodore Ts'od9c56d32000-02-08 00:47:55 +00002083 printf(_("\nSparse superblock flag set. %s"),
Theodore Ts'oa4fa1002000-02-08 21:35:41 +00002084 _(please_fsck));
Theodore Ts'o521e3681997-04-29 17:48:10 +00002085 }
Theodore Ts'o521e3681997-04-29 17:48:10 +00002086 }
2087 if (s_flag == 0) {
Theodore Ts'off662d52008-02-28 21:26:01 -05002088 fputs(_("\nClearing the sparse superflag not supported.\n"),
2089 stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -07002090 rc = 1;
2091 goto closefs;
Theodore Ts'o521e3681997-04-29 17:48:10 +00002092 }
Theodore Ts'od4de4aa2001-12-26 08:58:01 -05002093 if (T_flag) {
2094 sb->s_lastcheck = last_check_time;
2095 ext2fs_mark_super_dirty(fs);
2096 printf(_("Setting time filesystem last checked to %s\n"),
2097 ctime(&last_check_time));
2098 }
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00002099 if (u_flag) {
2100 sb->s_def_resuid = resuid;
Theodore Ts'of3db3561997-04-26 13:34:30 +00002101 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05002102 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
Theodore Ts'of3db3561997-04-26 13:34:30 +00002103 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002104 if (L_flag) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00002105 if (strlen(new_label) > sizeof(sb->s_volume_name))
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002106 fputs(_("Warning: label too long, truncating.\n"),
Theodore Ts'o54434922003-12-07 01:28:50 -05002107 stderr);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002108 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2109 strncpy(sb->s_volume_name, new_label,
2110 sizeof(sb->s_volume_name));
2111 ext2fs_mark_super_dirty(fs);
2112 }
2113 if (M_flag) {
2114 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2115 strncpy(sb->s_last_mounted, new_last_mounted,
2116 sizeof(sb->s_last_mounted));
2117 ext2fs_mark_super_dirty(fs);
2118 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002119 if (mntopts_cmd) {
2120 rc = update_mntopts(fs, mntopts_cmd);
2121 if (rc)
2122 goto closefs;
2123 }
2124 if (features_cmd) {
2125 rc = update_feature_set(fs, features_cmd);
2126 if (rc)
2127 goto closefs;
2128 }
2129 if (extended_cmd) {
2130 rc = parse_extended_opts(fs, extended_cmd);
2131 if (rc)
2132 goto closefs;
2133 if (clear_mmp && !f_flag) {
2134 fputs(_("Error in using clear_mmp. "
2135 "It must be used with -f\n"),
2136 stderr);
2137 goto closefs;
2138 }
2139 }
2140 if (clear_mmp) {
2141 rc = ext2fs_mmp_clear(fs);
2142 goto closefs;
2143 }
2144 if (journal_size || journal_device) {
2145 rc = add_journal(fs);
2146 if (rc)
2147 goto closefs;
2148 }
2149
2150 if (Q_flag) {
2151 if (mount_flags & EXT2_MF_MOUNTED) {
2152 fputs(_("The quota feature may only be changed when "
2153 "the filesystem is unmounted.\n"), stderr);
2154 rc = 1;
2155 goto closefs;
2156 }
2157 handle_quota_options(fs);
2158 }
Aditya Kali771e8db2011-07-20 11:40:05 -07002159
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002160 if (U_flag) {
Theodore Ts'o9d4a4dc2008-11-14 17:42:27 -05002161 int set_csum = 0;
2162 dgrp_t i;
2163
2164 if (sb->s_feature_ro_compat &
2165 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05002166 /*
JP Abgralle0ed7402014-03-19 19:08:39 -07002167 * Changing the UUID requires rewriting all metadata,
2168 * which can race with a mounted fs. Don't allow that.
2169 */
2170 if (mount_flags & EXT2_MF_MOUNTED) {
2171 fputs(_("The UUID may only be "
2172 "changed when the filesystem is "
2173 "unmounted.\n"), stderr);
2174 exit(1);
2175 }
2176 if (check_fsck_needed(fs))
2177 exit(1);
2178
2179 /*
Theodore Ts'o9d4a4dc2008-11-14 17:42:27 -05002180 * Determine if the block group checksums are
2181 * correct so we know whether or not to set
2182 * them later on.
2183 */
2184 for (i = 0; i < fs->group_desc_count; i++)
2185 if (!ext2fs_group_desc_csum_verify(fs, i))
2186 break;
2187 if (i >= fs->group_desc_count)
2188 set_csum = 1;
2189 }
Theodore Ts'o4d0f2282001-04-23 20:58:03 +00002190 if ((strcasecmp(new_UUID, "null") == 0) ||
2191 (strcasecmp(new_UUID, "clear") == 0)) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002192 uuid_clear(sb->s_uuid);
Theodore Ts'o63985322001-01-03 17:02:13 +00002193 } else if (strcasecmp(new_UUID, "time") == 0) {
2194 uuid_generate_time(sb->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002195 } else if (strcasecmp(new_UUID, "random") == 0) {
2196 uuid_generate(sb->s_uuid);
2197 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002198 com_err(program_name, 0, "%s",
2199 _("Invalid UUID format\n"));
2200 rc = 1;
2201 goto closefs;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002202 }
Theodore Ts'o9d4a4dc2008-11-14 17:42:27 -05002203 if (set_csum) {
2204 for (i = 0; i < fs->group_desc_count; i++)
2205 ext2fs_group_desc_csum_set(fs, i);
2206 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2207 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002208 ext2fs_mark_super_dirty(fs);
2209 }
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05302210 if (I_flag) {
2211 if (mount_flags & EXT2_MF_MOUNTED) {
2212 fputs(_("The inode size may only be "
2213 "changed when the filesystem is "
2214 "unmounted.\n"), stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -07002215 rc = 1;
2216 goto closefs;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05302217 }
Theodore Ts'o5bf81ba2009-01-20 01:50:07 -05002218 if (fs->super->s_feature_incompat &
2219 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05002220 fputs(_("Changing the inode size not supported for "
2221 "filesystems with the flex_bg\n"
2222 "feature enabled.\n"),
Theodore Ts'o5bf81ba2009-01-20 01:50:07 -05002223 stderr);
JP Abgralle0ed7402014-03-19 19:08:39 -07002224 rc = 1;
2225 goto closefs;
Theodore Ts'o5bf81ba2009-01-20 01:50:07 -05002226 }
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05302227 /*
2228 * We want to update group descriptor also
2229 * with the new free inode count
2230 */
2231 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05302232 if (resize_inode(fs, new_inode_size) == 0) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05002233 printf(_("Setting inode size %lu\n"),
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05302234 new_inode_size);
JP Abgralle0ed7402014-03-19 19:08:39 -07002235 } else {
2236 printf("%s", _("Failed to change inode size\n"));
2237 rc = 1;
2238 goto closefs;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05302239 }
2240 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00002241
Theodore Ts'o3839e651997-04-26 13:21:57 +00002242 if (l_flag)
Theodore Ts'oec43da22009-01-20 02:34:39 -05002243 list_super(sb);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05002244 if (stride_set) {
2245 sb->s_raid_stride = stride;
2246 ext2fs_mark_super_dirty(fs);
2247 printf(_("Setting stride size to %d\n"), stride);
2248 }
2249 if (stripe_width_set) {
2250 sb->s_raid_stripe_width = stripe_width;
2251 ext2fs_mark_super_dirty(fs);
2252 printf(_("Setting stripe width to %d\n"), stripe_width);
2253 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002254 if (ext_mount_opts) {
2255 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2256 sizeof(fs->super->s_mount_opts));
2257 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2258 ext2fs_mark_super_dirty(fs);
2259 printf(_("Setting extended default mount options to '%s'\n"),
2260 ext_mount_opts);
2261 free(ext_mount_opts);
2262 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -04002263 free(device_name);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002264 remove_error_table(&et_ext2_error_table);
JP Abgralle0ed7402014-03-19 19:08:39 -07002265
2266closefs:
JP Abgralleeaf45a2014-11-20 23:19:25 -08002267if (rc) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002268 ext2fs_mmp_stop(fs);
JP Abgralleeaf45a2014-11-20 23:19:25 -08002269#ifndef BUILD_AS_LIB
JP Abgralle0ed7402014-03-19 19:08:39 -07002270 exit(1);
JP Abgralleeaf45a2014-11-20 23:19:25 -08002271#endif
JP Abgralle0ed7402014-03-19 19:08:39 -07002272 }
Theodore Ts'oec43da22009-01-20 02:34:39 -05002273 return (ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002274}