blob: 41a063826659f0ba0a024f1bc9da45c252d0e4c2 [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() */
Matthias Andreeb969b1b2003-12-28 13:04:35 +010029#define _BSD_SOURCE /* for inclusion of strcasecmp() */
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <fcntl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000031#include <grp.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000032#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <getopt.h>
Theodore Ts'o373b8332000-04-03 16:22:35 +000034#else
35extern char *optarg;
36extern int optind;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000037#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000038#include <pwd.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000039#include <stdio.h>
Theodore Ts'of38cf3c2008-09-01 11:17:29 -040040#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000041#include <stdlib.h>
Theodore Ts'of38cf3c2008-09-01 11:17:29 -040042#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000043#include <string.h>
44#include <time.h>
45#include <unistd.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000046#include <sys/types.h>
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +053047#include <libgen.h>
Theodore Ts'o36585792008-06-07 22:07:50 -040048#include <limits.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
Theodore Ts'o54c637d2001-05-14 11:45:38 +000050#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "ext2fs/ext2fs.h"
52#include "et/com_err.h"
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000053#include "uuid/uuid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000054#include "e2p/e2p.h"
Theodore Ts'odc2ec522001-01-18 01:51:15 +000055#include "jfs_user.h"
Theodore Ts'o63985322001-01-03 17:02:13 +000056#include "util.h"
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050057#include "blkid/blkid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000058
59#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000060#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000061
Theodore Ts'oec43da22009-01-20 02:34:39 -050062const char *program_name = "tune2fs";
63char *device_name;
64char *new_label, *new_last_mounted, *new_UUID;
65char *io_options;
Theodore Ts'o4d0f2282001-04-23 20:58:03 +000066static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
Theodore Ts'od4de4aa2001-12-26 08:58:01 -050067static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +053068static int I_flag;
Theodore Ts'od4de4aa2001-12-26 08:58:01 -050069static time_t last_check_time;
Theodore Ts'o83238152001-01-09 00:16:26 +000070static int print_label;
Theodore Ts'o63985322001-01-03 17:02:13 +000071static int max_mount_count, mount_count, mount_flags;
Andreas Dilgerce911142005-07-06 11:50:08 -050072static unsigned long interval, reserved_blocks;
73static double reserved_ratio;
Theodore Ts'o63985322001-01-03 17:02:13 +000074static unsigned long resgid, resuid;
75static unsigned short errors;
Theodore Ts'o83238152001-01-09 00:16:26 +000076static int open_flag;
77static char *features_cmd;
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -040078static char *mntopts_cmd;
Theodore Ts'o0c17cb22008-02-18 22:56:25 -050079static int stride, stripe_width;
80static int stride_set, stripe_width_set;
Theodore Ts'o6cb27402008-01-26 19:06:35 -050081static char *extended_cmd;
Theodore Ts'o721b3672008-06-07 11:51:33 -040082static unsigned long new_inode_size;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083
Theodore Ts'o63985322001-01-03 17:02:13 +000084int journal_size, journal_flags;
85char *journal_device;
86
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +053087static struct list_head blk_move_list;
88
89struct blk_move {
90 struct list_head list;
91 blk_t old_loc;
92 blk_t new_loc;
93};
94
95
Theodore Ts'o63985322001-01-03 17:02:13 +000096static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000097
Theodore Ts'o14b596d2009-04-22 09:18:30 -040098#ifdef CONFIG_BUILD_FINDFS
Theodore Ts'o3e699062002-10-13 23:56:28 -040099void do_findfs(int argc, char **argv);
Theodore Ts'o14b596d2009-04-22 09:18:30 -0400100#endif
Theodore Ts'o3e699062002-10-13 23:56:28 -0400101
Theodore Ts'o818180c1998-06-27 05:11:14 +0000102static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103{
Theodore Ts'ob21e38a2001-01-01 15:26:58 +0000104 fprintf(stderr,
Theodore Ts'obb145b02005-06-20 08:35:27 -0400105 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
Theodore Ts'ob21e38a2001-01-01 15:26:58 +0000106 "[-g group]\n"
Theodore Ts'off662d52008-02-28 21:26:01 -0500107 "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
108 "\t[-m reserved_blocks_percent] "
109 "[-o [^]mount_options[,...]] \n"
110 "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
111 "[-L volume_label]\n"
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500112 "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
113 "\t[-E extended-option[,...]] [-T last_check_time] "
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530114 "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
Theodore Ts'oec43da22009-01-20 02:34:39 -0500115 exit(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116}
117
Theodore Ts'o896938d1999-10-23 01:04:50 +0000118static __u32 ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500119 /* Compat */
Theodore Ts'o843049c2002-09-22 15:37:40 -0400120 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'o558df542008-02-27 15:01:19 -0500121 EXT2_FEATURE_COMPAT_DIR_INDEX,
122 /* Incompat */
Theodore Ts'oa49670e2008-02-27 18:53:34 -0500123 EXT2_FEATURE_INCOMPAT_FILETYPE |
124 EXT3_FEATURE_INCOMPAT_EXTENTS |
Jose R. Santosc2d43002007-08-13 23:32:57 -0500125 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500126 /* R/O compat */
127 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400128 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
129 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
130 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500131 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
Theodore Ts'o558df542008-02-27 15:01:19 -0500132 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
Theodore Ts'o896938d1999-10-23 01:04:50 +0000133};
134
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500135static __u32 clear_ok_features[3] = {
Theodore Ts'o558df542008-02-27 15:01:19 -0500136 /* Compat */
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500137 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
Theodore Ts'o037914e2008-02-26 17:31:06 -0500138 EXT2_FEATURE_COMPAT_RESIZE_INODE |
Theodore Ts'o558df542008-02-27 15:01:19 -0500139 EXT2_FEATURE_COMPAT_DIR_INDEX,
140 /* Incompat */
Theodore Ts'oa49670e2008-02-27 18:53:34 -0500141 EXT2_FEATURE_INCOMPAT_FILETYPE |
142 EXT4_FEATURE_INCOMPAT_FLEX_BG,
Theodore Ts'o558df542008-02-27 15:01:19 -0500143 /* R/O compat */
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500144 EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400145 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
146 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
147 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500148 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
Theodore Ts'o896938d1999-10-23 01:04:50 +0000149};
150
Theodore Ts'o63985322001-01-03 17:02:13 +0000151/*
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000152 * Remove an external journal from the filesystem
153 */
154static void remove_journal_device(ext2_filsys fs)
155{
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500156 char *journal_path;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000157 ext2_filsys jfs;
158 char buf[1024];
159 journal_superblock_t *jsb;
160 int i, nr_users;
161 errcode_t retval;
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000162 int commit_remove_journal = 0;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400163 io_manager io_ptr;
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000164
165 if (f_flag)
166 commit_remove_journal = 1; /* force removal even if error */
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000167
Andreas Dilger2d155762001-08-17 03:48:11 -0600168 uuid_unparse(fs->super->s_journal_uuid, buf);
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500169 journal_path = blkid_get_devname(NULL, "UUID", buf);
Andreas Dilger2d155762001-08-17 03:48:11 -0600170
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500171 if (!journal_path) {
172 journal_path =
Andreas Dilger2d155762001-08-17 03:48:11 -0600173 ext2fs_find_block_device(fs->super->s_journal_dev);
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500174 if (!journal_path)
Andreas Dilger2d155762001-08-17 03:48:11 -0600175 return;
176 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000177
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400178#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400179 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
180 io_ptr = test_io_manager;
181 test_io_backing_manager = unix_io_manager;
182 } else
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400183#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400184 io_ptr = unix_io_manager;
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500185 retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000186 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400187 fs->blocksize, io_ptr, &jfs);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000188 if (retval) {
189 com_err(program_name, retval,
190 _("while trying to open external journal"));
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000191 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000192 }
193 if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Theodore Ts'o7141b542001-08-15 19:17:37 -0400194 fprintf(stderr, _("%s is not a journal device.\n"),
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500195 journal_path);
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000196 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000197 }
198
199 /* Get the journal superblock */
200 if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
201 com_err(program_name, retval,
202 _("while reading journal superblock"));
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000203 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000204 }
205
206 jsb = (journal_superblock_t *) buf;
207 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
208 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500209 fputs(_("Journal superblock not found!\n"), stderr);
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000210 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000211 }
212
213 /* Find the filesystem UUID */
214 nr_users = ntohl(jsb->s_nr_users);
Theodore Ts'oec43da22009-01-20 02:34:39 -0500215 for (i = 0; i < nr_users; i++) {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000216 if (memcmp(fs->super->s_uuid,
217 &jsb->s_users[i*16], 16) == 0)
218 break;
219 }
220 if (i >= nr_users) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400221 fputs(_("Filesystem's UUID not found on journal device.\n"),
Theodore Ts'o54434922003-12-07 01:28:50 -0500222 stderr);
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000223 commit_remove_journal = 1;
224 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000225 }
226 nr_users--;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500227 for (i = 0; i < nr_users; i++)
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000228 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
229 jsb->s_nr_users = htonl(nr_users);
230
231 /* Write back the journal superblock */
232 if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
233 com_err(program_name, retval,
234 "while writing journal superblock.");
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000235 goto no_valid_journal;
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000236 }
237
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000238 commit_remove_journal = 1;
239
240no_valid_journal:
241 if (commit_remove_journal == 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500242 fputs(_("Journal NOT removed\n"), stderr);
Theodore Ts'o4d0f2282001-04-23 20:58:03 +0000243 exit(1);
244 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000245 fs->super->s_journal_dev = 0;
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500246 uuid_clear(fs->super->s_journal_uuid);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000247 ext2fs_mark_super_dirty(fs);
Theodore Ts'o54434922003-12-07 01:28:50 -0500248 fputs(_("Journal removed\n"), stdout);
Theodore Ts'o4ea7bd02001-12-16 23:23:37 -0500249 free(journal_path);
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000250}
251
Theodore Ts'o194686b2001-07-31 12:03:23 -0400252/* Helper function for remove_journal_inode */
253static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400254 int blockcnt EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500255 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o194686b2001-07-31 12:03:23 -0400256{
257 blk_t block;
258 int group;
259
260 block = *blocknr;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500261 ext2fs_unmark_block_bitmap(fs->block_map, block);
Theodore Ts'o194686b2001-07-31 12:03:23 -0400262 group = ext2fs_group_of_blk(fs, block);
263 fs->group_desc[group].bg_free_blocks_count++;
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500264 ext2fs_group_desc_csum_set(fs, group);
Theodore Ts'o194686b2001-07-31 12:03:23 -0400265 fs->super->s_free_blocks_count++;
266 return 0;
267}
268
269/*
270 * Remove the journal inode from the filesystem
271 */
272static void remove_journal_inode(ext2_filsys fs)
273{
274 struct ext2_inode inode;
275 errcode_t retval;
276 ino_t ino = fs->super->s_journal_inum;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400277
Theodore Ts'o194686b2001-07-31 12:03:23 -0400278 retval = ext2fs_read_inode(fs, ino, &inode);
279 if (retval) {
280 com_err(program_name, retval,
281 _("while reading journal inode"));
282 exit(1);
283 }
284 if (ino == EXT2_JOURNAL_INO) {
285 retval = ext2fs_read_bitmaps(fs);
286 if (retval) {
287 com_err(program_name, retval,
288 _("while reading bitmaps"));
289 exit(1);
290 }
Theodore Ts'o43323be2008-02-07 14:37:17 -0500291 retval = ext2fs_block_iterate(fs, ino,
292 BLOCK_FLAG_READ_ONLY, NULL,
Theodore Ts'o194686b2001-07-31 12:03:23 -0400293 release_blocks_proc, NULL);
294 if (retval) {
295 com_err(program_name, retval,
296 _("while clearing journal inode"));
297 exit(1);
298 }
299 memset(&inode, 0, sizeof(inode));
300 ext2fs_mark_bb_dirty(fs);
Theodore Ts'o194686b2001-07-31 12:03:23 -0400301 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
302 } else
303 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
304 retval = ext2fs_write_inode(fs, ino, &inode);
305 if (retval) {
306 com_err(program_name, retval,
307 _("while writing journal inode"));
308 exit(1);
309 }
310 fs->super->s_journal_inum = 0;
311 ext2fs_mark_super_dirty(fs);
312}
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000313
314/*
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -0400315 * Update the default mount options
316 */
317static void update_mntopts(ext2_filsys fs, char *mntopts)
318{
Theodore Ts'oec43da22009-01-20 02:34:39 -0500319 struct ext2_super_block *sb = fs->super;
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -0400320
321 if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
322 fprintf(stderr, _("Invalid mount option set: %s\n"),
323 mntopts);
324 exit(1);
325 }
326 ext2fs_mark_super_dirty(fs);
327}
328
329/*
Theodore Ts'o63985322001-01-03 17:02:13 +0000330 * Update the feature set as provided by the user.
331 */
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000332static void update_feature_set(ext2_filsys fs, char *features)
Theodore Ts'o63985322001-01-03 17:02:13 +0000333{
Theodore Ts'oec43da22009-01-20 02:34:39 -0500334 struct ext2_super_block *sb = fs->super;
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500335 __u32 old_features[3];
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500336 int type_err;
337 unsigned int mask_err;
Theodore Ts'o2eb3b202004-04-07 09:27:36 -0400338
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500339#define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
340 ((&sb->s_feature_compat)[(type)] & (mask)))
341#define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
342 !((&sb->s_feature_compat)[(type)] & (mask)))
343#define FEATURE_CHANGED(type, mask) ((mask) & \
344 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
Theodore Ts'o63985322001-01-03 17:02:13 +0000345
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500346 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
347 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
348 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
349
Theodore Ts'o7c4a2ef2008-02-26 14:27:57 -0500350 if (e2p_edit_feature2(features, &sb->s_feature_compat,
351 ok_features, clear_ok_features,
352 &type_err, &mask_err)) {
353 if (!mask_err)
354 fprintf(stderr,
355 _("Invalid filesystem option set: %s\n"),
356 features);
357 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
358 fprintf(stderr, _("Clearing filesystem feature '%s' "
359 "not supported.\n"),
360 e2p_feature2string(type_err &
361 E2P_FEATURE_TYPE_MASK,
362 mask_err));
363 else
364 fprintf(stderr, _("Setting filesystem feature '%s' "
365 "not supported.\n"),
366 e2p_feature2string(type_err, mask_err));
Theodore Ts'o63985322001-01-03 17:02:13 +0000367 exit(1);
368 }
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500369
370 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o63985322001-01-03 17:02:13 +0000371 if ((mount_flags & EXT2_MF_MOUNTED) &&
372 !(mount_flags & EXT2_MF_READONLY)) {
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400373 fputs(_("The has_journal feature may only be "
Theodore Ts'o54434922003-12-07 01:28:50 -0500374 "cleared when the filesystem is\n"
375 "unmounted or mounted "
376 "read-only.\n"), stderr);
Theodore Ts'o63985322001-01-03 17:02:13 +0000377 exit(1);
378 }
379 if (sb->s_feature_incompat &
380 EXT3_FEATURE_INCOMPAT_RECOVER) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500381 fputs(_("The needs_recovery flag is set. "
382 "Please run e2fsck before clearing\n"
383 "the has_journal flag.\n"), stderr);
Theodore Ts'o63985322001-01-03 17:02:13 +0000384 exit(1);
385 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000386 if (sb->s_journal_inum) {
Theodore Ts'o194686b2001-07-31 12:03:23 -0400387 remove_journal_inode(fs);
Theodore Ts'o63985322001-01-03 17:02:13 +0000388 }
Theodore Ts'ode49f012001-07-30 16:31:30 -0400389 if (sb->s_journal_dev) {
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000390 remove_journal_device(fs);
Theodore Ts'ode49f012001-07-30 16:31:30 -0400391 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000392 }
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500393
394 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Theodore Ts'o63985322001-01-03 17:02:13 +0000395 /*
396 * If adding a journal flag, let the create journal
Benno Schulenberg7a0516a2008-07-17 23:58:35 +0200397 * code below handle setting the flag and creating the
398 * journal. We supply a default size if necessary.
Theodore Ts'o63985322001-01-03 17:02:13 +0000399 */
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000400 if (!journal_size)
401 journal_size = -1;
Theodore Ts'o08dd8302001-01-14 16:25:58 +0000402 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'o63985322001-01-03 17:02:13 +0000403 }
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500404
405 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
Theodore Ts'o843049c2002-09-22 15:37:40 -0400406 if (!sb->s_def_hash_version)
Theodore Ts'od1070d92008-08-28 23:09:35 -0400407 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
Theodore Ts'o843049c2002-09-22 15:37:40 -0400408 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
409 uuid_generate((unsigned char *) sb->s_hash_seed);
410 }
Theodore Ts'odc2ec522001-01-18 01:51:15 +0000411
Theodore Ts'oa49670e2008-02-27 18:53:34 -0500412 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
Jose R. Santosc2d43002007-08-13 23:32:57 -0500413 if (ext2fs_check_desc(fs)) {
414 fputs(_("Clearing the flex_bg flag would "
415 "cause the the filesystem to be\n"
416 "inconsistent.\n"), stderr);
417 exit(1);
418 }
419 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000420
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400421 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
422 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
423 if ((mount_flags & EXT2_MF_MOUNTED) &&
424 !(mount_flags & EXT2_MF_READONLY)) {
425 fputs(_("The huge_file feature may only be "
426 "cleared when the filesystem is\n"
427 "unmounted or mounted "
428 "read-only.\n"), stderr);
429 exit(1);
430 }
431 }
432
Theodore Ts'o63985322001-01-03 17:02:13 +0000433 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
434 (sb->s_feature_compat || sb->s_feature_ro_compat ||
435 sb->s_feature_incompat))
436 ext2fs_update_dynamic_rev(fs);
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500437
438 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
439 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
Jose R. Santos4e988cb2007-10-21 21:03:41 -0500440 FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
441 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) ||
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400442 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
443 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500444 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
Theodore Ts'o037914e2008-02-26 17:31:06 -0500445 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
446 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
Theodore Ts'o558df542008-02-27 15:01:19 -0500447 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
448 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
449 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
Theodore Ts'o63985322001-01-03 17:02:13 +0000450 sb->s_state &= ~EXT2_VALID_FS;
451 printf("\n%s\n", _(please_fsck));
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400452 if (mount_flags & EXT2_MF_READONLY)
453 printf(_("(and reboot afterwards!)\n"));
Theodore Ts'o63985322001-01-03 17:02:13 +0000454 }
Theodore Ts'o885bf6b2008-02-26 15:08:14 -0500455
456 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
457 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
458 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
Theodore Ts'o2eb3b202004-04-07 09:27:36 -0400459 ext2fs_mark_super_dirty(fs);
Theodore Ts'o63985322001-01-03 17:02:13 +0000460}
461
462/*
463 * Add a journal to the filesystem.
464 */
465static void add_journal(ext2_filsys fs)
466{
467 unsigned long journal_blocks;
468 errcode_t retval;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000469 ext2_filsys jfs;
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400470 io_manager io_ptr;
Theodore Ts'o63985322001-01-03 17:02:13 +0000471
472 if (fs->super->s_feature_compat &
473 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500474 fputs(_("The filesystem already has a journal.\n"), stderr);
Andreas Dilger2d155762001-08-17 03:48:11 -0600475 goto err;
Theodore Ts'o63985322001-01-03 17:02:13 +0000476 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000477 if (journal_device) {
478 check_plausibility(journal_device);
479 check_mount(journal_device, 0, _("journal"));
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400480#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400481 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
482 io_ptr = test_io_manager;
483 test_io_backing_manager = unix_io_manager;
484 } else
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400485#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -0400486 io_ptr = unix_io_manager;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000487 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
488 EXT2_FLAG_JOURNAL_DEV_OK, 0,
Theodore Ts'o2a29f132003-05-05 12:08:47 -0400489 fs->blocksize, io_ptr, &jfs);
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000490 if (retval) {
491 com_err(program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +0000492 _("\n\twhile trying to open journal on %s\n"),
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000493 journal_device);
Andreas Dilger2d155762001-08-17 03:48:11 -0600494 goto err;
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000495 }
Theodore Ts'o63985322001-01-03 17:02:13 +0000496 printf(_("Creating journal on device %s: "),
497 journal_device);
Theodore Ts'o4055ef72001-01-14 16:11:14 +0000498 fflush(stdout);
Andreas Dilger2d155762001-08-17 03:48:11 -0600499
Theodore Ts'o16ed5b32001-01-16 07:47:31 +0000500 retval = ext2fs_add_journal_device(fs, jfs);
Andreas Dilger2d155762001-08-17 03:48:11 -0600501 ext2fs_close(jfs);
Theodore Ts'o63985322001-01-03 17:02:13 +0000502 if (retval) {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500503 com_err(program_name, retval,
504 _("while adding filesystem to journal on %s"),
505 journal_device);
Andreas Dilger2d155762001-08-17 03:48:11 -0600506 goto err;
Theodore Ts'o63985322001-01-03 17:02:13 +0000507 }
Theodore Ts'o54434922003-12-07 01:28:50 -0500508 fputs(_("done\n"), stdout);
Theodore Ts'o63985322001-01-03 17:02:13 +0000509 } else if (journal_size) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500510 fputs(_("Creating journal inode: "), stdout);
Theodore Ts'o63985322001-01-03 17:02:13 +0000511 fflush(stdout);
Theodore Ts'o2537b6d2001-03-26 20:07:13 +0000512 journal_blocks = figure_journal_size(journal_size, fs);
513
Theodore Ts'o63985322001-01-03 17:02:13 +0000514 retval = ext2fs_add_journal_inode(fs, journal_blocks,
515 journal_flags);
516 if (retval) {
Theodore Ts'o7141b542001-08-15 19:17:37 -0400517 fprintf(stderr, "\n");
Theodore Ts'o63985322001-01-03 17:02:13 +0000518 com_err(program_name, retval,
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +0000519 _("\n\twhile trying to create journal file"));
Theodore Ts'o63985322001-01-03 17:02:13 +0000520 exit(1);
Theodore Ts'o1d08d9b2001-04-17 01:01:49 +0000521 } else
Theodore Ts'o54434922003-12-07 01:28:50 -0500522 fputs(_("done\n"), stdout);
Theodore Ts'o63985322001-01-03 17:02:13 +0000523 /*
524 * If the filesystem wasn't mounted, we need to force
525 * the block group descriptors out.
526 */
527 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
528 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
529 }
Theodore Ts'o66cf2f62001-06-14 06:42:44 +0000530 print_check_message(fs);
Andreas Dilger2d155762001-08-17 03:48:11 -0600531 return;
532
533err:
Jim Meyering45e338f2009-02-23 18:07:50 +0100534 free(journal_device);
Andreas Dilger2d155762001-08-17 03:48:11 -0600535 exit(1);
Theodore Ts'o63985322001-01-03 17:02:13 +0000536}
537
Theodore Ts'o896938d1999-10-23 01:04:50 +0000538
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000539static void parse_e2label_options(int argc, char ** argv)
Theodore Ts'o83238152001-01-09 00:16:26 +0000540{
541 if ((argc < 2) || (argc > 3)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500542 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
Theodore Ts'o83238152001-01-09 00:16:26 +0000543 exit(1);
544 }
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500545 io_options = strchr(argv[1], '?');
546 if (io_options)
547 *io_options++ = 0;
Theodore Ts'o332f2c22003-03-06 12:58:33 -0500548 device_name = blkid_get_devname(NULL, argv[1], NULL);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500549 if (!device_name) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400550 com_err("e2label", 0, _("Unable to resolve '%s'"),
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500551 argv[1]);
552 exit(1);
553 }
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400554 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
Theodore Ts'o83238152001-01-09 00:16:26 +0000555 if (argc == 3) {
Theodore Ts'o0ddfd9a2008-02-09 22:22:38 -0500556 open_flag |= EXT2_FLAG_RW;
Theodore Ts'o83238152001-01-09 00:16:26 +0000557 L_flag = 1;
558 new_label = argv[2];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400559 } else
Theodore Ts'o83238152001-01-09 00:16:26 +0000560 print_label++;
561}
562
Theodore Ts'od4de4aa2001-12-26 08:58:01 -0500563static time_t parse_time(char *str)
564{
565 struct tm ts;
566
567 if (strcmp(str, "now") == 0) {
568 return (time(0));
569 }
570 memset(&ts, 0, sizeof(ts));
Theodore Ts'obc7c14e2003-05-03 16:40:09 -0400571#ifdef HAVE_STRPTIME
Theodore Ts'o690e6932004-12-21 20:40:08 -0500572 strptime(str, "%Y%m%d%H%M%S", &ts);
Theodore Ts'obc7c14e2003-05-03 16:40:09 -0400573#else
Theodore Ts'o690e6932004-12-21 20:40:08 -0500574 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
Theodore Ts'obc7c14e2003-05-03 16:40:09 -0400575 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
576 ts.tm_year -= 1900;
577 ts.tm_mon -= 1;
578 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
579 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
580 ts.tm_min > 59 || ts.tm_sec > 61)
581 ts.tm_mday = 0;
582#endif
Theodore Ts'od4de4aa2001-12-26 08:58:01 -0500583 if (ts.tm_mday == 0) {
584 com_err(program_name, 0,
585 _("Couldn't parse date/time specifier: %s"),
586 str);
587 usage();
588 }
Theodore Ts'oa2ff0f32008-03-21 09:10:09 -0400589 ts.tm_isdst = -1;
Theodore Ts'od4de4aa2001-12-26 08:58:01 -0500590 return (mktime(&ts));
591}
Theodore Ts'o83238152001-01-09 00:16:26 +0000592
Theodore Ts'oc8c071a2001-01-11 16:08:23 +0000593static void parse_tune2fs_options(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594{
Theodore Ts'o519149f1997-10-25 03:49:49 +0000595 int c;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500596 char *tmp;
597 struct group *gr;
598 struct passwd *pw;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000599
Theodore Ts'o2be8fe42008-07-10 10:49:59 -0400600 open_flag = 0;
Theodore Ts'o0ddfd9a2008-02-09 22:22:38 -0500601
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400602 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530603 while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:")) != EOF)
Theodore Ts'oec43da22009-01-20 02:34:39 -0500604 switch (c) {
605 case 'c':
606 max_mount_count = strtol(optarg, &tmp, 0);
607 if (*tmp || max_mount_count > 16000) {
608 com_err(program_name, 0,
609 _("bad mounts count - %s"),
610 optarg);
611 usage();
612 }
613 if (max_mount_count == 0)
614 max_mount_count = -1;
615 c_flag = 1;
616 open_flag = EXT2_FLAG_RW;
617 break;
618 case 'C':
619 mount_count = strtoul(optarg, &tmp, 0);
620 if (*tmp || mount_count > 16000) {
621 com_err(program_name, 0,
622 _("bad mounts count - %s"),
623 optarg);
624 usage();
625 }
626 C_flag = 1;
627 open_flag = EXT2_FLAG_RW;
628 break;
629 case 'e':
630 if (strcmp(optarg, "continue") == 0)
631 errors = EXT2_ERRORS_CONTINUE;
632 else if (strcmp(optarg, "remount-ro") == 0)
633 errors = EXT2_ERRORS_RO;
634 else if (strcmp(optarg, "panic") == 0)
635 errors = EXT2_ERRORS_PANIC;
636 else {
637 com_err(program_name, 0,
638 _("bad error behavior - %s"),
639 optarg);
640 usage();
641 }
642 e_flag = 1;
643 open_flag = EXT2_FLAG_RW;
644 break;
645 case 'E':
646 extended_cmd = optarg;
647 open_flag |= EXT2_FLAG_RW;
648 break;
649 case 'f': /* Force */
650 f_flag = 1;
651 break;
652 case 'g':
653 resgid = strtoul(optarg, &tmp, 0);
654 if (*tmp) {
655 gr = getgrnam(optarg);
656 if (gr == NULL)
657 tmp = optarg;
Theodore Ts'o818180c1998-06-27 05:11:14 +0000658 else {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500659 resgid = gr->gr_gid;
660 *tmp = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 }
Theodore Ts'oec43da22009-01-20 02:34:39 -0500662 }
663 if (*tmp) {
664 com_err(program_name, 0,
665 _("bad gid/group name - %s"),
666 optarg);
667 usage();
668 }
669 g_flag = 1;
670 open_flag = EXT2_FLAG_RW;
671 break;
672 case 'i':
673 interval = strtoul(optarg, &tmp, 0);
674 switch (*tmp) {
675 case 's':
676 tmp++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500678 case '\0':
679 case 'd':
680 case 'D': /* days */
681 interval *= 86400;
682 if (*tmp != '\0')
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000683 tmp++;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000684 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685 case 'm':
Theodore Ts'oec43da22009-01-20 02:34:39 -0500686 case 'M': /* months! */
687 interval *= 86400 * 30;
688 tmp++;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000689 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500690 case 'w':
691 case 'W': /* weeks */
692 interval *= 86400 * 7;
693 tmp++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500695 }
696 if (*tmp) {
697 com_err(program_name, 0,
698 _("bad interval - %s"), optarg);
699 usage();
700 }
701 i_flag = 1;
702 open_flag = EXT2_FLAG_RW;
703 break;
704 case 'j':
705 if (!journal_size)
706 journal_size = -1;
707 open_flag = EXT2_FLAG_RW;
708 break;
709 case 'J':
710 parse_journal_opts(optarg);
711 open_flag = EXT2_FLAG_RW;
712 break;
713 case 'l':
714 l_flag = 1;
715 break;
716 case 'L':
717 new_label = optarg;
718 L_flag = 1;
719 open_flag |= EXT2_FLAG_RW |
720 EXT2_FLAG_JOURNAL_DEV_OK;
721 break;
722 case 'm':
723 reserved_ratio = strtod(optarg, &tmp);
Theodore Ts'o8d822452009-03-06 02:23:59 -0500724 if (*tmp || reserved_ratio > 50 ||
725 reserved_ratio < 0) {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500726 com_err(program_name, 0,
727 _("bad reserved block ratio - %s"),
728 optarg);
729 usage();
730 }
731 m_flag = 1;
732 open_flag = EXT2_FLAG_RW;
733 break;
734 case 'M':
735 new_last_mounted = optarg;
736 M_flag = 1;
737 open_flag = EXT2_FLAG_RW;
738 break;
739 case 'o':
740 if (mntopts_cmd) {
741 com_err(program_name, 0,
742 _("-o may only be specified once"));
743 usage();
744 }
745 mntopts_cmd = optarg;
746 open_flag = EXT2_FLAG_RW;
747 break;
748
749 case 'O':
750 if (features_cmd) {
751 com_err(program_name, 0,
752 _("-O may only be specified once"));
753 usage();
754 }
755 features_cmd = optarg;
756 open_flag = EXT2_FLAG_RW;
757 break;
758 case 'r':
759 reserved_blocks = strtoul(optarg, &tmp, 0);
760 if (*tmp) {
761 com_err(program_name, 0,
762 _("bad reserved blocks count - %s"),
763 optarg);
764 usage();
765 }
766 r_flag = 1;
767 open_flag = EXT2_FLAG_RW;
768 break;
769 case 's': /* Deprecated */
770 s_flag = atoi(optarg);
771 open_flag = EXT2_FLAG_RW;
772 break;
773 case 'T':
774 T_flag = 1;
775 last_check_time = parse_time(optarg);
776 open_flag = EXT2_FLAG_RW;
777 break;
778 case 'u':
779 resuid = strtoul(optarg, &tmp, 0);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000780 if (*tmp) {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500781 pw = getpwnam(optarg);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000782 if (pw == NULL)
783 tmp = optarg;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000784 else {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000785 resuid = pw->pw_uid;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000786 *tmp = 0;
787 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000788 }
Theodore Ts'o818180c1998-06-27 05:11:14 +0000789 if (*tmp) {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500790 com_err(program_name, 0,
791 _("bad uid/user name - %s"),
792 optarg);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000793 usage();
Theodore Ts'of3db3561997-04-26 13:34:30 +0000794 }
795 u_flag = 1;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000796 open_flag = EXT2_FLAG_RW;
797 break;
Theodore Ts'oec43da22009-01-20 02:34:39 -0500798 case 'U':
799 new_UUID = optarg;
800 U_flag = 1;
801 open_flag = EXT2_FLAG_RW |
802 EXT2_FLAG_JOURNAL_DEV_OK;
803 break;
804 case 'I':
805 new_inode_size = strtoul(optarg, &tmp, 0);
806 if (*tmp) {
807 com_err(program_name, 0,
808 _("bad inode size - %s"),
809 optarg);
Theodore Ts'o818180c1998-06-27 05:11:14 +0000810 usage();
Theodore Ts'oec43da22009-01-20 02:34:39 -0500811 }
812 if (!((new_inode_size &
813 (new_inode_size - 1)) == 0)) {
814 com_err(program_name, 0,
815 _("Inode size must be a "
816 "power of two- %s"),
817 optarg);
818 usage();
819 }
820 open_flag = EXT2_FLAG_RW;
821 I_flag = 1;
822 break;
823 default:
824 usage();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825 }
826 if (optind < argc - 1 || optind == argc)
Theodore Ts'o818180c1998-06-27 05:11:14 +0000827 usage();
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000828 if (!open_flag && !l_flag)
829 usage();
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500830 io_options = strchr(argv[optind], '?');
831 if (io_options)
832 *io_options++ = 0;
Theodore Ts'o332f2c22003-03-06 12:58:33 -0500833 device_name = blkid_get_devname(NULL, argv[optind], NULL);
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500834 if (!device_name) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400835 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
Theodore Ts'o817e49e2003-11-21 09:10:29 -0500836 argv[optind]);
837 exit(1);
838 }
Theodore Ts'o118d7da2002-08-17 23:01:22 -0400839}
Theodore Ts'o83238152001-01-09 00:16:26 +0000840
Theodore Ts'o14b596d2009-04-22 09:18:30 -0400841#ifdef CONFIG_BUILD_FINDFS
Theodore Ts'o3e699062002-10-13 23:56:28 -0400842void do_findfs(int argc, char **argv)
Theodore Ts'o118d7da2002-08-17 23:01:22 -0400843{
844 char *dev;
845
846 if ((argc != 2) ||
847 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
848 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
849 exit(2);
850 }
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500851 dev = blkid_get_devname(NULL, argv[1], NULL);
Theodore Ts'o118d7da2002-08-17 23:01:22 -0400852 if (!dev) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400853 com_err("findfs", 0, _("Unable to resolve '%s'"),
Theodore Ts'o118d7da2002-08-17 23:01:22 -0400854 argv[1]);
855 exit(1);
856 }
857 puts(dev);
858 exit(0);
859}
Theodore Ts'o14b596d2009-04-22 09:18:30 -0400860#endif
Theodore Ts'o83238152001-01-09 00:16:26 +0000861
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500862static void parse_extended_opts(ext2_filsys fs, const char *opts)
863{
864 char *buf, *token, *next, *p, *arg;
Theodore Ts'o10ff68d2008-08-29 21:21:19 -0400865 int len, hash_alg;
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500866 int r_usage = 0;
867
868 len = strlen(opts);
869 buf = malloc(len+1);
870 if (!buf) {
871 fprintf(stderr,
872 _("Couldn't allocate memory to parse options!\n"));
873 exit(1);
874 }
875 strcpy(buf, opts);
876 for (token = buf; token && *token; token = next) {
877 p = strchr(token, ',');
878 next = 0;
879 if (p) {
880 *p = 0;
881 next = p+1;
882 }
883 arg = strchr(token, '=');
884 if (arg) {
885 *arg = 0;
886 arg++;
887 }
888 if (!strcmp(token, "test_fs")) {
889 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
890 printf("Setting test filesystem flag\n");
891 ext2fs_mark_super_dirty(fs);
892 } else if (!strcmp(token, "^test_fs")) {
893 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
894 printf("Clearing test filesystem flag\n");
895 ext2fs_mark_super_dirty(fs);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500896 } else if (strcmp(token, "stride") == 0) {
897 if (!arg) {
898 r_usage++;
899 continue;
900 }
901 stride = strtoul(arg, &p, 0);
902 if (*p || (stride == 0)) {
903 fprintf(stderr,
904 _("Invalid RAID stride: %s\n"),
905 arg);
906 r_usage++;
907 continue;
908 }
909 stride_set = 1;
910 } else if (strcmp(token, "stripe-width") == 0 ||
911 strcmp(token, "stripe_width") == 0) {
912 if (!arg) {
913 r_usage++;
914 continue;
915 }
916 stripe_width = strtoul(arg, &p, 0);
917 if (*p || (stripe_width == 0)) {
918 fprintf(stderr,
919 _("Invalid RAID stripe-width: %s\n"),
920 arg);
921 r_usage++;
922 continue;
923 }
924 stripe_width_set = 1;
Theodore Ts'o10ff68d2008-08-29 21:21:19 -0400925 } else if (strcmp(token, "hash_alg") == 0 ||
926 strcmp(token, "hash-alg") == 0) {
927 if (!arg) {
928 r_usage++;
929 continue;
930 }
931 hash_alg = e2p_string2hash(arg);
932 if (hash_alg < 0) {
Theodore Ts'oec43da22009-01-20 02:34:39 -0500933 fprintf(stderr,
Theodore Ts'o10ff68d2008-08-29 21:21:19 -0400934 _("Invalid hash algorithm: %s\n"),
935 arg);
936 r_usage++;
937 continue;
938 }
939 fs->super->s_def_hash_version = hash_alg;
940 printf(_("Setting default hash algorithm "
Theodore Ts'oec43da22009-01-20 02:34:39 -0500941 "to %s (%d)\n"),
Theodore Ts'o10ff68d2008-08-29 21:21:19 -0400942 arg, hash_alg);
943 ext2fs_mark_super_dirty(fs);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400944 } else
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500945 r_usage++;
946 }
947 if (r_usage) {
948 fprintf(stderr, _("\nBad options specified.\n\n"
949 "Extended options are separated by commas, "
950 "and may take an argument which\n"
951 "\tis set off by an equals ('=') sign.\n\n"
952 "Valid extended options are:\n"
Theodore Ts'o0c17cb22008-02-18 22:56:25 -0500953 "\tstride=<RAID per-disk chunk size in blocks>\n"
Theodore Ts'o10ff68d2008-08-29 21:21:19 -0400954 "\tstripe_width=<RAID stride*data disks in blocks>\n"
955 "\thash_alg=<hash algorithm>\n"
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500956 "\ttest_fs\n"
957 "\t^test_fs\n"));
958 free(buf);
959 exit(1);
960 }
961 free(buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400962}
Theodore Ts'o6cb27402008-01-26 19:06:35 -0500963
Theodore Ts'o31f18152009-01-20 11:49:17 -0500964/*
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +0530965 * Fill in the block bitmap bmap with the information regarding the
966 * blocks to be moved
Theodore Ts'o31f18152009-01-20 11:49:17 -0500967 */
968static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +0530969 ext2fs_block_bitmap bmap)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530970{
971 dgrp_t i;
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +0530972 int retval;
973 ext2_badblocks_list bb_list = 0;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530974 blk_t j, needed_blocks = 0;
975 blk_t start_blk, end_blk;
976
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +0530977 retval = ext2fs_read_bb_inode(fs, &bb_list);
978 if (retval)
979 return retval;
980
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530981 for (i = 0; i < fs->group_desc_count; i++) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530982 start_blk = fs->group_desc[i].bg_inode_table +
983 fs->inode_blocks_per_group;
984
985 end_blk = fs->group_desc[i].bg_inode_table +
986 new_ino_blks_per_grp;
987
988 for (j = start_blk; j < end_blk; j++) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530989 if (ext2fs_test_block_bitmap(fs->block_map, j)) {
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +0530990 /*
991 * IF the block is a bad block we fail
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530992 */
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +0530993 if (ext2fs_badblocks_list_test(bb_list, j)) {
994 ext2fs_badblocks_list_free(bb_list);
995 return ENOSPC;
996 }
997
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530998 ext2fs_mark_block_bitmap(bmap, j);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +0530999 } else {
1000 /*
1001 * We are going to use this block for
1002 * inode table. So mark them used.
1003 */
1004 ext2fs_mark_block_bitmap(fs->block_map, j);
1005 }
1006 }
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301007 needed_blocks += end_blk - start_blk;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301008 }
1009
Aneesh Kumar K.V154a5d72009-08-06 11:42:31 +05301010 ext2fs_badblocks_list_free(bb_list);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001011 if (needed_blocks > fs->super->s_free_blocks_count)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301012 return ENOSPC;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301013
1014 return 0;
1015}
1016
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301017static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1018{
1019 dgrp_t group;
1020 group = ext2fs_group_of_blk(fs, blk);
1021 if (fs->group_desc[group].bg_block_bitmap == blk)
1022 return 1;
1023 if (fs->group_desc[group].bg_inode_bitmap == blk)
1024 return 1;
1025 return 0;
1026}
1027
1028static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1029{
1030 blk_t start_blk, end_blk;
1031 start_blk = fs->super->s_first_data_block +
1032 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1033 /*
1034 * We cannot get new block beyond end_blk for for the last block group
1035 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1036 */
1037 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1038 if (blk >= start_blk && blk <= end_blk)
1039 return 1;
1040 return 0;
1041}
1042
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301043static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301044{
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301045
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301046 char *buf;
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301047 dgrp_t group;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301048 errcode_t retval;
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301049 int meta_data = 0;
Theodore Ts'o31f18152009-01-20 11:49:17 -05001050 blk_t blk, new_blk, goal;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301051 struct blk_move *bmv;
1052
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301053 retval = ext2fs_get_mem(fs->blocksize, &buf);
1054 if (retval)
1055 return retval;
1056
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001057 for (new_blk = blk = fs->super->s_first_data_block;
1058 blk < fs->super->s_blocks_count; blk++) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301059 if (!ext2fs_test_block_bitmap(bmap, blk))
1060 continue;
1061
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301062 if (ext2fs_is_meta_block(fs, blk)) {
1063 /*
1064 * If the block is mapping a fs meta data block
1065 * like group desc/block bitmap/inode bitmap. We
1066 * should find a block in the same group and fix
1067 * the respective fs metadata pointers. Otherwise
1068 * fail
1069 */
1070 group = ext2fs_group_of_blk(fs, blk);
1071 goal = ext2fs_group_first_block(fs, group);
1072 meta_data = 1;
1073
1074 } else {
1075 goal = new_blk;
1076 }
Theodore Ts'o31f18152009-01-20 11:49:17 -05001077 retval = ext2fs_new_block(fs, goal, NULL, &new_blk);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301078 if (retval)
1079 goto err_out;
1080
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301081 /* new fs meta data block should be in the same group */
1082 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1083 retval = ENOSPC;
1084 goto err_out;
1085 }
1086
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301087 /* Mark this block as allocated */
1088 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
1089
1090 /* Add it to block move list */
1091 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1092 if (retval)
1093 goto err_out;
1094
1095 bmv->old_loc = blk;
1096 bmv->new_loc = new_blk;
1097
1098 list_add(&(bmv->list), &blk_move_list);
1099
1100 retval = io_channel_read_blk(fs->io, blk, 1, buf);
1101 if (retval)
1102 goto err_out;
1103
1104 retval = io_channel_write_blk(fs->io, new_blk, 1, buf);
1105 if (retval)
1106 goto err_out;
1107 }
1108
1109err_out:
1110 ext2fs_free_mem(&buf);
1111 return retval;
1112}
1113
Theodore Ts'oec43da22009-01-20 02:34:39 -05001114static blk_t translate_block(blk_t blk)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301115{
1116 struct list_head *entry;
1117 struct blk_move *bmv;
1118
1119 list_for_each(entry, &blk_move_list) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301120 bmv = list_entry(entry, struct blk_move, list);
1121 if (bmv->old_loc == blk)
1122 return bmv->new_loc;
1123 }
1124
1125 return 0;
1126}
1127
Theodore Ts'o721b3672008-06-07 11:51:33 -04001128static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1129 blk_t *block_nr,
1130 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301131 blk_t ref_block EXT2FS_ATTR((unused)),
1132 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001133 void *priv_data)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301134{
1135 int ret = 0;
1136 blk_t new_blk;
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001137 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301138
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001139 if (!ext2fs_test_block_bitmap(bmap, *block_nr))
1140 return 0;
Theodore Ts'oec43da22009-01-20 02:34:39 -05001141 new_blk = translate_block(*block_nr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301142 if (new_blk) {
1143 *block_nr = new_blk;
1144 /*
1145 * This will force the ext2fs_write_inode in the iterator
1146 */
1147 ret |= BLOCK_CHANGED;
1148 }
1149
1150 return ret;
1151}
1152
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001153static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301154{
1155 errcode_t retval = 0;
1156 ext2_ino_t ino;
1157 blk_t blk;
1158 char *block_buf = 0;
1159 struct ext2_inode inode;
1160 ext2_inode_scan scan = NULL;
1161
1162 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1163 if (retval)
1164 return retval;
1165
1166 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1167 if (retval)
1168 goto err_out;
1169
1170 while (1) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301171 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1172 if (retval)
1173 goto err_out;
1174
1175 if (!ino)
1176 break;
1177
1178 if (inode.i_links_count == 0)
1179 continue; /* inode not in use */
1180
1181 /* FIXME!!
1182 * If we end up modifying the journal inode
1183 * the sb->s_jnl_blocks will differ. But a
1184 * subsequent e2fsck fixes that.
1185 * Do we need to fix this ??
1186 */
1187
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001188 if (inode.i_file_acl &&
1189 ext2fs_test_block_bitmap(bmap, inode.i_file_acl)) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001190 blk = translate_block(inode.i_file_acl);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301191 if (!blk)
1192 continue;
1193
1194 inode.i_file_acl = blk;
1195
1196 /*
1197 * Write the inode to disk so that inode table
1198 * resizing can work
1199 */
1200 retval = ext2fs_write_inode(fs, ino, &inode);
1201 if (retval)
1202 goto err_out;
1203 }
1204
1205 if (!ext2fs_inode_has_valid_blocks(&inode))
1206 continue;
1207
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001208 retval = ext2fs_block_iterate2(fs, ino, 0, block_buf,
1209 process_block, bmap);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301210 if (retval)
1211 goto err_out;
1212
1213 }
1214
1215err_out:
1216 ext2fs_free_mem(&block_buf);
1217
1218 return retval;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301219}
1220
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301221/*
1222 * We need to scan for inode and block bitmaps that may need to be
1223 * moved. This can take place if the filesystem was formatted for
1224 * RAID arrays using the mke2fs's extended option "stride".
1225 */
1226static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1227{
1228 dgrp_t i;
1229 blk_t blk, new_blk;
1230
1231 for (i = 0; i < fs->group_desc_count; i++) {
1232 blk = fs->group_desc[i].bg_block_bitmap;
1233 if (ext2fs_test_block_bitmap(bmap, blk)) {
1234 new_blk = translate_block(blk);
1235 if (!new_blk)
1236 continue;
1237 fs->group_desc[i].bg_block_bitmap = new_blk;
1238 }
1239
1240 blk = fs->group_desc[i].bg_inode_bitmap;
1241 if (ext2fs_test_block_bitmap(bmap, blk)) {
1242 new_blk = translate_block(blk);
1243 if (!new_blk)
1244 continue;
1245 fs->group_desc[i].bg_inode_bitmap = new_blk;
1246 }
1247 }
1248 return 0;
1249}
1250
Theodore Ts'o721b3672008-06-07 11:51:33 -04001251static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301252{
1253 dgrp_t i;
1254 blk_t blk;
1255 errcode_t retval;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001256 int new_ino_blks_per_grp;
1257 unsigned int j;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301258 char *old_itable = NULL, *new_itable = NULL;
1259 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001260 unsigned long old_ino_size;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301261 int old_itable_size, new_itable_size;
1262
1263 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001264 old_ino_size = EXT2_INODE_SIZE(fs->super);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301265
1266 new_ino_blks_per_grp = ext2fs_div_ceil(
1267 EXT2_INODES_PER_GROUP(fs->super) *
Theodore Ts'o721b3672008-06-07 11:51:33 -04001268 new_ino_size,
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301269 fs->blocksize);
1270
1271 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1272
1273 retval = ext2fs_get_mem(old_itable_size, &old_itable);
1274 if (retval)
1275 return retval;
1276
1277 retval = ext2fs_get_mem(new_itable_size, &new_itable);
1278 if (retval)
1279 goto err_out;
1280
1281 tmp_old_itable = old_itable;
1282 tmp_new_itable = new_itable;
1283
1284 for (i = 0; i < fs->group_desc_count; i++) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301285 blk = fs->group_desc[i].bg_inode_table;
1286 retval = io_channel_read_blk(fs->io, blk,
1287 fs->inode_blocks_per_group, old_itable);
1288 if (retval)
1289 goto err_out;
1290
1291 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
Theodore Ts'o721b3672008-06-07 11:51:33 -04001292 memcpy(new_itable, old_itable, old_ino_size);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301293
Theodore Ts'o721b3672008-06-07 11:51:33 -04001294 memset(new_itable+old_ino_size, 0,
1295 new_ino_size - old_ino_size);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301296
Theodore Ts'o721b3672008-06-07 11:51:33 -04001297 new_itable += new_ino_size;
1298 old_itable += old_ino_size;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301299 }
1300
1301 /* reset the pointer */
1302 old_itable = tmp_old_itable;
1303 new_itable = tmp_new_itable;
1304
1305 retval = io_channel_write_blk(fs->io, blk,
1306 new_ino_blks_per_grp, new_itable);
1307 if (retval)
1308 goto err_out;
1309 }
1310
1311 /* Update the meta data */
1312 fs->inode_blocks_per_group = new_ino_blks_per_grp;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001313 fs->super->s_inode_size = new_ino_size;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301314
1315err_out:
1316 if (old_itable)
1317 ext2fs_free_mem(&old_itable);
1318
1319 if (new_itable)
1320 ext2fs_free_mem(&new_itable);
1321
1322 return retval;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301323}
1324
1325static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1326{
1327 blk_t blk;
1328 ext2_ino_t ino;
1329 unsigned int group = 0;
1330 unsigned int count = 0;
1331 int total_free = 0;
1332 int group_free = 0;
1333
1334 /*
1335 * First calculate the block statistics
1336 */
1337 for (blk = fs->super->s_first_data_block;
1338 blk < fs->super->s_blocks_count; blk++) {
1339 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1340 group_free++;
1341 total_free++;
1342 }
1343 count++;
1344 if ((count == fs->super->s_blocks_per_group) ||
1345 (blk == fs->super->s_blocks_count-1)) {
1346 fs->group_desc[group++].bg_free_blocks_count =
1347 group_free;
1348 count = 0;
1349 group_free = 0;
1350 }
1351 }
1352 fs->super->s_free_blocks_count = total_free;
1353
1354 /*
1355 * Next, calculate the inode statistics
1356 */
1357 group_free = 0;
1358 total_free = 0;
1359 count = 0;
1360 group = 0;
1361
1362 /* Protect loop from wrap-around if s_inodes_count maxed */
1363 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1364 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1365 group_free++;
1366 total_free++;
1367 }
1368 count++;
1369 if ((count == fs->super->s_inodes_per_group) ||
1370 (ino == fs->super->s_inodes_count)) {
1371 fs->group_desc[group++].bg_free_inodes_count =
1372 group_free;
1373 count = 0;
1374 group_free = 0;
1375 }
1376 }
1377 fs->super->s_free_inodes_count = total_free;
1378 ext2fs_mark_super_dirty(fs);
1379 return 0;
1380}
1381
1382#define list_for_each_safe(pos, pnext, head) \
1383 for (pos = (head)->next, pnext = pos->next; pos != (head); \
1384 pos = pnext, pnext = pos->next)
1385
Theodore Ts'o721b3672008-06-07 11:51:33 -04001386static void free_blk_move_list(void)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301387{
1388 struct list_head *entry, *tmp;
1389 struct blk_move *bmv;
1390
1391 list_for_each_safe(entry, tmp, &blk_move_list) {
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301392 bmv = list_entry(entry, struct blk_move, list);
1393 list_del(entry);
1394 ext2fs_free_mem(&bmv);
1395 }
Theodore Ts'o31f18152009-01-20 11:49:17 -05001396 return;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301397}
Theodore Ts'o721b3672008-06-07 11:51:33 -04001398
1399static int resize_inode(ext2_filsys fs, unsigned long new_size)
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301400{
1401 errcode_t retval;
1402 int new_ino_blks_per_grp;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301403 ext2fs_block_bitmap bmap;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301404
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301405 ext2fs_read_inode_bitmap(fs);
1406 ext2fs_read_block_bitmap(fs);
1407 INIT_LIST_HEAD(&blk_move_list);
1408
1409
1410 new_ino_blks_per_grp = ext2fs_div_ceil(
1411 EXT2_INODES_PER_GROUP(fs->super)*
Theodore Ts'o721b3672008-06-07 11:51:33 -04001412 new_size,
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301413 fs->blocksize);
1414
1415 /* We may change the file system.
1416 * Mark the file system as invalid so that
1417 * the user is prompted to run fsck.
1418 */
1419 fs->super->s_state &= ~EXT2_VALID_FS;
1420
1421 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1422 &bmap);
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301423 if (retval) {
1424 fputs(_("Failed to allocate block bitmap when "
1425 "increasing inode size\n"), stderr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301426 return retval;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301427 }
1428 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1429 if (retval) {
1430 fputs(_("Not enough space to increase inode size \n"), stderr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301431 goto err_out;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301432 }
1433 retval = move_block(fs, bmap);
1434 if (retval) {
1435 fputs(_("Failed to relocate blocks during inode resize \n"),
1436 stderr);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301437 goto err_out;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301438 }
Theodore Ts'o27c6de42008-11-15 00:32:39 -05001439 retval = inode_scan_and_fix(fs, bmap);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301440 if (retval)
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301441 goto err_out_undo;
Theodore Ts'o31f18152009-01-20 11:49:17 -05001442
Aneesh Kumar K.V91fac972009-08-06 11:42:30 +05301443 retval = group_desc_scan_and_fix(fs, bmap);
1444 if (retval)
1445 goto err_out_undo;
1446
Theodore Ts'o721b3672008-06-07 11:51:33 -04001447 retval = expand_inode_table(fs, new_size);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301448 if (retval)
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301449 goto err_out_undo;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301450
1451 ext2fs_calculate_summary_stats(fs);
1452
1453 fs->super->s_state |= EXT2_VALID_FS;
1454 /* mark super block and block bitmap as dirty */
1455 ext2fs_mark_super_dirty(fs);
1456 ext2fs_mark_bb_dirty(fs);
1457
1458err_out:
1459 free_blk_move_list();
1460 ext2fs_free_block_bitmap(bmap);
1461
1462 return retval;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301463
1464err_out_undo:
1465 free_blk_move_list();
1466 ext2fs_free_block_bitmap(bmap);
1467 fputs(_("Error in resizing the inode size.\n"
1468 "Run e2undo to undo the "
1469 "file system changes. \n"), stderr);
1470
1471 return retval;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301472}
1473
1474static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1475{
1476 errcode_t retval = 0;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001477 const char *tdb_dir;
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001478 char *tdb_file;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001479 char *dev_name, *tmp_name;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301480
1481#if 0 /* FIXME!! */
1482 /*
1483 * Configuration via a conf file would be
1484 * nice
1485 */
1486 profile_get_string(profile, "scratch_files",
1487 "directory", 0, 0,
1488 &tdb_dir);
1489#endif
1490 tmp_name = strdup(name);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001491 if (!tmp_name) {
1492 alloc_fn_fail:
1493 com_err(program_name, ENOMEM,
1494 _("Couldn't allocate memory for tdb filename\n"));
1495 return ENOMEM;
1496 }
Theodore Ts'o721b3672008-06-07 11:51:33 -04001497 dev_name = basename(tmp_name);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301498
1499 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1500 if (!tdb_dir)
Theodore Ts'oec43da22009-01-20 02:34:39 -05001501 tdb_dir = "/var/lib/e2fsprogs";
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301502
1503 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1504 access(tdb_dir, W_OK))
1505 return 0;
1506
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001507 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1508 if (!tdb_file)
1509 goto alloc_fn_fail;
Theodore Ts'o721b3672008-06-07 11:51:33 -04001510 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301511
1512 if (!access(tdb_file, F_OK)) {
1513 if (unlink(tdb_file) < 0) {
1514 retval = errno;
1515 com_err(program_name, retval,
1516 _("while trying to delete %s"),
1517 tdb_file);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001518 free(tdb_file);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301519 return retval;
1520 }
1521 }
1522
1523 set_undo_io_backing_manager(*io_ptr);
1524 *io_ptr = undo_io_manager;
1525 set_undo_io_backup_file(tdb_file);
Theodore Ts'o5bf81ba2009-01-20 01:50:07 -05001526 printf(_("To undo the tune2fs operation please run "
Benno Schulenberg577b5c42008-07-17 23:24:09 +02001527 "the command\n e2undo %s %s\n\n"),
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301528 tdb_file, name);
Theodore Ts'of203bbd2009-04-18 10:53:32 -04001529 free(tdb_file);
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301530 free(tmp_name);
1531 return retval;
1532}
Theodore Ts'o83238152001-01-09 00:16:26 +00001533
Theodore Ts'oec43da22009-01-20 02:34:39 -05001534int main(int argc, char **argv)
Theodore Ts'o83238152001-01-09 00:16:26 +00001535{
1536 errcode_t retval;
1537 ext2_filsys fs;
1538 struct ext2_super_block *sb;
Theodore Ts'od887f882008-07-27 16:00:48 -04001539 io_manager io_ptr, io_ptr_orig = NULL;
Theodore Ts'o83238152001-01-09 00:16:26 +00001540
1541#ifdef ENABLE_NLS
1542 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -05001543 setlocale(LC_CTYPE, "");
Theodore Ts'o83238152001-01-09 00:16:26 +00001544 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1545 textdomain(NLS_CAT_NAME);
1546#endif
1547 if (argc && *argv)
1548 program_name = *argv;
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001549 add_error_table(&et_ext2_error_table);
Theodore Ts'o83238152001-01-09 00:16:26 +00001550
Theodore Ts'o14b596d2009-04-22 09:18:30 -04001551#ifdef CONFIG_BUILD_FINDFS
Theodore Ts'o118d7da2002-08-17 23:01:22 -04001552 if (strcmp(get_progname(argv[0]), "findfs") == 0)
1553 do_findfs(argc, argv);
Theodore Ts'o14b596d2009-04-22 09:18:30 -04001554#endif
Theodore Ts'o83238152001-01-09 00:16:26 +00001555 if (strcmp(get_progname(argv[0]), "e2label") == 0)
1556 parse_e2label_options(argc, argv);
1557 else
1558 parse_tune2fs_options(argc, argv);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001559
Theodore Ts'o2a29f132003-05-05 12:08:47 -04001560#ifdef CONFIG_TESTIO_DEBUG
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001561 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1562 io_ptr = test_io_manager;
1563 test_io_backing_manager = unix_io_manager;
1564 } else
Theodore Ts'o2a29f132003-05-05 12:08:47 -04001565#endif
Theodore Ts'of38cf3c2008-09-01 11:17:29 -04001566 io_ptr = unix_io_manager;
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301567
Theodore Ts'od887f882008-07-27 16:00:48 -04001568retry_open:
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001569 retval = ext2fs_open2(device_name, io_options, open_flag,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -05001570 0, 0, io_ptr, &fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001571 if (retval) {
1572 com_err(program_name, retval,
1573 _("while trying to open %s"),
1574 device_name);
Theodore Ts'o7141b542001-08-15 19:17:37 -04001575 fprintf(stderr,
1576 _("Couldn't find valid filesystem superblock.\n"));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001577 exit(1);
1578 }
Theodore Ts'od887f882008-07-27 16:00:48 -04001579
1580 if (I_flag && !io_ptr_orig) {
1581 /*
1582 * Check the inode size is right so we can issue an
1583 * error message and bail before setting up the tdb
1584 * file.
1585 */
1586 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
Theodore Ts'o9266fc72009-04-22 15:13:37 -04001587 fprintf(stderr, _("The inode size is already %lu\n"),
Theodore Ts'od887f882008-07-27 16:00:48 -04001588 new_inode_size);
1589 exit(1);
1590 }
1591 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1592 fprintf(stderr, _("Shrinking the inode size is "
1593 "not supported\n"));
1594 exit(1);
1595 }
1596
1597 /*
1598 * If inode resize is requested use the
1599 * Undo I/O manager
1600 */
1601 io_ptr_orig = io_ptr;
1602 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1603 if (retval)
1604 exit(1);
1605 if (io_ptr != io_ptr_orig) {
1606 ext2fs_close(fs);
1607 goto retry_open;
1608 }
1609 }
1610
Theodore Ts'o83238152001-01-09 00:16:26 +00001611 sb = fs->super;
Theodore Ts'o058ad1c2007-06-18 18:26:50 -04001612 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
Theodore Ts'o2be8fe42008-07-10 10:49:59 -04001613
Theodore Ts'o83238152001-01-09 00:16:26 +00001614 if (print_label) {
1615 /* For e2label emulation */
Theodore Ts'oc8c071a2001-01-11 16:08:23 +00001616 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1617 sb->s_volume_name);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001618 remove_error_table(&et_ext2_error_table);
Theodore Ts'o83238152001-01-09 00:16:26 +00001619 exit(0);
1620 }
Theodore Ts'o2be8fe42008-07-10 10:49:59 -04001621
Theodore Ts'od06863f2004-04-12 12:37:55 -04001622 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1623 if (retval) {
1624 com_err("ext2fs_check_if_mount", retval,
1625 _("while determining whether %s is mounted."),
1626 device_name);
1627 exit(1);
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001628 }
Theodore Ts'o63985322001-01-03 17:02:13 +00001629 /* Normally we only need to write out the superblock */
1630 fs->flags |= EXT2_FLAG_SUPER_ONLY;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001631
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001632 if (c_flag) {
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001633 sb->s_max_mnt_count = max_mount_count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001634 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001635 printf(_("Setting maximal mount count to %d\n"),
1636 max_mount_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001637 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001638 if (C_flag) {
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001639 sb->s_mnt_count = mount_count;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001640 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001641 printf(_("Setting current mount count to %d\n"), mount_count);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001642 }
1643 if (e_flag) {
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001644 sb->s_errors = errors;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001645 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001646 printf(_("Setting error behavior to %d\n"), errors);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001647 }
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001648 if (g_flag) {
1649 sb->s_def_resgid = resgid;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001650 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001651 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001652 }
Theodore Ts'o818180c1998-06-27 05:11:14 +00001653 if (i_flag) {
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001654 sb->s_checkinterval = interval;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001655 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001656 printf(_("Setting interval between checks to %lu seconds\n"),
1657 interval);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001658 }
Theodore Ts'o818180c1998-06-27 05:11:14 +00001659 if (m_flag) {
Theodore Ts'o9ff8ece2008-06-17 19:58:29 -04001660 sb->s_r_blocks_count = (unsigned int) (reserved_ratio *
1661 sb->s_blocks_count / 100.0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001662 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001663 printf(_("Setting reserved blocks percentage to %g%% "
1664 "(%u blocks)\n"),
1665 reserved_ratio, sb->s_r_blocks_count);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001666 }
Theodore Ts'o818180c1998-06-27 05:11:14 +00001667 if (r_flag) {
Theodore Ts'o22ba4c12005-01-06 14:05:17 -05001668 if (reserved_blocks >= sb->s_blocks_count/2) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001669 com_err(program_name, 0,
1670 _("reserved blocks count is too big (%lu)"),
1671 reserved_blocks);
1672 exit(1);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001673 }
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001674 sb->s_r_blocks_count = reserved_blocks;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001675 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001676 printf(_("Setting reserved blocks count to %lu\n"),
1677 reserved_blocks);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001678 }
Theodore Ts'o521e3681997-04-29 17:48:10 +00001679 if (s_flag == 1) {
Theodore Ts'o521e3681997-04-29 17:48:10 +00001680 if (sb->s_feature_ro_compat &
1681 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
Theodore Ts'o54434922003-12-07 01:28:50 -05001682 fputs(_("\nThe filesystem already has sparse "
1683 "superblocks.\n"), stderr);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001684 else {
1685 sb->s_feature_ro_compat |=
1686 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001687 sb->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001688 ext2fs_mark_super_dirty(fs);
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001689 printf(_("\nSparse superblock flag set. %s"),
Theodore Ts'oa4fa1002000-02-08 21:35:41 +00001690 _(please_fsck));
Theodore Ts'o521e3681997-04-29 17:48:10 +00001691 }
Theodore Ts'o521e3681997-04-29 17:48:10 +00001692 }
1693 if (s_flag == 0) {
Theodore Ts'off662d52008-02-28 21:26:01 -05001694 fputs(_("\nClearing the sparse superflag not supported.\n"),
1695 stderr);
1696 exit(1);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001697 }
Theodore Ts'od4de4aa2001-12-26 08:58:01 -05001698 if (T_flag) {
1699 sb->s_lastcheck = last_check_time;
1700 ext2fs_mark_super_dirty(fs);
1701 printf(_("Setting time filesystem last checked to %s\n"),
1702 ctime(&last_check_time));
1703 }
Theodore Ts'ob21e38a2001-01-01 15:26:58 +00001704 if (u_flag) {
1705 sb->s_def_resuid = resuid;
Theodore Ts'of3db3561997-04-26 13:34:30 +00001706 ext2fs_mark_super_dirty(fs);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001707 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001708 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001709 if (L_flag) {
Theodore Ts'oa789d841998-03-30 01:20:55 +00001710 if (strlen(new_label) > sizeof(sb->s_volume_name))
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001711 fputs(_("Warning: label too long, truncating.\n"),
Theodore Ts'o54434922003-12-07 01:28:50 -05001712 stderr);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001713 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1714 strncpy(sb->s_volume_name, new_label,
1715 sizeof(sb->s_volume_name));
1716 ext2fs_mark_super_dirty(fs);
1717 }
1718 if (M_flag) {
1719 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1720 strncpy(sb->s_last_mounted, new_last_mounted,
1721 sizeof(sb->s_last_mounted));
1722 ext2fs_mark_super_dirty(fs);
1723 }
Theodore Ts'oa0c3fd52002-10-15 17:43:43 -04001724 if (mntopts_cmd)
1725 update_mntopts(fs, mntopts_cmd);
Theodore Ts'o63985322001-01-03 17:02:13 +00001726 if (features_cmd)
1727 update_feature_set(fs, features_cmd);
Theodore Ts'o6cb27402008-01-26 19:06:35 -05001728 if (extended_cmd)
1729 parse_extended_opts(fs, extended_cmd);
Theodore Ts'odc2ec522001-01-18 01:51:15 +00001730 if (journal_size || journal_device)
Theodore Ts'o63985322001-01-03 17:02:13 +00001731 add_journal(fs);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001732
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001733 if (U_flag) {
Theodore Ts'o9d4a4dc2008-11-14 17:42:27 -05001734 int set_csum = 0;
1735 dgrp_t i;
1736
1737 if (sb->s_feature_ro_compat &
1738 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001739 /*
Theodore Ts'o9d4a4dc2008-11-14 17:42:27 -05001740 * Determine if the block group checksums are
1741 * correct so we know whether or not to set
1742 * them later on.
1743 */
1744 for (i = 0; i < fs->group_desc_count; i++)
1745 if (!ext2fs_group_desc_csum_verify(fs, i))
1746 break;
1747 if (i >= fs->group_desc_count)
1748 set_csum = 1;
1749 }
Theodore Ts'o4d0f2282001-04-23 20:58:03 +00001750 if ((strcasecmp(new_UUID, "null") == 0) ||
1751 (strcasecmp(new_UUID, "clear") == 0)) {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001752 uuid_clear(sb->s_uuid);
Theodore Ts'o63985322001-01-03 17:02:13 +00001753 } else if (strcasecmp(new_UUID, "time") == 0) {
1754 uuid_generate_time(sb->s_uuid);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001755 } else if (strcasecmp(new_UUID, "random") == 0) {
1756 uuid_generate(sb->s_uuid);
1757 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +00001758 com_err(program_name, 0, _("Invalid UUID format\n"));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001759 exit(1);
1760 }
Theodore Ts'o9d4a4dc2008-11-14 17:42:27 -05001761 if (set_csum) {
1762 for (i = 0; i < fs->group_desc_count; i++)
1763 ext2fs_group_desc_csum_set(fs, i);
1764 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1765 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001766 ext2fs_mark_super_dirty(fs);
1767 }
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301768 if (I_flag) {
1769 if (mount_flags & EXT2_MF_MOUNTED) {
1770 fputs(_("The inode size may only be "
1771 "changed when the filesystem is "
1772 "unmounted.\n"), stderr);
1773 exit(1);
1774 }
Theodore Ts'o5bf81ba2009-01-20 01:50:07 -05001775 if (fs->super->s_feature_incompat &
1776 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001777 fputs(_("Changing the inode size not supported for "
1778 "filesystems with the flex_bg\n"
1779 "feature enabled.\n"),
Theodore Ts'o5bf81ba2009-01-20 01:50:07 -05001780 stderr);
1781 exit(1);
1782 }
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301783 /*
1784 * We want to update group descriptor also
1785 * with the new free inode count
1786 */
1787 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
Aneesh Kumar K.Va9e51772009-08-06 11:42:29 +05301788 if (resize_inode(fs, new_inode_size) == 0) {
Theodore Ts'oec43da22009-01-20 02:34:39 -05001789 printf(_("Setting inode size %lu\n"),
Aneesh Kumar K.V64d588c2007-08-13 15:56:24 +05301790 new_inode_size);
1791 }
1792 }
Theodore Ts'o1e3472c1997-04-29 14:53:37 +00001793
Theodore Ts'o3839e651997-04-26 13:21:57 +00001794 if (l_flag)
Theodore Ts'oec43da22009-01-20 02:34:39 -05001795 list_super(sb);
Theodore Ts'o0c17cb22008-02-18 22:56:25 -05001796 if (stride_set) {
1797 sb->s_raid_stride = stride;
1798 ext2fs_mark_super_dirty(fs);
1799 printf(_("Setting stride size to %d\n"), stride);
1800 }
1801 if (stripe_width_set) {
1802 sb->s_raid_stripe_width = stripe_width;
1803 ext2fs_mark_super_dirty(fs);
1804 printf(_("Setting stripe width to %d\n"), stripe_width);
1805 }
Theodore Ts'o1acde2b2009-06-15 03:53:04 -04001806 free(device_name);
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001807 remove_error_table(&et_ext2_error_table);
Theodore Ts'oec43da22009-01-20 02:34:39 -05001808 return (ext2fs_close(fs) ? 1 : 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001809}