xref: /aosp_15_r20/external/e2fsprogs/misc/tune2fs.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * tune2fs.c - Change the file system parameters on an ext2 file system
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1992, 1993, 1994  Remy Card <[email protected]>
5*6a54128fSAndroid Build Coastguard Worker  *                                 Laboratoire MASI, Institut Blaise Pascal
6*6a54128fSAndroid Build Coastguard Worker  *                                 Universite Pierre et Marie Curie (Paris VI)
7*6a54128fSAndroid Build Coastguard Worker  *
8*6a54128fSAndroid Build Coastguard Worker  * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
9*6a54128fSAndroid Build Coastguard Worker  *
10*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
11*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Public
12*6a54128fSAndroid Build Coastguard Worker  * License.
13*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
14*6a54128fSAndroid Build Coastguard Worker  */
15*6a54128fSAndroid Build Coastguard Worker 
16*6a54128fSAndroid Build Coastguard Worker /*
17*6a54128fSAndroid Build Coastguard Worker  * History:
18*6a54128fSAndroid Build Coastguard Worker  * 93/06/01	- Creation
19*6a54128fSAndroid Build Coastguard Worker  * 93/10/31	- Added the -c option to change the maximal mount counts
20*6a54128fSAndroid Build Coastguard Worker  * 93/12/14	- Added -l flag to list contents of superblock
21*6a54128fSAndroid Build Coastguard Worker  *                M.J.E. Mol ([email protected])
22*6a54128fSAndroid Build Coastguard Worker  *                F.W. ten Wolde ([email protected])
23*6a54128fSAndroid Build Coastguard Worker  * 93/12/29	- Added the -e option to change errors behavior
24*6a54128fSAndroid Build Coastguard Worker  * 94/02/27	- Ported to use the ext2fs library
25*6a54128fSAndroid Build Coastguard Worker  * 94/03/06	- Added the checks interval from Uwe Ohse ([email protected])
26*6a54128fSAndroid Build Coastguard Worker  */
27*6a54128fSAndroid Build Coastguard Worker 
28*6a54128fSAndroid Build Coastguard Worker #define _XOPEN_SOURCE 600 /* for inclusion of strptime() */
29*6a54128fSAndroid Build Coastguard Worker #include "config.h"
30*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
31*6a54128fSAndroid Build Coastguard Worker #include <grp.h>
32*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETOPT_H
33*6a54128fSAndroid Build Coastguard Worker #include <getopt.h>
34*6a54128fSAndroid Build Coastguard Worker #else
35*6a54128fSAndroid Build Coastguard Worker extern char *optarg;
36*6a54128fSAndroid Build Coastguard Worker extern int optind;
37*6a54128fSAndroid Build Coastguard Worker #endif
38*6a54128fSAndroid Build Coastguard Worker #include <pwd.h>
39*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
40*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_STDLIB_H
41*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
42*6a54128fSAndroid Build Coastguard Worker #endif
43*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_STRINGS_H
44*6a54128fSAndroid Build Coastguard Worker #include <strings.h>	/* for strcasecmp() */
45*6a54128fSAndroid Build Coastguard Worker #else
46*6a54128fSAndroid Build Coastguard Worker #define _BSD_SOURCE	/* for inclusion of strcasecmp() via <string.h> */
47*6a54128fSAndroid Build Coastguard Worker #define _DEFAULT_SOURCE	  /* since glibc 2.20 _BSD_SOURCE is deprecated */
48*6a54128fSAndroid Build Coastguard Worker #endif
49*6a54128fSAndroid Build Coastguard Worker #include <string.h>
50*6a54128fSAndroid Build Coastguard Worker #include <time.h>
51*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
52*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
53*6a54128fSAndroid Build Coastguard Worker #include <libgen.h>
54*6a54128fSAndroid Build Coastguard Worker #include <limits.h>
55*6a54128fSAndroid Build Coastguard Worker 
56*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2_fs.h"
57*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2fs.h"
58*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/kernel-jbd.h"
59*6a54128fSAndroid Build Coastguard Worker #include "et/com_err.h"
60*6a54128fSAndroid Build Coastguard Worker #include "support/plausible.h"
61*6a54128fSAndroid Build Coastguard Worker #include "support/quotaio.h"
62*6a54128fSAndroid Build Coastguard Worker #include "support/devname.h"
63*6a54128fSAndroid Build Coastguard Worker #include "uuid/uuid.h"
64*6a54128fSAndroid Build Coastguard Worker #include "e2p/e2p.h"
65*6a54128fSAndroid Build Coastguard Worker #include "util.h"
66*6a54128fSAndroid Build Coastguard Worker #include "blkid/blkid.h"
67*6a54128fSAndroid Build Coastguard Worker 
68*6a54128fSAndroid Build Coastguard Worker #include "../version.h"
69*6a54128fSAndroid Build Coastguard Worker #include "support/nls-enable.h"
70*6a54128fSAndroid Build Coastguard Worker 
71*6a54128fSAndroid Build Coastguard Worker #define QOPT_ENABLE	(1)
72*6a54128fSAndroid Build Coastguard Worker #define QOPT_DISABLE	(-1)
73*6a54128fSAndroid Build Coastguard Worker 
74*6a54128fSAndroid Build Coastguard Worker extern int ask_yn(const char *string, int def);
75*6a54128fSAndroid Build Coastguard Worker 
76*6a54128fSAndroid Build Coastguard Worker const char *program_name = "tune2fs";
77*6a54128fSAndroid Build Coastguard Worker char *device_name;
78*6a54128fSAndroid Build Coastguard Worker char *new_label, *new_last_mounted, *new_UUID;
79*6a54128fSAndroid Build Coastguard Worker char *io_options;
80*6a54128fSAndroid Build Coastguard Worker static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
81*6a54128fSAndroid Build Coastguard Worker static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
82*6a54128fSAndroid Build Coastguard Worker static int I_flag;
83*6a54128fSAndroid Build Coastguard Worker static int clear_mmp;
84*6a54128fSAndroid Build Coastguard Worker static time_t last_check_time;
85*6a54128fSAndroid Build Coastguard Worker static int print_label;
86*6a54128fSAndroid Build Coastguard Worker static int max_mount_count, mount_count, mount_flags;
87*6a54128fSAndroid Build Coastguard Worker static unsigned long interval;
88*6a54128fSAndroid Build Coastguard Worker static blk64_t reserved_blocks;
89*6a54128fSAndroid Build Coastguard Worker static double reserved_ratio;
90*6a54128fSAndroid Build Coastguard Worker static unsigned long resgid, resuid;
91*6a54128fSAndroid Build Coastguard Worker static unsigned short errors;
92*6a54128fSAndroid Build Coastguard Worker static int open_flag;
93*6a54128fSAndroid Build Coastguard Worker static char *features_cmd;
94*6a54128fSAndroid Build Coastguard Worker static char *mntopts_cmd;
95*6a54128fSAndroid Build Coastguard Worker static int stride, stripe_width;
96*6a54128fSAndroid Build Coastguard Worker static int stride_set, stripe_width_set;
97*6a54128fSAndroid Build Coastguard Worker static char *extended_cmd;
98*6a54128fSAndroid Build Coastguard Worker static unsigned long new_inode_size;
99*6a54128fSAndroid Build Coastguard Worker static char *ext_mount_opts;
100*6a54128fSAndroid Build Coastguard Worker static int quota_enable[MAXQUOTAS];
101*6a54128fSAndroid Build Coastguard Worker static int rewrite_checksums;
102*6a54128fSAndroid Build Coastguard Worker static int feature_64bit;
103*6a54128fSAndroid Build Coastguard Worker static int fsck_requested;
104*6a54128fSAndroid Build Coastguard Worker static char *undo_file;
105*6a54128fSAndroid Build Coastguard Worker int enabling_casefold;
106*6a54128fSAndroid Build Coastguard Worker 
107*6a54128fSAndroid Build Coastguard Worker int journal_size, journal_fc_size, journal_flags;
108*6a54128fSAndroid Build Coastguard Worker char *journal_device;
109*6a54128fSAndroid Build Coastguard Worker static blk64_t journal_location = ~0LL;
110*6a54128fSAndroid Build Coastguard Worker 
111*6a54128fSAndroid Build Coastguard Worker static struct list_head blk_move_list;
112*6a54128fSAndroid Build Coastguard Worker 
113*6a54128fSAndroid Build Coastguard Worker struct blk_move {
114*6a54128fSAndroid Build Coastguard Worker 	struct list_head list;
115*6a54128fSAndroid Build Coastguard Worker 	blk64_t old_loc;
116*6a54128fSAndroid Build Coastguard Worker 	blk64_t new_loc;
117*6a54128fSAndroid Build Coastguard Worker };
118*6a54128fSAndroid Build Coastguard Worker 
119*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs);
120*6a54128fSAndroid Build Coastguard Worker 
121*6a54128fSAndroid Build Coastguard Worker static const char *fsck_explain = N_("\nThis operation requires a freshly checked filesystem.\n");
122*6a54128fSAndroid Build Coastguard Worker 
123*6a54128fSAndroid Build Coastguard Worker static const char *please_fsck = N_("Please run e2fsck -f on the filesystem.\n");
124*6a54128fSAndroid Build Coastguard Worker static const char *please_dir_fsck =
125*6a54128fSAndroid Build Coastguard Worker 		N_("Please run e2fsck -fD on the filesystem.\n");
126*6a54128fSAndroid Build Coastguard Worker 
127*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_BUILD_FINDFS
128*6a54128fSAndroid Build Coastguard Worker void do_findfs(int argc, char **argv);
129*6a54128fSAndroid Build Coastguard Worker #endif
130*6a54128fSAndroid Build Coastguard Worker 
131*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_JBD_DEBUG		/* Enabled by configure --enable-jbd-debug */
132*6a54128fSAndroid Build Coastguard Worker int journal_enable_debug = -1;
133*6a54128fSAndroid Build Coastguard Worker #endif
134*6a54128fSAndroid Build Coastguard Worker 
usage(void)135*6a54128fSAndroid Build Coastguard Worker static void usage(void)
136*6a54128fSAndroid Build Coastguard Worker {
137*6a54128fSAndroid Build Coastguard Worker 	fprintf(stderr,
138*6a54128fSAndroid Build Coastguard Worker 		_("Usage: %s [-c max_mounts_count] [-e errors_behavior] [-f] "
139*6a54128fSAndroid Build Coastguard Worker 		  "[-g group]\n"
140*6a54128fSAndroid Build Coastguard Worker 		  "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
141*6a54128fSAndroid Build Coastguard Worker 		  "\t[-m reserved_blocks_percent] [-o [^]mount_options[,...]]\n"
142*6a54128fSAndroid Build Coastguard Worker 		  "\t[-r reserved_blocks_count] [-u user] [-C mount_count]\n"
143*6a54128fSAndroid Build Coastguard Worker 		  "\t[-L volume_label] [-M last_mounted_dir]\n"
144*6a54128fSAndroid Build Coastguard Worker 		  "\t[-O [^]feature[,...]] [-Q quota_options]\n"
145*6a54128fSAndroid Build Coastguard Worker 		  "\t[-E extended-option[,...]] [-T last_check_time] "
146*6a54128fSAndroid Build Coastguard Worker 		  "[-U UUID]\n\t[-I new_inode_size] [-z undo_file] device\n"),
147*6a54128fSAndroid Build Coastguard Worker 		program_name);
148*6a54128fSAndroid Build Coastguard Worker 	exit(1);
149*6a54128fSAndroid Build Coastguard Worker }
150*6a54128fSAndroid Build Coastguard Worker 
151*6a54128fSAndroid Build Coastguard Worker static __u32 ok_features[3] = {
152*6a54128fSAndroid Build Coastguard Worker 	/* Compat */
153*6a54128fSAndroid Build Coastguard Worker 	EXT3_FEATURE_COMPAT_HAS_JOURNAL |
154*6a54128fSAndroid Build Coastguard Worker 		EXT2_FEATURE_COMPAT_DIR_INDEX |
155*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_COMPAT_FAST_COMMIT |
156*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_COMPAT_STABLE_INODES,
157*6a54128fSAndroid Build Coastguard Worker 	/* Incompat */
158*6a54128fSAndroid Build Coastguard Worker 	EXT2_FEATURE_INCOMPAT_FILETYPE |
159*6a54128fSAndroid Build Coastguard Worker 		EXT3_FEATURE_INCOMPAT_EXTENTS |
160*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_FLEX_BG |
161*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_EA_INODE|
162*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_MMP |
163*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_64BIT |
164*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_ENCRYPT |
165*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_CSUM_SEED |
166*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_LARGEDIR |
167*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_CASEFOLD,
168*6a54128fSAndroid Build Coastguard Worker 	/* R/O compat */
169*6a54128fSAndroid Build Coastguard Worker 	EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
170*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
171*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
172*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
173*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
174*6a54128fSAndroid Build Coastguard Worker 		EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
175*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_QUOTA |
176*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
177*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_READONLY |
178*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_PROJECT |
179*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_VERITY
180*6a54128fSAndroid Build Coastguard Worker };
181*6a54128fSAndroid Build Coastguard Worker 
182*6a54128fSAndroid Build Coastguard Worker static __u32 clear_ok_features[3] = {
183*6a54128fSAndroid Build Coastguard Worker 	/* Compat */
184*6a54128fSAndroid Build Coastguard Worker 	EXT3_FEATURE_COMPAT_HAS_JOURNAL |
185*6a54128fSAndroid Build Coastguard Worker 		EXT2_FEATURE_COMPAT_RESIZE_INODE |
186*6a54128fSAndroid Build Coastguard Worker 		EXT2_FEATURE_COMPAT_DIR_INDEX |
187*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_COMPAT_FAST_COMMIT,
188*6a54128fSAndroid Build Coastguard Worker 	/* Incompat */
189*6a54128fSAndroid Build Coastguard Worker 	EXT2_FEATURE_INCOMPAT_FILETYPE |
190*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_FLEX_BG |
191*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_MMP |
192*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_64BIT |
193*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_CSUM_SEED,
194*6a54128fSAndroid Build Coastguard Worker 	/* R/O compat */
195*6a54128fSAndroid Build Coastguard Worker 	EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
196*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
197*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
198*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
199*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
200*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_QUOTA |
201*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_PROJECT |
202*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM |
203*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_RO_COMPAT_READONLY
204*6a54128fSAndroid Build Coastguard Worker };
205*6a54128fSAndroid Build Coastguard Worker 
206*6a54128fSAndroid Build Coastguard Worker /**
207*6a54128fSAndroid Build Coastguard Worker  * Try to get journal super block if any
208*6a54128fSAndroid Build Coastguard Worker  */
get_journal_sb(ext2_filsys jfs,char buf[SUPERBLOCK_SIZE])209*6a54128fSAndroid Build Coastguard Worker static int get_journal_sb(ext2_filsys jfs, char buf[SUPERBLOCK_SIZE])
210*6a54128fSAndroid Build Coastguard Worker {
211*6a54128fSAndroid Build Coastguard Worker 	int retval;
212*6a54128fSAndroid Build Coastguard Worker 	journal_superblock_t *jsb;
213*6a54128fSAndroid Build Coastguard Worker 
214*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_journal_dev(jfs->super)) {
215*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_UNSUPP_FEATURE;
216*6a54128fSAndroid Build Coastguard Worker 	}
217*6a54128fSAndroid Build Coastguard Worker 
218*6a54128fSAndroid Build Coastguard Worker 	/* Get the journal superblock */
219*6a54128fSAndroid Build Coastguard Worker 	if ((retval = io_channel_read_blk64(jfs->io,
220*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_journal_sb_start(jfs->blocksize), -SUPERBLOCK_SIZE, buf))) {
221*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval, "%s",
222*6a54128fSAndroid Build Coastguard Worker 		_("while reading journal superblock"));
223*6a54128fSAndroid Build Coastguard Worker 		return retval;
224*6a54128fSAndroid Build Coastguard Worker 	}
225*6a54128fSAndroid Build Coastguard Worker 
226*6a54128fSAndroid Build Coastguard Worker 	jsb = (journal_superblock_t *) buf;
227*6a54128fSAndroid Build Coastguard Worker 	if ((jsb->s_header.h_magic != (unsigned)ntohl(JBD2_MAGIC_NUMBER)) ||
228*6a54128fSAndroid Build Coastguard Worker 	    (jsb->s_header.h_blocktype != (unsigned)ntohl(JBD2_SUPERBLOCK_V2))) {
229*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Journal superblock not found!\n"), stderr);
230*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_BAD_MAGIC;
231*6a54128fSAndroid Build Coastguard Worker 	}
232*6a54128fSAndroid Build Coastguard Worker 
233*6a54128fSAndroid Build Coastguard Worker 	return 0;
234*6a54128fSAndroid Build Coastguard Worker }
235*6a54128fSAndroid Build Coastguard Worker 
journal_user(__u8 uuid[UUID_SIZE],__u8 s_users[JBD2_USERS_SIZE],int nr_users)236*6a54128fSAndroid Build Coastguard Worker static __u8 *journal_user(__u8 uuid[UUID_SIZE], __u8 s_users[JBD2_USERS_SIZE],
237*6a54128fSAndroid Build Coastguard Worker 			  int nr_users)
238*6a54128fSAndroid Build Coastguard Worker {
239*6a54128fSAndroid Build Coastguard Worker 	int i;
240*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < nr_users; i++) {
241*6a54128fSAndroid Build Coastguard Worker 		if (memcmp(uuid, &s_users[i * UUID_SIZE], UUID_SIZE) == 0)
242*6a54128fSAndroid Build Coastguard Worker 			return &s_users[i * UUID_SIZE];
243*6a54128fSAndroid Build Coastguard Worker 	}
244*6a54128fSAndroid Build Coastguard Worker 
245*6a54128fSAndroid Build Coastguard Worker 	return NULL;
246*6a54128fSAndroid Build Coastguard Worker }
247*6a54128fSAndroid Build Coastguard Worker 
248*6a54128fSAndroid Build Coastguard Worker /*
249*6a54128fSAndroid Build Coastguard Worker  * Remove an external journal from the filesystem
250*6a54128fSAndroid Build Coastguard Worker  */
remove_journal_device(ext2_filsys fs)251*6a54128fSAndroid Build Coastguard Worker static int remove_journal_device(ext2_filsys fs)
252*6a54128fSAndroid Build Coastguard Worker {
253*6a54128fSAndroid Build Coastguard Worker 	char		*journal_path;
254*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys	jfs;
255*6a54128fSAndroid Build Coastguard Worker 	char		buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
256*6a54128fSAndroid Build Coastguard Worker 	journal_superblock_t	*jsb;
257*6a54128fSAndroid Build Coastguard Worker 	int		i, nr_users;
258*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
259*6a54128fSAndroid Build Coastguard Worker 	int		commit_remove_journal = 0;
260*6a54128fSAndroid Build Coastguard Worker 	io_manager	io_ptr;
261*6a54128fSAndroid Build Coastguard Worker 
262*6a54128fSAndroid Build Coastguard Worker 	if (f_flag)
263*6a54128fSAndroid Build Coastguard Worker 		commit_remove_journal = 1; /* force removal even if error */
264*6a54128fSAndroid Build Coastguard Worker 
265*6a54128fSAndroid Build Coastguard Worker 	uuid_unparse(fs->super->s_journal_uuid, buf);
266*6a54128fSAndroid Build Coastguard Worker 	journal_path = blkid_get_devname(NULL, "UUID", buf);
267*6a54128fSAndroid Build Coastguard Worker 
268*6a54128fSAndroid Build Coastguard Worker 	if (!journal_path) {
269*6a54128fSAndroid Build Coastguard Worker 		journal_path =
270*6a54128fSAndroid Build Coastguard Worker 			ext2fs_find_block_device(fs->super->s_journal_dev);
271*6a54128fSAndroid Build Coastguard Worker 		if (!journal_path)
272*6a54128fSAndroid Build Coastguard Worker 			goto no_valid_journal;
273*6a54128fSAndroid Build Coastguard Worker 	}
274*6a54128fSAndroid Build Coastguard Worker 
275*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TESTIO_DEBUG
276*6a54128fSAndroid Build Coastguard Worker 	if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
277*6a54128fSAndroid Build Coastguard Worker 		io_ptr = test_io_manager;
278*6a54128fSAndroid Build Coastguard Worker 		test_io_backing_manager = unix_io_manager;
279*6a54128fSAndroid Build Coastguard Worker 	} else
280*6a54128fSAndroid Build Coastguard Worker #endif
281*6a54128fSAndroid Build Coastguard Worker 		io_ptr = unix_io_manager;
282*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
283*6a54128fSAndroid Build Coastguard Worker 			     EXT2_FLAG_JOURNAL_DEV_OK, 0,
284*6a54128fSAndroid Build Coastguard Worker 			     fs->blocksize, io_ptr, &jfs);
285*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
286*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval, "%s",
287*6a54128fSAndroid Build Coastguard Worker 			_("while trying to open external journal"));
288*6a54128fSAndroid Build Coastguard Worker 		goto no_valid_journal;
289*6a54128fSAndroid Build Coastguard Worker 	}
290*6a54128fSAndroid Build Coastguard Worker 
291*6a54128fSAndroid Build Coastguard Worker 	if ((retval = get_journal_sb(jfs, buf))) {
292*6a54128fSAndroid Build Coastguard Worker 		if (retval == EXT2_ET_UNSUPP_FEATURE)
293*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("%s is not a journal device.\n"),
294*6a54128fSAndroid Build Coastguard Worker 				journal_path);
295*6a54128fSAndroid Build Coastguard Worker 		goto no_valid_journal;
296*6a54128fSAndroid Build Coastguard Worker 	}
297*6a54128fSAndroid Build Coastguard Worker 
298*6a54128fSAndroid Build Coastguard Worker 	jsb = (journal_superblock_t *) buf;
299*6a54128fSAndroid Build Coastguard Worker 	/* Find the filesystem UUID */
300*6a54128fSAndroid Build Coastguard Worker 	nr_users = ntohl(jsb->s_nr_users);
301*6a54128fSAndroid Build Coastguard Worker 	if (nr_users > JBD2_USERS_MAX) {
302*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("Journal superblock is corrupted, nr_users\n"
303*6a54128fSAndroid Build Coastguard Worker 				 "is too high (%d).\n"), nr_users);
304*6a54128fSAndroid Build Coastguard Worker 		commit_remove_journal = 1;
305*6a54128fSAndroid Build Coastguard Worker 		goto no_valid_journal;
306*6a54128fSAndroid Build Coastguard Worker 	}
307*6a54128fSAndroid Build Coastguard Worker 
308*6a54128fSAndroid Build Coastguard Worker 	if (!journal_user(fs->super->s_uuid, jsb->s_users, nr_users)) {
309*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Filesystem's UUID not found on journal device.\n"),
310*6a54128fSAndroid Build Coastguard Worker 		      stderr);
311*6a54128fSAndroid Build Coastguard Worker 		commit_remove_journal = 1;
312*6a54128fSAndroid Build Coastguard Worker 		goto no_valid_journal;
313*6a54128fSAndroid Build Coastguard Worker 	}
314*6a54128fSAndroid Build Coastguard Worker 	nr_users--;
315*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < nr_users; i++)
316*6a54128fSAndroid Build Coastguard Worker 		memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
317*6a54128fSAndroid Build Coastguard Worker 	jsb->s_nr_users = htonl(nr_users);
318*6a54128fSAndroid Build Coastguard Worker 
319*6a54128fSAndroid Build Coastguard Worker 	/* Write back the journal superblock */
320*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_write_blk64(jfs->io,
321*6a54128fSAndroid Build Coastguard Worker 					ext2fs_journal_sb_start(fs->blocksize),
322*6a54128fSAndroid Build Coastguard Worker 					-SUPERBLOCK_SIZE, buf);
323*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
324*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval,
325*6a54128fSAndroid Build Coastguard Worker 			"while writing journal superblock.");
326*6a54128fSAndroid Build Coastguard Worker 		goto no_valid_journal;
327*6a54128fSAndroid Build Coastguard Worker 	}
328*6a54128fSAndroid Build Coastguard Worker 
329*6a54128fSAndroid Build Coastguard Worker 	commit_remove_journal = 1;
330*6a54128fSAndroid Build Coastguard Worker 
331*6a54128fSAndroid Build Coastguard Worker no_valid_journal:
332*6a54128fSAndroid Build Coastguard Worker 	if (commit_remove_journal == 0) {
333*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Cannot locate journal device. It was NOT removed\n"
334*6a54128fSAndroid Build Coastguard Worker 			"Use -f option to remove missing journal device.\n"),
335*6a54128fSAndroid Build Coastguard Worker 		      stderr);
336*6a54128fSAndroid Build Coastguard Worker 		return 1;
337*6a54128fSAndroid Build Coastguard Worker 	}
338*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_journal_dev = 0;
339*6a54128fSAndroid Build Coastguard Worker 	memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
340*6a54128fSAndroid Build Coastguard Worker 	uuid_clear(fs->super->s_journal_uuid);
341*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
342*6a54128fSAndroid Build Coastguard Worker 	fputs(_("Journal removed\n"), stdout);
343*6a54128fSAndroid Build Coastguard Worker 	free(journal_path);
344*6a54128fSAndroid Build Coastguard Worker 
345*6a54128fSAndroid Build Coastguard Worker 	return 0;
346*6a54128fSAndroid Build Coastguard Worker }
347*6a54128fSAndroid Build Coastguard Worker 
348*6a54128fSAndroid Build Coastguard Worker /* Helper function for remove_journal_inode */
release_blocks_proc(ext2_filsys fs,blk64_t * blocknr,e2_blkcnt_t blockcnt EXT2FS_ATTR ((unused)),blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * private EXT2FS_ATTR ((unused)))349*6a54128fSAndroid Build Coastguard Worker static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
350*6a54128fSAndroid Build Coastguard Worker 			       e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
351*6a54128fSAndroid Build Coastguard Worker 			       blk64_t ref_block EXT2FS_ATTR((unused)),
352*6a54128fSAndroid Build Coastguard Worker 			       int ref_offset EXT2FS_ATTR((unused)),
353*6a54128fSAndroid Build Coastguard Worker 			       void *private EXT2FS_ATTR((unused)))
354*6a54128fSAndroid Build Coastguard Worker {
355*6a54128fSAndroid Build Coastguard Worker 	blk64_t	block;
356*6a54128fSAndroid Build Coastguard Worker 	int	group;
357*6a54128fSAndroid Build Coastguard Worker 
358*6a54128fSAndroid Build Coastguard Worker 	block = *blocknr;
359*6a54128fSAndroid Build Coastguard Worker 	ext2fs_unmark_block_bitmap2(fs->block_map, block);
360*6a54128fSAndroid Build Coastguard Worker 	group = ext2fs_group_of_blk2(fs, block);
361*6a54128fSAndroid Build Coastguard Worker 	ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
362*6a54128fSAndroid Build Coastguard Worker 	ext2fs_group_desc_csum_set(fs, group);
363*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
364*6a54128fSAndroid Build Coastguard Worker 	return 0;
365*6a54128fSAndroid Build Coastguard Worker }
366*6a54128fSAndroid Build Coastguard Worker 
367*6a54128fSAndroid Build Coastguard Worker /*
368*6a54128fSAndroid Build Coastguard Worker  * Remove the journal inode from the filesystem
369*6a54128fSAndroid Build Coastguard Worker  */
remove_journal_inode(ext2_filsys fs)370*6a54128fSAndroid Build Coastguard Worker static errcode_t remove_journal_inode(ext2_filsys fs)
371*6a54128fSAndroid Build Coastguard Worker {
372*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode	inode;
373*6a54128fSAndroid Build Coastguard Worker 	errcode_t		retval;
374*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t		ino = fs->super->s_journal_inum;
375*6a54128fSAndroid Build Coastguard Worker 
376*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_inode(fs, ino,  &inode);
377*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
378*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval, "%s",
379*6a54128fSAndroid Build Coastguard Worker 			_("while reading journal inode"));
380*6a54128fSAndroid Build Coastguard Worker 		return retval;
381*6a54128fSAndroid Build Coastguard Worker 	}
382*6a54128fSAndroid Build Coastguard Worker 	if (ino == EXT2_JOURNAL_INO) {
383*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_read_bitmaps(fs);
384*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
385*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, retval, "%s",
386*6a54128fSAndroid Build Coastguard Worker 				_("while reading bitmaps"));
387*6a54128fSAndroid Build Coastguard Worker 			return retval;
388*6a54128fSAndroid Build Coastguard Worker 		}
389*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_block_iterate3(fs, ino,
390*6a54128fSAndroid Build Coastguard Worker 					       BLOCK_FLAG_READ_ONLY, NULL,
391*6a54128fSAndroid Build Coastguard Worker 					       release_blocks_proc, NULL);
392*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
393*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, retval, "%s",
394*6a54128fSAndroid Build Coastguard Worker 				_("while clearing journal inode"));
395*6a54128fSAndroid Build Coastguard Worker 			return retval;
396*6a54128fSAndroid Build Coastguard Worker 		}
397*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_overhead_clusters -=
398*6a54128fSAndroid Build Coastguard Worker 			EXT2FS_NUM_B2C(fs, EXT2_I_SIZE(&inode) / fs->blocksize);
399*6a54128fSAndroid Build Coastguard Worker 		memset(&inode, 0, sizeof(inode));
400*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_bb_dirty(fs);
401*6a54128fSAndroid Build Coastguard Worker 		fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
402*6a54128fSAndroid Build Coastguard Worker 	} else
403*6a54128fSAndroid Build Coastguard Worker 		inode.i_flags &= ~EXT2_IMMUTABLE_FL;
404*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_write_inode(fs, ino, &inode);
405*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
406*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval, "%s",
407*6a54128fSAndroid Build Coastguard Worker 			_("while writing journal inode"));
408*6a54128fSAndroid Build Coastguard Worker 		return retval;
409*6a54128fSAndroid Build Coastguard Worker 	}
410*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_journal_inum = 0;
411*6a54128fSAndroid Build Coastguard Worker 	memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
412*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
413*6a54128fSAndroid Build Coastguard Worker 
414*6a54128fSAndroid Build Coastguard Worker 	return 0;
415*6a54128fSAndroid Build Coastguard Worker }
416*6a54128fSAndroid Build Coastguard Worker 
417*6a54128fSAndroid Build Coastguard Worker /*
418*6a54128fSAndroid Build Coastguard Worker  * Update the default mount options
419*6a54128fSAndroid Build Coastguard Worker  */
update_mntopts(ext2_filsys fs,char * mntopts)420*6a54128fSAndroid Build Coastguard Worker static int update_mntopts(ext2_filsys fs, char *mntopts)
421*6a54128fSAndroid Build Coastguard Worker {
422*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb = fs->super;
423*6a54128fSAndroid Build Coastguard Worker 
424*6a54128fSAndroid Build Coastguard Worker 	if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
425*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("Invalid mount option set: %s\n"),
426*6a54128fSAndroid Build Coastguard Worker 			mntopts);
427*6a54128fSAndroid Build Coastguard Worker 		return 1;
428*6a54128fSAndroid Build Coastguard Worker 	}
429*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
430*6a54128fSAndroid Build Coastguard Worker 
431*6a54128fSAndroid Build Coastguard Worker 	return 0;
432*6a54128fSAndroid Build Coastguard Worker }
433*6a54128fSAndroid Build Coastguard Worker 
check_fsck_needed(ext2_filsys fs,const char * prompt)434*6a54128fSAndroid Build Coastguard Worker static int check_fsck_needed(ext2_filsys fs, const char *prompt)
435*6a54128fSAndroid Build Coastguard Worker {
436*6a54128fSAndroid Build Coastguard Worker 	/* Refuse to modify anything but a freshly checked valid filesystem. */
437*6a54128fSAndroid Build Coastguard Worker 	if (!(fs->super->s_state & EXT2_VALID_FS) ||
438*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_state & EXT2_ERROR_FS) ||
439*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_lastcheck < fs->super->s_mtime)) {
440*6a54128fSAndroid Build Coastguard Worker 		puts(_(fsck_explain));
441*6a54128fSAndroid Build Coastguard Worker 		puts(_(please_fsck));
442*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_READONLY)
443*6a54128fSAndroid Build Coastguard Worker 			printf("%s", _("(and reboot afterwards!)\n"));
444*6a54128fSAndroid Build Coastguard Worker 		return 1;
445*6a54128fSAndroid Build Coastguard Worker 	}
446*6a54128fSAndroid Build Coastguard Worker 
447*6a54128fSAndroid Build Coastguard Worker 	/* Give the admin a few seconds to bail out of a dangerous op. */
448*6a54128fSAndroid Build Coastguard Worker 	if (!getenv("TUNE2FS_FORCE_PROMPT") && (!isatty(0) || !isatty(1)))
449*6a54128fSAndroid Build Coastguard Worker 		return 0;
450*6a54128fSAndroid Build Coastguard Worker 
451*6a54128fSAndroid Build Coastguard Worker 	puts(prompt);
452*6a54128fSAndroid Build Coastguard Worker 	proceed_question(5);
453*6a54128fSAndroid Build Coastguard Worker 
454*6a54128fSAndroid Build Coastguard Worker 	return 0;
455*6a54128fSAndroid Build Coastguard Worker }
456*6a54128fSAndroid Build Coastguard Worker 
request_dir_fsck_afterwards(ext2_filsys fs)457*6a54128fSAndroid Build Coastguard Worker static void request_dir_fsck_afterwards(ext2_filsys fs)
458*6a54128fSAndroid Build Coastguard Worker {
459*6a54128fSAndroid Build Coastguard Worker 	static int requested;
460*6a54128fSAndroid Build Coastguard Worker 
461*6a54128fSAndroid Build Coastguard Worker 	if (requested++)
462*6a54128fSAndroid Build Coastguard Worker 		return;
463*6a54128fSAndroid Build Coastguard Worker 	fsck_requested++;
464*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_state &= ~EXT2_VALID_FS;
465*6a54128fSAndroid Build Coastguard Worker 	puts(_(fsck_explain));
466*6a54128fSAndroid Build Coastguard Worker 	puts(_(please_dir_fsck));
467*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_READONLY)
468*6a54128fSAndroid Build Coastguard Worker 		printf("%s", _("(and reboot afterwards!)\n"));
469*6a54128fSAndroid Build Coastguard Worker }
470*6a54128fSAndroid Build Coastguard Worker 
request_fsck_afterwards(ext2_filsys fs)471*6a54128fSAndroid Build Coastguard Worker static void request_fsck_afterwards(ext2_filsys fs)
472*6a54128fSAndroid Build Coastguard Worker {
473*6a54128fSAndroid Build Coastguard Worker 	static int requested = 0;
474*6a54128fSAndroid Build Coastguard Worker 
475*6a54128fSAndroid Build Coastguard Worker 	if (requested++)
476*6a54128fSAndroid Build Coastguard Worker 		return;
477*6a54128fSAndroid Build Coastguard Worker 	fsck_requested++;
478*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_state &= ~EXT2_VALID_FS;
479*6a54128fSAndroid Build Coastguard Worker 	printf("\n%s\n", _(please_fsck));
480*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_READONLY)
481*6a54128fSAndroid Build Coastguard Worker 		printf("%s", _("(and reboot afterwards!)\n"));
482*6a54128fSAndroid Build Coastguard Worker }
483*6a54128fSAndroid Build Coastguard Worker 
convert_64bit(ext2_filsys fs,int direction)484*6a54128fSAndroid Build Coastguard Worker static void convert_64bit(ext2_filsys fs, int direction)
485*6a54128fSAndroid Build Coastguard Worker {
486*6a54128fSAndroid Build Coastguard Worker 	/*
487*6a54128fSAndroid Build Coastguard Worker 	 * Is resize2fs going to demand a fsck run? Might as well tell the
488*6a54128fSAndroid Build Coastguard Worker 	 * user now.
489*6a54128fSAndroid Build Coastguard Worker 	 */
490*6a54128fSAndroid Build Coastguard Worker 	if (!fsck_requested &&
491*6a54128fSAndroid Build Coastguard Worker 	    ((fs->super->s_state & EXT2_ERROR_FS) ||
492*6a54128fSAndroid Build Coastguard Worker 	     !(fs->super->s_state & EXT2_VALID_FS) ||
493*6a54128fSAndroid Build Coastguard Worker 	     fs->super->s_lastcheck < fs->super->s_mtime))
494*6a54128fSAndroid Build Coastguard Worker 		request_fsck_afterwards(fs);
495*6a54128fSAndroid Build Coastguard Worker 	if (fsck_requested)
496*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("After running e2fsck, please run `resize2fs %s %s"),
497*6a54128fSAndroid Build Coastguard Worker 			direction > 0 ? "-b" : "-s", fs->device_name);
498*6a54128fSAndroid Build Coastguard Worker 	else
499*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("Please run `resize2fs %s %s"),
500*6a54128fSAndroid Build Coastguard Worker 			direction > 0 ? "-b" : "-s", fs->device_name);
501*6a54128fSAndroid Build Coastguard Worker 
502*6a54128fSAndroid Build Coastguard Worker 	if (undo_file)
503*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _(" -z \"%s\""), undo_file);
504*6a54128fSAndroid Build Coastguard Worker 	if (direction > 0)
505*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("' to enable 64-bit mode.\n"));
506*6a54128fSAndroid Build Coastguard Worker 	else
507*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("' to disable 64-bit mode.\n"));
508*6a54128fSAndroid Build Coastguard Worker }
509*6a54128fSAndroid Build Coastguard Worker 
510*6a54128fSAndroid Build Coastguard Worker /*
511*6a54128fSAndroid Build Coastguard Worker  * Rewrite directory blocks with checksums
512*6a54128fSAndroid Build Coastguard Worker  */
513*6a54128fSAndroid Build Coastguard Worker struct rewrite_dir_context {
514*6a54128fSAndroid Build Coastguard Worker 	char *buf;
515*6a54128fSAndroid Build Coastguard Worker 	errcode_t errcode;
516*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t dir;
517*6a54128fSAndroid Build Coastguard Worker 	int is_htree:1;
518*6a54128fSAndroid Build Coastguard Worker 	int clear_htree:1;
519*6a54128fSAndroid Build Coastguard Worker };
520*6a54128fSAndroid Build Coastguard Worker 
rewrite_dir_block(ext2_filsys fs,blk64_t * blocknr,e2_blkcnt_t blockcnt EXT2FS_ATTR ((unused)),blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)521*6a54128fSAndroid Build Coastguard Worker static int rewrite_dir_block(ext2_filsys fs,
522*6a54128fSAndroid Build Coastguard Worker 			     blk64_t	*blocknr,
523*6a54128fSAndroid Build Coastguard Worker 			     e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
524*6a54128fSAndroid Build Coastguard Worker 			     blk64_t	ref_block EXT2FS_ATTR((unused)),
525*6a54128fSAndroid Build Coastguard Worker 			     int	ref_offset EXT2FS_ATTR((unused)),
526*6a54128fSAndroid Build Coastguard Worker 			     void	*priv_data)
527*6a54128fSAndroid Build Coastguard Worker {
528*6a54128fSAndroid Build Coastguard Worker 	struct ext2_dx_countlimit *dcl = NULL;
529*6a54128fSAndroid Build Coastguard Worker 	struct rewrite_dir_context *ctx = priv_data;
530*6a54128fSAndroid Build Coastguard Worker 	int dcl_offset, changed = 0;
531*6a54128fSAndroid Build Coastguard Worker 
532*6a54128fSAndroid Build Coastguard Worker 	ctx->errcode = ext2fs_read_dir_block4(fs, *blocknr, ctx->buf, 0,
533*6a54128fSAndroid Build Coastguard Worker 					      ctx->dir);
534*6a54128fSAndroid Build Coastguard Worker 	if (ctx->errcode)
535*6a54128fSAndroid Build Coastguard Worker 		return BLOCK_ABORT;
536*6a54128fSAndroid Build Coastguard Worker 
537*6a54128fSAndroid Build Coastguard Worker 	/*
538*6a54128fSAndroid Build Coastguard Worker 	 * if htree node... Note that if we are clearing htree structures from
539*6a54128fSAndroid Build Coastguard Worker 	 * the directory, we treat the htree internal block as an ordinary leaf.
540*6a54128fSAndroid Build Coastguard Worker 	 * The code below will do the right thing and make space for checksum
541*6a54128fSAndroid Build Coastguard Worker 	 * there.
542*6a54128fSAndroid Build Coastguard Worker 	 */
543*6a54128fSAndroid Build Coastguard Worker 	if (ctx->is_htree && !ctx->clear_htree)
544*6a54128fSAndroid Build Coastguard Worker 		ext2fs_get_dx_countlimit(fs, (struct ext2_dir_entry *)ctx->buf,
545*6a54128fSAndroid Build Coastguard Worker 					 &dcl, &dcl_offset);
546*6a54128fSAndroid Build Coastguard Worker 	if (dcl) {
547*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_metadata_csum(fs->super)) {
548*6a54128fSAndroid Build Coastguard Worker 			/* Ensure limit is the max size */
549*6a54128fSAndroid Build Coastguard Worker 			int max_entries = (fs->blocksize - dcl_offset) /
550*6a54128fSAndroid Build Coastguard Worker 					  sizeof(struct ext2_dx_entry);
551*6a54128fSAndroid Build Coastguard Worker 			if (ext2fs_le16_to_cpu(dcl->limit) != max_entries) {
552*6a54128fSAndroid Build Coastguard Worker 				changed = 1;
553*6a54128fSAndroid Build Coastguard Worker 				dcl->limit = ext2fs_cpu_to_le16(max_entries);
554*6a54128fSAndroid Build Coastguard Worker 			}
555*6a54128fSAndroid Build Coastguard Worker 		} else {
556*6a54128fSAndroid Build Coastguard Worker 			/* If htree block is full then rebuild the dir */
557*6a54128fSAndroid Build Coastguard Worker 			if (ext2fs_le16_to_cpu(dcl->count) ==
558*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_le16_to_cpu(dcl->limit)) {
559*6a54128fSAndroid Build Coastguard Worker 				request_dir_fsck_afterwards(fs);
560*6a54128fSAndroid Build Coastguard Worker 				return 0;
561*6a54128fSAndroid Build Coastguard Worker 			}
562*6a54128fSAndroid Build Coastguard Worker 			/*
563*6a54128fSAndroid Build Coastguard Worker 			 * Ensure dcl->limit is small enough to leave room for
564*6a54128fSAndroid Build Coastguard Worker 			 * the checksum tail.
565*6a54128fSAndroid Build Coastguard Worker 			 */
566*6a54128fSAndroid Build Coastguard Worker 			int max_entries = (fs->blocksize - (dcl_offset +
567*6a54128fSAndroid Build Coastguard Worker 						sizeof(struct ext2_dx_tail))) /
568*6a54128fSAndroid Build Coastguard Worker 					  sizeof(struct ext2_dx_entry);
569*6a54128fSAndroid Build Coastguard Worker 			if (ext2fs_le16_to_cpu(dcl->limit) != max_entries)
570*6a54128fSAndroid Build Coastguard Worker 				dcl->limit = ext2fs_cpu_to_le16(max_entries);
571*6a54128fSAndroid Build Coastguard Worker 			/* Always rewrite checksum */
572*6a54128fSAndroid Build Coastguard Worker 			changed = 1;
573*6a54128fSAndroid Build Coastguard Worker 		}
574*6a54128fSAndroid Build Coastguard Worker 	} else {
575*6a54128fSAndroid Build Coastguard Worker 		unsigned int rec_len, name_size;
576*6a54128fSAndroid Build Coastguard Worker 		char *top = ctx->buf + fs->blocksize;
577*6a54128fSAndroid Build Coastguard Worker 		struct ext2_dir_entry *de = (struct ext2_dir_entry *)ctx->buf;
578*6a54128fSAndroid Build Coastguard Worker 		struct ext2_dir_entry *last_de = NULL, *penultimate_de = NULL;
579*6a54128fSAndroid Build Coastguard Worker 
580*6a54128fSAndroid Build Coastguard Worker 		/* Find last and penultimate dirent */
581*6a54128fSAndroid Build Coastguard Worker 		while ((char *)de < top) {
582*6a54128fSAndroid Build Coastguard Worker 			penultimate_de = last_de;
583*6a54128fSAndroid Build Coastguard Worker 			last_de = de;
584*6a54128fSAndroid Build Coastguard Worker 			ctx->errcode = ext2fs_get_rec_len(fs, de, &rec_len);
585*6a54128fSAndroid Build Coastguard Worker 			if (!ctx->errcode && !rec_len)
586*6a54128fSAndroid Build Coastguard Worker 				ctx->errcode = EXT2_ET_DIR_CORRUPTED;
587*6a54128fSAndroid Build Coastguard Worker 			if (ctx->errcode)
588*6a54128fSAndroid Build Coastguard Worker 				return BLOCK_ABORT;
589*6a54128fSAndroid Build Coastguard Worker 			de = (struct ext2_dir_entry *)(((char *)de) + rec_len);
590*6a54128fSAndroid Build Coastguard Worker 		}
591*6a54128fSAndroid Build Coastguard Worker 		ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
592*6a54128fSAndroid Build Coastguard Worker 		if (ctx->errcode)
593*6a54128fSAndroid Build Coastguard Worker 			return BLOCK_ABORT;
594*6a54128fSAndroid Build Coastguard Worker 		name_size = ext2fs_dirent_name_len(last_de);
595*6a54128fSAndroid Build Coastguard Worker 
596*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_metadata_csum(fs->super)) {
597*6a54128fSAndroid Build Coastguard Worker 			if (!penultimate_de)
598*6a54128fSAndroid Build Coastguard Worker 				return 0;
599*6a54128fSAndroid Build Coastguard Worker 			if (last_de->inode ||
600*6a54128fSAndroid Build Coastguard Worker 			    name_size ||
601*6a54128fSAndroid Build Coastguard Worker 			    rec_len != sizeof(struct ext2_dir_entry_tail))
602*6a54128fSAndroid Build Coastguard Worker 				return 0;
603*6a54128fSAndroid Build Coastguard Worker 			/*
604*6a54128fSAndroid Build Coastguard Worker 			 * The last dirent is unused and the right length to
605*6a54128fSAndroid Build Coastguard Worker 			 * have stored a checksum.  Erase it.
606*6a54128fSAndroid Build Coastguard Worker 			 */
607*6a54128fSAndroid Build Coastguard Worker 			ctx->errcode = ext2fs_get_rec_len(fs, penultimate_de,
608*6a54128fSAndroid Build Coastguard Worker 							  &rec_len);
609*6a54128fSAndroid Build Coastguard Worker 			if (!rec_len)
610*6a54128fSAndroid Build Coastguard Worker 				ctx->errcode = EXT2_ET_DIR_CORRUPTED;
611*6a54128fSAndroid Build Coastguard Worker 			if (ctx->errcode)
612*6a54128fSAndroid Build Coastguard Worker 				return BLOCK_ABORT;
613*6a54128fSAndroid Build Coastguard Worker 			ext2fs_set_rec_len(fs, rec_len +
614*6a54128fSAndroid Build Coastguard Worker 					sizeof(struct ext2_dir_entry_tail),
615*6a54128fSAndroid Build Coastguard Worker 					penultimate_de);
616*6a54128fSAndroid Build Coastguard Worker 			changed = 1;
617*6a54128fSAndroid Build Coastguard Worker 		} else {
618*6a54128fSAndroid Build Coastguard Worker 			unsigned csum_size = sizeof(struct ext2_dir_entry_tail);
619*6a54128fSAndroid Build Coastguard Worker 			struct ext2_dir_entry_tail *t;
620*6a54128fSAndroid Build Coastguard Worker 
621*6a54128fSAndroid Build Coastguard Worker 			/*
622*6a54128fSAndroid Build Coastguard Worker 			 * If the last dirent looks like the tail, just update
623*6a54128fSAndroid Build Coastguard Worker 			 * the checksum.
624*6a54128fSAndroid Build Coastguard Worker 			 */
625*6a54128fSAndroid Build Coastguard Worker 			if (!last_de->inode &&
626*6a54128fSAndroid Build Coastguard Worker 			    rec_len == csum_size) {
627*6a54128fSAndroid Build Coastguard Worker 				t = (struct ext2_dir_entry_tail *)last_de;
628*6a54128fSAndroid Build Coastguard Worker 				t->det_reserved_name_len =
629*6a54128fSAndroid Build Coastguard Worker 						EXT2_DIR_NAME_LEN_CSUM;
630*6a54128fSAndroid Build Coastguard Worker 				changed = 1;
631*6a54128fSAndroid Build Coastguard Worker 				goto out;
632*6a54128fSAndroid Build Coastguard Worker 			}
633*6a54128fSAndroid Build Coastguard Worker 			if (name_size & 3)
634*6a54128fSAndroid Build Coastguard Worker 				name_size = (name_size & ~3) + 4;
635*6a54128fSAndroid Build Coastguard Worker 			/* If there's not enough space for the tail, e2fsck */
636*6a54128fSAndroid Build Coastguard Worker 			if (rec_len <= (8 + name_size + csum_size)) {
637*6a54128fSAndroid Build Coastguard Worker 				request_dir_fsck_afterwards(fs);
638*6a54128fSAndroid Build Coastguard Worker 				return 0;
639*6a54128fSAndroid Build Coastguard Worker 			}
640*6a54128fSAndroid Build Coastguard Worker 			/* Shorten that last de and insert the tail */
641*6a54128fSAndroid Build Coastguard Worker 			ext2fs_set_rec_len(fs, rec_len - csum_size, last_de);
642*6a54128fSAndroid Build Coastguard Worker 			t = EXT2_DIRENT_TAIL(ctx->buf, fs->blocksize);
643*6a54128fSAndroid Build Coastguard Worker 			ext2fs_initialize_dirent_tail(fs, t);
644*6a54128fSAndroid Build Coastguard Worker 
645*6a54128fSAndroid Build Coastguard Worker 			/* Always update checksum */
646*6a54128fSAndroid Build Coastguard Worker 			changed = 1;
647*6a54128fSAndroid Build Coastguard Worker 		}
648*6a54128fSAndroid Build Coastguard Worker 	}
649*6a54128fSAndroid Build Coastguard Worker 
650*6a54128fSAndroid Build Coastguard Worker out:
651*6a54128fSAndroid Build Coastguard Worker 	if (!changed)
652*6a54128fSAndroid Build Coastguard Worker 		return 0;
653*6a54128fSAndroid Build Coastguard Worker 
654*6a54128fSAndroid Build Coastguard Worker 	ctx->errcode = ext2fs_write_dir_block4(fs, *blocknr, ctx->buf,
655*6a54128fSAndroid Build Coastguard Worker 					       0, ctx->dir);
656*6a54128fSAndroid Build Coastguard Worker 	if (ctx->errcode)
657*6a54128fSAndroid Build Coastguard Worker 		return BLOCK_ABORT;
658*6a54128fSAndroid Build Coastguard Worker 
659*6a54128fSAndroid Build Coastguard Worker 	return 0;
660*6a54128fSAndroid Build Coastguard Worker }
661*6a54128fSAndroid Build Coastguard Worker 
rewrite_directory(ext2_filsys fs,ext2_ino_t dir,struct ext2_inode * inode)662*6a54128fSAndroid Build Coastguard Worker static errcode_t rewrite_directory(ext2_filsys fs, ext2_ino_t dir,
663*6a54128fSAndroid Build Coastguard Worker 				   struct ext2_inode *inode)
664*6a54128fSAndroid Build Coastguard Worker {
665*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
666*6a54128fSAndroid Build Coastguard Worker 	struct rewrite_dir_context ctx;
667*6a54128fSAndroid Build Coastguard Worker 
668*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(fs->blocksize, &ctx.buf);
669*6a54128fSAndroid Build Coastguard Worker 	if (retval)
670*6a54128fSAndroid Build Coastguard Worker 		return retval;
671*6a54128fSAndroid Build Coastguard Worker 
672*6a54128fSAndroid Build Coastguard Worker 	ctx.is_htree = !!(inode->i_flags & EXT2_INDEX_FL);
673*6a54128fSAndroid Build Coastguard Worker 	ctx.clear_htree = !ext2fs_has_feature_dir_index(fs->super);
674*6a54128fSAndroid Build Coastguard Worker 	ctx.dir = dir;
675*6a54128fSAndroid Build Coastguard Worker 	ctx.errcode = 0;
676*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_READ_ONLY |
677*6a54128fSAndroid Build Coastguard Worker 						BLOCK_FLAG_DATA_ONLY,
678*6a54128fSAndroid Build Coastguard Worker 				       0, rewrite_dir_block, &ctx);
679*6a54128fSAndroid Build Coastguard Worker 
680*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&ctx.buf);
681*6a54128fSAndroid Build Coastguard Worker 	if (retval)
682*6a54128fSAndroid Build Coastguard Worker 		return retval;
683*6a54128fSAndroid Build Coastguard Worker 
684*6a54128fSAndroid Build Coastguard Worker 	if (ctx.is_htree && ctx.clear_htree) {
685*6a54128fSAndroid Build Coastguard Worker 		inode->i_flags &= ~EXT2_INDEX_FL;
686*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_write_inode(fs, dir, inode);
687*6a54128fSAndroid Build Coastguard Worker 		if (retval)
688*6a54128fSAndroid Build Coastguard Worker 			return retval;
689*6a54128fSAndroid Build Coastguard Worker 	}
690*6a54128fSAndroid Build Coastguard Worker 
691*6a54128fSAndroid Build Coastguard Worker 	return ctx.errcode;
692*6a54128fSAndroid Build Coastguard Worker }
693*6a54128fSAndroid Build Coastguard Worker 
694*6a54128fSAndroid Build Coastguard Worker /*
695*6a54128fSAndroid Build Coastguard Worker  * Context information that does not change across rewrite_one_inode()
696*6a54128fSAndroid Build Coastguard Worker  * invocations.
697*6a54128fSAndroid Build Coastguard Worker  */
698*6a54128fSAndroid Build Coastguard Worker struct rewrite_context {
699*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs;
700*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode *zero_inode;
701*6a54128fSAndroid Build Coastguard Worker 	char *ea_buf;
702*6a54128fSAndroid Build Coastguard Worker 	int inode_size;
703*6a54128fSAndroid Build Coastguard Worker };
704*6a54128fSAndroid Build Coastguard Worker 
705*6a54128fSAndroid Build Coastguard Worker #define fatal_err(code, args...)		\
706*6a54128fSAndroid Build Coastguard Worker 	do {					\
707*6a54128fSAndroid Build Coastguard Worker 		com_err(__func__, code, args);	\
708*6a54128fSAndroid Build Coastguard Worker 		exit(1);			\
709*6a54128fSAndroid Build Coastguard Worker 	} while (0);
710*6a54128fSAndroid Build Coastguard Worker 
update_ea_inode_hash(struct rewrite_context * ctx,ext2_ino_t ino,struct ext2_inode * inode)711*6a54128fSAndroid Build Coastguard Worker static void update_ea_inode_hash(struct rewrite_context *ctx, ext2_ino_t ino,
712*6a54128fSAndroid Build Coastguard Worker 				 struct ext2_inode *inode)
713*6a54128fSAndroid Build Coastguard Worker {
714*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
715*6a54128fSAndroid Build Coastguard Worker 	ext2_file_t file;
716*6a54128fSAndroid Build Coastguard Worker 	__u32 hash;
717*6a54128fSAndroid Build Coastguard Worker 
718*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_file_open(ctx->fs, ino, 0, &file);
719*6a54128fSAndroid Build Coastguard Worker 	if (retval)
720*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "open ea_inode");
721*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_file_read(file, ctx->ea_buf, inode->i_size,
722*6a54128fSAndroid Build Coastguard Worker 				  NULL);
723*6a54128fSAndroid Build Coastguard Worker 	if (retval)
724*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "read ea_inode");
725*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_file_close(file);
726*6a54128fSAndroid Build Coastguard Worker 	if (retval)
727*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "close ea_inode");
728*6a54128fSAndroid Build Coastguard Worker 
729*6a54128fSAndroid Build Coastguard Worker 	hash = ext2fs_crc32c_le(ctx->fs->csum_seed,
730*6a54128fSAndroid Build Coastguard Worker 				(unsigned char *) ctx->ea_buf, inode->i_size);
731*6a54128fSAndroid Build Coastguard Worker 	ext2fs_set_ea_inode_hash(inode, hash);
732*6a54128fSAndroid Build Coastguard Worker }
733*6a54128fSAndroid Build Coastguard Worker 
update_xattr_entry_hashes(ext2_filsys fs,struct ext2_ext_attr_entry * entry,struct ext2_ext_attr_entry * end)734*6a54128fSAndroid Build Coastguard Worker static int update_xattr_entry_hashes(ext2_filsys fs,
735*6a54128fSAndroid Build Coastguard Worker 				     struct ext2_ext_attr_entry *entry,
736*6a54128fSAndroid Build Coastguard Worker 				     struct ext2_ext_attr_entry *end)
737*6a54128fSAndroid Build Coastguard Worker {
738*6a54128fSAndroid Build Coastguard Worker 	int modified = 0;
739*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
740*6a54128fSAndroid Build Coastguard Worker 
741*6a54128fSAndroid Build Coastguard Worker 	while (entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry)) {
742*6a54128fSAndroid Build Coastguard Worker 		if (entry->e_value_inum) {
743*6a54128fSAndroid Build Coastguard Worker 			retval = ext2fs_ext_attr_hash_entry2(fs, entry, NULL,
744*6a54128fSAndroid Build Coastguard Worker 							     &entry->e_hash);
745*6a54128fSAndroid Build Coastguard Worker 			if (retval)
746*6a54128fSAndroid Build Coastguard Worker 				fatal_err(retval, "hash ea_inode entry");
747*6a54128fSAndroid Build Coastguard Worker 			modified = 1;
748*6a54128fSAndroid Build Coastguard Worker 		}
749*6a54128fSAndroid Build Coastguard Worker 		entry = EXT2_EXT_ATTR_NEXT(entry);
750*6a54128fSAndroid Build Coastguard Worker 	}
751*6a54128fSAndroid Build Coastguard Worker 	return modified;
752*6a54128fSAndroid Build Coastguard Worker }
753*6a54128fSAndroid Build Coastguard Worker 
update_inline_xattr_hashes(struct rewrite_context * ctx,struct ext2_inode_large * inode)754*6a54128fSAndroid Build Coastguard Worker static void update_inline_xattr_hashes(struct rewrite_context *ctx,
755*6a54128fSAndroid Build Coastguard Worker 				       struct ext2_inode_large *inode)
756*6a54128fSAndroid Build Coastguard Worker {
757*6a54128fSAndroid Build Coastguard Worker 	struct ext2_ext_attr_entry *start, *end;
758*6a54128fSAndroid Build Coastguard Worker 	__u32 *ea_magic;
759*6a54128fSAndroid Build Coastguard Worker 
760*6a54128fSAndroid Build Coastguard Worker 	if (inode->i_extra_isize == 0)
761*6a54128fSAndroid Build Coastguard Worker 		return;
762*6a54128fSAndroid Build Coastguard Worker 
763*6a54128fSAndroid Build Coastguard Worker 	if (inode->i_extra_isize & 3 ||
764*6a54128fSAndroid Build Coastguard Worker 	    inode->i_extra_isize > ctx->inode_size - EXT2_GOOD_OLD_INODE_SIZE)
765*6a54128fSAndroid Build Coastguard Worker 		fatal_err(EXT2_ET_INODE_CORRUPTED, "bad i_extra_isize")
766*6a54128fSAndroid Build Coastguard Worker 
767*6a54128fSAndroid Build Coastguard Worker 	ea_magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
768*6a54128fSAndroid Build Coastguard Worker 				inode->i_extra_isize);
769*6a54128fSAndroid Build Coastguard Worker 	if (*ea_magic != EXT2_EXT_ATTR_MAGIC)
770*6a54128fSAndroid Build Coastguard Worker 		return;
771*6a54128fSAndroid Build Coastguard Worker 
772*6a54128fSAndroid Build Coastguard Worker 	start = (struct ext2_ext_attr_entry *)(ea_magic + 1);
773*6a54128fSAndroid Build Coastguard Worker 	end = (struct ext2_ext_attr_entry *)((char *)inode + ctx->inode_size);
774*6a54128fSAndroid Build Coastguard Worker 
775*6a54128fSAndroid Build Coastguard Worker 	update_xattr_entry_hashes(ctx->fs, start, end);
776*6a54128fSAndroid Build Coastguard Worker }
777*6a54128fSAndroid Build Coastguard Worker 
update_block_xattr_hashes(struct rewrite_context * ctx,char * block_buf)778*6a54128fSAndroid Build Coastguard Worker static void update_block_xattr_hashes(struct rewrite_context *ctx,
779*6a54128fSAndroid Build Coastguard Worker 				      char *block_buf)
780*6a54128fSAndroid Build Coastguard Worker {
781*6a54128fSAndroid Build Coastguard Worker 	struct ext2_ext_attr_header *header;
782*6a54128fSAndroid Build Coastguard Worker 	struct ext2_ext_attr_entry *start, *end;
783*6a54128fSAndroid Build Coastguard Worker 
784*6a54128fSAndroid Build Coastguard Worker 	header = (struct ext2_ext_attr_header *)block_buf;
785*6a54128fSAndroid Build Coastguard Worker 	if (header->h_magic != EXT2_EXT_ATTR_MAGIC)
786*6a54128fSAndroid Build Coastguard Worker 		return;
787*6a54128fSAndroid Build Coastguard Worker 
788*6a54128fSAndroid Build Coastguard Worker 	start = (struct ext2_ext_attr_entry *)(header+1);
789*6a54128fSAndroid Build Coastguard Worker 	end = (struct ext2_ext_attr_entry *)(block_buf + ctx->fs->blocksize);
790*6a54128fSAndroid Build Coastguard Worker 
791*6a54128fSAndroid Build Coastguard Worker 	if (update_xattr_entry_hashes(ctx->fs, start, end))
792*6a54128fSAndroid Build Coastguard Worker 		ext2fs_ext_attr_block_rehash(header, end);
793*6a54128fSAndroid Build Coastguard Worker }
794*6a54128fSAndroid Build Coastguard Worker 
rewrite_one_inode(struct rewrite_context * ctx,ext2_ino_t ino,struct ext2_inode * inode)795*6a54128fSAndroid Build Coastguard Worker static void rewrite_one_inode(struct rewrite_context *ctx, ext2_ino_t ino,
796*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_inode *inode)
797*6a54128fSAndroid Build Coastguard Worker {
798*6a54128fSAndroid Build Coastguard Worker 	blk64_t file_acl_block;
799*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
800*6a54128fSAndroid Build Coastguard Worker 
801*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_test_inode_bitmap2(ctx->fs->inode_map, ino)) {
802*6a54128fSAndroid Build Coastguard Worker 		if (!memcmp(inode, ctx->zero_inode, ctx->inode_size))
803*6a54128fSAndroid Build Coastguard Worker 			return;
804*6a54128fSAndroid Build Coastguard Worker 		memset(inode, 0, ctx->inode_size);
805*6a54128fSAndroid Build Coastguard Worker 	}
806*6a54128fSAndroid Build Coastguard Worker 
807*6a54128fSAndroid Build Coastguard Worker 	if (inode->i_flags & EXT4_EA_INODE_FL)
808*6a54128fSAndroid Build Coastguard Worker 		update_ea_inode_hash(ctx, ino, inode);
809*6a54128fSAndroid Build Coastguard Worker 
810*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_size != EXT2_GOOD_OLD_INODE_SIZE)
811*6a54128fSAndroid Build Coastguard Worker 		update_inline_xattr_hashes(ctx,
812*6a54128fSAndroid Build Coastguard Worker 					   (struct ext2_inode_large *)inode);
813*6a54128fSAndroid Build Coastguard Worker 
814*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_write_inode_full(ctx->fs, ino, inode, ctx->inode_size);
815*6a54128fSAndroid Build Coastguard Worker 	if (retval)
816*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while writing inode");
817*6a54128fSAndroid Build Coastguard Worker 
818*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_fix_extents_checksums(ctx->fs, ino, inode);
819*6a54128fSAndroid Build Coastguard Worker 	if (retval)
820*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while rewriting extents");
821*6a54128fSAndroid Build Coastguard Worker 
822*6a54128fSAndroid Build Coastguard Worker 	if (LINUX_S_ISDIR(inode->i_mode) &&
823*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_inode_has_valid_blocks2(ctx->fs, inode)) {
824*6a54128fSAndroid Build Coastguard Worker 		retval = rewrite_directory(ctx->fs, ino, inode);
825*6a54128fSAndroid Build Coastguard Worker 		if (retval)
826*6a54128fSAndroid Build Coastguard Worker 			fatal_err(retval, "while rewriting directories");
827*6a54128fSAndroid Build Coastguard Worker 	}
828*6a54128fSAndroid Build Coastguard Worker 
829*6a54128fSAndroid Build Coastguard Worker 	file_acl_block = ext2fs_file_acl_block(ctx->fs, inode);
830*6a54128fSAndroid Build Coastguard Worker 	if (!file_acl_block)
831*6a54128fSAndroid Build Coastguard Worker 		return;
832*6a54128fSAndroid Build Coastguard Worker 
833*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_ext_attr3(ctx->fs, file_acl_block, ctx->ea_buf,
834*6a54128fSAndroid Build Coastguard Worker 				       ino);
835*6a54128fSAndroid Build Coastguard Worker 	if (retval)
836*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while rewriting extended attribute");
837*6a54128fSAndroid Build Coastguard Worker 
838*6a54128fSAndroid Build Coastguard Worker 	update_block_xattr_hashes(ctx, ctx->ea_buf);
839*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_write_ext_attr3(ctx->fs, file_acl_block, ctx->ea_buf,
840*6a54128fSAndroid Build Coastguard Worker 					ino);
841*6a54128fSAndroid Build Coastguard Worker 	if (retval)
842*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while rewriting extended attribute");
843*6a54128fSAndroid Build Coastguard Worker }
844*6a54128fSAndroid Build Coastguard Worker 
845*6a54128fSAndroid Build Coastguard Worker #define REWRITE_EA_FL		0x01	/* Rewrite EA inodes */
846*6a54128fSAndroid Build Coastguard Worker #define REWRITE_DIR_FL		0x02	/* Rewrite directories */
847*6a54128fSAndroid Build Coastguard Worker #define REWRITE_NONDIR_FL	0x04	/* Rewrite other inodes */
848*6a54128fSAndroid Build Coastguard Worker #define REWRITE_ALL (REWRITE_EA_FL | REWRITE_DIR_FL | REWRITE_NONDIR_FL)
849*6a54128fSAndroid Build Coastguard Worker 
rewrite_inodes_pass(struct rewrite_context * ctx,unsigned int flags)850*6a54128fSAndroid Build Coastguard Worker static void rewrite_inodes_pass(struct rewrite_context *ctx, unsigned int flags)
851*6a54128fSAndroid Build Coastguard Worker {
852*6a54128fSAndroid Build Coastguard Worker 	ext2_inode_scan	scan;
853*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
854*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	ino;
855*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode *inode;
856*6a54128fSAndroid Build Coastguard Worker 	int rewrite;
857*6a54128fSAndroid Build Coastguard Worker 
858*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(ctx->inode_size, &inode);
859*6a54128fSAndroid Build Coastguard Worker 	if (retval)
860*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while allocating memory");
861*6a54128fSAndroid Build Coastguard Worker 
862*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_open_inode_scan(ctx->fs, 0, &scan);
863*6a54128fSAndroid Build Coastguard Worker 	if (retval)
864*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while opening inode scan");
865*6a54128fSAndroid Build Coastguard Worker 
866*6a54128fSAndroid Build Coastguard Worker 	do {
867*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_get_next_inode_full(scan, &ino, inode,
868*6a54128fSAndroid Build Coastguard Worker 						    ctx->inode_size);
869*6a54128fSAndroid Build Coastguard Worker 		if (retval)
870*6a54128fSAndroid Build Coastguard Worker 			fatal_err(retval, "while getting next inode");
871*6a54128fSAndroid Build Coastguard Worker 		if (!ino)
872*6a54128fSAndroid Build Coastguard Worker 			break;
873*6a54128fSAndroid Build Coastguard Worker 
874*6a54128fSAndroid Build Coastguard Worker 		rewrite = 0;
875*6a54128fSAndroid Build Coastguard Worker 		if (inode->i_flags & EXT4_EA_INODE_FL) {
876*6a54128fSAndroid Build Coastguard Worker 			if (flags & REWRITE_EA_FL)
877*6a54128fSAndroid Build Coastguard Worker 				rewrite = 1;
878*6a54128fSAndroid Build Coastguard Worker 		} else if (LINUX_S_ISDIR(inode->i_mode)) {
879*6a54128fSAndroid Build Coastguard Worker 			if (flags & REWRITE_DIR_FL)
880*6a54128fSAndroid Build Coastguard Worker 				rewrite = 1;
881*6a54128fSAndroid Build Coastguard Worker 		} else {
882*6a54128fSAndroid Build Coastguard Worker 			if (flags & REWRITE_NONDIR_FL)
883*6a54128fSAndroid Build Coastguard Worker 				rewrite = 1;
884*6a54128fSAndroid Build Coastguard Worker 		}
885*6a54128fSAndroid Build Coastguard Worker 		if (rewrite)
886*6a54128fSAndroid Build Coastguard Worker 			rewrite_one_inode(ctx, ino, inode);
887*6a54128fSAndroid Build Coastguard Worker 	} while (ino);
888*6a54128fSAndroid Build Coastguard Worker 	ext2fs_close_inode_scan(scan);
889*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&inode);
890*6a54128fSAndroid Build Coastguard Worker }
891*6a54128fSAndroid Build Coastguard Worker 
892*6a54128fSAndroid Build Coastguard Worker /*
893*6a54128fSAndroid Build Coastguard Worker  * Forcibly rewrite checksums in inodes specified by 'flags'
894*6a54128fSAndroid Build Coastguard Worker  */
rewrite_inodes(ext2_filsys fs,unsigned int flags)895*6a54128fSAndroid Build Coastguard Worker static void rewrite_inodes(ext2_filsys fs, unsigned int flags)
896*6a54128fSAndroid Build Coastguard Worker {
897*6a54128fSAndroid Build Coastguard Worker 	struct rewrite_context ctx = {
898*6a54128fSAndroid Build Coastguard Worker 		.fs = fs,
899*6a54128fSAndroid Build Coastguard Worker 		.inode_size = EXT2_INODE_SIZE(fs->super),
900*6a54128fSAndroid Build Coastguard Worker 	};
901*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
902*6a54128fSAndroid Build Coastguard Worker 
903*6a54128fSAndroid Build Coastguard Worker 	if (fs->super->s_creator_os == EXT2_OS_HURD)
904*6a54128fSAndroid Build Coastguard Worker 		return;
905*6a54128fSAndroid Build Coastguard Worker 
906*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_memzero(ctx.inode_size, &ctx.zero_inode);
907*6a54128fSAndroid Build Coastguard Worker 	if (retval)
908*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while allocating memory");
909*6a54128fSAndroid Build Coastguard Worker 
910*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(64 * 1024, &ctx.ea_buf);
911*6a54128fSAndroid Build Coastguard Worker 	if (retval)
912*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while allocating memory");
913*6a54128fSAndroid Build Coastguard Worker 
914*6a54128fSAndroid Build Coastguard Worker 	/*
915*6a54128fSAndroid Build Coastguard Worker 	 * Extended attribute inodes have a lookup hash that needs to be
916*6a54128fSAndroid Build Coastguard Worker 	 * recalculated with the new csum_seed. Other inodes referencing xattr
917*6a54128fSAndroid Build Coastguard Worker 	 * inodes need this value to be up to date. That's why we do two passes:
918*6a54128fSAndroid Build Coastguard Worker 	 *
919*6a54128fSAndroid Build Coastguard Worker 	 * pass 1: update xattr inodes to update their lookup hash as well as
920*6a54128fSAndroid Build Coastguard Worker 	 *         other checksums.
921*6a54128fSAndroid Build Coastguard Worker 	 *
922*6a54128fSAndroid Build Coastguard Worker 	 * pass 2: go over other inodes to update their checksums.
923*6a54128fSAndroid Build Coastguard Worker 	 */
924*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_ea_inode(fs->super) && (flags & REWRITE_EA_FL))
925*6a54128fSAndroid Build Coastguard Worker 		rewrite_inodes_pass(&ctx, REWRITE_EA_FL);
926*6a54128fSAndroid Build Coastguard Worker 	flags &= ~REWRITE_EA_FL;
927*6a54128fSAndroid Build Coastguard Worker 	rewrite_inodes_pass(&ctx, flags);
928*6a54128fSAndroid Build Coastguard Worker 
929*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&ctx.zero_inode);
930*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&ctx.ea_buf);
931*6a54128fSAndroid Build Coastguard Worker }
932*6a54128fSAndroid Build Coastguard Worker 
rewrite_metadata_checksums(ext2_filsys fs,unsigned int flags)933*6a54128fSAndroid Build Coastguard Worker static errcode_t rewrite_metadata_checksums(ext2_filsys fs, unsigned int flags)
934*6a54128fSAndroid Build Coastguard Worker {
935*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
936*6a54128fSAndroid Build Coastguard Worker 	dgrp_t i;
937*6a54128fSAndroid Build Coastguard Worker 
938*6a54128fSAndroid Build Coastguard Worker 	fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
939*6a54128fSAndroid Build Coastguard Worker 	ext2fs_init_csum_seed(fs);
940*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++)
941*6a54128fSAndroid Build Coastguard Worker 		ext2fs_group_desc_csum_set(fs, i);
942*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_bitmaps(fs);
943*6a54128fSAndroid Build Coastguard Worker 	if (retval)
944*6a54128fSAndroid Build Coastguard Worker 		fatal_err(retval, "while reading bitmaps");
945*6a54128fSAndroid Build Coastguard Worker 	rewrite_inodes(fs, flags);
946*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_ib_dirty(fs);
947*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_bb_dirty(fs);
948*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_mmp_update2(fs, 1);
949*6a54128fSAndroid Build Coastguard Worker 	if (retval)
950*6a54128fSAndroid Build Coastguard Worker 		return retval;
951*6a54128fSAndroid Build Coastguard Worker 	fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
952*6a54128fSAndroid Build Coastguard Worker 	fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
953*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_metadata_csum(fs->super))
954*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
955*6a54128fSAndroid Build Coastguard Worker 	else
956*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_checksum_type = 0;
957*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
958*6a54128fSAndroid Build Coastguard Worker 	return 0;
959*6a54128fSAndroid Build Coastguard Worker }
960*6a54128fSAndroid Build Coastguard Worker 
enable_uninit_bg(ext2_filsys fs)961*6a54128fSAndroid Build Coastguard Worker static void enable_uninit_bg(ext2_filsys fs)
962*6a54128fSAndroid Build Coastguard Worker {
963*6a54128fSAndroid Build Coastguard Worker 	struct ext2_group_desc *gd;
964*6a54128fSAndroid Build Coastguard Worker 	dgrp_t i;
965*6a54128fSAndroid Build Coastguard Worker 
966*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++) {
967*6a54128fSAndroid Build Coastguard Worker 		gd = ext2fs_group_desc(fs, fs->group_desc, i);
968*6a54128fSAndroid Build Coastguard Worker 		gd->bg_itable_unused = 0;
969*6a54128fSAndroid Build Coastguard Worker 		gd->bg_flags = EXT2_BG_INODE_ZEROED;
970*6a54128fSAndroid Build Coastguard Worker 		ext2fs_group_desc_csum_set(fs, i);
971*6a54128fSAndroid Build Coastguard Worker 	}
972*6a54128fSAndroid Build Coastguard Worker 	fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
973*6a54128fSAndroid Build Coastguard Worker }
974*6a54128fSAndroid Build Coastguard Worker 
zero_empty_inodes(ext2_filsys fs)975*6a54128fSAndroid Build Coastguard Worker static errcode_t zero_empty_inodes(ext2_filsys fs)
976*6a54128fSAndroid Build Coastguard Worker {
977*6a54128fSAndroid Build Coastguard Worker 	int length = EXT2_INODE_SIZE(fs->super);
978*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode *inode = NULL;
979*6a54128fSAndroid Build Coastguard Worker 	ext2_inode_scan	scan;
980*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
981*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	ino;
982*6a54128fSAndroid Build Coastguard Worker 
983*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_open_inode_scan(fs, 0, &scan);
984*6a54128fSAndroid Build Coastguard Worker 	if (retval)
985*6a54128fSAndroid Build Coastguard Worker 		goto out;
986*6a54128fSAndroid Build Coastguard Worker 
987*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(length, &inode);
988*6a54128fSAndroid Build Coastguard Worker 	if (retval)
989*6a54128fSAndroid Build Coastguard Worker 		goto out;
990*6a54128fSAndroid Build Coastguard Worker 
991*6a54128fSAndroid Build Coastguard Worker 	do {
992*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_get_next_inode_full(scan, &ino, inode, length);
993*6a54128fSAndroid Build Coastguard Worker 		if (retval)
994*6a54128fSAndroid Build Coastguard Worker 			goto out;
995*6a54128fSAndroid Build Coastguard Worker 		if (!ino)
996*6a54128fSAndroid Build Coastguard Worker 			break;
997*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_test_inode_bitmap2(fs->inode_map, ino)) {
998*6a54128fSAndroid Build Coastguard Worker 			memset(inode, 0, length);
999*6a54128fSAndroid Build Coastguard Worker 			retval = ext2fs_write_inode_full(fs, ino, inode,
1000*6a54128fSAndroid Build Coastguard Worker 							 length);
1001*6a54128fSAndroid Build Coastguard Worker 			if (retval)
1002*6a54128fSAndroid Build Coastguard Worker 				goto out;
1003*6a54128fSAndroid Build Coastguard Worker 		}
1004*6a54128fSAndroid Build Coastguard Worker 	} while (1);
1005*6a54128fSAndroid Build Coastguard Worker 
1006*6a54128fSAndroid Build Coastguard Worker out:
1007*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&inode);
1008*6a54128fSAndroid Build Coastguard Worker 	ext2fs_close_inode_scan(scan);
1009*6a54128fSAndroid Build Coastguard Worker 	return retval;
1010*6a54128fSAndroid Build Coastguard Worker }
1011*6a54128fSAndroid Build Coastguard Worker 
disable_uninit_bg(ext2_filsys fs,__u32 csum_feature_flag)1012*6a54128fSAndroid Build Coastguard Worker static errcode_t disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
1013*6a54128fSAndroid Build Coastguard Worker {
1014*6a54128fSAndroid Build Coastguard Worker 	struct ext2_group_desc *gd;
1015*6a54128fSAndroid Build Coastguard Worker 	dgrp_t i;
1016*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
1017*6a54128fSAndroid Build Coastguard Worker 	blk64_t b, c, d;
1018*6a54128fSAndroid Build Coastguard Worker 
1019*6a54128fSAndroid Build Coastguard Worker 	/* Load bitmaps to ensure that the uninit ones get written out */
1020*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_feature_ro_compat |= csum_feature_flag;
1021*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_bitmaps(fs);
1022*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_feature_ro_compat &= ~csum_feature_flag;
1023*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
1024*6a54128fSAndroid Build Coastguard Worker 		com_err("disable_uninit_bg", retval,
1025*6a54128fSAndroid Build Coastguard Worker 			"while reading bitmaps");
1026*6a54128fSAndroid Build Coastguard Worker 		request_fsck_afterwards(fs);
1027*6a54128fSAndroid Build Coastguard Worker 		return retval;
1028*6a54128fSAndroid Build Coastguard Worker 	}
1029*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_ib_dirty(fs);
1030*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_bb_dirty(fs);
1031*6a54128fSAndroid Build Coastguard Worker 
1032*6a54128fSAndroid Build Coastguard Worker 	/* If we're only turning off uninit_bg, zero the inodes */
1033*6a54128fSAndroid Build Coastguard Worker 	if (csum_feature_flag == EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
1034*6a54128fSAndroid Build Coastguard Worker 		retval = zero_empty_inodes(fs);
1035*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1036*6a54128fSAndroid Build Coastguard Worker 			com_err("disable_uninit_bg", retval,
1037*6a54128fSAndroid Build Coastguard Worker 				"while zeroing unused inodes");
1038*6a54128fSAndroid Build Coastguard Worker 			request_fsck_afterwards(fs);
1039*6a54128fSAndroid Build Coastguard Worker 			return retval;
1040*6a54128fSAndroid Build Coastguard Worker 		}
1041*6a54128fSAndroid Build Coastguard Worker 	}
1042*6a54128fSAndroid Build Coastguard Worker 
1043*6a54128fSAndroid Build Coastguard Worker 	/* The bbitmap is zeroed; we must mark group metadata blocks in use */
1044*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++) {
1045*6a54128fSAndroid Build Coastguard Worker 		b = ext2fs_block_bitmap_loc(fs, i);
1046*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(fs->block_map, b);
1047*6a54128fSAndroid Build Coastguard Worker 		b = ext2fs_inode_bitmap_loc(fs, i);
1048*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(fs->block_map, b);
1049*6a54128fSAndroid Build Coastguard Worker 
1050*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_super_and_bgd_loc2(fs, i, &b, &c, &d, NULL);
1051*6a54128fSAndroid Build Coastguard Worker 		if (retval == 0 && b)
1052*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_block_bitmap2(fs->block_map, b);
1053*6a54128fSAndroid Build Coastguard Worker 		if (retval == 0 && c)
1054*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_block_bitmap2(fs->block_map, c);
1055*6a54128fSAndroid Build Coastguard Worker 		if (retval == 0 && d)
1056*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_block_bitmap2(fs->block_map, d);
1057*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1058*6a54128fSAndroid Build Coastguard Worker 			com_err("disable_uninit_bg", retval,
1059*6a54128fSAndroid Build Coastguard Worker 				"while initializing block bitmaps");
1060*6a54128fSAndroid Build Coastguard Worker 			request_fsck_afterwards(fs);
1061*6a54128fSAndroid Build Coastguard Worker 		}
1062*6a54128fSAndroid Build Coastguard Worker 
1063*6a54128fSAndroid Build Coastguard Worker 		gd = ext2fs_group_desc(fs, fs->group_desc, i);
1064*6a54128fSAndroid Build Coastguard Worker 		gd->bg_itable_unused = 0;
1065*6a54128fSAndroid Build Coastguard Worker 		gd->bg_flags = 0;
1066*6a54128fSAndroid Build Coastguard Worker 		ext2fs_group_desc_csum_set(fs, i);
1067*6a54128fSAndroid Build Coastguard Worker 	}
1068*6a54128fSAndroid Build Coastguard Worker 	fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1069*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
1070*6a54128fSAndroid Build Coastguard Worker 
1071*6a54128fSAndroid Build Coastguard Worker 	return 0;
1072*6a54128fSAndroid Build Coastguard Worker }
1073*6a54128fSAndroid Build Coastguard Worker 
1074*6a54128fSAndroid Build Coastguard Worker static void
try_confirm_csum_seed_support(void)1075*6a54128fSAndroid Build Coastguard Worker try_confirm_csum_seed_support(void)
1076*6a54128fSAndroid Build Coastguard Worker {
1077*6a54128fSAndroid Build Coastguard Worker 	if (access("/sys/fs/ext4/features/metadata_csum_seed", R_OK))
1078*6a54128fSAndroid Build Coastguard Worker 		fputs(_("WARNING: Could not confirm kernel support for "
1079*6a54128fSAndroid Build Coastguard Worker 			"metadata_csum_seed.\n  This requires Linux >= "
1080*6a54128fSAndroid Build Coastguard Worker 			"v4.4.\n"), stderr);
1081*6a54128fSAndroid Build Coastguard Worker }
1082*6a54128fSAndroid Build Coastguard Worker 
1083*6a54128fSAndroid Build Coastguard Worker /*
1084*6a54128fSAndroid Build Coastguard Worker  * Update the feature set as provided by the user.
1085*6a54128fSAndroid Build Coastguard Worker  */
update_feature_set(ext2_filsys fs,char * features)1086*6a54128fSAndroid Build Coastguard Worker static int update_feature_set(ext2_filsys fs, char *features)
1087*6a54128fSAndroid Build Coastguard Worker {
1088*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb = fs->super;
1089*6a54128fSAndroid Build Coastguard Worker 	__u32		old_features[3];
1090*6a54128fSAndroid Build Coastguard Worker 	int		type_err;
1091*6a54128fSAndroid Build Coastguard Worker 	unsigned int	mask_err;
1092*6a54128fSAndroid Build Coastguard Worker 	errcode_t	err;
1093*6a54128fSAndroid Build Coastguard Worker 	enum quota_type qtype;
1094*6a54128fSAndroid Build Coastguard Worker 
1095*6a54128fSAndroid Build Coastguard Worker #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
1096*6a54128fSAndroid Build Coastguard Worker 				((&sb->s_feature_compat)[(type)] & (mask)))
1097*6a54128fSAndroid Build Coastguard Worker #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
1098*6a54128fSAndroid Build Coastguard Worker 				 !((&sb->s_feature_compat)[(type)] & (mask)))
1099*6a54128fSAndroid Build Coastguard Worker #define FEATURE_CHANGED(type, mask) ((mask) & \
1100*6a54128fSAndroid Build Coastguard Worker 		     (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
1101*6a54128fSAndroid Build Coastguard Worker 
1102*6a54128fSAndroid Build Coastguard Worker 	old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
1103*6a54128fSAndroid Build Coastguard Worker 	old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
1104*6a54128fSAndroid Build Coastguard Worker 	old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
1105*6a54128fSAndroid Build Coastguard Worker 
1106*6a54128fSAndroid Build Coastguard Worker 	if (e2p_edit_feature2(features, &sb->s_feature_compat,
1107*6a54128fSAndroid Build Coastguard Worker 			      ok_features, clear_ok_features,
1108*6a54128fSAndroid Build Coastguard Worker 			      &type_err, &mask_err)) {
1109*6a54128fSAndroid Build Coastguard Worker 		if (!mask_err)
1110*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr,
1111*6a54128fSAndroid Build Coastguard Worker 				_("Invalid filesystem option set: %s\n"),
1112*6a54128fSAndroid Build Coastguard Worker 				features);
1113*6a54128fSAndroid Build Coastguard Worker 		else if (type_err & E2P_FEATURE_NEGATE_FLAG)
1114*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("Clearing filesystem feature '%s' "
1115*6a54128fSAndroid Build Coastguard Worker 					  "not supported.\n"),
1116*6a54128fSAndroid Build Coastguard Worker 				e2p_feature2string(type_err &
1117*6a54128fSAndroid Build Coastguard Worker 						   E2P_FEATURE_TYPE_MASK,
1118*6a54128fSAndroid Build Coastguard Worker 						   mask_err));
1119*6a54128fSAndroid Build Coastguard Worker 		else
1120*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("Setting filesystem feature '%s' "
1121*6a54128fSAndroid Build Coastguard Worker 					  "not supported.\n"),
1122*6a54128fSAndroid Build Coastguard Worker 				e2p_feature2string(type_err, mask_err));
1123*6a54128fSAndroid Build Coastguard Worker 		return 1;
1124*6a54128fSAndroid Build Coastguard Worker 	}
1125*6a54128fSAndroid Build Coastguard Worker 
1126*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1127*6a54128fSAndroid Build Coastguard Worker 		if ((mount_flags & EXT2_MF_MOUNTED) &&
1128*6a54128fSAndroid Build Coastguard Worker 		    !(mount_flags & EXT2_MF_READONLY)) {
1129*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The has_journal feature may only be "
1130*6a54128fSAndroid Build Coastguard Worker 				"cleared when the filesystem is\n"
1131*6a54128fSAndroid Build Coastguard Worker 				"unmounted or mounted "
1132*6a54128fSAndroid Build Coastguard Worker 				"read-only.\n"), stderr);
1133*6a54128fSAndroid Build Coastguard Worker 			return 1;
1134*6a54128fSAndroid Build Coastguard Worker 		}
1135*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_journal_needs_recovery(sb) &&
1136*6a54128fSAndroid Build Coastguard Worker 		    f_flag < 2) {
1137*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The needs_recovery flag is set.  "
1138*6a54128fSAndroid Build Coastguard Worker 				"Please run e2fsck before clearing\n"
1139*6a54128fSAndroid Build Coastguard Worker 				"the has_journal flag.\n"), stderr);
1140*6a54128fSAndroid Build Coastguard Worker 			return 1;
1141*6a54128fSAndroid Build Coastguard Worker 		}
1142*6a54128fSAndroid Build Coastguard Worker 		if (sb->s_journal_inum) {
1143*6a54128fSAndroid Build Coastguard Worker 			if (remove_journal_inode(fs))
1144*6a54128fSAndroid Build Coastguard Worker 				return 1;
1145*6a54128fSAndroid Build Coastguard Worker 		}
1146*6a54128fSAndroid Build Coastguard Worker 		if (sb->s_journal_dev) {
1147*6a54128fSAndroid Build Coastguard Worker 			if (remove_journal_device(fs))
1148*6a54128fSAndroid Build Coastguard Worker 				return 1;
1149*6a54128fSAndroid Build Coastguard Worker 		}
1150*6a54128fSAndroid Build Coastguard Worker 	}
1151*6a54128fSAndroid Build Coastguard Worker 
1152*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1153*6a54128fSAndroid Build Coastguard Worker 		EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1154*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_meta_bg(sb)) {
1155*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Setting filesystem feature 'sparse_super' "
1156*6a54128fSAndroid Build Coastguard Worker 				"not supported\nfor filesystems with "
1157*6a54128fSAndroid Build Coastguard Worker 				"the meta_bg feature enabled.\n"),
1158*6a54128fSAndroid Build Coastguard Worker 				stderr);
1159*6a54128fSAndroid Build Coastguard Worker 			return 1;
1160*6a54128fSAndroid Build Coastguard Worker 		}
1161*6a54128fSAndroid Build Coastguard Worker 	}
1162*6a54128fSAndroid Build Coastguard Worker 
1163*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1164*6a54128fSAndroid Build Coastguard Worker 		int error;
1165*6a54128fSAndroid Build Coastguard Worker 
1166*6a54128fSAndroid Build Coastguard Worker 		if ((mount_flags & EXT2_MF_MOUNTED) ||
1167*6a54128fSAndroid Build Coastguard Worker 		    (mount_flags & EXT2_MF_READONLY)) {
1168*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The multiple mount protection feature can't\n"
1169*6a54128fSAndroid Build Coastguard Worker 				"be set if the filesystem is mounted or\n"
1170*6a54128fSAndroid Build Coastguard Worker 				"read-only.\n"), stderr);
1171*6a54128fSAndroid Build Coastguard Worker 			return 1;
1172*6a54128fSAndroid Build Coastguard Worker 		}
1173*6a54128fSAndroid Build Coastguard Worker 
1174*6a54128fSAndroid Build Coastguard Worker 		error = ext2fs_mmp_init(fs);
1175*6a54128fSAndroid Build Coastguard Worker 		if (error) {
1176*6a54128fSAndroid Build Coastguard Worker 			fputs(_("\nError while enabling multiple mount "
1177*6a54128fSAndroid Build Coastguard Worker 				"protection feature."), stderr);
1178*6a54128fSAndroid Build Coastguard Worker 			return 1;
1179*6a54128fSAndroid Build Coastguard Worker 		}
1180*6a54128fSAndroid Build Coastguard Worker 
1181*6a54128fSAndroid Build Coastguard Worker 		/*
1182*6a54128fSAndroid Build Coastguard Worker 		 * We want to update group desc with the new free blocks count
1183*6a54128fSAndroid Build Coastguard Worker 		 */
1184*6a54128fSAndroid Build Coastguard Worker 		fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1185*6a54128fSAndroid Build Coastguard Worker 
1186*6a54128fSAndroid Build Coastguard Worker 		printf(_("Multiple mount protection has been enabled "
1187*6a54128fSAndroid Build Coastguard Worker 			 "with update interval %ds.\n"),
1188*6a54128fSAndroid Build Coastguard Worker 		       sb->s_mmp_update_interval);
1189*6a54128fSAndroid Build Coastguard Worker 	}
1190*6a54128fSAndroid Build Coastguard Worker 
1191*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
1192*6a54128fSAndroid Build Coastguard Worker 		int error;
1193*6a54128fSAndroid Build Coastguard Worker 
1194*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_READONLY) {
1195*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The multiple mount protection feature cannot\n"
1196*6a54128fSAndroid Build Coastguard Worker 				"be disabled if the filesystem is readonly.\n"),
1197*6a54128fSAndroid Build Coastguard Worker 				stderr);
1198*6a54128fSAndroid Build Coastguard Worker 			return 1;
1199*6a54128fSAndroid Build Coastguard Worker 		}
1200*6a54128fSAndroid Build Coastguard Worker 
1201*6a54128fSAndroid Build Coastguard Worker 		error = ext2fs_read_bitmaps(fs);
1202*6a54128fSAndroid Build Coastguard Worker 		if (error) {
1203*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Error while reading bitmaps\n"), stderr);
1204*6a54128fSAndroid Build Coastguard Worker 			return 1;
1205*6a54128fSAndroid Build Coastguard Worker 		}
1206*6a54128fSAndroid Build Coastguard Worker 
1207*6a54128fSAndroid Build Coastguard Worker 		error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
1208*6a54128fSAndroid Build Coastguard Worker 		if (error) {
1209*6a54128fSAndroid Build Coastguard Worker 			struct mmp_struct *mmp_cmp = fs->mmp_cmp;
1210*6a54128fSAndroid Build Coastguard Worker 
1211*6a54128fSAndroid Build Coastguard Worker 			if (error == EXT2_ET_MMP_MAGIC_INVALID)
1212*6a54128fSAndroid Build Coastguard Worker 				printf(_("Magic number in MMP block does not "
1213*6a54128fSAndroid Build Coastguard Worker 					 "match. expected: %x, actual: %x\n"),
1214*6a54128fSAndroid Build Coastguard Worker 					 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
1215*6a54128fSAndroid Build Coastguard Worker 			else
1216*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, error, "%s",
1217*6a54128fSAndroid Build Coastguard Worker 					_("while reading MMP block."));
1218*6a54128fSAndroid Build Coastguard Worker 			goto mmp_error;
1219*6a54128fSAndroid Build Coastguard Worker 		}
1220*6a54128fSAndroid Build Coastguard Worker 
1221*6a54128fSAndroid Build Coastguard Worker 		/* We need to force out the group descriptors as well */
1222*6a54128fSAndroid Build Coastguard Worker 		fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1223*6a54128fSAndroid Build Coastguard Worker 		ext2fs_block_alloc_stats2(fs, sb->s_mmp_block, -1);
1224*6a54128fSAndroid Build Coastguard Worker mmp_error:
1225*6a54128fSAndroid Build Coastguard Worker 		sb->s_mmp_block = 0;
1226*6a54128fSAndroid Build Coastguard Worker 		sb->s_mmp_update_interval = 0;
1227*6a54128fSAndroid Build Coastguard Worker 	}
1228*6a54128fSAndroid Build Coastguard Worker 
1229*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1230*6a54128fSAndroid Build Coastguard Worker 		/*
1231*6a54128fSAndroid Build Coastguard Worker 		 * If adding a journal flag, let the create journal
1232*6a54128fSAndroid Build Coastguard Worker 		 * code below handle setting the flag and creating the
1233*6a54128fSAndroid Build Coastguard Worker 		 * journal.  We supply a default size if necessary.
1234*6a54128fSAndroid Build Coastguard Worker 		 */
1235*6a54128fSAndroid Build Coastguard Worker 		if (!journal_size)
1236*6a54128fSAndroid Build Coastguard Worker 			journal_size = -1;
1237*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_journal(sb);
1238*6a54128fSAndroid Build Coastguard Worker 	}
1239*6a54128fSAndroid Build Coastguard Worker 
1240*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
1241*6a54128fSAndroid Build Coastguard Worker 		if (!sb->s_def_hash_version)
1242*6a54128fSAndroid Build Coastguard Worker 			sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
1243*6a54128fSAndroid Build Coastguard Worker 		if (uuid_is_null((unsigned char *) sb->s_hash_seed))
1244*6a54128fSAndroid Build Coastguard Worker 			uuid_generate((unsigned char *) sb->s_hash_seed);
1245*6a54128fSAndroid Build Coastguard Worker 	}
1246*6a54128fSAndroid Build Coastguard Worker 
1247*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1248*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_has_feature_metadata_csum(sb)) {
1249*6a54128fSAndroid Build Coastguard Worker 		if (check_fsck_needed(fs,
1250*6a54128fSAndroid Build Coastguard Worker 			_("Disabling directory index on filesystem with "
1251*6a54128fSAndroid Build Coastguard Worker 			  "checksums could take some time.")))
1252*6a54128fSAndroid Build Coastguard Worker 			return 1;
1253*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1254*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Cannot disable dir_index on a mounted "
1255*6a54128fSAndroid Build Coastguard Worker 				"filesystem!\n"), stderr);
1256*6a54128fSAndroid Build Coastguard Worker 			exit(1);
1257*6a54128fSAndroid Build Coastguard Worker 		}
1258*6a54128fSAndroid Build Coastguard Worker 		/*
1259*6a54128fSAndroid Build Coastguard Worker 		 * Clearing dir_index on checksummed filesystem requires
1260*6a54128fSAndroid Build Coastguard Worker 		 * rewriting all directories to update checksums.
1261*6a54128fSAndroid Build Coastguard Worker 		 */
1262*6a54128fSAndroid Build Coastguard Worker 		rewrite_checksums |= REWRITE_DIR_FL;
1263*6a54128fSAndroid Build Coastguard Worker 	}
1264*6a54128fSAndroid Build Coastguard Worker 
1265*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1266*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_check_desc(fs)) {
1267*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Clearing the flex_bg flag would "
1268*6a54128fSAndroid Build Coastguard Worker 				"cause the the filesystem to be\n"
1269*6a54128fSAndroid Build Coastguard Worker 				"inconsistent.\n"), stderr);
1270*6a54128fSAndroid Build Coastguard Worker 			return 1;
1271*6a54128fSAndroid Build Coastguard Worker 		}
1272*6a54128fSAndroid Build Coastguard Worker 	}
1273*6a54128fSAndroid Build Coastguard Worker 
1274*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1275*6a54128fSAndroid Build Coastguard Worker 			    EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
1276*6a54128fSAndroid Build Coastguard Worker 		if ((mount_flags & EXT2_MF_MOUNTED) &&
1277*6a54128fSAndroid Build Coastguard Worker 		    !(mount_flags & EXT2_MF_READONLY)) {
1278*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The huge_file feature may only be "
1279*6a54128fSAndroid Build Coastguard Worker 				"cleared when the filesystem is\n"
1280*6a54128fSAndroid Build Coastguard Worker 				"unmounted or mounted "
1281*6a54128fSAndroid Build Coastguard Worker 				"read-only.\n"), stderr);
1282*6a54128fSAndroid Build Coastguard Worker 			return 1;
1283*6a54128fSAndroid Build Coastguard Worker 		}
1284*6a54128fSAndroid Build Coastguard Worker 	}
1285*6a54128fSAndroid Build Coastguard Worker 
1286*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1287*6a54128fSAndroid Build Coastguard Worker 		       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1288*6a54128fSAndroid Build Coastguard Worker 		if (check_fsck_needed(fs,
1289*6a54128fSAndroid Build Coastguard Worker 			_("Enabling checksums could take some time.")))
1290*6a54128fSAndroid Build Coastguard Worker 			return 1;
1291*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1292*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Cannot enable metadata_csum on a mounted "
1293*6a54128fSAndroid Build Coastguard Worker 				"filesystem!\n"), stderr);
1294*6a54128fSAndroid Build Coastguard Worker 			return 1;
1295*6a54128fSAndroid Build Coastguard Worker 		}
1296*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_extents(fs->super))
1297*6a54128fSAndroid Build Coastguard Worker 			printf("%s",
1298*6a54128fSAndroid Build Coastguard Worker 			       _("Extents are not enabled.  The file extent "
1299*6a54128fSAndroid Build Coastguard Worker 				 "tree can be checksummed, whereas block maps "
1300*6a54128fSAndroid Build Coastguard Worker 				 "cannot.  Not enabling extents reduces the "
1301*6a54128fSAndroid Build Coastguard Worker 				 "coverage of metadata checksumming.  "
1302*6a54128fSAndroid Build Coastguard Worker 				 "Re-run with -O extent to rectify.\n"));
1303*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_64bit(fs->super))
1304*6a54128fSAndroid Build Coastguard Worker 			printf("%s",
1305*6a54128fSAndroid Build Coastguard Worker 			       _("64-bit filesystem support is not enabled.  "
1306*6a54128fSAndroid Build Coastguard Worker 				 "The larger fields afforded by this feature "
1307*6a54128fSAndroid Build Coastguard Worker 				 "enable full-strength checksumming.  "
1308*6a54128fSAndroid Build Coastguard Worker 				 "Run resize2fs -b to rectify.\n"));
1309*6a54128fSAndroid Build Coastguard Worker 		rewrite_checksums = REWRITE_ALL;
1310*6a54128fSAndroid Build Coastguard Worker 		/* metadata_csum supersedes uninit_bg */
1311*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_gdt_csum(fs->super);
1312*6a54128fSAndroid Build Coastguard Worker 
1313*6a54128fSAndroid Build Coastguard Worker 		/* if uninit_bg was previously off, rewrite group desc */
1314*6a54128fSAndroid Build Coastguard Worker 		if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
1315*6a54128fSAndroid Build Coastguard Worker 		      EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
1316*6a54128fSAndroid Build Coastguard Worker 			enable_uninit_bg(fs);
1317*6a54128fSAndroid Build Coastguard Worker 
1318*6a54128fSAndroid Build Coastguard Worker 		/*
1319*6a54128fSAndroid Build Coastguard Worker 		 * Since metadata_csum supersedes uninit_bg, pretend like
1320*6a54128fSAndroid Build Coastguard Worker 		 * uninit_bg has been off all along.
1321*6a54128fSAndroid Build Coastguard Worker 		 */
1322*6a54128fSAndroid Build Coastguard Worker 		old_features[E2P_FEATURE_RO_INCOMPAT] &=
1323*6a54128fSAndroid Build Coastguard Worker 			~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1324*6a54128fSAndroid Build Coastguard Worker 	}
1325*6a54128fSAndroid Build Coastguard Worker 
1326*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1327*6a54128fSAndroid Build Coastguard Worker 			EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
1328*6a54128fSAndroid Build Coastguard Worker 		__u32	test_features[3];
1329*6a54128fSAndroid Build Coastguard Worker 
1330*6a54128fSAndroid Build Coastguard Worker 		if (check_fsck_needed(fs,
1331*6a54128fSAndroid Build Coastguard Worker 			_("Disabling checksums could take some time.")))
1332*6a54128fSAndroid Build Coastguard Worker 			return 1;
1333*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1334*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Cannot disable metadata_csum on a mounted "
1335*6a54128fSAndroid Build Coastguard Worker 				"filesystem!\n"), stderr);
1336*6a54128fSAndroid Build Coastguard Worker 			return 1;
1337*6a54128fSAndroid Build Coastguard Worker 		}
1338*6a54128fSAndroid Build Coastguard Worker 		rewrite_checksums = REWRITE_ALL;
1339*6a54128fSAndroid Build Coastguard Worker 
1340*6a54128fSAndroid Build Coastguard Worker 		/* Enable uninit_bg unless the user expressly turned it off */
1341*6a54128fSAndroid Build Coastguard Worker 		memcpy(test_features, old_features, sizeof(test_features));
1342*6a54128fSAndroid Build Coastguard Worker 		test_features[E2P_FEATURE_RO_INCOMPAT] |=
1343*6a54128fSAndroid Build Coastguard Worker 						EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1344*6a54128fSAndroid Build Coastguard Worker 		e2p_edit_feature2(features, test_features, ok_features,
1345*6a54128fSAndroid Build Coastguard Worker 				  clear_ok_features, NULL, NULL);
1346*6a54128fSAndroid Build Coastguard Worker 		if (test_features[E2P_FEATURE_RO_INCOMPAT] &
1347*6a54128fSAndroid Build Coastguard Worker 						EXT4_FEATURE_RO_COMPAT_GDT_CSUM)
1348*6a54128fSAndroid Build Coastguard Worker 			ext2fs_set_feature_gdt_csum(fs->super);
1349*6a54128fSAndroid Build Coastguard Worker 
1350*6a54128fSAndroid Build Coastguard Worker 		/*
1351*6a54128fSAndroid Build Coastguard Worker 		 * If we're turning off metadata_csum and not turning on
1352*6a54128fSAndroid Build Coastguard Worker 		 * uninit_bg, rewrite group desc.
1353*6a54128fSAndroid Build Coastguard Worker 		 */
1354*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_gdt_csum(fs->super)) {
1355*6a54128fSAndroid Build Coastguard Worker 			err = disable_uninit_bg(fs,
1356*6a54128fSAndroid Build Coastguard Worker 					EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
1357*6a54128fSAndroid Build Coastguard Worker 			if (err)
1358*6a54128fSAndroid Build Coastguard Worker 				return 1;
1359*6a54128fSAndroid Build Coastguard Worker 		} else
1360*6a54128fSAndroid Build Coastguard Worker 			/*
1361*6a54128fSAndroid Build Coastguard Worker 			 * metadata_csum previously provided uninit_bg, so if
1362*6a54128fSAndroid Build Coastguard Worker 			 * we're also setting the uninit_bg feature bit,
1363*6a54128fSAndroid Build Coastguard Worker 			 * pretend like it was previously enabled.  Checksums
1364*6a54128fSAndroid Build Coastguard Worker 			 * will be rewritten with crc16 later.
1365*6a54128fSAndroid Build Coastguard Worker 			 */
1366*6a54128fSAndroid Build Coastguard Worker 			old_features[E2P_FEATURE_RO_INCOMPAT] |=
1367*6a54128fSAndroid Build Coastguard Worker 				EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
1368*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_checksum_seed = 0;
1369*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_csum_seed(fs->super);
1370*6a54128fSAndroid Build Coastguard Worker 	}
1371*6a54128fSAndroid Build Coastguard Worker 
1372*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1373*6a54128fSAndroid Build Coastguard Worker 		       EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1374*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1375*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Cannot enable uninit_bg on a mounted "
1376*6a54128fSAndroid Build Coastguard Worker 				"filesystem!\n"), stderr);
1377*6a54128fSAndroid Build Coastguard Worker 			return 1;
1378*6a54128fSAndroid Build Coastguard Worker 		}
1379*6a54128fSAndroid Build Coastguard Worker 
1380*6a54128fSAndroid Build Coastguard Worker 		/* Do not enable uninit_bg when metadata_csum enabled */
1381*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_metadata_csum(fs->super))
1382*6a54128fSAndroid Build Coastguard Worker 			ext2fs_clear_feature_gdt_csum(fs->super);
1383*6a54128fSAndroid Build Coastguard Worker 		else
1384*6a54128fSAndroid Build Coastguard Worker 			enable_uninit_bg(fs);
1385*6a54128fSAndroid Build Coastguard Worker 	}
1386*6a54128fSAndroid Build Coastguard Worker 
1387*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1388*6a54128fSAndroid Build Coastguard Worker 			EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1389*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1390*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Cannot disable uninit_bg on a mounted "
1391*6a54128fSAndroid Build Coastguard Worker 				"filesystem!\n"), stderr);
1392*6a54128fSAndroid Build Coastguard Worker 			return 1;
1393*6a54128fSAndroid Build Coastguard Worker 		}
1394*6a54128fSAndroid Build Coastguard Worker 
1395*6a54128fSAndroid Build Coastguard Worker 		err = disable_uninit_bg(fs,
1396*6a54128fSAndroid Build Coastguard Worker 				EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1397*6a54128fSAndroid Build Coastguard Worker 		if (err)
1398*6a54128fSAndroid Build Coastguard Worker 			return 1;
1399*6a54128fSAndroid Build Coastguard Worker 	}
1400*6a54128fSAndroid Build Coastguard Worker 
1401*6a54128fSAndroid Build Coastguard Worker 	/*
1402*6a54128fSAndroid Build Coastguard Worker 	 * We don't actually toggle 64bit; resize2fs does that.  But this
1403*6a54128fSAndroid Build Coastguard Worker 	 * must come after the metadata_csum feature_on so that it won't
1404*6a54128fSAndroid Build Coastguard Worker 	 * complain about the lack of 64bit.
1405*6a54128fSAndroid Build Coastguard Worker 	 */
1406*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1407*6a54128fSAndroid Build Coastguard Worker 		       EXT4_FEATURE_INCOMPAT_64BIT)) {
1408*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1409*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("Cannot enable 64-bit mode "
1410*6a54128fSAndroid Build Coastguard Worker 					  "while mounted!\n"));
1411*6a54128fSAndroid Build Coastguard Worker 			return 1;
1412*6a54128fSAndroid Build Coastguard Worker 		}
1413*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_64bit(sb);
1414*6a54128fSAndroid Build Coastguard Worker 		feature_64bit = 1;
1415*6a54128fSAndroid Build Coastguard Worker 	}
1416*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1417*6a54128fSAndroid Build Coastguard Worker 			EXT4_FEATURE_INCOMPAT_64BIT)) {
1418*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1419*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("Cannot disable 64-bit mode "
1420*6a54128fSAndroid Build Coastguard Worker 					  "while mounted!\n"));
1421*6a54128fSAndroid Build Coastguard Worker 			return 1;
1422*6a54128fSAndroid Build Coastguard Worker 		}
1423*6a54128fSAndroid Build Coastguard Worker 		ext2fs_set_feature_64bit(sb);
1424*6a54128fSAndroid Build Coastguard Worker 		feature_64bit = -1;
1425*6a54128fSAndroid Build Coastguard Worker 	}
1426*6a54128fSAndroid Build Coastguard Worker 
1427*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1428*6a54128fSAndroid Build Coastguard Worker 				EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1429*6a54128fSAndroid Build Coastguard Worker 		/*
1430*6a54128fSAndroid Build Coastguard Worker 		 * Set the Q_flag here and handle the quota options in the code
1431*6a54128fSAndroid Build Coastguard Worker 		 * below.
1432*6a54128fSAndroid Build Coastguard Worker 		 */
1433*6a54128fSAndroid Build Coastguard Worker 		if (!Q_flag) {
1434*6a54128fSAndroid Build Coastguard Worker 			Q_flag = 1;
1435*6a54128fSAndroid Build Coastguard Worker 			/* Enable usr/grp quota by default */
1436*6a54128fSAndroid Build Coastguard Worker 			for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
1437*6a54128fSAndroid Build Coastguard Worker 				if (qtype != PRJQUOTA)
1438*6a54128fSAndroid Build Coastguard Worker 					quota_enable[qtype] = QOPT_ENABLE;
1439*6a54128fSAndroid Build Coastguard Worker 				else
1440*6a54128fSAndroid Build Coastguard Worker 					quota_enable[qtype] = QOPT_DISABLE;
1441*6a54128fSAndroid Build Coastguard Worker 			}
1442*6a54128fSAndroid Build Coastguard Worker 		}
1443*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_quota(sb);
1444*6a54128fSAndroid Build Coastguard Worker 	}
1445*6a54128fSAndroid Build Coastguard Worker 
1446*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
1447*6a54128fSAndroid Build Coastguard Worker 		       EXT4_FEATURE_RO_COMPAT_PROJECT)) {
1448*6a54128fSAndroid Build Coastguard Worker 		if (fs->super->s_inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
1449*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("Cannot enable project feature; "
1450*6a54128fSAndroid Build Coastguard Worker 					  "inode size too small.\n"));
1451*6a54128fSAndroid Build Coastguard Worker 			return 1;
1452*6a54128fSAndroid Build Coastguard Worker 		}
1453*6a54128fSAndroid Build Coastguard Worker 		Q_flag = 1;
1454*6a54128fSAndroid Build Coastguard Worker 		quota_enable[PRJQUOTA] = QOPT_ENABLE;
1455*6a54128fSAndroid Build Coastguard Worker 	}
1456*6a54128fSAndroid Build Coastguard Worker 
1457*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1458*6a54128fSAndroid Build Coastguard Worker 			EXT4_FEATURE_RO_COMPAT_PROJECT)) {
1459*6a54128fSAndroid Build Coastguard Worker 		Q_flag = 1;
1460*6a54128fSAndroid Build Coastguard Worker 		quota_enable[PRJQUOTA] = QOPT_DISABLE;
1461*6a54128fSAndroid Build Coastguard Worker 	}
1462*6a54128fSAndroid Build Coastguard Worker 
1463*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1464*6a54128fSAndroid Build Coastguard Worker 				EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1465*6a54128fSAndroid Build Coastguard Worker 		/*
1466*6a54128fSAndroid Build Coastguard Worker 		 * Set the Q_flag here and handle the quota options in the code
1467*6a54128fSAndroid Build Coastguard Worker 		 * below.
1468*6a54128fSAndroid Build Coastguard Worker 		 */
1469*6a54128fSAndroid Build Coastguard Worker 		if (Q_flag)
1470*6a54128fSAndroid Build Coastguard Worker 			fputs(_("\nWarning: '^quota' option overrides '-Q'"
1471*6a54128fSAndroid Build Coastguard Worker 				"arguments.\n"), stderr);
1472*6a54128fSAndroid Build Coastguard Worker 		Q_flag = 1;
1473*6a54128fSAndroid Build Coastguard Worker 		/* Disable all quota by default */
1474*6a54128fSAndroid Build Coastguard Worker 		for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1475*6a54128fSAndroid Build Coastguard Worker 			quota_enable[qtype] = QOPT_DISABLE;
1476*6a54128fSAndroid Build Coastguard Worker 	}
1477*6a54128fSAndroid Build Coastguard Worker 
1478*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_ENCRYPT)) {
1479*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_encrypt_algos[0] =
1480*6a54128fSAndroid Build Coastguard Worker 			EXT4_ENCRYPTION_MODE_AES_256_XTS;
1481*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_encrypt_algos[1] =
1482*6a54128fSAndroid Build Coastguard Worker 			EXT4_ENCRYPTION_MODE_AES_256_CTS;
1483*6a54128fSAndroid Build Coastguard Worker 	}
1484*6a54128fSAndroid Build Coastguard Worker 
1485*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_CASEFOLD)) {
1486*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
1487*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The casefold feature may only be enabled when "
1488*6a54128fSAndroid Build Coastguard Worker 				"the filesystem is unmounted.\n"), stderr);
1489*6a54128fSAndroid Build Coastguard Worker 			return 1;
1490*6a54128fSAndroid Build Coastguard Worker 		}
1491*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_encoding = EXT4_ENC_UTF8_12_1;
1492*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_encoding_flags = e2p_get_encoding_flags(EXT4_ENC_UTF8_12_1);
1493*6a54128fSAndroid Build Coastguard Worker 		enabling_casefold = 1;
1494*6a54128fSAndroid Build Coastguard Worker 	}
1495*6a54128fSAndroid Build Coastguard Worker 
1496*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_ON(E2P_FEATURE_INCOMPAT,
1497*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_CSUM_SEED)) {
1498*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_metadata_csum(sb)) {
1499*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Setting feature 'metadata_csum_seed' "
1500*6a54128fSAndroid Build Coastguard Worker 				"is only supported\non filesystems with "
1501*6a54128fSAndroid Build Coastguard Worker 				"the metadata_csum feature enabled.\n"),
1502*6a54128fSAndroid Build Coastguard Worker 				stderr);
1503*6a54128fSAndroid Build Coastguard Worker 			return 1;
1504*6a54128fSAndroid Build Coastguard Worker 		}
1505*6a54128fSAndroid Build Coastguard Worker 		try_confirm_csum_seed_support();
1506*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_checksum_seed = fs->csum_seed;
1507*6a54128fSAndroid Build Coastguard Worker 	}
1508*6a54128fSAndroid Build Coastguard Worker 
1509*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_OFF(E2P_FEATURE_INCOMPAT,
1510*6a54128fSAndroid Build Coastguard Worker 		EXT4_FEATURE_INCOMPAT_CSUM_SEED)) {
1511*6a54128fSAndroid Build Coastguard Worker 		__le32 uuid_seed;
1512*6a54128fSAndroid Build Coastguard Worker 
1513*6a54128fSAndroid Build Coastguard Worker 		uuid_seed = ext2fs_crc32c_le(~0, fs->super->s_uuid,
1514*6a54128fSAndroid Build Coastguard Worker 					sizeof(fs->super->s_uuid));
1515*6a54128fSAndroid Build Coastguard Worker 		if (fs->super->s_checksum_seed != uuid_seed) {
1516*6a54128fSAndroid Build Coastguard Worker 			if (mount_flags & (EXT2_MF_BUSY|EXT2_MF_MOUNTED)) {
1517*6a54128fSAndroid Build Coastguard Worker 				fputs(_("UUID has changed since enabling "
1518*6a54128fSAndroid Build Coastguard Worker 		"metadata_csum.  Filesystem must be unmounted "
1519*6a54128fSAndroid Build Coastguard Worker 		"\nto safely rewrite all metadata to match the new UUID.\n"),
1520*6a54128fSAndroid Build Coastguard Worker 				      stderr);
1521*6a54128fSAndroid Build Coastguard Worker 				return 1;
1522*6a54128fSAndroid Build Coastguard Worker 			}
1523*6a54128fSAndroid Build Coastguard Worker 			if (check_fsck_needed(fs, _("Recalculating checksums "
1524*6a54128fSAndroid Build Coastguard Worker 						    "could take some time.")))
1525*6a54128fSAndroid Build Coastguard Worker 				return 1;
1526*6a54128fSAndroid Build Coastguard Worker 			rewrite_checksums = REWRITE_ALL;
1527*6a54128fSAndroid Build Coastguard Worker 		}
1528*6a54128fSAndroid Build Coastguard Worker 	}
1529*6a54128fSAndroid Build Coastguard Worker 
1530*6a54128fSAndroid Build Coastguard Worker 	if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1531*6a54128fSAndroid Build Coastguard Worker 	    (sb->s_feature_compat || sb->s_feature_ro_compat ||
1532*6a54128fSAndroid Build Coastguard Worker 	     sb->s_feature_incompat))
1533*6a54128fSAndroid Build Coastguard Worker 		ext2fs_update_dynamic_rev(fs);
1534*6a54128fSAndroid Build Coastguard Worker 
1535*6a54128fSAndroid Build Coastguard Worker 	if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1536*6a54128fSAndroid Build Coastguard Worker 			    EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1537*6a54128fSAndroid Build Coastguard Worker 	    FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1538*6a54128fSAndroid Build Coastguard Worker 			EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1539*6a54128fSAndroid Build Coastguard Worker 	    FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1540*6a54128fSAndroid Build Coastguard Worker 			    EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1541*6a54128fSAndroid Build Coastguard Worker 	    FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1542*6a54128fSAndroid Build Coastguard Worker 			    EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1543*6a54128fSAndroid Build Coastguard Worker 	    FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1544*6a54128fSAndroid Build Coastguard Worker 			EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1545*6a54128fSAndroid Build Coastguard Worker 		request_fsck_afterwards(fs);
1546*6a54128fSAndroid Build Coastguard Worker 
1547*6a54128fSAndroid Build Coastguard Worker 	if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1548*6a54128fSAndroid Build Coastguard Worker 	    (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1549*6a54128fSAndroid Build Coastguard Worker 	    (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1550*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
1551*6a54128fSAndroid Build Coastguard Worker 
1552*6a54128fSAndroid Build Coastguard Worker 	return 0;
1553*6a54128fSAndroid Build Coastguard Worker }
1554*6a54128fSAndroid Build Coastguard Worker 
1555*6a54128fSAndroid Build Coastguard Worker /*
1556*6a54128fSAndroid Build Coastguard Worker  * Add a journal to the filesystem.
1557*6a54128fSAndroid Build Coastguard Worker  */
add_journal(ext2_filsys fs)1558*6a54128fSAndroid Build Coastguard Worker static int add_journal(ext2_filsys fs)
1559*6a54128fSAndroid Build Coastguard Worker {
1560*6a54128fSAndroid Build Coastguard Worker 	struct ext2fs_journal_params	jparams;
1561*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
1562*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys	jfs;
1563*6a54128fSAndroid Build Coastguard Worker 	io_manager	io_ptr;
1564*6a54128fSAndroid Build Coastguard Worker 
1565*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_journal(fs->super)) {
1566*6a54128fSAndroid Build Coastguard Worker 		fputs(_("The filesystem already has a journal.\n"), stderr);
1567*6a54128fSAndroid Build Coastguard Worker 		goto err;
1568*6a54128fSAndroid Build Coastguard Worker 	}
1569*6a54128fSAndroid Build Coastguard Worker 	if (journal_device) {
1570*6a54128fSAndroid Build Coastguard Worker 		if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
1571*6a54128fSAndroid Build Coastguard Worker 					NULL))
1572*6a54128fSAndroid Build Coastguard Worker 			proceed_question(-1);
1573*6a54128fSAndroid Build Coastguard Worker 		check_mount(journal_device, 0, _("journal"));
1574*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TESTIO_DEBUG
1575*6a54128fSAndroid Build Coastguard Worker 		if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1576*6a54128fSAndroid Build Coastguard Worker 			io_ptr = test_io_manager;
1577*6a54128fSAndroid Build Coastguard Worker 			test_io_backing_manager = unix_io_manager;
1578*6a54128fSAndroid Build Coastguard Worker 		} else
1579*6a54128fSAndroid Build Coastguard Worker #endif
1580*6a54128fSAndroid Build Coastguard Worker 			io_ptr = unix_io_manager;
1581*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1582*6a54128fSAndroid Build Coastguard Worker 				     EXT2_FLAG_JOURNAL_DEV_OK, 0,
1583*6a54128fSAndroid Build Coastguard Worker 				     fs->blocksize, io_ptr, &jfs);
1584*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1585*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, retval,
1586*6a54128fSAndroid Build Coastguard Worker 				_("\n\twhile trying to open journal on %s\n"),
1587*6a54128fSAndroid Build Coastguard Worker 				journal_device);
1588*6a54128fSAndroid Build Coastguard Worker 			goto err;
1589*6a54128fSAndroid Build Coastguard Worker 		}
1590*6a54128fSAndroid Build Coastguard Worker 		printf(_("Creating journal on device %s: "),
1591*6a54128fSAndroid Build Coastguard Worker 		       journal_device);
1592*6a54128fSAndroid Build Coastguard Worker 		fflush(stdout);
1593*6a54128fSAndroid Build Coastguard Worker 
1594*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_add_journal_device(fs, jfs);
1595*6a54128fSAndroid Build Coastguard Worker 		ext2fs_close_free(&jfs);
1596*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1597*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, retval,
1598*6a54128fSAndroid Build Coastguard Worker 				_("while adding filesystem to journal on %s"),
1599*6a54128fSAndroid Build Coastguard Worker 				journal_device);
1600*6a54128fSAndroid Build Coastguard Worker 			goto err;
1601*6a54128fSAndroid Build Coastguard Worker 		}
1602*6a54128fSAndroid Build Coastguard Worker 		fputs(_("done\n"), stdout);
1603*6a54128fSAndroid Build Coastguard Worker 	} else if (journal_size) {
1604*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Creating journal inode: "), stdout);
1605*6a54128fSAndroid Build Coastguard Worker 		fflush(stdout);
1606*6a54128fSAndroid Build Coastguard Worker 		figure_journal_size(&jparams, journal_size, journal_fc_size, fs);
1607*6a54128fSAndroid Build Coastguard Worker 
1608*6a54128fSAndroid Build Coastguard Worker 		if (journal_location_string)
1609*6a54128fSAndroid Build Coastguard Worker 			journal_location =
1610*6a54128fSAndroid Build Coastguard Worker 				parse_num_blocks2(journal_location_string,
1611*6a54128fSAndroid Build Coastguard Worker 						  fs->super->s_log_block_size);
1612*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_add_journal_inode3(fs, &jparams,
1613*6a54128fSAndroid Build Coastguard Worker 						   journal_location,
1614*6a54128fSAndroid Build Coastguard Worker 						   journal_flags);
1615*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
1616*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, "\n");
1617*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, retval, "%s",
1618*6a54128fSAndroid Build Coastguard Worker 				_("\n\twhile trying to create journal file"));
1619*6a54128fSAndroid Build Coastguard Worker 			return retval;
1620*6a54128fSAndroid Build Coastguard Worker 		}
1621*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_overhead_clusters += EXT2FS_NUM_B2C(fs,
1622*6a54128fSAndroid Build Coastguard Worker 			jparams.num_journal_blocks + jparams.num_fc_blocks);
1623*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
1624*6a54128fSAndroid Build Coastguard Worker 		fputs(_("done\n"), stdout);
1625*6a54128fSAndroid Build Coastguard Worker 
1626*6a54128fSAndroid Build Coastguard Worker 		/*
1627*6a54128fSAndroid Build Coastguard Worker 		 * If the filesystem wasn't mounted, we need to force
1628*6a54128fSAndroid Build Coastguard Worker 		 * the block group descriptors out.
1629*6a54128fSAndroid Build Coastguard Worker 		 */
1630*6a54128fSAndroid Build Coastguard Worker 		if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1631*6a54128fSAndroid Build Coastguard Worker 			fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1632*6a54128fSAndroid Build Coastguard Worker 	}
1633*6a54128fSAndroid Build Coastguard Worker 	print_check_message(fs->super->s_max_mnt_count,
1634*6a54128fSAndroid Build Coastguard Worker 			    fs->super->s_checkinterval);
1635*6a54128fSAndroid Build Coastguard Worker 	return 0;
1636*6a54128fSAndroid Build Coastguard Worker 
1637*6a54128fSAndroid Build Coastguard Worker err:
1638*6a54128fSAndroid Build Coastguard Worker 	free(journal_device);
1639*6a54128fSAndroid Build Coastguard Worker 	return 1;
1640*6a54128fSAndroid Build Coastguard Worker }
1641*6a54128fSAndroid Build Coastguard Worker 
handle_quota_options(ext2_filsys fs)1642*6a54128fSAndroid Build Coastguard Worker static int handle_quota_options(ext2_filsys fs)
1643*6a54128fSAndroid Build Coastguard Worker {
1644*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
1645*6a54128fSAndroid Build Coastguard Worker 	quota_ctx_t qctx;
1646*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t qf_ino;
1647*6a54128fSAndroid Build Coastguard Worker 	enum quota_type qtype;
1648*6a54128fSAndroid Build Coastguard Worker 	unsigned int qtype_bits = 0;
1649*6a54128fSAndroid Build Coastguard Worker 	int need_dirty = 0;
1650*6a54128fSAndroid Build Coastguard Worker 
1651*6a54128fSAndroid Build Coastguard Worker 	for (qtype = 0 ; qtype < MAXQUOTAS; qtype++)
1652*6a54128fSAndroid Build Coastguard Worker 		if (quota_enable[qtype] != 0)
1653*6a54128fSAndroid Build Coastguard Worker 			break;
1654*6a54128fSAndroid Build Coastguard Worker 	if (qtype == MAXQUOTAS)
1655*6a54128fSAndroid Build Coastguard Worker 		/* Nothing to do. */
1656*6a54128fSAndroid Build Coastguard Worker 		return 0;
1657*6a54128fSAndroid Build Coastguard Worker 
1658*6a54128fSAndroid Build Coastguard Worker 	if (quota_enable[PRJQUOTA] == QOPT_ENABLE &&
1659*6a54128fSAndroid Build Coastguard Worker 	    fs->super->s_inode_size == EXT2_GOOD_OLD_INODE_SIZE) {
1660*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("Cannot enable project quota; "
1661*6a54128fSAndroid Build Coastguard Worker 				  "inode size too small.\n"));
1662*6a54128fSAndroid Build Coastguard Worker 		return 1;
1663*6a54128fSAndroid Build Coastguard Worker 	}
1664*6a54128fSAndroid Build Coastguard Worker 
1665*6a54128fSAndroid Build Coastguard Worker 	for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
1666*6a54128fSAndroid Build Coastguard Worker 		if (quota_enable[qtype] == QOPT_ENABLE)
1667*6a54128fSAndroid Build Coastguard Worker 			qtype_bits |= 1 << qtype;
1668*6a54128fSAndroid Build Coastguard Worker 	}
1669*6a54128fSAndroid Build Coastguard Worker 
1670*6a54128fSAndroid Build Coastguard Worker 	retval = quota_init_context(&qctx, fs, qtype_bits);
1671*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
1672*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval,
1673*6a54128fSAndroid Build Coastguard Worker 			_("while initializing quota context in support library"));
1674*6a54128fSAndroid Build Coastguard Worker 		return 1;
1675*6a54128fSAndroid Build Coastguard Worker 	}
1676*6a54128fSAndroid Build Coastguard Worker 
1677*6a54128fSAndroid Build Coastguard Worker 	if (qtype_bits)
1678*6a54128fSAndroid Build Coastguard Worker 		quota_compute_usage(qctx);
1679*6a54128fSAndroid Build Coastguard Worker 
1680*6a54128fSAndroid Build Coastguard Worker 	for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
1681*6a54128fSAndroid Build Coastguard Worker 		if (quota_enable[qtype] == QOPT_ENABLE &&
1682*6a54128fSAndroid Build Coastguard Worker 		    *quota_sb_inump(fs->super, qtype) == 0) {
1683*6a54128fSAndroid Build Coastguard Worker 			if ((qf_ino = quota_file_exists(fs, qtype)) > 0) {
1684*6a54128fSAndroid Build Coastguard Worker 				retval = quota_read_all_dquots(qctx, qf_ino,
1685*6a54128fSAndroid Build Coastguard Worker 							       qtype,
1686*6a54128fSAndroid Build Coastguard Worker 							       QREAD_LIMITS);
1687*6a54128fSAndroid Build Coastguard Worker 				if (retval) {
1688*6a54128fSAndroid Build Coastguard Worker 					com_err(program_name, retval,
1689*6a54128fSAndroid Build Coastguard Worker 						_("while updating quota limits (%d)"),
1690*6a54128fSAndroid Build Coastguard Worker 						qtype);
1691*6a54128fSAndroid Build Coastguard Worker 				quota_errout:
1692*6a54128fSAndroid Build Coastguard Worker 					quota_release_context(&qctx);
1693*6a54128fSAndroid Build Coastguard Worker 					return 1;
1694*6a54128fSAndroid Build Coastguard Worker 				}
1695*6a54128fSAndroid Build Coastguard Worker 			}
1696*6a54128fSAndroid Build Coastguard Worker 			retval = quota_write_inode(qctx, 1 << qtype);
1697*6a54128fSAndroid Build Coastguard Worker 			if (retval) {
1698*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, retval,
1699*6a54128fSAndroid Build Coastguard Worker 					_("while writing quota file (%d)"),
1700*6a54128fSAndroid Build Coastguard Worker 					qtype);
1701*6a54128fSAndroid Build Coastguard Worker 				goto quota_errout;
1702*6a54128fSAndroid Build Coastguard Worker 			}
1703*6a54128fSAndroid Build Coastguard Worker 			/* Enable Quota feature if one of quota enabled */
1704*6a54128fSAndroid Build Coastguard Worker 			if (!ext2fs_has_feature_quota(fs->super)) {
1705*6a54128fSAndroid Build Coastguard Worker 				ext2fs_set_feature_quota(fs->super);
1706*6a54128fSAndroid Build Coastguard Worker 				need_dirty = 1;
1707*6a54128fSAndroid Build Coastguard Worker 			}
1708*6a54128fSAndroid Build Coastguard Worker 			if (qtype == PRJQUOTA &&
1709*6a54128fSAndroid Build Coastguard Worker 			    !ext2fs_has_feature_project(fs->super)) {
1710*6a54128fSAndroid Build Coastguard Worker 				ext2fs_set_feature_project(fs->super);
1711*6a54128fSAndroid Build Coastguard Worker 				need_dirty = 1;
1712*6a54128fSAndroid Build Coastguard Worker 			}
1713*6a54128fSAndroid Build Coastguard Worker 		} else if (quota_enable[qtype] == QOPT_DISABLE) {
1714*6a54128fSAndroid Build Coastguard Worker 			retval = quota_remove_inode(fs, qtype);
1715*6a54128fSAndroid Build Coastguard Worker 			if (retval) {
1716*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, retval,
1717*6a54128fSAndroid Build Coastguard Worker 					_("while removing quota file (%d)"),
1718*6a54128fSAndroid Build Coastguard Worker 					qtype);
1719*6a54128fSAndroid Build Coastguard Worker 				goto quota_errout;
1720*6a54128fSAndroid Build Coastguard Worker 			}
1721*6a54128fSAndroid Build Coastguard Worker 			if (qtype == PRJQUOTA) {
1722*6a54128fSAndroid Build Coastguard Worker 				ext2fs_clear_feature_project(fs->super);
1723*6a54128fSAndroid Build Coastguard Worker 				need_dirty = 1;
1724*6a54128fSAndroid Build Coastguard Worker 			}
1725*6a54128fSAndroid Build Coastguard Worker 		}
1726*6a54128fSAndroid Build Coastguard Worker 	}
1727*6a54128fSAndroid Build Coastguard Worker 
1728*6a54128fSAndroid Build Coastguard Worker 	quota_release_context(&qctx);
1729*6a54128fSAndroid Build Coastguard Worker 	/* Clear Quota feature if all quota types disabled. */
1730*6a54128fSAndroid Build Coastguard Worker 	if (!qtype_bits) {
1731*6a54128fSAndroid Build Coastguard Worker 		for (qtype = 0 ; qtype < MAXQUOTAS; qtype++)
1732*6a54128fSAndroid Build Coastguard Worker 			if (*quota_sb_inump(fs->super, qtype))
1733*6a54128fSAndroid Build Coastguard Worker 				break;
1734*6a54128fSAndroid Build Coastguard Worker 		if (qtype == MAXQUOTAS) {
1735*6a54128fSAndroid Build Coastguard Worker 			ext2fs_clear_feature_quota(fs->super);
1736*6a54128fSAndroid Build Coastguard Worker 			need_dirty = 1;
1737*6a54128fSAndroid Build Coastguard Worker 		}
1738*6a54128fSAndroid Build Coastguard Worker 
1739*6a54128fSAndroid Build Coastguard Worker 	}
1740*6a54128fSAndroid Build Coastguard Worker 	if (need_dirty)
1741*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
1742*6a54128fSAndroid Build Coastguard Worker 	return 0;
1743*6a54128fSAndroid Build Coastguard Worker }
1744*6a54128fSAndroid Build Coastguard Worker 
option_handle_function(char * token)1745*6a54128fSAndroid Build Coastguard Worker static int option_handle_function(char *token)
1746*6a54128fSAndroid Build Coastguard Worker {
1747*6a54128fSAndroid Build Coastguard Worker 	if (strncmp(token, "usr", 3) == 0) {
1748*6a54128fSAndroid Build Coastguard Worker 		quota_enable[USRQUOTA] = QOPT_ENABLE;
1749*6a54128fSAndroid Build Coastguard Worker 	} else if (strncmp(token, "^usr", 4) == 0) {
1750*6a54128fSAndroid Build Coastguard Worker 		quota_enable[USRQUOTA] = QOPT_DISABLE;
1751*6a54128fSAndroid Build Coastguard Worker 	} else if (strncmp(token, "grp", 3) == 0) {
1752*6a54128fSAndroid Build Coastguard Worker 		quota_enable[GRPQUOTA] = QOPT_ENABLE;
1753*6a54128fSAndroid Build Coastguard Worker 	} else if (strncmp(token, "^grp", 4) == 0) {
1754*6a54128fSAndroid Build Coastguard Worker 		quota_enable[GRPQUOTA] = QOPT_DISABLE;
1755*6a54128fSAndroid Build Coastguard Worker 	} else if (strncmp(token, "prj", 3) == 0) {
1756*6a54128fSAndroid Build Coastguard Worker 		quota_enable[PRJQUOTA] = QOPT_ENABLE;
1757*6a54128fSAndroid Build Coastguard Worker 	} else if (strncmp(token, "^prj", 4) == 0) {
1758*6a54128fSAndroid Build Coastguard Worker 		quota_enable[PRJQUOTA] = QOPT_DISABLE;
1759*6a54128fSAndroid Build Coastguard Worker 	} else {
1760*6a54128fSAndroid Build Coastguard Worker 		fputs(_("\nBad quota options specified.\n\n"
1761*6a54128fSAndroid Build Coastguard Worker 			"Following valid quota options are available "
1762*6a54128fSAndroid Build Coastguard Worker 			"(pass by separating with comma):\n"
1763*6a54128fSAndroid Build Coastguard Worker 			"\t[^]usr[quota]\n"
1764*6a54128fSAndroid Build Coastguard Worker 			"\t[^]grp[quota]\n"
1765*6a54128fSAndroid Build Coastguard Worker 			"\t[^]prj[quota]\n"
1766*6a54128fSAndroid Build Coastguard Worker 			"\n\n"), stderr);
1767*6a54128fSAndroid Build Coastguard Worker 		return 1;
1768*6a54128fSAndroid Build Coastguard Worker 	}
1769*6a54128fSAndroid Build Coastguard Worker 	return 0;
1770*6a54128fSAndroid Build Coastguard Worker }
1771*6a54128fSAndroid Build Coastguard Worker 
parse_e2label_options(int argc,char ** argv)1772*6a54128fSAndroid Build Coastguard Worker static void parse_e2label_options(int argc, char ** argv)
1773*6a54128fSAndroid Build Coastguard Worker {
1774*6a54128fSAndroid Build Coastguard Worker 	if ((argc < 2) || (argc > 3)) {
1775*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1776*6a54128fSAndroid Build Coastguard Worker 		exit(1);
1777*6a54128fSAndroid Build Coastguard Worker 	}
1778*6a54128fSAndroid Build Coastguard Worker 	io_options = strchr(argv[1], '?');
1779*6a54128fSAndroid Build Coastguard Worker 	if (io_options)
1780*6a54128fSAndroid Build Coastguard Worker 		*io_options++ = 0;
1781*6a54128fSAndroid Build Coastguard Worker 	device_name = get_devname(NULL, argv[1], NULL);
1782*6a54128fSAndroid Build Coastguard Worker 	if (!device_name) {
1783*6a54128fSAndroid Build Coastguard Worker 		com_err("e2label", 0, _("Unable to resolve '%s'"),
1784*6a54128fSAndroid Build Coastguard Worker 			argv[1]);
1785*6a54128fSAndroid Build Coastguard Worker 		exit(1);
1786*6a54128fSAndroid Build Coastguard Worker 	}
1787*6a54128fSAndroid Build Coastguard Worker 	open_flag = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SUPER_ONLY;
1788*6a54128fSAndroid Build Coastguard Worker 	if (argc == 3) {
1789*6a54128fSAndroid Build Coastguard Worker 		open_flag |= EXT2_FLAG_RW;
1790*6a54128fSAndroid Build Coastguard Worker 		L_flag = 1;
1791*6a54128fSAndroid Build Coastguard Worker 		new_label = argv[2];
1792*6a54128fSAndroid Build Coastguard Worker 	} else
1793*6a54128fSAndroid Build Coastguard Worker 		print_label++;
1794*6a54128fSAndroid Build Coastguard Worker }
1795*6a54128fSAndroid Build Coastguard Worker 
parse_time(char * str)1796*6a54128fSAndroid Build Coastguard Worker static time_t parse_time(char *str)
1797*6a54128fSAndroid Build Coastguard Worker {
1798*6a54128fSAndroid Build Coastguard Worker 	struct	tm	ts;
1799*6a54128fSAndroid Build Coastguard Worker 
1800*6a54128fSAndroid Build Coastguard Worker 	if (strcmp(str, "now") == 0) {
1801*6a54128fSAndroid Build Coastguard Worker 		return (time(0));
1802*6a54128fSAndroid Build Coastguard Worker 	}
1803*6a54128fSAndroid Build Coastguard Worker 	memset(&ts, 0, sizeof(ts));
1804*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_STRPTIME
1805*6a54128fSAndroid Build Coastguard Worker 	strptime(str, "%Y%m%d%H%M%S", &ts);
1806*6a54128fSAndroid Build Coastguard Worker #else
1807*6a54128fSAndroid Build Coastguard Worker 	sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1808*6a54128fSAndroid Build Coastguard Worker 	       &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1809*6a54128fSAndroid Build Coastguard Worker 	ts.tm_year -= 1900;
1810*6a54128fSAndroid Build Coastguard Worker 	ts.tm_mon -= 1;
1811*6a54128fSAndroid Build Coastguard Worker 	if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1812*6a54128fSAndroid Build Coastguard Worker 	    ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1813*6a54128fSAndroid Build Coastguard Worker 	    ts.tm_min > 59 || ts.tm_sec > 61)
1814*6a54128fSAndroid Build Coastguard Worker 		ts.tm_mday = 0;
1815*6a54128fSAndroid Build Coastguard Worker #endif
1816*6a54128fSAndroid Build Coastguard Worker 	if (ts.tm_mday == 0) {
1817*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, 0,
1818*6a54128fSAndroid Build Coastguard Worker 			_("Couldn't parse date/time specifier: %s"),
1819*6a54128fSAndroid Build Coastguard Worker 			str);
1820*6a54128fSAndroid Build Coastguard Worker 		usage();
1821*6a54128fSAndroid Build Coastguard Worker 	}
1822*6a54128fSAndroid Build Coastguard Worker 	ts.tm_isdst = -1;
1823*6a54128fSAndroid Build Coastguard Worker 	return (mktime(&ts));
1824*6a54128fSAndroid Build Coastguard Worker }
1825*6a54128fSAndroid Build Coastguard Worker 
parse_tune2fs_options(int argc,char ** argv)1826*6a54128fSAndroid Build Coastguard Worker static void parse_tune2fs_options(int argc, char **argv)
1827*6a54128fSAndroid Build Coastguard Worker {
1828*6a54128fSAndroid Build Coastguard Worker 	int c;
1829*6a54128fSAndroid Build Coastguard Worker 	char *tmp;
1830*6a54128fSAndroid Build Coastguard Worker 	struct group *gr;
1831*6a54128fSAndroid Build Coastguard Worker 	struct passwd *pw;
1832*6a54128fSAndroid Build Coastguard Worker 	int ret;
1833*6a54128fSAndroid Build Coastguard Worker 	char optstring[100] = "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:z:Q:";
1834*6a54128fSAndroid Build Coastguard Worker 
1835*6a54128fSAndroid Build Coastguard Worker 	open_flag = 0;
1836*6a54128fSAndroid Build Coastguard Worker 	printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1837*6a54128fSAndroid Build Coastguard Worker 	while ((c = getopt(argc, argv, optstring)) != EOF)
1838*6a54128fSAndroid Build Coastguard Worker 		switch (c) {
1839*6a54128fSAndroid Build Coastguard Worker 		case 'c':
1840*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1841*6a54128fSAndroid Build Coastguard Worker 			c_flag = 1;
1842*6a54128fSAndroid Build Coastguard Worker 			if (strcmp(optarg, "random") == 0) {
1843*6a54128fSAndroid Build Coastguard Worker 				max_mount_count = 65536;
1844*6a54128fSAndroid Build Coastguard Worker 				break;
1845*6a54128fSAndroid Build Coastguard Worker 			}
1846*6a54128fSAndroid Build Coastguard Worker 			max_mount_count = strtol(optarg, &tmp, 0);
1847*6a54128fSAndroid Build Coastguard Worker 			if (*tmp || max_mount_count > 16000 ||
1848*6a54128fSAndroid Build Coastguard Worker 			    max_mount_count < -16000) {
1849*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
1850*6a54128fSAndroid Build Coastguard Worker 					_("bad mounts count - %s"),
1851*6a54128fSAndroid Build Coastguard Worker 					optarg);
1852*6a54128fSAndroid Build Coastguard Worker 				usage();
1853*6a54128fSAndroid Build Coastguard Worker 			}
1854*6a54128fSAndroid Build Coastguard Worker 			if (max_mount_count == 0)
1855*6a54128fSAndroid Build Coastguard Worker 				max_mount_count = -1;
1856*6a54128fSAndroid Build Coastguard Worker 			break;
1857*6a54128fSAndroid Build Coastguard Worker 		case 'C':
1858*6a54128fSAndroid Build Coastguard Worker 			mount_count = strtoul(optarg, &tmp, 0);
1859*6a54128fSAndroid Build Coastguard Worker 			if (*tmp || mount_count > 16000) {
1860*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
1861*6a54128fSAndroid Build Coastguard Worker 					_("bad mounts count - %s"),
1862*6a54128fSAndroid Build Coastguard Worker 					optarg);
1863*6a54128fSAndroid Build Coastguard Worker 				usage();
1864*6a54128fSAndroid Build Coastguard Worker 			}
1865*6a54128fSAndroid Build Coastguard Worker 			C_flag = 1;
1866*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1867*6a54128fSAndroid Build Coastguard Worker 			break;
1868*6a54128fSAndroid Build Coastguard Worker 		case 'e':
1869*6a54128fSAndroid Build Coastguard Worker 			if (strcmp(optarg, "continue") == 0)
1870*6a54128fSAndroid Build Coastguard Worker 				errors = EXT2_ERRORS_CONTINUE;
1871*6a54128fSAndroid Build Coastguard Worker 			else if (strcmp(optarg, "remount-ro") == 0)
1872*6a54128fSAndroid Build Coastguard Worker 				errors = EXT2_ERRORS_RO;
1873*6a54128fSAndroid Build Coastguard Worker 			else if (strcmp(optarg, "panic") == 0)
1874*6a54128fSAndroid Build Coastguard Worker 				errors = EXT2_ERRORS_PANIC;
1875*6a54128fSAndroid Build Coastguard Worker 			else {
1876*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
1877*6a54128fSAndroid Build Coastguard Worker 					_("bad error behavior - %s"),
1878*6a54128fSAndroid Build Coastguard Worker 					optarg);
1879*6a54128fSAndroid Build Coastguard Worker 				usage();
1880*6a54128fSAndroid Build Coastguard Worker 			}
1881*6a54128fSAndroid Build Coastguard Worker 			e_flag = 1;
1882*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1883*6a54128fSAndroid Build Coastguard Worker 			break;
1884*6a54128fSAndroid Build Coastguard Worker 		case 'E':
1885*6a54128fSAndroid Build Coastguard Worker 			extended_cmd = optarg;
1886*6a54128fSAndroid Build Coastguard Worker 			open_flag |= EXT2_FLAG_RW;
1887*6a54128fSAndroid Build Coastguard Worker 			break;
1888*6a54128fSAndroid Build Coastguard Worker 		case 'f': /* Force */
1889*6a54128fSAndroid Build Coastguard Worker 			f_flag++;
1890*6a54128fSAndroid Build Coastguard Worker 			break;
1891*6a54128fSAndroid Build Coastguard Worker 		case 'g':
1892*6a54128fSAndroid Build Coastguard Worker 			resgid = strtoul(optarg, &tmp, 0);
1893*6a54128fSAndroid Build Coastguard Worker 			if (*tmp) {
1894*6a54128fSAndroid Build Coastguard Worker 				gr = getgrnam(optarg);
1895*6a54128fSAndroid Build Coastguard Worker 				if (gr == NULL)
1896*6a54128fSAndroid Build Coastguard Worker 					tmp = optarg;
1897*6a54128fSAndroid Build Coastguard Worker 				else {
1898*6a54128fSAndroid Build Coastguard Worker 					resgid = gr->gr_gid;
1899*6a54128fSAndroid Build Coastguard Worker 					*tmp = 0;
1900*6a54128fSAndroid Build Coastguard Worker 				}
1901*6a54128fSAndroid Build Coastguard Worker 			}
1902*6a54128fSAndroid Build Coastguard Worker 			if (*tmp) {
1903*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
1904*6a54128fSAndroid Build Coastguard Worker 					_("bad gid/group name - %s"),
1905*6a54128fSAndroid Build Coastguard Worker 					optarg);
1906*6a54128fSAndroid Build Coastguard Worker 				usage();
1907*6a54128fSAndroid Build Coastguard Worker 			}
1908*6a54128fSAndroid Build Coastguard Worker 			g_flag = 1;
1909*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1910*6a54128fSAndroid Build Coastguard Worker 			break;
1911*6a54128fSAndroid Build Coastguard Worker 		case 'i':
1912*6a54128fSAndroid Build Coastguard Worker 			interval = strtoul(optarg, &tmp, 0);
1913*6a54128fSAndroid Build Coastguard Worker 			switch (*tmp) {
1914*6a54128fSAndroid Build Coastguard Worker 			case 's':
1915*6a54128fSAndroid Build Coastguard Worker 				tmp++;
1916*6a54128fSAndroid Build Coastguard Worker 				break;
1917*6a54128fSAndroid Build Coastguard Worker 			case '\0':
1918*6a54128fSAndroid Build Coastguard Worker 			case 'd':
1919*6a54128fSAndroid Build Coastguard Worker 			case 'D': /* days */
1920*6a54128fSAndroid Build Coastguard Worker 				interval *= 86400;
1921*6a54128fSAndroid Build Coastguard Worker 				if (*tmp != '\0')
1922*6a54128fSAndroid Build Coastguard Worker 					tmp++;
1923*6a54128fSAndroid Build Coastguard Worker 				break;
1924*6a54128fSAndroid Build Coastguard Worker 			case 'm':
1925*6a54128fSAndroid Build Coastguard Worker 			case 'M': /* months! */
1926*6a54128fSAndroid Build Coastguard Worker 				interval *= 86400 * 30;
1927*6a54128fSAndroid Build Coastguard Worker 				tmp++;
1928*6a54128fSAndroid Build Coastguard Worker 				break;
1929*6a54128fSAndroid Build Coastguard Worker 			case 'w':
1930*6a54128fSAndroid Build Coastguard Worker 			case 'W': /* weeks */
1931*6a54128fSAndroid Build Coastguard Worker 				interval *= 86400 * 7;
1932*6a54128fSAndroid Build Coastguard Worker 				tmp++;
1933*6a54128fSAndroid Build Coastguard Worker 				break;
1934*6a54128fSAndroid Build Coastguard Worker 			}
1935*6a54128fSAndroid Build Coastguard Worker 			if (*tmp) {
1936*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
1937*6a54128fSAndroid Build Coastguard Worker 					_("bad interval - %s"), optarg);
1938*6a54128fSAndroid Build Coastguard Worker 				usage();
1939*6a54128fSAndroid Build Coastguard Worker 			}
1940*6a54128fSAndroid Build Coastguard Worker 			i_flag = 1;
1941*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1942*6a54128fSAndroid Build Coastguard Worker 			break;
1943*6a54128fSAndroid Build Coastguard Worker 		case 'j':
1944*6a54128fSAndroid Build Coastguard Worker 			if (!journal_size)
1945*6a54128fSAndroid Build Coastguard Worker 				journal_size = -1;
1946*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1947*6a54128fSAndroid Build Coastguard Worker 			break;
1948*6a54128fSAndroid Build Coastguard Worker 		case 'J':
1949*6a54128fSAndroid Build Coastguard Worker 			parse_journal_opts(optarg);
1950*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1951*6a54128fSAndroid Build Coastguard Worker 			break;
1952*6a54128fSAndroid Build Coastguard Worker 		case 'l':
1953*6a54128fSAndroid Build Coastguard Worker 			l_flag = 1;
1954*6a54128fSAndroid Build Coastguard Worker 			break;
1955*6a54128fSAndroid Build Coastguard Worker 		case 'L':
1956*6a54128fSAndroid Build Coastguard Worker 			new_label = optarg;
1957*6a54128fSAndroid Build Coastguard Worker 			L_flag = 1;
1958*6a54128fSAndroid Build Coastguard Worker 			open_flag |= EXT2_FLAG_RW |
1959*6a54128fSAndroid Build Coastguard Worker 				EXT2_FLAG_JOURNAL_DEV_OK;
1960*6a54128fSAndroid Build Coastguard Worker 			break;
1961*6a54128fSAndroid Build Coastguard Worker 		case 'm':
1962*6a54128fSAndroid Build Coastguard Worker 			reserved_ratio = strtod(optarg, &tmp);
1963*6a54128fSAndroid Build Coastguard Worker 			if (*tmp || reserved_ratio > 50 ||
1964*6a54128fSAndroid Build Coastguard Worker 			    reserved_ratio < 0) {
1965*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
1966*6a54128fSAndroid Build Coastguard Worker 					_("bad reserved block ratio - %s"),
1967*6a54128fSAndroid Build Coastguard Worker 					optarg);
1968*6a54128fSAndroid Build Coastguard Worker 				usage();
1969*6a54128fSAndroid Build Coastguard Worker 			}
1970*6a54128fSAndroid Build Coastguard Worker 			m_flag = 1;
1971*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1972*6a54128fSAndroid Build Coastguard Worker 			break;
1973*6a54128fSAndroid Build Coastguard Worker 		case 'M':
1974*6a54128fSAndroid Build Coastguard Worker 			new_last_mounted = optarg;
1975*6a54128fSAndroid Build Coastguard Worker 			M_flag = 1;
1976*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1977*6a54128fSAndroid Build Coastguard Worker 			break;
1978*6a54128fSAndroid Build Coastguard Worker 		case 'o':
1979*6a54128fSAndroid Build Coastguard Worker 			if (mntopts_cmd) {
1980*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0, "%s",
1981*6a54128fSAndroid Build Coastguard Worker 					_("-o may only be specified once"));
1982*6a54128fSAndroid Build Coastguard Worker 				usage();
1983*6a54128fSAndroid Build Coastguard Worker 			}
1984*6a54128fSAndroid Build Coastguard Worker 			mntopts_cmd = optarg;
1985*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1986*6a54128fSAndroid Build Coastguard Worker 			break;
1987*6a54128fSAndroid Build Coastguard Worker 		case 'O':
1988*6a54128fSAndroid Build Coastguard Worker 			if (features_cmd) {
1989*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0, "%s",
1990*6a54128fSAndroid Build Coastguard Worker 					_("-O may only be specified once"));
1991*6a54128fSAndroid Build Coastguard Worker 				usage();
1992*6a54128fSAndroid Build Coastguard Worker 			}
1993*6a54128fSAndroid Build Coastguard Worker 			features_cmd = optarg;
1994*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
1995*6a54128fSAndroid Build Coastguard Worker 			break;
1996*6a54128fSAndroid Build Coastguard Worker 		case 'Q':
1997*6a54128fSAndroid Build Coastguard Worker 			Q_flag = 1;
1998*6a54128fSAndroid Build Coastguard Worker 			ret = parse_quota_opts(optarg, option_handle_function);
1999*6a54128fSAndroid Build Coastguard Worker 			if (ret)
2000*6a54128fSAndroid Build Coastguard Worker 				exit(1);
2001*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
2002*6a54128fSAndroid Build Coastguard Worker 			break;
2003*6a54128fSAndroid Build Coastguard Worker 		case 'r':
2004*6a54128fSAndroid Build Coastguard Worker 			reserved_blocks = strtoul(optarg, &tmp, 0);
2005*6a54128fSAndroid Build Coastguard Worker 			if (*tmp) {
2006*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
2007*6a54128fSAndroid Build Coastguard Worker 					_("bad reserved blocks count - %s"),
2008*6a54128fSAndroid Build Coastguard Worker 					optarg);
2009*6a54128fSAndroid Build Coastguard Worker 				usage();
2010*6a54128fSAndroid Build Coastguard Worker 			}
2011*6a54128fSAndroid Build Coastguard Worker 			r_flag = 1;
2012*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
2013*6a54128fSAndroid Build Coastguard Worker 			break;
2014*6a54128fSAndroid Build Coastguard Worker 		case 's': /* Deprecated */
2015*6a54128fSAndroid Build Coastguard Worker 			s_flag = atoi(optarg);
2016*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
2017*6a54128fSAndroid Build Coastguard Worker 			break;
2018*6a54128fSAndroid Build Coastguard Worker 		case 'T':
2019*6a54128fSAndroid Build Coastguard Worker 			T_flag = 1;
2020*6a54128fSAndroid Build Coastguard Worker 			last_check_time = parse_time(optarg);
2021*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
2022*6a54128fSAndroid Build Coastguard Worker 			break;
2023*6a54128fSAndroid Build Coastguard Worker 		case 'u':
2024*6a54128fSAndroid Build Coastguard Worker 				resuid = strtoul(optarg, &tmp, 0);
2025*6a54128fSAndroid Build Coastguard Worker 				if (*tmp) {
2026*6a54128fSAndroid Build Coastguard Worker 					pw = getpwnam(optarg);
2027*6a54128fSAndroid Build Coastguard Worker 					if (pw == NULL)
2028*6a54128fSAndroid Build Coastguard Worker 						tmp = optarg;
2029*6a54128fSAndroid Build Coastguard Worker 					else {
2030*6a54128fSAndroid Build Coastguard Worker 						resuid = pw->pw_uid;
2031*6a54128fSAndroid Build Coastguard Worker 						*tmp = 0;
2032*6a54128fSAndroid Build Coastguard Worker 					}
2033*6a54128fSAndroid Build Coastguard Worker 				}
2034*6a54128fSAndroid Build Coastguard Worker 				if (*tmp) {
2035*6a54128fSAndroid Build Coastguard Worker 					com_err(program_name, 0,
2036*6a54128fSAndroid Build Coastguard Worker 						_("bad uid/user name - %s"),
2037*6a54128fSAndroid Build Coastguard Worker 						optarg);
2038*6a54128fSAndroid Build Coastguard Worker 					usage();
2039*6a54128fSAndroid Build Coastguard Worker 				}
2040*6a54128fSAndroid Build Coastguard Worker 				u_flag = 1;
2041*6a54128fSAndroid Build Coastguard Worker 				open_flag = EXT2_FLAG_RW;
2042*6a54128fSAndroid Build Coastguard Worker 				break;
2043*6a54128fSAndroid Build Coastguard Worker 		case 'U':
2044*6a54128fSAndroid Build Coastguard Worker 			new_UUID = optarg;
2045*6a54128fSAndroid Build Coastguard Worker 			U_flag = 1;
2046*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW |
2047*6a54128fSAndroid Build Coastguard Worker 				EXT2_FLAG_JOURNAL_DEV_OK;
2048*6a54128fSAndroid Build Coastguard Worker 			break;
2049*6a54128fSAndroid Build Coastguard Worker 		case 'I':
2050*6a54128fSAndroid Build Coastguard Worker 			new_inode_size = strtoul(optarg, &tmp, 0);
2051*6a54128fSAndroid Build Coastguard Worker 			if (*tmp) {
2052*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
2053*6a54128fSAndroid Build Coastguard Worker 					_("bad inode size - %s"),
2054*6a54128fSAndroid Build Coastguard Worker 					optarg);
2055*6a54128fSAndroid Build Coastguard Worker 				usage();
2056*6a54128fSAndroid Build Coastguard Worker 			}
2057*6a54128fSAndroid Build Coastguard Worker 			if (!((new_inode_size &
2058*6a54128fSAndroid Build Coastguard Worker 			       (new_inode_size - 1)) == 0)) {
2059*6a54128fSAndroid Build Coastguard Worker 				com_err(program_name, 0,
2060*6a54128fSAndroid Build Coastguard Worker 					_("Inode size must be a "
2061*6a54128fSAndroid Build Coastguard Worker 					  "power of two- %s"),
2062*6a54128fSAndroid Build Coastguard Worker 					optarg);
2063*6a54128fSAndroid Build Coastguard Worker 				usage();
2064*6a54128fSAndroid Build Coastguard Worker 			}
2065*6a54128fSAndroid Build Coastguard Worker 			open_flag = EXT2_FLAG_RW;
2066*6a54128fSAndroid Build Coastguard Worker 			I_flag = 1;
2067*6a54128fSAndroid Build Coastguard Worker 			break;
2068*6a54128fSAndroid Build Coastguard Worker 		case 'z':
2069*6a54128fSAndroid Build Coastguard Worker 			undo_file = optarg;
2070*6a54128fSAndroid Build Coastguard Worker 			break;
2071*6a54128fSAndroid Build Coastguard Worker 		default:
2072*6a54128fSAndroid Build Coastguard Worker 			usage();
2073*6a54128fSAndroid Build Coastguard Worker 		}
2074*6a54128fSAndroid Build Coastguard Worker 	if (optind < argc - 1 || optind == argc)
2075*6a54128fSAndroid Build Coastguard Worker 		usage();
2076*6a54128fSAndroid Build Coastguard Worker 	if (!open_flag && !l_flag)
2077*6a54128fSAndroid Build Coastguard Worker 		usage();
2078*6a54128fSAndroid Build Coastguard Worker 	io_options = strchr(argv[optind], '?');
2079*6a54128fSAndroid Build Coastguard Worker 	if (io_options)
2080*6a54128fSAndroid Build Coastguard Worker 		*io_options++ = 0;
2081*6a54128fSAndroid Build Coastguard Worker 	device_name = get_devname(NULL, argv[optind], NULL);
2082*6a54128fSAndroid Build Coastguard Worker 	if (!device_name) {
2083*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, 0, _("Unable to resolve '%s'"),
2084*6a54128fSAndroid Build Coastguard Worker 			argv[optind]);
2085*6a54128fSAndroid Build Coastguard Worker 		exit(1);
2086*6a54128fSAndroid Build Coastguard Worker 	}
2087*6a54128fSAndroid Build Coastguard Worker }
2088*6a54128fSAndroid Build Coastguard Worker 
2089*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_BUILD_FINDFS
do_findfs(int argc,char ** argv)2090*6a54128fSAndroid Build Coastguard Worker void do_findfs(int argc, char **argv)
2091*6a54128fSAndroid Build Coastguard Worker {
2092*6a54128fSAndroid Build Coastguard Worker 	char	*dev;
2093*6a54128fSAndroid Build Coastguard Worker 
2094*6a54128fSAndroid Build Coastguard Worker 	if ((argc != 2) ||
2095*6a54128fSAndroid Build Coastguard Worker 	    (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
2096*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
2097*6a54128fSAndroid Build Coastguard Worker 		exit(2);
2098*6a54128fSAndroid Build Coastguard Worker 	}
2099*6a54128fSAndroid Build Coastguard Worker 	dev = blkid_get_devname(NULL, argv[1], NULL);
2100*6a54128fSAndroid Build Coastguard Worker 	if (!dev) {
2101*6a54128fSAndroid Build Coastguard Worker 		com_err("findfs", 0, _("Unable to resolve '%s'"),
2102*6a54128fSAndroid Build Coastguard Worker 			argv[1]);
2103*6a54128fSAndroid Build Coastguard Worker 		exit(1);
2104*6a54128fSAndroid Build Coastguard Worker 	}
2105*6a54128fSAndroid Build Coastguard Worker 	puts(dev);
2106*6a54128fSAndroid Build Coastguard Worker 	exit(0);
2107*6a54128fSAndroid Build Coastguard Worker }
2108*6a54128fSAndroid Build Coastguard Worker #endif
2109*6a54128fSAndroid Build Coastguard Worker 
parse_extended_opts(ext2_filsys fs,const char * opts)2110*6a54128fSAndroid Build Coastguard Worker static int parse_extended_opts(ext2_filsys fs, const char *opts)
2111*6a54128fSAndroid Build Coastguard Worker {
2112*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb = fs->super;
2113*6a54128fSAndroid Build Coastguard Worker 	char	*buf, *token, *next, *p, *arg;
2114*6a54128fSAndroid Build Coastguard Worker 	int	len, hash_alg;
2115*6a54128fSAndroid Build Coastguard Worker 	int	r_usage = 0;
2116*6a54128fSAndroid Build Coastguard Worker 	int encoding = 0;
2117*6a54128fSAndroid Build Coastguard Worker 	char	*encoding_flags = NULL;
2118*6a54128fSAndroid Build Coastguard Worker 
2119*6a54128fSAndroid Build Coastguard Worker 	len = strlen(opts);
2120*6a54128fSAndroid Build Coastguard Worker 	buf = malloc(len+1);
2121*6a54128fSAndroid Build Coastguard Worker 	if (!buf) {
2122*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "%s",
2123*6a54128fSAndroid Build Coastguard Worker 			_("Couldn't allocate memory to parse options!\n"));
2124*6a54128fSAndroid Build Coastguard Worker 		return 1;
2125*6a54128fSAndroid Build Coastguard Worker 	}
2126*6a54128fSAndroid Build Coastguard Worker 	strcpy(buf, opts);
2127*6a54128fSAndroid Build Coastguard Worker 	for (token = buf; token && *token; token = next) {
2128*6a54128fSAndroid Build Coastguard Worker 		p = strchr(token, ',');
2129*6a54128fSAndroid Build Coastguard Worker 		next = 0;
2130*6a54128fSAndroid Build Coastguard Worker 		if (p) {
2131*6a54128fSAndroid Build Coastguard Worker 			*p = 0;
2132*6a54128fSAndroid Build Coastguard Worker 			next = p+1;
2133*6a54128fSAndroid Build Coastguard Worker 		}
2134*6a54128fSAndroid Build Coastguard Worker 		arg = strchr(token, '=');
2135*6a54128fSAndroid Build Coastguard Worker 		if (arg) {
2136*6a54128fSAndroid Build Coastguard Worker 			*arg = 0;
2137*6a54128fSAndroid Build Coastguard Worker 			arg++;
2138*6a54128fSAndroid Build Coastguard Worker 		}
2139*6a54128fSAndroid Build Coastguard Worker 		if (strcmp(token, "clear-mmp") == 0 ||
2140*6a54128fSAndroid Build Coastguard Worker 		    strcmp(token, "clear_mmp") == 0) {
2141*6a54128fSAndroid Build Coastguard Worker 			clear_mmp = 1;
2142*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "mmp_update_interval") == 0) {
2143*6a54128fSAndroid Build Coastguard Worker 			unsigned long intv;
2144*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
2145*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2146*6a54128fSAndroid Build Coastguard Worker 				continue;
2147*6a54128fSAndroid Build Coastguard Worker 			}
2148*6a54128fSAndroid Build Coastguard Worker 			intv = strtoul(arg, &p, 0);
2149*6a54128fSAndroid Build Coastguard Worker 			if (*p) {
2150*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
2151*6a54128fSAndroid Build Coastguard Worker 					_("Invalid mmp_update_interval: %s\n"),
2152*6a54128fSAndroid Build Coastguard Worker 					arg);
2153*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2154*6a54128fSAndroid Build Coastguard Worker 				continue;
2155*6a54128fSAndroid Build Coastguard Worker 			}
2156*6a54128fSAndroid Build Coastguard Worker 			if (intv == 0) {
2157*6a54128fSAndroid Build Coastguard Worker 				intv = EXT4_MMP_UPDATE_INTERVAL;
2158*6a54128fSAndroid Build Coastguard Worker 			} else if (intv > EXT4_MMP_MAX_UPDATE_INTERVAL) {
2159*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
2160*6a54128fSAndroid Build Coastguard Worker 					_("mmp_update_interval too big: %lu\n"),
2161*6a54128fSAndroid Build Coastguard Worker 					intv);
2162*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2163*6a54128fSAndroid Build Coastguard Worker 				continue;
2164*6a54128fSAndroid Build Coastguard Worker 			}
2165*6a54128fSAndroid Build Coastguard Worker 			printf(P_("Setting multiple mount protection update "
2166*6a54128fSAndroid Build Coastguard Worker 				  "interval to %lu second\n",
2167*6a54128fSAndroid Build Coastguard Worker 				  "Setting multiple mount protection update "
2168*6a54128fSAndroid Build Coastguard Worker 				  "interval to %lu seconds\n", intv),
2169*6a54128fSAndroid Build Coastguard Worker 			       intv);
2170*6a54128fSAndroid Build Coastguard Worker 			sb->s_mmp_update_interval = intv;
2171*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
2172*6a54128fSAndroid Build Coastguard Worker 		} else if (!strcmp(token, "force_fsck")) {
2173*6a54128fSAndroid Build Coastguard Worker 			sb->s_state |= EXT2_ERROR_FS;
2174*6a54128fSAndroid Build Coastguard Worker 			printf(_("Setting filesystem error flag to force fsck.\n"));
2175*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
2176*6a54128fSAndroid Build Coastguard Worker 		} else if (!strcmp(token, "test_fs")) {
2177*6a54128fSAndroid Build Coastguard Worker 			sb->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2178*6a54128fSAndroid Build Coastguard Worker 			printf("Setting test filesystem flag\n");
2179*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
2180*6a54128fSAndroid Build Coastguard Worker 		} else if (!strcmp(token, "^test_fs")) {
2181*6a54128fSAndroid Build Coastguard Worker 			sb->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
2182*6a54128fSAndroid Build Coastguard Worker 			printf("Clearing test filesystem flag\n");
2183*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
2184*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "stride") == 0) {
2185*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
2186*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2187*6a54128fSAndroid Build Coastguard Worker 				continue;
2188*6a54128fSAndroid Build Coastguard Worker 			}
2189*6a54128fSAndroid Build Coastguard Worker 			stride = strtoul(arg, &p, 0);
2190*6a54128fSAndroid Build Coastguard Worker 			if (*p) {
2191*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
2192*6a54128fSAndroid Build Coastguard Worker 					_("Invalid RAID stride: %s\n"),
2193*6a54128fSAndroid Build Coastguard Worker 					arg);
2194*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2195*6a54128fSAndroid Build Coastguard Worker 				continue;
2196*6a54128fSAndroid Build Coastguard Worker 			}
2197*6a54128fSAndroid Build Coastguard Worker 			stride_set = 1;
2198*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "stripe-width") == 0 ||
2199*6a54128fSAndroid Build Coastguard Worker 			   strcmp(token, "stripe_width") == 0) {
2200*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
2201*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2202*6a54128fSAndroid Build Coastguard Worker 				continue;
2203*6a54128fSAndroid Build Coastguard Worker 			}
2204*6a54128fSAndroid Build Coastguard Worker 			stripe_width = strtoul(arg, &p, 0);
2205*6a54128fSAndroid Build Coastguard Worker 			if (*p) {
2206*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
2207*6a54128fSAndroid Build Coastguard Worker 					_("Invalid RAID stripe-width: %s\n"),
2208*6a54128fSAndroid Build Coastguard Worker 					arg);
2209*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2210*6a54128fSAndroid Build Coastguard Worker 				continue;
2211*6a54128fSAndroid Build Coastguard Worker 			}
2212*6a54128fSAndroid Build Coastguard Worker 			stripe_width_set = 1;
2213*6a54128fSAndroid Build Coastguard Worker 		} else if (strcmp(token, "hash_alg") == 0 ||
2214*6a54128fSAndroid Build Coastguard Worker 			   strcmp(token, "hash-alg") == 0) {
2215*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
2216*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2217*6a54128fSAndroid Build Coastguard Worker 				continue;
2218*6a54128fSAndroid Build Coastguard Worker 			}
2219*6a54128fSAndroid Build Coastguard Worker 			hash_alg = e2p_string2hash(arg);
2220*6a54128fSAndroid Build Coastguard Worker 			if (hash_alg < 0) {
2221*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
2222*6a54128fSAndroid Build Coastguard Worker 					_("Invalid hash algorithm: %s\n"),
2223*6a54128fSAndroid Build Coastguard Worker 					arg);
2224*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2225*6a54128fSAndroid Build Coastguard Worker 				continue;
2226*6a54128fSAndroid Build Coastguard Worker 			}
2227*6a54128fSAndroid Build Coastguard Worker 			sb->s_def_hash_version = hash_alg;
2228*6a54128fSAndroid Build Coastguard Worker 			printf(_("Setting default hash algorithm "
2229*6a54128fSAndroid Build Coastguard Worker 				 "to %s (%d)\n"),
2230*6a54128fSAndroid Build Coastguard Worker 			       arg, hash_alg);
2231*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
2232*6a54128fSAndroid Build Coastguard Worker 		} else if (!strcmp(token, "mount_opts")) {
2233*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
2234*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2235*6a54128fSAndroid Build Coastguard Worker 				continue;
2236*6a54128fSAndroid Build Coastguard Worker 			}
2237*6a54128fSAndroid Build Coastguard Worker 			if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
2238*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr,
2239*6a54128fSAndroid Build Coastguard Worker 					"Extended mount options too long\n");
2240*6a54128fSAndroid Build Coastguard Worker 				continue;
2241*6a54128fSAndroid Build Coastguard Worker 			}
2242*6a54128fSAndroid Build Coastguard Worker 			ext_mount_opts = strdup(arg);
2243*6a54128fSAndroid Build Coastguard Worker 		} else if (!strcmp(token, "encoding")) {
2244*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
2245*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2246*6a54128fSAndroid Build Coastguard Worker 				continue;
2247*6a54128fSAndroid Build Coastguard Worker 			}
2248*6a54128fSAndroid Build Coastguard Worker 			if (mount_flags & EXT2_MF_MOUNTED) {
2249*6a54128fSAndroid Build Coastguard Worker 				fputs(_("The casefold feature may only be enabled when "
2250*6a54128fSAndroid Build Coastguard Worker 					"the filesystem is unmounted.\n"), stderr);
2251*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2252*6a54128fSAndroid Build Coastguard Worker 				continue;
2253*6a54128fSAndroid Build Coastguard Worker 			}
2254*6a54128fSAndroid Build Coastguard Worker 			if (ext2fs_has_feature_casefold(sb) && !enabling_casefold) {
2255*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr, _("Cannot alter existing encoding\n"));
2256*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2257*6a54128fSAndroid Build Coastguard Worker 				continue;
2258*6a54128fSAndroid Build Coastguard Worker 			}
2259*6a54128fSAndroid Build Coastguard Worker 			encoding = e2p_str2encoding(arg);
2260*6a54128fSAndroid Build Coastguard Worker 			if (encoding < 0) {
2261*6a54128fSAndroid Build Coastguard Worker 				fprintf(stderr, _("Invalid encoding: %s\n"), arg);
2262*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2263*6a54128fSAndroid Build Coastguard Worker 				continue;
2264*6a54128fSAndroid Build Coastguard Worker 			}
2265*6a54128fSAndroid Build Coastguard Worker 			enabling_casefold = 1;
2266*6a54128fSAndroid Build Coastguard Worker 			sb->s_encoding = encoding;
2267*6a54128fSAndroid Build Coastguard Worker 			printf(_("Setting encoding to '%s'\n"), arg);
2268*6a54128fSAndroid Build Coastguard Worker 			sb->s_encoding_flags =
2269*6a54128fSAndroid Build Coastguard Worker 				e2p_get_encoding_flags(sb->s_encoding);
2270*6a54128fSAndroid Build Coastguard Worker 		} else if (!strcmp(token, "encoding_flags")) {
2271*6a54128fSAndroid Build Coastguard Worker 			if (!arg) {
2272*6a54128fSAndroid Build Coastguard Worker 				r_usage++;
2273*6a54128fSAndroid Build Coastguard Worker 				continue;
2274*6a54128fSAndroid Build Coastguard Worker 			}
2275*6a54128fSAndroid Build Coastguard Worker 			encoding_flags = arg;
2276*6a54128fSAndroid Build Coastguard Worker 		} else
2277*6a54128fSAndroid Build Coastguard Worker 			r_usage++;
2278*6a54128fSAndroid Build Coastguard Worker 	}
2279*6a54128fSAndroid Build Coastguard Worker 
2280*6a54128fSAndroid Build Coastguard Worker 	if (encoding > 0 && !r_usage) {
2281*6a54128fSAndroid Build Coastguard Worker 		sb->s_encoding_flags =
2282*6a54128fSAndroid Build Coastguard Worker 			e2p_get_encoding_flags(sb->s_encoding);
2283*6a54128fSAndroid Build Coastguard Worker 
2284*6a54128fSAndroid Build Coastguard Worker 		if (encoding_flags &&
2285*6a54128fSAndroid Build Coastguard Worker 		    e2p_str2encoding_flags(sb->s_encoding, encoding_flags,
2286*6a54128fSAndroid Build Coastguard Worker 					   &sb->s_encoding_flags)) {
2287*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("error: Invalid encoding flag: %s\n"),
2288*6a54128fSAndroid Build Coastguard Worker 					encoding_flags);
2289*6a54128fSAndroid Build Coastguard Worker 			r_usage++;
2290*6a54128fSAndroid Build Coastguard Worker 		} else if (encoding_flags)
2291*6a54128fSAndroid Build Coastguard Worker 			printf(_("Setting encoding_flags to '%s'\n"),
2292*6a54128fSAndroid Build Coastguard Worker 				 encoding_flags);
2293*6a54128fSAndroid Build Coastguard Worker 		ext2fs_set_feature_casefold(sb);
2294*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
2295*6a54128fSAndroid Build Coastguard Worker 	} else if (encoding_flags && !r_usage) {
2296*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("error: An encoding must be explicitly "
2297*6a54128fSAndroid Build Coastguard Worker 				  "specified when passing encoding-flags\n"));
2298*6a54128fSAndroid Build Coastguard Worker 		r_usage++;
2299*6a54128fSAndroid Build Coastguard Worker 	}
2300*6a54128fSAndroid Build Coastguard Worker 	if (r_usage) {
2301*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "%s", _("\nBad options specified.\n\n"
2302*6a54128fSAndroid Build Coastguard Worker 			"Extended options are separated by commas, "
2303*6a54128fSAndroid Build Coastguard Worker 			"and may take an argument which\n"
2304*6a54128fSAndroid Build Coastguard Worker 			"\tis set off by an equals ('=') sign.\n\n"
2305*6a54128fSAndroid Build Coastguard Worker 			"Valid extended options are:\n"
2306*6a54128fSAndroid Build Coastguard Worker 			"\tclear_mmp\n"
2307*6a54128fSAndroid Build Coastguard Worker 			"\thash_alg=<hash algorithm>\n"
2308*6a54128fSAndroid Build Coastguard Worker 			"\tmount_opts=<extended default mount options>\n"
2309*6a54128fSAndroid Build Coastguard Worker 			"\tmmp_update_interval=<mmp update interval in seconds>\n"
2310*6a54128fSAndroid Build Coastguard Worker 			"\tstride=<RAID per-disk chunk size in blocks>\n"
2311*6a54128fSAndroid Build Coastguard Worker 			"\tstripe_width=<RAID stride*data disks in blocks>\n"
2312*6a54128fSAndroid Build Coastguard Worker 			"\tforce_fsck\n"
2313*6a54128fSAndroid Build Coastguard Worker 			"\ttest_fs\n"
2314*6a54128fSAndroid Build Coastguard Worker 			"\t^test_fs\n"
2315*6a54128fSAndroid Build Coastguard Worker 			"\tencoding=<encoding>\n"
2316*6a54128fSAndroid Build Coastguard Worker 			"\tencoding_flags=<flags>\n"));
2317*6a54128fSAndroid Build Coastguard Worker 		free(buf);
2318*6a54128fSAndroid Build Coastguard Worker 		return 1;
2319*6a54128fSAndroid Build Coastguard Worker 	}
2320*6a54128fSAndroid Build Coastguard Worker 	free(buf);
2321*6a54128fSAndroid Build Coastguard Worker 
2322*6a54128fSAndroid Build Coastguard Worker 	return 0;
2323*6a54128fSAndroid Build Coastguard Worker }
2324*6a54128fSAndroid Build Coastguard Worker 
2325*6a54128fSAndroid Build Coastguard Worker /*
2326*6a54128fSAndroid Build Coastguard Worker  * Fill in the block bitmap bmap with the information regarding the
2327*6a54128fSAndroid Build Coastguard Worker  * blocks to be moved
2328*6a54128fSAndroid Build Coastguard Worker  */
get_move_bitmaps(ext2_filsys fs,int new_ino_blks_per_grp,ext2fs_block_bitmap bmap)2329*6a54128fSAndroid Build Coastguard Worker static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
2330*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_block_bitmap bmap)
2331*6a54128fSAndroid Build Coastguard Worker {
2332*6a54128fSAndroid Build Coastguard Worker 	dgrp_t i;
2333*6a54128fSAndroid Build Coastguard Worker 	int retval;
2334*6a54128fSAndroid Build Coastguard Worker 	ext2_badblocks_list bb_list = 0;
2335*6a54128fSAndroid Build Coastguard Worker 	blk64_t j, needed_blocks = 0;
2336*6a54128fSAndroid Build Coastguard Worker 	blk64_t start_blk, end_blk;
2337*6a54128fSAndroid Build Coastguard Worker 
2338*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_bb_inode(fs, &bb_list);
2339*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2340*6a54128fSAndroid Build Coastguard Worker 		return retval;
2341*6a54128fSAndroid Build Coastguard Worker 
2342*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++) {
2343*6a54128fSAndroid Build Coastguard Worker 		start_blk = ext2fs_inode_table_loc(fs, i) +
2344*6a54128fSAndroid Build Coastguard Worker 					fs->inode_blocks_per_group;
2345*6a54128fSAndroid Build Coastguard Worker 
2346*6a54128fSAndroid Build Coastguard Worker 		end_blk = ext2fs_inode_table_loc(fs, i) +
2347*6a54128fSAndroid Build Coastguard Worker 					new_ino_blks_per_grp;
2348*6a54128fSAndroid Build Coastguard Worker 
2349*6a54128fSAndroid Build Coastguard Worker 		for (j = start_blk; j < end_blk; j++) {
2350*6a54128fSAndroid Build Coastguard Worker 			if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
2351*6a54128fSAndroid Build Coastguard Worker 				/*
2352*6a54128fSAndroid Build Coastguard Worker 				 * IF the block is a bad block we fail
2353*6a54128fSAndroid Build Coastguard Worker 				 */
2354*6a54128fSAndroid Build Coastguard Worker 				if (ext2fs_badblocks_list_test(bb_list, j)) {
2355*6a54128fSAndroid Build Coastguard Worker 					ext2fs_badblocks_list_free(bb_list);
2356*6a54128fSAndroid Build Coastguard Worker 					return ENOSPC;
2357*6a54128fSAndroid Build Coastguard Worker 				}
2358*6a54128fSAndroid Build Coastguard Worker 
2359*6a54128fSAndroid Build Coastguard Worker 				ext2fs_mark_block_bitmap2(bmap, j);
2360*6a54128fSAndroid Build Coastguard Worker 			} else {
2361*6a54128fSAndroid Build Coastguard Worker 				/*
2362*6a54128fSAndroid Build Coastguard Worker 				 * We are going to use this block for
2363*6a54128fSAndroid Build Coastguard Worker 				 * inode table. So mark them used.
2364*6a54128fSAndroid Build Coastguard Worker 				 */
2365*6a54128fSAndroid Build Coastguard Worker 				ext2fs_mark_block_bitmap2(fs->block_map, j);
2366*6a54128fSAndroid Build Coastguard Worker 			}
2367*6a54128fSAndroid Build Coastguard Worker 		}
2368*6a54128fSAndroid Build Coastguard Worker 		needed_blocks += end_blk - start_blk;
2369*6a54128fSAndroid Build Coastguard Worker 	}
2370*6a54128fSAndroid Build Coastguard Worker 
2371*6a54128fSAndroid Build Coastguard Worker 	ext2fs_badblocks_list_free(bb_list);
2372*6a54128fSAndroid Build Coastguard Worker 	if (needed_blocks > ext2fs_free_blocks_count(fs->super))
2373*6a54128fSAndroid Build Coastguard Worker 		return ENOSPC;
2374*6a54128fSAndroid Build Coastguard Worker 
2375*6a54128fSAndroid Build Coastguard Worker 	return 0;
2376*6a54128fSAndroid Build Coastguard Worker }
2377*6a54128fSAndroid Build Coastguard Worker 
ext2fs_is_meta_block(ext2_filsys fs,blk64_t blk)2378*6a54128fSAndroid Build Coastguard Worker static int ext2fs_is_meta_block(ext2_filsys fs, blk64_t blk)
2379*6a54128fSAndroid Build Coastguard Worker {
2380*6a54128fSAndroid Build Coastguard Worker 	dgrp_t group;
2381*6a54128fSAndroid Build Coastguard Worker 	group = ext2fs_group_of_blk2(fs, blk);
2382*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_block_bitmap_loc(fs, group) == blk)
2383*6a54128fSAndroid Build Coastguard Worker 		return 1;
2384*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_inode_bitmap_loc(fs, group) == blk)
2385*6a54128fSAndroid Build Coastguard Worker 		return 1;
2386*6a54128fSAndroid Build Coastguard Worker 	return 0;
2387*6a54128fSAndroid Build Coastguard Worker }
2388*6a54128fSAndroid Build Coastguard Worker 
ext2fs_is_block_in_group(ext2_filsys fs,dgrp_t group,blk64_t blk)2389*6a54128fSAndroid Build Coastguard Worker static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
2390*6a54128fSAndroid Build Coastguard Worker {
2391*6a54128fSAndroid Build Coastguard Worker 	blk64_t start_blk, end_blk;
2392*6a54128fSAndroid Build Coastguard Worker 	start_blk = fs->super->s_first_data_block +
2393*6a54128fSAndroid Build Coastguard Worker 			EXT2_GROUPS_TO_BLOCKS(fs->super, group);
2394*6a54128fSAndroid Build Coastguard Worker 	/*
2395*6a54128fSAndroid Build Coastguard Worker 	 * We cannot get new block beyond end_blk for for the last block group
2396*6a54128fSAndroid Build Coastguard Worker 	 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
2397*6a54128fSAndroid Build Coastguard Worker 	 */
2398*6a54128fSAndroid Build Coastguard Worker 	end_blk   = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
2399*6a54128fSAndroid Build Coastguard Worker 	if (blk >= start_blk && blk <= end_blk)
2400*6a54128fSAndroid Build Coastguard Worker 		return 1;
2401*6a54128fSAndroid Build Coastguard Worker 	return 0;
2402*6a54128fSAndroid Build Coastguard Worker }
2403*6a54128fSAndroid Build Coastguard Worker 
move_block(ext2_filsys fs,ext2fs_block_bitmap bmap)2404*6a54128fSAndroid Build Coastguard Worker static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
2405*6a54128fSAndroid Build Coastguard Worker {
2406*6a54128fSAndroid Build Coastguard Worker 
2407*6a54128fSAndroid Build Coastguard Worker 	char *buf;
2408*6a54128fSAndroid Build Coastguard Worker 	dgrp_t group = 0;
2409*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
2410*6a54128fSAndroid Build Coastguard Worker 	int meta_data = 0;
2411*6a54128fSAndroid Build Coastguard Worker 	blk64_t blk, new_blk, goal;
2412*6a54128fSAndroid Build Coastguard Worker 	struct blk_move *bmv;
2413*6a54128fSAndroid Build Coastguard Worker 
2414*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(fs->blocksize, &buf);
2415*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2416*6a54128fSAndroid Build Coastguard Worker 		return retval;
2417*6a54128fSAndroid Build Coastguard Worker 
2418*6a54128fSAndroid Build Coastguard Worker 	for (new_blk = blk = fs->super->s_first_data_block;
2419*6a54128fSAndroid Build Coastguard Worker 	     blk < ext2fs_blocks_count(fs->super); blk++) {
2420*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_test_block_bitmap2(bmap, blk))
2421*6a54128fSAndroid Build Coastguard Worker 			continue;
2422*6a54128fSAndroid Build Coastguard Worker 
2423*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_is_meta_block(fs, blk)) {
2424*6a54128fSAndroid Build Coastguard Worker 			/*
2425*6a54128fSAndroid Build Coastguard Worker 			 * If the block is mapping a fs meta data block
2426*6a54128fSAndroid Build Coastguard Worker 			 * like group desc/block bitmap/inode bitmap. We
2427*6a54128fSAndroid Build Coastguard Worker 			 * should find a block in the same group and fix
2428*6a54128fSAndroid Build Coastguard Worker 			 * the respective fs metadata pointers. Otherwise
2429*6a54128fSAndroid Build Coastguard Worker 			 * fail
2430*6a54128fSAndroid Build Coastguard Worker 			 */
2431*6a54128fSAndroid Build Coastguard Worker 			group = ext2fs_group_of_blk2(fs, blk);
2432*6a54128fSAndroid Build Coastguard Worker 			goal = ext2fs_group_first_block2(fs, group);
2433*6a54128fSAndroid Build Coastguard Worker 			meta_data = 1;
2434*6a54128fSAndroid Build Coastguard Worker 
2435*6a54128fSAndroid Build Coastguard Worker 		} else {
2436*6a54128fSAndroid Build Coastguard Worker 			goal = new_blk;
2437*6a54128fSAndroid Build Coastguard Worker 		}
2438*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
2439*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2440*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2441*6a54128fSAndroid Build Coastguard Worker 
2442*6a54128fSAndroid Build Coastguard Worker 		/* new fs meta data block should be in the same group */
2443*6a54128fSAndroid Build Coastguard Worker 		if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
2444*6a54128fSAndroid Build Coastguard Worker 			retval = ENOSPC;
2445*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2446*6a54128fSAndroid Build Coastguard Worker 		}
2447*6a54128fSAndroid Build Coastguard Worker 
2448*6a54128fSAndroid Build Coastguard Worker 		/* Mark this block as allocated */
2449*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
2450*6a54128fSAndroid Build Coastguard Worker 
2451*6a54128fSAndroid Build Coastguard Worker 		/* Add it to block move list */
2452*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
2453*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2454*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2455*6a54128fSAndroid Build Coastguard Worker 
2456*6a54128fSAndroid Build Coastguard Worker 		bmv->old_loc = blk;
2457*6a54128fSAndroid Build Coastguard Worker 		bmv->new_loc = new_blk;
2458*6a54128fSAndroid Build Coastguard Worker 
2459*6a54128fSAndroid Build Coastguard Worker 		list_add(&(bmv->list), &blk_move_list);
2460*6a54128fSAndroid Build Coastguard Worker 
2461*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(fs->io, blk, 1, buf);
2462*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2463*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2464*6a54128fSAndroid Build Coastguard Worker 
2465*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
2466*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2467*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2468*6a54128fSAndroid Build Coastguard Worker 	}
2469*6a54128fSAndroid Build Coastguard Worker 
2470*6a54128fSAndroid Build Coastguard Worker err_out:
2471*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&buf);
2472*6a54128fSAndroid Build Coastguard Worker 	return retval;
2473*6a54128fSAndroid Build Coastguard Worker }
2474*6a54128fSAndroid Build Coastguard Worker 
translate_block(blk64_t blk)2475*6a54128fSAndroid Build Coastguard Worker static blk64_t translate_block(blk64_t blk)
2476*6a54128fSAndroid Build Coastguard Worker {
2477*6a54128fSAndroid Build Coastguard Worker 	struct list_head *entry;
2478*6a54128fSAndroid Build Coastguard Worker 	struct blk_move *bmv;
2479*6a54128fSAndroid Build Coastguard Worker 
2480*6a54128fSAndroid Build Coastguard Worker 	list_for_each(entry, &blk_move_list) {
2481*6a54128fSAndroid Build Coastguard Worker 		bmv = list_entry(entry, struct blk_move, list);
2482*6a54128fSAndroid Build Coastguard Worker 		if (bmv->old_loc == blk)
2483*6a54128fSAndroid Build Coastguard Worker 			return bmv->new_loc;
2484*6a54128fSAndroid Build Coastguard Worker 	}
2485*6a54128fSAndroid Build Coastguard Worker 
2486*6a54128fSAndroid Build Coastguard Worker 	return 0;
2487*6a54128fSAndroid Build Coastguard Worker }
2488*6a54128fSAndroid Build Coastguard Worker 
process_block(ext2_filsys fs EXT2FS_ATTR ((unused)),blk64_t * block_nr,e2_blkcnt_t blockcnt EXT2FS_ATTR ((unused)),blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)2489*6a54128fSAndroid Build Coastguard Worker static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
2490*6a54128fSAndroid Build Coastguard Worker 			 blk64_t *block_nr,
2491*6a54128fSAndroid Build Coastguard Worker 			 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
2492*6a54128fSAndroid Build Coastguard Worker 			 blk64_t ref_block EXT2FS_ATTR((unused)),
2493*6a54128fSAndroid Build Coastguard Worker 			 int ref_offset EXT2FS_ATTR((unused)),
2494*6a54128fSAndroid Build Coastguard Worker 			 void *priv_data)
2495*6a54128fSAndroid Build Coastguard Worker {
2496*6a54128fSAndroid Build Coastguard Worker 	int ret = 0;
2497*6a54128fSAndroid Build Coastguard Worker 	blk64_t new_blk;
2498*6a54128fSAndroid Build Coastguard Worker 	ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
2499*6a54128fSAndroid Build Coastguard Worker 
2500*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
2501*6a54128fSAndroid Build Coastguard Worker 		return 0;
2502*6a54128fSAndroid Build Coastguard Worker 	new_blk = translate_block(*block_nr);
2503*6a54128fSAndroid Build Coastguard Worker 	if (new_blk) {
2504*6a54128fSAndroid Build Coastguard Worker 		*block_nr = new_blk;
2505*6a54128fSAndroid Build Coastguard Worker 		/*
2506*6a54128fSAndroid Build Coastguard Worker 		 * This will force the ext2fs_write_inode in the iterator
2507*6a54128fSAndroid Build Coastguard Worker 		 */
2508*6a54128fSAndroid Build Coastguard Worker 		ret |= BLOCK_CHANGED;
2509*6a54128fSAndroid Build Coastguard Worker 	}
2510*6a54128fSAndroid Build Coastguard Worker 
2511*6a54128fSAndroid Build Coastguard Worker 	return ret;
2512*6a54128fSAndroid Build Coastguard Worker }
2513*6a54128fSAndroid Build Coastguard Worker 
inode_scan_and_fix(ext2_filsys fs,ext2fs_block_bitmap bmap)2514*6a54128fSAndroid Build Coastguard Worker static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2515*6a54128fSAndroid Build Coastguard Worker {
2516*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = 0;
2517*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t ino;
2518*6a54128fSAndroid Build Coastguard Worker 	blk64_t blk;
2519*6a54128fSAndroid Build Coastguard Worker 	char *block_buf = 0;
2520*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode inode;
2521*6a54128fSAndroid Build Coastguard Worker 	ext2_inode_scan	scan = NULL;
2522*6a54128fSAndroid Build Coastguard Worker 
2523*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
2524*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2525*6a54128fSAndroid Build Coastguard Worker 		return retval;
2526*6a54128fSAndroid Build Coastguard Worker 
2527*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_open_inode_scan(fs, 0, &scan);
2528*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2529*6a54128fSAndroid Build Coastguard Worker 		goto err_out;
2530*6a54128fSAndroid Build Coastguard Worker 
2531*6a54128fSAndroid Build Coastguard Worker 	while (1) {
2532*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_get_next_inode(scan, &ino, &inode);
2533*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2534*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2535*6a54128fSAndroid Build Coastguard Worker 
2536*6a54128fSAndroid Build Coastguard Worker 		if (!ino)
2537*6a54128fSAndroid Build Coastguard Worker 			break;
2538*6a54128fSAndroid Build Coastguard Worker 
2539*6a54128fSAndroid Build Coastguard Worker 		if (inode.i_links_count == 0)
2540*6a54128fSAndroid Build Coastguard Worker 			continue; /* inode not in use */
2541*6a54128fSAndroid Build Coastguard Worker 
2542*6a54128fSAndroid Build Coastguard Worker 		/* FIXME!!
2543*6a54128fSAndroid Build Coastguard Worker 		 * If we end up modifying the journal inode
2544*6a54128fSAndroid Build Coastguard Worker 		 * the sb->s_jnl_blocks will differ. But a
2545*6a54128fSAndroid Build Coastguard Worker 		 * subsequent e2fsck fixes that.
2546*6a54128fSAndroid Build Coastguard Worker 		 * Do we need to fix this ??
2547*6a54128fSAndroid Build Coastguard Worker 		 */
2548*6a54128fSAndroid Build Coastguard Worker 
2549*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_file_acl_block(fs, &inode) &&
2550*6a54128fSAndroid Build Coastguard Worker 		    ext2fs_test_block_bitmap2(bmap,
2551*6a54128fSAndroid Build Coastguard Worker 					ext2fs_file_acl_block(fs, &inode))) {
2552*6a54128fSAndroid Build Coastguard Worker 			blk = translate_block(ext2fs_file_acl_block(fs,
2553*6a54128fSAndroid Build Coastguard Worker 								    &inode));
2554*6a54128fSAndroid Build Coastguard Worker 			if (!blk)
2555*6a54128fSAndroid Build Coastguard Worker 				continue;
2556*6a54128fSAndroid Build Coastguard Worker 
2557*6a54128fSAndroid Build Coastguard Worker 			ext2fs_file_acl_block_set(fs, &inode, blk);
2558*6a54128fSAndroid Build Coastguard Worker 
2559*6a54128fSAndroid Build Coastguard Worker 			/*
2560*6a54128fSAndroid Build Coastguard Worker 			 * Write the inode to disk so that inode table
2561*6a54128fSAndroid Build Coastguard Worker 			 * resizing can work
2562*6a54128fSAndroid Build Coastguard Worker 			 */
2563*6a54128fSAndroid Build Coastguard Worker 			retval = ext2fs_write_inode(fs, ino, &inode);
2564*6a54128fSAndroid Build Coastguard Worker 			if (retval)
2565*6a54128fSAndroid Build Coastguard Worker 				goto err_out;
2566*6a54128fSAndroid Build Coastguard Worker 		}
2567*6a54128fSAndroid Build Coastguard Worker 
2568*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
2569*6a54128fSAndroid Build Coastguard Worker 			continue;
2570*6a54128fSAndroid Build Coastguard Worker 
2571*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
2572*6a54128fSAndroid Build Coastguard Worker 					       process_block, bmap);
2573*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2574*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2575*6a54128fSAndroid Build Coastguard Worker 
2576*6a54128fSAndroid Build Coastguard Worker 	}
2577*6a54128fSAndroid Build Coastguard Worker 
2578*6a54128fSAndroid Build Coastguard Worker err_out:
2579*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&block_buf);
2580*6a54128fSAndroid Build Coastguard Worker 	ext2fs_close_inode_scan(scan);
2581*6a54128fSAndroid Build Coastguard Worker 
2582*6a54128fSAndroid Build Coastguard Worker 	return retval;
2583*6a54128fSAndroid Build Coastguard Worker }
2584*6a54128fSAndroid Build Coastguard Worker 
2585*6a54128fSAndroid Build Coastguard Worker /*
2586*6a54128fSAndroid Build Coastguard Worker  * We need to scan for inode and block bitmaps that may need to be
2587*6a54128fSAndroid Build Coastguard Worker  * moved.  This can take place if the filesystem was formatted for
2588*6a54128fSAndroid Build Coastguard Worker  * RAID arrays using the mke2fs's extended option "stride".
2589*6a54128fSAndroid Build Coastguard Worker  */
group_desc_scan_and_fix(ext2_filsys fs,ext2fs_block_bitmap bmap)2590*6a54128fSAndroid Build Coastguard Worker static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
2591*6a54128fSAndroid Build Coastguard Worker {
2592*6a54128fSAndroid Build Coastguard Worker 	dgrp_t i;
2593*6a54128fSAndroid Build Coastguard Worker 	blk64_t blk, new_blk;
2594*6a54128fSAndroid Build Coastguard Worker 
2595*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++) {
2596*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_block_bitmap_loc(fs, i);
2597*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_test_block_bitmap2(bmap, blk)) {
2598*6a54128fSAndroid Build Coastguard Worker 			new_blk = translate_block(blk);
2599*6a54128fSAndroid Build Coastguard Worker 			if (!new_blk)
2600*6a54128fSAndroid Build Coastguard Worker 				continue;
2601*6a54128fSAndroid Build Coastguard Worker 			ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2602*6a54128fSAndroid Build Coastguard Worker 		}
2603*6a54128fSAndroid Build Coastguard Worker 
2604*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_inode_bitmap_loc(fs, i);
2605*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_test_block_bitmap2(bmap, blk)) {
2606*6a54128fSAndroid Build Coastguard Worker 			new_blk = translate_block(blk);
2607*6a54128fSAndroid Build Coastguard Worker 			if (!new_blk)
2608*6a54128fSAndroid Build Coastguard Worker 				continue;
2609*6a54128fSAndroid Build Coastguard Worker 			ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2610*6a54128fSAndroid Build Coastguard Worker 		}
2611*6a54128fSAndroid Build Coastguard Worker 	}
2612*6a54128fSAndroid Build Coastguard Worker 	return 0;
2613*6a54128fSAndroid Build Coastguard Worker }
2614*6a54128fSAndroid Build Coastguard Worker 
expand_inode_table(ext2_filsys fs,unsigned long new_ino_size)2615*6a54128fSAndroid Build Coastguard Worker static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
2616*6a54128fSAndroid Build Coastguard Worker {
2617*6a54128fSAndroid Build Coastguard Worker 	dgrp_t i;
2618*6a54128fSAndroid Build Coastguard Worker 	blk64_t blk;
2619*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
2620*6a54128fSAndroid Build Coastguard Worker 	int new_ino_blks_per_grp;
2621*6a54128fSAndroid Build Coastguard Worker 	unsigned int j;
2622*6a54128fSAndroid Build Coastguard Worker 	char *old_itable = NULL, *new_itable = NULL;
2623*6a54128fSAndroid Build Coastguard Worker 	char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
2624*6a54128fSAndroid Build Coastguard Worker 	unsigned long old_ino_size;
2625*6a54128fSAndroid Build Coastguard Worker 	int old_itable_size, new_itable_size;
2626*6a54128fSAndroid Build Coastguard Worker 
2627*6a54128fSAndroid Build Coastguard Worker 	old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
2628*6a54128fSAndroid Build Coastguard Worker 	old_ino_size = EXT2_INODE_SIZE(fs->super);
2629*6a54128fSAndroid Build Coastguard Worker 
2630*6a54128fSAndroid Build Coastguard Worker 	new_ino_blks_per_grp = ext2fs_div_ceil(
2631*6a54128fSAndroid Build Coastguard Worker 					EXT2_INODES_PER_GROUP(fs->super) *
2632*6a54128fSAndroid Build Coastguard Worker 					new_ino_size,
2633*6a54128fSAndroid Build Coastguard Worker 					fs->blocksize);
2634*6a54128fSAndroid Build Coastguard Worker 
2635*6a54128fSAndroid Build Coastguard Worker 	new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2636*6a54128fSAndroid Build Coastguard Worker 
2637*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(old_itable_size, &old_itable);
2638*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2639*6a54128fSAndroid Build Coastguard Worker 		return retval;
2640*6a54128fSAndroid Build Coastguard Worker 
2641*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_mem(new_itable_size, &new_itable);
2642*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2643*6a54128fSAndroid Build Coastguard Worker 		goto err_out;
2644*6a54128fSAndroid Build Coastguard Worker 
2645*6a54128fSAndroid Build Coastguard Worker 	tmp_old_itable = old_itable;
2646*6a54128fSAndroid Build Coastguard Worker 	tmp_new_itable = new_itable;
2647*6a54128fSAndroid Build Coastguard Worker 
2648*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++) {
2649*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_inode_table_loc(fs, i);
2650*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk64(fs->io, blk,
2651*6a54128fSAndroid Build Coastguard Worker 				fs->inode_blocks_per_group, old_itable);
2652*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2653*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2654*6a54128fSAndroid Build Coastguard Worker 
2655*6a54128fSAndroid Build Coastguard Worker 		for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2656*6a54128fSAndroid Build Coastguard Worker 			memcpy(new_itable, old_itable, old_ino_size);
2657*6a54128fSAndroid Build Coastguard Worker 
2658*6a54128fSAndroid Build Coastguard Worker 			memset(new_itable+old_ino_size, 0,
2659*6a54128fSAndroid Build Coastguard Worker 					new_ino_size - old_ino_size);
2660*6a54128fSAndroid Build Coastguard Worker 
2661*6a54128fSAndroid Build Coastguard Worker 			new_itable += new_ino_size;
2662*6a54128fSAndroid Build Coastguard Worker 			old_itable += old_ino_size;
2663*6a54128fSAndroid Build Coastguard Worker 		}
2664*6a54128fSAndroid Build Coastguard Worker 
2665*6a54128fSAndroid Build Coastguard Worker 		/* reset the pointer */
2666*6a54128fSAndroid Build Coastguard Worker 		old_itable = tmp_old_itable;
2667*6a54128fSAndroid Build Coastguard Worker 		new_itable = tmp_new_itable;
2668*6a54128fSAndroid Build Coastguard Worker 
2669*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_write_blk64(fs->io, blk,
2670*6a54128fSAndroid Build Coastguard Worker 					new_ino_blks_per_grp, new_itable);
2671*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2672*6a54128fSAndroid Build Coastguard Worker 			goto err_out;
2673*6a54128fSAndroid Build Coastguard Worker 	}
2674*6a54128fSAndroid Build Coastguard Worker 
2675*6a54128fSAndroid Build Coastguard Worker 	/* Update the meta data */
2676*6a54128fSAndroid Build Coastguard Worker 	fs->inode_blocks_per_group = new_ino_blks_per_grp;
2677*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_inode_cache(fs->icache);
2678*6a54128fSAndroid Build Coastguard Worker 	fs->icache = 0;
2679*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_inode_size = new_ino_size;
2680*6a54128fSAndroid Build Coastguard Worker 
2681*6a54128fSAndroid Build Coastguard Worker err_out:
2682*6a54128fSAndroid Build Coastguard Worker 	if (old_itable)
2683*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&old_itable);
2684*6a54128fSAndroid Build Coastguard Worker 
2685*6a54128fSAndroid Build Coastguard Worker 	if (new_itable)
2686*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&new_itable);
2687*6a54128fSAndroid Build Coastguard Worker 
2688*6a54128fSAndroid Build Coastguard Worker 	return retval;
2689*6a54128fSAndroid Build Coastguard Worker }
2690*6a54128fSAndroid Build Coastguard Worker 
2691*6a54128fSAndroid Build Coastguard Worker 
2692*6a54128fSAndroid Build Coastguard Worker #define list_for_each_safe(pos, pnext, head) \
2693*6a54128fSAndroid Build Coastguard Worker 	for (pos = (head)->next, pnext = pos->next; pos != (head); \
2694*6a54128fSAndroid Build Coastguard Worker 	     pos = pnext, pnext = pos->next)
2695*6a54128fSAndroid Build Coastguard Worker 
free_blk_move_list(void)2696*6a54128fSAndroid Build Coastguard Worker static void free_blk_move_list(void)
2697*6a54128fSAndroid Build Coastguard Worker {
2698*6a54128fSAndroid Build Coastguard Worker 	struct list_head *entry, *tmp;
2699*6a54128fSAndroid Build Coastguard Worker 	struct blk_move *bmv;
2700*6a54128fSAndroid Build Coastguard Worker 
2701*6a54128fSAndroid Build Coastguard Worker 	list_for_each_safe(entry, tmp, &blk_move_list) {
2702*6a54128fSAndroid Build Coastguard Worker 		bmv = list_entry(entry, struct blk_move, list);
2703*6a54128fSAndroid Build Coastguard Worker 		list_del(entry);
2704*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&bmv);
2705*6a54128fSAndroid Build Coastguard Worker 	}
2706*6a54128fSAndroid Build Coastguard Worker 	return;
2707*6a54128fSAndroid Build Coastguard Worker }
2708*6a54128fSAndroid Build Coastguard Worker 
resize_inode(ext2_filsys fs,unsigned long new_size)2709*6a54128fSAndroid Build Coastguard Worker static int resize_inode(ext2_filsys fs, unsigned long new_size)
2710*6a54128fSAndroid Build Coastguard Worker {
2711*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
2712*6a54128fSAndroid Build Coastguard Worker 	int new_ino_blks_per_grp;
2713*6a54128fSAndroid Build Coastguard Worker 	ext2fs_block_bitmap bmap;
2714*6a54128fSAndroid Build Coastguard Worker 
2715*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_inode_bitmap(fs);
2716*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
2717*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Failed to read inode bitmap\n"), stderr);
2718*6a54128fSAndroid Build Coastguard Worker 		return retval;
2719*6a54128fSAndroid Build Coastguard Worker 	}
2720*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_block_bitmap(fs);
2721*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
2722*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Failed to read block bitmap\n"), stderr);
2723*6a54128fSAndroid Build Coastguard Worker 		return retval;
2724*6a54128fSAndroid Build Coastguard Worker 	}
2725*6a54128fSAndroid Build Coastguard Worker 	INIT_LIST_HEAD(&blk_move_list);
2726*6a54128fSAndroid Build Coastguard Worker 
2727*6a54128fSAndroid Build Coastguard Worker 
2728*6a54128fSAndroid Build Coastguard Worker 	new_ino_blks_per_grp = ext2fs_div_ceil(
2729*6a54128fSAndroid Build Coastguard Worker 					EXT2_INODES_PER_GROUP(fs->super)*
2730*6a54128fSAndroid Build Coastguard Worker 					new_size,
2731*6a54128fSAndroid Build Coastguard Worker 					fs->blocksize);
2732*6a54128fSAndroid Build Coastguard Worker 
2733*6a54128fSAndroid Build Coastguard Worker 	/* We may change the file system.
2734*6a54128fSAndroid Build Coastguard Worker 	 * Mark the file system as invalid so that
2735*6a54128fSAndroid Build Coastguard Worker 	 * the user is prompted to run fsck.
2736*6a54128fSAndroid Build Coastguard Worker 	 */
2737*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_state &= ~EXT2_VALID_FS;
2738*6a54128fSAndroid Build Coastguard Worker 
2739*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2740*6a54128fSAndroid Build Coastguard Worker 						&bmap);
2741*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
2742*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Failed to allocate block bitmap when "
2743*6a54128fSAndroid Build Coastguard Worker 				"increasing inode size\n"), stderr);
2744*6a54128fSAndroid Build Coastguard Worker 		return retval;
2745*6a54128fSAndroid Build Coastguard Worker 	}
2746*6a54128fSAndroid Build Coastguard Worker 	retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2747*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
2748*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Not enough space to increase inode size \n"), stderr);
2749*6a54128fSAndroid Build Coastguard Worker 		goto err_out;
2750*6a54128fSAndroid Build Coastguard Worker 	}
2751*6a54128fSAndroid Build Coastguard Worker 	retval = move_block(fs, bmap);
2752*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
2753*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Failed to relocate blocks during inode resize \n"),
2754*6a54128fSAndroid Build Coastguard Worker 		      stderr);
2755*6a54128fSAndroid Build Coastguard Worker 		goto err_out;
2756*6a54128fSAndroid Build Coastguard Worker 	}
2757*6a54128fSAndroid Build Coastguard Worker 	retval = inode_scan_and_fix(fs, bmap);
2758*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2759*6a54128fSAndroid Build Coastguard Worker 		goto err_out_undo;
2760*6a54128fSAndroid Build Coastguard Worker 
2761*6a54128fSAndroid Build Coastguard Worker 	retval = group_desc_scan_and_fix(fs, bmap);
2762*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2763*6a54128fSAndroid Build Coastguard Worker 		goto err_out_undo;
2764*6a54128fSAndroid Build Coastguard Worker 
2765*6a54128fSAndroid Build Coastguard Worker 	retval = expand_inode_table(fs, new_size);
2766*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2767*6a54128fSAndroid Build Coastguard Worker 		goto err_out_undo;
2768*6a54128fSAndroid Build Coastguard Worker 
2769*6a54128fSAndroid Build Coastguard Worker 	ext2fs_calculate_summary_stats(fs, 1 /* super only */);
2770*6a54128fSAndroid Build Coastguard Worker 
2771*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_state |= EXT2_VALID_FS;
2772*6a54128fSAndroid Build Coastguard Worker 	/* mark super block and block bitmap as dirty */
2773*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
2774*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_bb_dirty(fs);
2775*6a54128fSAndroid Build Coastguard Worker 
2776*6a54128fSAndroid Build Coastguard Worker err_out:
2777*6a54128fSAndroid Build Coastguard Worker 	free_blk_move_list();
2778*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_block_bitmap(bmap);
2779*6a54128fSAndroid Build Coastguard Worker 
2780*6a54128fSAndroid Build Coastguard Worker 	return retval;
2781*6a54128fSAndroid Build Coastguard Worker 
2782*6a54128fSAndroid Build Coastguard Worker err_out_undo:
2783*6a54128fSAndroid Build Coastguard Worker 	free_blk_move_list();
2784*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_block_bitmap(bmap);
2785*6a54128fSAndroid Build Coastguard Worker 	fputs(_("Error in resizing the inode size.\n"
2786*6a54128fSAndroid Build Coastguard Worker 			"Run e2undo to undo the "
2787*6a54128fSAndroid Build Coastguard Worker 			"file system changes. \n"), stderr);
2788*6a54128fSAndroid Build Coastguard Worker 
2789*6a54128fSAndroid Build Coastguard Worker 	return retval;
2790*6a54128fSAndroid Build Coastguard Worker }
2791*6a54128fSAndroid Build Coastguard Worker 
tune2fs_setup_tdb(const char * name,io_manager * io_ptr)2792*6a54128fSAndroid Build Coastguard Worker static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2793*6a54128fSAndroid Build Coastguard Worker {
2794*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = 0;
2795*6a54128fSAndroid Build Coastguard Worker 	const char *tdb_dir;
2796*6a54128fSAndroid Build Coastguard Worker 	char *tdb_file = NULL;
2797*6a54128fSAndroid Build Coastguard Worker 	char *dev_name, *tmp_name;
2798*6a54128fSAndroid Build Coastguard Worker 
2799*6a54128fSAndroid Build Coastguard Worker 	/* (re)open a specific undo file */
2800*6a54128fSAndroid Build Coastguard Worker 	if (undo_file && undo_file[0] != 0) {
2801*6a54128fSAndroid Build Coastguard Worker 		retval = set_undo_io_backing_manager(*io_ptr);
2802*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2803*6a54128fSAndroid Build Coastguard Worker 			goto err;
2804*6a54128fSAndroid Build Coastguard Worker 		*io_ptr = undo_io_manager;
2805*6a54128fSAndroid Build Coastguard Worker 		retval = set_undo_io_backup_file(undo_file);
2806*6a54128fSAndroid Build Coastguard Worker 		if (retval)
2807*6a54128fSAndroid Build Coastguard Worker 			goto err;
2808*6a54128fSAndroid Build Coastguard Worker 		printf(_("Overwriting existing filesystem; this can be undone "
2809*6a54128fSAndroid Build Coastguard Worker 			 "using the command:\n"
2810*6a54128fSAndroid Build Coastguard Worker 			 "    e2undo %s %s\n\n"),
2811*6a54128fSAndroid Build Coastguard Worker 			 undo_file, name);
2812*6a54128fSAndroid Build Coastguard Worker 		return retval;
2813*6a54128fSAndroid Build Coastguard Worker 	}
2814*6a54128fSAndroid Build Coastguard Worker 
2815*6a54128fSAndroid Build Coastguard Worker 	/*
2816*6a54128fSAndroid Build Coastguard Worker 	 * Configuration via a conf file would be
2817*6a54128fSAndroid Build Coastguard Worker 	 * nice
2818*6a54128fSAndroid Build Coastguard Worker 	 */
2819*6a54128fSAndroid Build Coastguard Worker 	tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2820*6a54128fSAndroid Build Coastguard Worker 	if (!tdb_dir)
2821*6a54128fSAndroid Build Coastguard Worker 		tdb_dir = "/var/lib/e2fsprogs";
2822*6a54128fSAndroid Build Coastguard Worker 
2823*6a54128fSAndroid Build Coastguard Worker 	if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2824*6a54128fSAndroid Build Coastguard Worker 	    access(tdb_dir, W_OK))
2825*6a54128fSAndroid Build Coastguard Worker 		return 0;
2826*6a54128fSAndroid Build Coastguard Worker 
2827*6a54128fSAndroid Build Coastguard Worker 	tmp_name = strdup(name);
2828*6a54128fSAndroid Build Coastguard Worker 	if (!tmp_name)
2829*6a54128fSAndroid Build Coastguard Worker 		goto errout;
2830*6a54128fSAndroid Build Coastguard Worker 	dev_name = basename(tmp_name);
2831*6a54128fSAndroid Build Coastguard Worker 	tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2832*6a54128fSAndroid Build Coastguard Worker 	if (!tdb_file) {
2833*6a54128fSAndroid Build Coastguard Worker 		free(tmp_name);
2834*6a54128fSAndroid Build Coastguard Worker 		goto errout;
2835*6a54128fSAndroid Build Coastguard Worker 	}
2836*6a54128fSAndroid Build Coastguard Worker 	sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2837*6a54128fSAndroid Build Coastguard Worker 	free(tmp_name);
2838*6a54128fSAndroid Build Coastguard Worker 
2839*6a54128fSAndroid Build Coastguard Worker 	if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2840*6a54128fSAndroid Build Coastguard Worker 		retval = errno;
2841*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval,
2842*6a54128fSAndroid Build Coastguard Worker 			_("while trying to delete %s"), tdb_file);
2843*6a54128fSAndroid Build Coastguard Worker 		goto errout;
2844*6a54128fSAndroid Build Coastguard Worker 	}
2845*6a54128fSAndroid Build Coastguard Worker 
2846*6a54128fSAndroid Build Coastguard Worker 	retval = set_undo_io_backing_manager(*io_ptr);
2847*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2848*6a54128fSAndroid Build Coastguard Worker 		goto errout;
2849*6a54128fSAndroid Build Coastguard Worker 	*io_ptr = undo_io_manager;
2850*6a54128fSAndroid Build Coastguard Worker 	retval = set_undo_io_backup_file(tdb_file);
2851*6a54128fSAndroid Build Coastguard Worker 	if (retval)
2852*6a54128fSAndroid Build Coastguard Worker 		goto errout;
2853*6a54128fSAndroid Build Coastguard Worker 	printf(_("Overwriting existing filesystem; this can be undone "
2854*6a54128fSAndroid Build Coastguard Worker 		 "using the command:\n"
2855*6a54128fSAndroid Build Coastguard Worker 		 "    e2undo %s %s\n\n"),
2856*6a54128fSAndroid Build Coastguard Worker 		 tdb_file, name);
2857*6a54128fSAndroid Build Coastguard Worker 
2858*6a54128fSAndroid Build Coastguard Worker 	free(tdb_file);
2859*6a54128fSAndroid Build Coastguard Worker 	return 0;
2860*6a54128fSAndroid Build Coastguard Worker errout:
2861*6a54128fSAndroid Build Coastguard Worker 	free(tdb_file);
2862*6a54128fSAndroid Build Coastguard Worker err:
2863*6a54128fSAndroid Build Coastguard Worker 	com_err("tune2fs", retval, "while trying to setup undo file\n");
2864*6a54128fSAndroid Build Coastguard Worker 	return retval;
2865*6a54128fSAndroid Build Coastguard Worker }
2866*6a54128fSAndroid Build Coastguard Worker 
2867*6a54128fSAndroid Build Coastguard Worker static int
fs_update_journal_user(struct ext2_super_block * sb,__u8 old_uuid[UUID_SIZE])2868*6a54128fSAndroid Build Coastguard Worker fs_update_journal_user(struct ext2_super_block *sb, __u8 old_uuid[UUID_SIZE])
2869*6a54128fSAndroid Build Coastguard Worker {
2870*6a54128fSAndroid Build Coastguard Worker 	int retval, nr_users, start;
2871*6a54128fSAndroid Build Coastguard Worker 	journal_superblock_t *jsb;
2872*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys jfs;
2873*6a54128fSAndroid Build Coastguard Worker 	__u8 *j_uuid;
2874*6a54128fSAndroid Build Coastguard Worker 	char *journal_path;
2875*6a54128fSAndroid Build Coastguard Worker 	char uuid[UUID_STR_SIZE];
2876*6a54128fSAndroid Build Coastguard Worker 	char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
2877*6a54128fSAndroid Build Coastguard Worker 
2878*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_journal(sb) || uuid_is_null(sb->s_journal_uuid))
2879*6a54128fSAndroid Build Coastguard Worker 		return 0;
2880*6a54128fSAndroid Build Coastguard Worker 
2881*6a54128fSAndroid Build Coastguard Worker 	uuid_unparse(sb->s_journal_uuid, uuid);
2882*6a54128fSAndroid Build Coastguard Worker 	journal_path = blkid_get_devname(NULL, "UUID", uuid);
2883*6a54128fSAndroid Build Coastguard Worker 	if (!journal_path)
2884*6a54128fSAndroid Build Coastguard Worker 		return 0;
2885*6a54128fSAndroid Build Coastguard Worker 
2886*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_open2(journal_path, io_options,
2887*6a54128fSAndroid Build Coastguard Worker 			      EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_RW,
2888*6a54128fSAndroid Build Coastguard Worker 			      0, 0, unix_io_manager, &jfs);
2889*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
2890*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval,
2891*6a54128fSAndroid Build Coastguard Worker 			_("while trying to open %s"),
2892*6a54128fSAndroid Build Coastguard Worker 			journal_path);
2893*6a54128fSAndroid Build Coastguard Worker 		return retval;
2894*6a54128fSAndroid Build Coastguard Worker 	}
2895*6a54128fSAndroid Build Coastguard Worker 
2896*6a54128fSAndroid Build Coastguard Worker 	retval = get_journal_sb(jfs, buf);
2897*6a54128fSAndroid Build Coastguard Worker 	if (retval != 0) {
2898*6a54128fSAndroid Build Coastguard Worker 		if (retval == EXT2_ET_UNSUPP_FEATURE)
2899*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("%s is not a journal device.\n"),
2900*6a54128fSAndroid Build Coastguard Worker 				journal_path);
2901*6a54128fSAndroid Build Coastguard Worker 		return retval;
2902*6a54128fSAndroid Build Coastguard Worker 	}
2903*6a54128fSAndroid Build Coastguard Worker 
2904*6a54128fSAndroid Build Coastguard Worker 	jsb = (journal_superblock_t *) buf;
2905*6a54128fSAndroid Build Coastguard Worker 	/* Find the filesystem UUID */
2906*6a54128fSAndroid Build Coastguard Worker 	nr_users = ntohl(jsb->s_nr_users);
2907*6a54128fSAndroid Build Coastguard Worker 	if (nr_users > JBD2_USERS_MAX) {
2908*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, _("Journal superblock is corrupted, nr_users\n"
2909*6a54128fSAndroid Build Coastguard Worker 				 "is too high (%d).\n"), nr_users);
2910*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_CORRUPT_JOURNAL_SB;
2911*6a54128fSAndroid Build Coastguard Worker 	}
2912*6a54128fSAndroid Build Coastguard Worker 
2913*6a54128fSAndroid Build Coastguard Worker 	j_uuid = journal_user(old_uuid, jsb->s_users, nr_users);
2914*6a54128fSAndroid Build Coastguard Worker 	if (j_uuid == NULL) {
2915*6a54128fSAndroid Build Coastguard Worker 		fputs(_("Filesystem's UUID not found on journal device.\n"),
2916*6a54128fSAndroid Build Coastguard Worker 		      stderr);
2917*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_LOAD_EXT_JOURNAL;
2918*6a54128fSAndroid Build Coastguard Worker 	}
2919*6a54128fSAndroid Build Coastguard Worker 
2920*6a54128fSAndroid Build Coastguard Worker 	memcpy(j_uuid, sb->s_uuid, UUID_SIZE);
2921*6a54128fSAndroid Build Coastguard Worker 
2922*6a54128fSAndroid Build Coastguard Worker 	start = ext2fs_journal_sb_start(jfs->blocksize);
2923*6a54128fSAndroid Build Coastguard Worker 	/* Write back the journal superblock */
2924*6a54128fSAndroid Build Coastguard Worker 	retval = io_channel_write_blk64(jfs->io, start, -SUPERBLOCK_SIZE, buf);
2925*6a54128fSAndroid Build Coastguard Worker 	if (retval != 0) {
2926*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval,
2927*6a54128fSAndroid Build Coastguard Worker 			"while writing journal superblock.");
2928*6a54128fSAndroid Build Coastguard Worker 		return retval;
2929*6a54128fSAndroid Build Coastguard Worker 	}
2930*6a54128fSAndroid Build Coastguard Worker 
2931*6a54128fSAndroid Build Coastguard Worker 	ext2fs_close(jfs);
2932*6a54128fSAndroid Build Coastguard Worker 
2933*6a54128fSAndroid Build Coastguard Worker 	return 0;
2934*6a54128fSAndroid Build Coastguard Worker }
2935*6a54128fSAndroid Build Coastguard Worker 
2936*6a54128fSAndroid Build Coastguard Worker #ifndef BUILD_AS_LIB
main(int argc,char ** argv)2937*6a54128fSAndroid Build Coastguard Worker int main(int argc, char **argv)
2938*6a54128fSAndroid Build Coastguard Worker #else
2939*6a54128fSAndroid Build Coastguard Worker int tune2fs_main(int argc, char **argv)
2940*6a54128fSAndroid Build Coastguard Worker #endif  /* BUILD_AS_LIB */
2941*6a54128fSAndroid Build Coastguard Worker {
2942*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
2943*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs;
2944*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb;
2945*6a54128fSAndroid Build Coastguard Worker 	io_manager io_ptr, io_ptr_orig = NULL;
2946*6a54128fSAndroid Build Coastguard Worker 	int rc = 0;
2947*6a54128fSAndroid Build Coastguard Worker 	char default_undo_file[1] = { 0 };
2948*6a54128fSAndroid Build Coastguard Worker 
2949*6a54128fSAndroid Build Coastguard Worker #ifdef ENABLE_NLS
2950*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_MESSAGES, "");
2951*6a54128fSAndroid Build Coastguard Worker 	setlocale(LC_CTYPE, "");
2952*6a54128fSAndroid Build Coastguard Worker 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2953*6a54128fSAndroid Build Coastguard Worker 	textdomain(NLS_CAT_NAME);
2954*6a54128fSAndroid Build Coastguard Worker 	set_com_err_gettext(gettext);
2955*6a54128fSAndroid Build Coastguard Worker #endif
2956*6a54128fSAndroid Build Coastguard Worker 	if (argc && *argv)
2957*6a54128fSAndroid Build Coastguard Worker 		program_name = *argv;
2958*6a54128fSAndroid Build Coastguard Worker 	else
2959*6a54128fSAndroid Build Coastguard Worker 		usage();
2960*6a54128fSAndroid Build Coastguard Worker 	add_error_table(&et_ext2_error_table);
2961*6a54128fSAndroid Build Coastguard Worker 
2962*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_BUILD_FINDFS
2963*6a54128fSAndroid Build Coastguard Worker 	if (strcmp(get_progname(argv[0]), "findfs") == 0)
2964*6a54128fSAndroid Build Coastguard Worker 		do_findfs(argc, argv);
2965*6a54128fSAndroid Build Coastguard Worker #endif
2966*6a54128fSAndroid Build Coastguard Worker 	if (strcmp(get_progname(argv[0]), "e2label") == 0)
2967*6a54128fSAndroid Build Coastguard Worker 		parse_e2label_options(argc, argv);
2968*6a54128fSAndroid Build Coastguard Worker 	else
2969*6a54128fSAndroid Build Coastguard Worker 		parse_tune2fs_options(argc, argv);
2970*6a54128fSAndroid Build Coastguard Worker 
2971*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_TESTIO_DEBUG
2972*6a54128fSAndroid Build Coastguard Worker 	if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2973*6a54128fSAndroid Build Coastguard Worker 		io_ptr = test_io_manager;
2974*6a54128fSAndroid Build Coastguard Worker 		test_io_backing_manager = unix_io_manager;
2975*6a54128fSAndroid Build Coastguard Worker 	} else
2976*6a54128fSAndroid Build Coastguard Worker #endif
2977*6a54128fSAndroid Build Coastguard Worker 		io_ptr = unix_io_manager;
2978*6a54128fSAndroid Build Coastguard Worker 
2979*6a54128fSAndroid Build Coastguard Worker retry_open:
2980*6a54128fSAndroid Build Coastguard Worker 	if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2981*6a54128fSAndroid Build Coastguard Worker 		open_flag |= EXT2_FLAG_SKIP_MMP;
2982*6a54128fSAndroid Build Coastguard Worker 
2983*6a54128fSAndroid Build Coastguard Worker 	open_flag |= EXT2_FLAG_64BITS | EXT2_FLAG_THREADS |
2984*6a54128fSAndroid Build Coastguard Worker 		EXT2_FLAG_JOURNAL_DEV_OK;
2985*6a54128fSAndroid Build Coastguard Worker 
2986*6a54128fSAndroid Build Coastguard Worker 	/* keep the filesystem struct around to dump MMP data */
2987*6a54128fSAndroid Build Coastguard Worker 	open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2988*6a54128fSAndroid Build Coastguard Worker 
2989*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_open2(device_name, io_options, open_flag,
2990*6a54128fSAndroid Build Coastguard Worker 			      0, 0, io_ptr, &fs);
2991*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
2992*6a54128fSAndroid Build Coastguard Worker 		com_err(program_name, retval,
2993*6a54128fSAndroid Build Coastguard Worker 			_("while trying to open %s"),
2994*6a54128fSAndroid Build Coastguard Worker 			device_name);
2995*6a54128fSAndroid Build Coastguard Worker 		if (retval == EXT2_ET_MMP_FSCK_ON ||
2996*6a54128fSAndroid Build Coastguard Worker 		    retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2997*6a54128fSAndroid Build Coastguard Worker 			dump_mmp_msg(fs->mmp_buf,
2998*6a54128fSAndroid Build Coastguard Worker 				     _("If you are sure the filesystem "
2999*6a54128fSAndroid Build Coastguard Worker 				       "is not in use on any node, run:\n"
3000*6a54128fSAndroid Build Coastguard Worker 				       "'tune2fs -f -E clear_mmp {device}'\n"));
3001*6a54128fSAndroid Build Coastguard Worker 		else if (retval == EXT2_ET_MMP_FAILED)
3002*6a54128fSAndroid Build Coastguard Worker 			dump_mmp_msg(fs->mmp_buf, NULL);
3003*6a54128fSAndroid Build Coastguard Worker 		else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
3004*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr,
3005*6a54128fSAndroid Build Coastguard Worker 				_("MMP block magic is bad. Try to fix it by "
3006*6a54128fSAndroid Build Coastguard Worker 				  "running:\n'e2fsck -f %s'\n"), device_name);
3007*6a54128fSAndroid Build Coastguard Worker 		else if (retval == EXT2_ET_BAD_MAGIC)
3008*6a54128fSAndroid Build Coastguard Worker 			check_plausibility(device_name, CHECK_FS_EXIST, NULL);
3009*6a54128fSAndroid Build Coastguard Worker 		else if (retval != EXT2_ET_MMP_FAILED)
3010*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, "%s",
3011*6a54128fSAndroid Build Coastguard Worker 			     _("Couldn't find valid filesystem superblock.\n"));
3012*6a54128fSAndroid Build Coastguard Worker 
3013*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free(fs);
3014*6a54128fSAndroid Build Coastguard Worker 		exit(1);
3015*6a54128fSAndroid Build Coastguard Worker 	}
3016*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_journal_dev(fs->super)) {
3017*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "%s", _("Cannot modify a journal device.\n"));
3018*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free(fs);
3019*6a54128fSAndroid Build Coastguard Worker 		exit(1);
3020*6a54128fSAndroid Build Coastguard Worker 	}
3021*6a54128fSAndroid Build Coastguard Worker 	fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
3022*6a54128fSAndroid Build Coastguard Worker 
3023*6a54128fSAndroid Build Coastguard Worker 	if (I_flag) {
3024*6a54128fSAndroid Build Coastguard Worker 		/*
3025*6a54128fSAndroid Build Coastguard Worker 		 * Check the inode size is right so we can issue an
3026*6a54128fSAndroid Build Coastguard Worker 		 * error message and bail before setting up the tdb
3027*6a54128fSAndroid Build Coastguard Worker 		 * file.
3028*6a54128fSAndroid Build Coastguard Worker 		 */
3029*6a54128fSAndroid Build Coastguard Worker 		if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
3030*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("The inode size is already %lu\n"),
3031*6a54128fSAndroid Build Coastguard Worker 				new_inode_size);
3032*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3033*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3034*6a54128fSAndroid Build Coastguard Worker 		}
3035*6a54128fSAndroid Build Coastguard Worker 		if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
3036*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, "%s",
3037*6a54128fSAndroid Build Coastguard Worker 				_("Shrinking inode size is not supported\n"));
3038*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3039*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3040*6a54128fSAndroid Build Coastguard Worker 		}
3041*6a54128fSAndroid Build Coastguard Worker 		if (new_inode_size > fs->blocksize) {
3042*6a54128fSAndroid Build Coastguard Worker 			fprintf(stderr, _("Invalid inode size %lu (max %d)\n"),
3043*6a54128fSAndroid Build Coastguard Worker 				new_inode_size, fs->blocksize);
3044*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3045*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3046*6a54128fSAndroid Build Coastguard Worker 		}
3047*6a54128fSAndroid Build Coastguard Worker 		rc = check_fsck_needed(fs,
3048*6a54128fSAndroid Build Coastguard Worker 			_("Resizing inodes could take some time."));
3049*6a54128fSAndroid Build Coastguard Worker 		if (rc)
3050*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3051*6a54128fSAndroid Build Coastguard Worker 		/*
3052*6a54128fSAndroid Build Coastguard Worker 		 * If inode resize is requested use the
3053*6a54128fSAndroid Build Coastguard Worker 		 * Undo I/O manager
3054*6a54128fSAndroid Build Coastguard Worker 		 */
3055*6a54128fSAndroid Build Coastguard Worker 		undo_file = default_undo_file;
3056*6a54128fSAndroid Build Coastguard Worker 	}
3057*6a54128fSAndroid Build Coastguard Worker 
3058*6a54128fSAndroid Build Coastguard Worker 	/* Set up an undo file */
3059*6a54128fSAndroid Build Coastguard Worker 	if (undo_file && io_ptr_orig == NULL) {
3060*6a54128fSAndroid Build Coastguard Worker 		io_ptr_orig = io_ptr;
3061*6a54128fSAndroid Build Coastguard Worker 		retval = tune2fs_setup_tdb(device_name, &io_ptr);
3062*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
3063*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3064*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3065*6a54128fSAndroid Build Coastguard Worker 		}
3066*6a54128fSAndroid Build Coastguard Worker 		if (io_ptr != io_ptr_orig) {
3067*6a54128fSAndroid Build Coastguard Worker 			ext2fs_close_free(&fs);
3068*6a54128fSAndroid Build Coastguard Worker 			goto retry_open;
3069*6a54128fSAndroid Build Coastguard Worker 		}
3070*6a54128fSAndroid Build Coastguard Worker 	}
3071*6a54128fSAndroid Build Coastguard Worker 
3072*6a54128fSAndroid Build Coastguard Worker 	sb = fs->super;
3073*6a54128fSAndroid Build Coastguard Worker 	fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3074*6a54128fSAndroid Build Coastguard Worker 
3075*6a54128fSAndroid Build Coastguard Worker 	if (print_label) {
3076*6a54128fSAndroid Build Coastguard Worker 		/* For e2label emulation */
3077*6a54128fSAndroid Build Coastguard Worker 		printf("%.*s\n", EXT2_LEN_STR(sb->s_volume_name));
3078*6a54128fSAndroid Build Coastguard Worker 		remove_error_table(&et_ext2_error_table);
3079*6a54128fSAndroid Build Coastguard Worker 		goto closefs;
3080*6a54128fSAndroid Build Coastguard Worker 	}
3081*6a54128fSAndroid Build Coastguard Worker 
3082*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_check_if_mounted(device_name, &mount_flags);
3083*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
3084*6a54128fSAndroid Build Coastguard Worker 		com_err("ext2fs_check_if_mount", retval,
3085*6a54128fSAndroid Build Coastguard Worker 			_("while determining whether %s is mounted."),
3086*6a54128fSAndroid Build Coastguard Worker 			device_name);
3087*6a54128fSAndroid Build Coastguard Worker 		rc = 1;
3088*6a54128fSAndroid Build Coastguard Worker 		goto closefs;
3089*6a54128fSAndroid Build Coastguard Worker 	}
3090*6a54128fSAndroid Build Coastguard Worker 
3091*6a54128fSAndroid Build Coastguard Worker #ifdef NO_RECOVERY
3092*6a54128fSAndroid Build Coastguard Worker 	/* Warn if file system needs recovery and it is opened for writing. */
3093*6a54128fSAndroid Build Coastguard Worker 	if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & EXT2_MF_MOUNTED) &&
3094*6a54128fSAndroid Build Coastguard Worker 	    (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
3095*6a54128fSAndroid Build Coastguard Worker 	    (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER)) {
3096*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr,
3097*6a54128fSAndroid Build Coastguard Worker _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
3098*6a54128fSAndroid Build Coastguard Worker   "\te2fsck -E journal_only %s\n\n"
3099*6a54128fSAndroid Build Coastguard Worker   "then rerun this command.  Otherwise, any changes made may be overwritten\n"
3100*6a54128fSAndroid Build Coastguard Worker   "by journal recovery.\n"), device_name);
3101*6a54128fSAndroid Build Coastguard Worker 	}
3102*6a54128fSAndroid Build Coastguard Worker #else
3103*6a54128fSAndroid Build Coastguard Worker 	/* Recover the journal if possible. */
3104*6a54128fSAndroid Build Coastguard Worker 	if ((open_flag & EXT2_FLAG_RW) && !(mount_flags & (EXT2_MF_BUSY | EXT2_MF_MOUNTED)) &&
3105*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_has_feature_journal_needs_recovery(fs->super)) {
3106*6a54128fSAndroid Build Coastguard Worker 		printf(_("Recovering journal.\n"));
3107*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_run_ext3_journal(&fs);
3108*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
3109*6a54128fSAndroid Build Coastguard Worker 			com_err("tune2fs", retval,
3110*6a54128fSAndroid Build Coastguard Worker 				"while recovering journal.\n");
3111*6a54128fSAndroid Build Coastguard Worker 			printf(_("Please run e2fsck -fy %s.\n"), device_name);
3112*6a54128fSAndroid Build Coastguard Worker 			if (!fs)
3113*6a54128fSAndroid Build Coastguard Worker 				exit(1);
3114*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3115*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3116*6a54128fSAndroid Build Coastguard Worker 		}
3117*6a54128fSAndroid Build Coastguard Worker 		sb = fs->super;
3118*6a54128fSAndroid Build Coastguard Worker 	}
3119*6a54128fSAndroid Build Coastguard Worker #endif
3120*6a54128fSAndroid Build Coastguard Worker 
3121*6a54128fSAndroid Build Coastguard Worker 	/* Normally we only need to write out the superblock */
3122*6a54128fSAndroid Build Coastguard Worker 	fs->flags |= EXT2_FLAG_SUPER_ONLY;
3123*6a54128fSAndroid Build Coastguard Worker 
3124*6a54128fSAndroid Build Coastguard Worker 	if (c_flag) {
3125*6a54128fSAndroid Build Coastguard Worker 		if (max_mount_count == 65536)
3126*6a54128fSAndroid Build Coastguard Worker 			max_mount_count = EXT2_DFL_MAX_MNT_COUNT +
3127*6a54128fSAndroid Build Coastguard Worker 				(random() % EXT2_DFL_MAX_MNT_COUNT);
3128*6a54128fSAndroid Build Coastguard Worker 		sb->s_max_mnt_count = max_mount_count;
3129*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3130*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting maximal mount count to %d\n"),
3131*6a54128fSAndroid Build Coastguard Worker 		       max_mount_count);
3132*6a54128fSAndroid Build Coastguard Worker 	}
3133*6a54128fSAndroid Build Coastguard Worker 	if (C_flag) {
3134*6a54128fSAndroid Build Coastguard Worker 		sb->s_mnt_count = mount_count;
3135*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3136*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting current mount count to %d\n"), mount_count);
3137*6a54128fSAndroid Build Coastguard Worker 	}
3138*6a54128fSAndroid Build Coastguard Worker 	if (e_flag) {
3139*6a54128fSAndroid Build Coastguard Worker 		sb->s_errors = errors;
3140*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3141*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting error behavior to %d\n"), errors);
3142*6a54128fSAndroid Build Coastguard Worker 	}
3143*6a54128fSAndroid Build Coastguard Worker 	if (g_flag) {
3144*6a54128fSAndroid Build Coastguard Worker 		sb->s_def_resgid = resgid;
3145*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3146*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting reserved blocks gid to %lu\n"), resgid);
3147*6a54128fSAndroid Build Coastguard Worker 	}
3148*6a54128fSAndroid Build Coastguard Worker 	if (i_flag) {
3149*6a54128fSAndroid Build Coastguard Worker 		if ((unsigned long long)interval >= (1ULL << 32)) {
3150*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, 0,
3151*6a54128fSAndroid Build Coastguard Worker 				_("interval between checks is too big (%lu)"),
3152*6a54128fSAndroid Build Coastguard Worker 				interval);
3153*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3154*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3155*6a54128fSAndroid Build Coastguard Worker 		}
3156*6a54128fSAndroid Build Coastguard Worker 		sb->s_checkinterval = interval;
3157*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3158*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting interval between checks to %lu seconds\n"),
3159*6a54128fSAndroid Build Coastguard Worker 		       interval);
3160*6a54128fSAndroid Build Coastguard Worker 	}
3161*6a54128fSAndroid Build Coastguard Worker 	if (m_flag) {
3162*6a54128fSAndroid Build Coastguard Worker 		ext2fs_r_blocks_count_set(sb, reserved_ratio *
3163*6a54128fSAndroid Build Coastguard Worker 					  ext2fs_blocks_count(sb) / 100.0);
3164*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3165*6a54128fSAndroid Build Coastguard Worker 		printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
3166*6a54128fSAndroid Build Coastguard Worker 			reserved_ratio,
3167*6a54128fSAndroid Build Coastguard Worker 			(unsigned long long) ext2fs_r_blocks_count(sb));
3168*6a54128fSAndroid Build Coastguard Worker 	}
3169*6a54128fSAndroid Build Coastguard Worker 	if (r_flag) {
3170*6a54128fSAndroid Build Coastguard Worker 		if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
3171*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, 0,
3172*6a54128fSAndroid Build Coastguard Worker 				_("reserved blocks count is too big (%llu)"),
3173*6a54128fSAndroid Build Coastguard Worker 				(unsigned long long) reserved_blocks);
3174*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3175*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3176*6a54128fSAndroid Build Coastguard Worker 		}
3177*6a54128fSAndroid Build Coastguard Worker 		ext2fs_r_blocks_count_set(sb, reserved_blocks);
3178*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3179*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting reserved blocks count to %llu\n"),
3180*6a54128fSAndroid Build Coastguard Worker 		       (unsigned long long) reserved_blocks);
3181*6a54128fSAndroid Build Coastguard Worker 	}
3182*6a54128fSAndroid Build Coastguard Worker 	if (s_flag == 1) {
3183*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_sparse_super(sb)) {
3184*6a54128fSAndroid Build Coastguard Worker 			fputs(_("\nThe filesystem already has sparse "
3185*6a54128fSAndroid Build Coastguard Worker 				"superblocks.\n"), stderr);
3186*6a54128fSAndroid Build Coastguard Worker 		} else if (ext2fs_has_feature_meta_bg(sb)) {
3187*6a54128fSAndroid Build Coastguard Worker 			fputs(_("\nSetting the sparse superblock flag not "
3188*6a54128fSAndroid Build Coastguard Worker 				"supported\nfor filesystems with "
3189*6a54128fSAndroid Build Coastguard Worker 				"the meta_bg feature enabled.\n"),
3190*6a54128fSAndroid Build Coastguard Worker 				stderr);
3191*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3192*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3193*6a54128fSAndroid Build Coastguard Worker 		} else {
3194*6a54128fSAndroid Build Coastguard Worker 			ext2fs_set_feature_sparse_super(sb);
3195*6a54128fSAndroid Build Coastguard Worker 			sb->s_state &= ~EXT2_VALID_FS;
3196*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
3197*6a54128fSAndroid Build Coastguard Worker 			printf(_("\nSparse superblock flag set.  %s"),
3198*6a54128fSAndroid Build Coastguard Worker 			       _(please_fsck));
3199*6a54128fSAndroid Build Coastguard Worker 		}
3200*6a54128fSAndroid Build Coastguard Worker 	}
3201*6a54128fSAndroid Build Coastguard Worker 	if (s_flag == 0) {
3202*6a54128fSAndroid Build Coastguard Worker 		fputs(_("\nClearing the sparse superblock flag not supported.\n"),
3203*6a54128fSAndroid Build Coastguard Worker 		      stderr);
3204*6a54128fSAndroid Build Coastguard Worker 		rc = 1;
3205*6a54128fSAndroid Build Coastguard Worker 		goto closefs;
3206*6a54128fSAndroid Build Coastguard Worker 	}
3207*6a54128fSAndroid Build Coastguard Worker 	if (T_flag) {
3208*6a54128fSAndroid Build Coastguard Worker 		sb->s_lastcheck = last_check_time;
3209*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3210*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting time filesystem last checked to %s\n"),
3211*6a54128fSAndroid Build Coastguard Worker 		       ctime(&last_check_time));
3212*6a54128fSAndroid Build Coastguard Worker 	}
3213*6a54128fSAndroid Build Coastguard Worker 	if (u_flag) {
3214*6a54128fSAndroid Build Coastguard Worker 		sb->s_def_resuid = resuid;
3215*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3216*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting reserved blocks uid to %lu\n"), resuid);
3217*6a54128fSAndroid Build Coastguard Worker 	}
3218*6a54128fSAndroid Build Coastguard Worker 	if (L_flag) {
3219*6a54128fSAndroid Build Coastguard Worker 		if (strlen(new_label) > sizeof(sb->s_volume_name))
3220*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Warning: label too long, truncating.\n"),
3221*6a54128fSAndroid Build Coastguard Worker 			      stderr);
3222*6a54128fSAndroid Build Coastguard Worker 		memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
3223*6a54128fSAndroid Build Coastguard Worker 		strncpy((char *)sb->s_volume_name, new_label,
3224*6a54128fSAndroid Build Coastguard Worker 			sizeof(sb->s_volume_name));
3225*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3226*6a54128fSAndroid Build Coastguard Worker 	}
3227*6a54128fSAndroid Build Coastguard Worker 	if (M_flag) {
3228*6a54128fSAndroid Build Coastguard Worker 		memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
3229*6a54128fSAndroid Build Coastguard Worker 		strncpy((char *)sb->s_last_mounted, new_last_mounted,
3230*6a54128fSAndroid Build Coastguard Worker 			sizeof(sb->s_last_mounted));
3231*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3232*6a54128fSAndroid Build Coastguard Worker 	}
3233*6a54128fSAndroid Build Coastguard Worker 	if (mntopts_cmd) {
3234*6a54128fSAndroid Build Coastguard Worker 		rc = update_mntopts(fs, mntopts_cmd);
3235*6a54128fSAndroid Build Coastguard Worker 		if (rc)
3236*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3237*6a54128fSAndroid Build Coastguard Worker 	}
3238*6a54128fSAndroid Build Coastguard Worker 	if (features_cmd) {
3239*6a54128fSAndroid Build Coastguard Worker 		rc = update_feature_set(fs, features_cmd);
3240*6a54128fSAndroid Build Coastguard Worker 		if (rc)
3241*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3242*6a54128fSAndroid Build Coastguard Worker 	}
3243*6a54128fSAndroid Build Coastguard Worker 	if (extended_cmd) {
3244*6a54128fSAndroid Build Coastguard Worker 		rc = parse_extended_opts(fs, extended_cmd);
3245*6a54128fSAndroid Build Coastguard Worker 		if (rc)
3246*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3247*6a54128fSAndroid Build Coastguard Worker 		if (clear_mmp && !f_flag) {
3248*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Error in using clear_mmp. "
3249*6a54128fSAndroid Build Coastguard Worker 				"It must be used with -f\n"),
3250*6a54128fSAndroid Build Coastguard Worker 			      stderr);
3251*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3252*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3253*6a54128fSAndroid Build Coastguard Worker 		}
3254*6a54128fSAndroid Build Coastguard Worker 	}
3255*6a54128fSAndroid Build Coastguard Worker 	if (clear_mmp) {
3256*6a54128fSAndroid Build Coastguard Worker 		rc = ext2fs_mmp_clear(fs);
3257*6a54128fSAndroid Build Coastguard Worker 		goto closefs;
3258*6a54128fSAndroid Build Coastguard Worker 	}
3259*6a54128fSAndroid Build Coastguard Worker 	if (journal_size || journal_device) {
3260*6a54128fSAndroid Build Coastguard Worker 		rc = add_journal(fs);
3261*6a54128fSAndroid Build Coastguard Worker 		if (rc)
3262*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3263*6a54128fSAndroid Build Coastguard Worker 	}
3264*6a54128fSAndroid Build Coastguard Worker 
3265*6a54128fSAndroid Build Coastguard Worker 	if (Q_flag) {
3266*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
3267*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The quota feature may only be changed when "
3268*6a54128fSAndroid Build Coastguard Worker 				"the filesystem is unmounted.\n"), stderr);
3269*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3270*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3271*6a54128fSAndroid Build Coastguard Worker 		}
3272*6a54128fSAndroid Build Coastguard Worker 		rc = handle_quota_options(fs);
3273*6a54128fSAndroid Build Coastguard Worker 		if (rc)
3274*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3275*6a54128fSAndroid Build Coastguard Worker 	}
3276*6a54128fSAndroid Build Coastguard Worker 
3277*6a54128fSAndroid Build Coastguard Worker 	if (U_flag) {
3278*6a54128fSAndroid Build Coastguard Worker 		int set_csum = 0;
3279*6a54128fSAndroid Build Coastguard Worker 		dgrp_t i;
3280*6a54128fSAndroid Build Coastguard Worker 		char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
3281*6a54128fSAndroid Build Coastguard Worker 		__u8 old_uuid[UUID_SIZE];
3282*6a54128fSAndroid Build Coastguard Worker 
3283*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_stable_inodes(fs->super)) {
3284*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Cannot change the UUID of this filesystem "
3285*6a54128fSAndroid Build Coastguard Worker 				"because it has the stable_inodes feature "
3286*6a54128fSAndroid Build Coastguard Worker 				"flag.\n"), stderr);
3287*6a54128fSAndroid Build Coastguard Worker 			exit(1);
3288*6a54128fSAndroid Build Coastguard Worker 		}
3289*6a54128fSAndroid Build Coastguard Worker 
3290*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_csum_seed(fs->super) &&
3291*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_has_feature_metadata_csum(fs->super) ||
3292*6a54128fSAndroid Build Coastguard Worker 		     ext2fs_has_feature_ea_inode(fs->super))) {
3293*6a54128fSAndroid Build Coastguard Worker 			rc = check_fsck_needed(fs,
3294*6a54128fSAndroid Build Coastguard Worker 				_("Setting the UUID on this "
3295*6a54128fSAndroid Build Coastguard Worker 				  "filesystem could take some time."));
3296*6a54128fSAndroid Build Coastguard Worker 			if (rc)
3297*6a54128fSAndroid Build Coastguard Worker 				goto closefs;
3298*6a54128fSAndroid Build Coastguard Worker 			rewrite_checksums = REWRITE_ALL;
3299*6a54128fSAndroid Build Coastguard Worker 		}
3300*6a54128fSAndroid Build Coastguard Worker 
3301*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_group_desc_csum(fs)) {
3302*6a54128fSAndroid Build Coastguard Worker 			/*
3303*6a54128fSAndroid Build Coastguard Worker 			 * Changing the UUID on a metadata_csum FS requires
3304*6a54128fSAndroid Build Coastguard Worker 			 * rewriting all metadata, which can race with a
3305*6a54128fSAndroid Build Coastguard Worker 			 * mounted fs.  Don't allow that unless we're saving
3306*6a54128fSAndroid Build Coastguard Worker 			 * the checksum seed.
3307*6a54128fSAndroid Build Coastguard Worker 			 */
3308*6a54128fSAndroid Build Coastguard Worker 			if ((mount_flags & EXT2_MF_MOUNTED) &&
3309*6a54128fSAndroid Build Coastguard Worker 			    !ext2fs_has_feature_csum_seed(fs->super) &&
3310*6a54128fSAndroid Build Coastguard Worker 			    ext2fs_has_feature_metadata_csum(fs->super)) {
3311*6a54128fSAndroid Build Coastguard Worker 				fputs(_("The UUID may only be "
3312*6a54128fSAndroid Build Coastguard Worker 					"changed when the filesystem is "
3313*6a54128fSAndroid Build Coastguard Worker 					"unmounted.\n"), stderr);
3314*6a54128fSAndroid Build Coastguard Worker 				fputs(_("If you only use kernels newer than "
3315*6a54128fSAndroid Build Coastguard Worker 					"v4.4, run 'tune2fs -O "
3316*6a54128fSAndroid Build Coastguard Worker 					"metadata_csum_seed' and re-run this "
3317*6a54128fSAndroid Build Coastguard Worker 					"command.\n"), stderr);
3318*6a54128fSAndroid Build Coastguard Worker 				try_confirm_csum_seed_support();
3319*6a54128fSAndroid Build Coastguard Worker 				rc = 1;
3320*6a54128fSAndroid Build Coastguard Worker 				goto closefs;
3321*6a54128fSAndroid Build Coastguard Worker 			}
3322*6a54128fSAndroid Build Coastguard Worker 
3323*6a54128fSAndroid Build Coastguard Worker 			/*
3324*6a54128fSAndroid Build Coastguard Worker 			 * Determine if the block group checksums are
3325*6a54128fSAndroid Build Coastguard Worker 			 * correct so we know whether or not to set
3326*6a54128fSAndroid Build Coastguard Worker 			 * them later on.
3327*6a54128fSAndroid Build Coastguard Worker 			 */
3328*6a54128fSAndroid Build Coastguard Worker 			for (i = 0; i < fs->group_desc_count; i++)
3329*6a54128fSAndroid Build Coastguard Worker 				if (!ext2fs_group_desc_csum_verify(fs, i))
3330*6a54128fSAndroid Build Coastguard Worker 					break;
3331*6a54128fSAndroid Build Coastguard Worker 			if (i >= fs->group_desc_count)
3332*6a54128fSAndroid Build Coastguard Worker 				set_csum = 1;
3333*6a54128fSAndroid Build Coastguard Worker 		}
3334*6a54128fSAndroid Build Coastguard Worker 
3335*6a54128fSAndroid Build Coastguard Worker 		memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
3336*6a54128fSAndroid Build Coastguard Worker 		if ((strcasecmp(new_UUID, "null") == 0) ||
3337*6a54128fSAndroid Build Coastguard Worker 		    (strcasecmp(new_UUID, "clear") == 0)) {
3338*6a54128fSAndroid Build Coastguard Worker 			uuid_clear(sb->s_uuid);
3339*6a54128fSAndroid Build Coastguard Worker 		} else if (strcasecmp(new_UUID, "time") == 0) {
3340*6a54128fSAndroid Build Coastguard Worker 			uuid_generate_time(sb->s_uuid);
3341*6a54128fSAndroid Build Coastguard Worker 		} else if (strcasecmp(new_UUID, "random") == 0) {
3342*6a54128fSAndroid Build Coastguard Worker 			uuid_generate(sb->s_uuid);
3343*6a54128fSAndroid Build Coastguard Worker 		} else if (uuid_parse(new_UUID, sb->s_uuid)) {
3344*6a54128fSAndroid Build Coastguard Worker 			com_err(program_name, 0, "%s",
3345*6a54128fSAndroid Build Coastguard Worker 				_("Invalid UUID format\n"));
3346*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3347*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3348*6a54128fSAndroid Build Coastguard Worker 		}
3349*6a54128fSAndroid Build Coastguard Worker 		ext2fs_init_csum_seed(fs);
3350*6a54128fSAndroid Build Coastguard Worker 		if (set_csum) {
3351*6a54128fSAndroid Build Coastguard Worker 			for (i = 0; i < fs->group_desc_count; i++)
3352*6a54128fSAndroid Build Coastguard Worker 				ext2fs_group_desc_csum_set(fs, i);
3353*6a54128fSAndroid Build Coastguard Worker 			fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
3354*6a54128fSAndroid Build Coastguard Worker 		}
3355*6a54128fSAndroid Build Coastguard Worker 
3356*6a54128fSAndroid Build Coastguard Worker 		/* If this is a journal dev, we need to copy UUID into jsb */
3357*6a54128fSAndroid Build Coastguard Worker 		if (!(rc = get_journal_sb(fs, buf))) {
3358*6a54128fSAndroid Build Coastguard Worker 			journal_superblock_t *jsb;
3359*6a54128fSAndroid Build Coastguard Worker 
3360*6a54128fSAndroid Build Coastguard Worker 			jsb = (journal_superblock_t *) buf;
3361*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Need to update journal superblock.\n"), stdout);
3362*6a54128fSAndroid Build Coastguard Worker 			memcpy(jsb->s_uuid, sb->s_uuid, sizeof(sb->s_uuid));
3363*6a54128fSAndroid Build Coastguard Worker 
3364*6a54128fSAndroid Build Coastguard Worker 			/* Writeback the journal superblock */
3365*6a54128fSAndroid Build Coastguard Worker 			if ((rc = io_channel_write_blk64(fs->io,
3366*6a54128fSAndroid Build Coastguard Worker 				ext2fs_journal_sb_start(fs->blocksize),
3367*6a54128fSAndroid Build Coastguard Worker 					-SUPERBLOCK_SIZE, buf)))
3368*6a54128fSAndroid Build Coastguard Worker 				goto closefs;
3369*6a54128fSAndroid Build Coastguard Worker 		} else if (rc != EXT2_ET_UNSUPP_FEATURE)
3370*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3371*6a54128fSAndroid Build Coastguard Worker 		else {
3372*6a54128fSAndroid Build Coastguard Worker 			rc = 0; /** Reset rc to avoid ext2fs_mmp_stop() */
3373*6a54128fSAndroid Build Coastguard Worker 
3374*6a54128fSAndroid Build Coastguard Worker 			if ((rc = fs_update_journal_user(sb, old_uuid)))
3375*6a54128fSAndroid Build Coastguard Worker 				goto closefs;
3376*6a54128fSAndroid Build Coastguard Worker 		}
3377*6a54128fSAndroid Build Coastguard Worker 
3378*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3379*6a54128fSAndroid Build Coastguard Worker 	}
3380*6a54128fSAndroid Build Coastguard Worker 
3381*6a54128fSAndroid Build Coastguard Worker 	if (I_flag) {
3382*6a54128fSAndroid Build Coastguard Worker 		if (mount_flags & EXT2_MF_MOUNTED) {
3383*6a54128fSAndroid Build Coastguard Worker 			fputs(_("The inode size may only be "
3384*6a54128fSAndroid Build Coastguard Worker 				"changed when the filesystem is "
3385*6a54128fSAndroid Build Coastguard Worker 				"unmounted.\n"), stderr);
3386*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3387*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3388*6a54128fSAndroid Build Coastguard Worker 		}
3389*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_flex_bg(fs->super)) {
3390*6a54128fSAndroid Build Coastguard Worker 			fputs(_("Changing the inode size not supported for "
3391*6a54128fSAndroid Build Coastguard Worker 				"filesystems with the flex_bg\n"
3392*6a54128fSAndroid Build Coastguard Worker 				"feature enabled.\n"),
3393*6a54128fSAndroid Build Coastguard Worker 			      stderr);
3394*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3395*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3396*6a54128fSAndroid Build Coastguard Worker 		}
3397*6a54128fSAndroid Build Coastguard Worker 		/*
3398*6a54128fSAndroid Build Coastguard Worker 		 * We want to update group descriptor also
3399*6a54128fSAndroid Build Coastguard Worker 		 * with the new free inode count
3400*6a54128fSAndroid Build Coastguard Worker 		 */
3401*6a54128fSAndroid Build Coastguard Worker 		if (rewrite_checksums)
3402*6a54128fSAndroid Build Coastguard Worker 			fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3403*6a54128fSAndroid Build Coastguard Worker 		fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
3404*6a54128fSAndroid Build Coastguard Worker 		retval = resize_inode(fs, new_inode_size);
3405*6a54128fSAndroid Build Coastguard Worker 		if (rewrite_checksums)
3406*6a54128fSAndroid Build Coastguard Worker 			fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
3407*6a54128fSAndroid Build Coastguard Worker 		if (retval == 0) {
3408*6a54128fSAndroid Build Coastguard Worker 			printf(_("Setting inode size %lu\n"),
3409*6a54128fSAndroid Build Coastguard Worker 							new_inode_size);
3410*6a54128fSAndroid Build Coastguard Worker 			rewrite_checksums = REWRITE_ALL;
3411*6a54128fSAndroid Build Coastguard Worker 		} else {
3412*6a54128fSAndroid Build Coastguard Worker 			printf("%s", _("Failed to change inode size\n"));
3413*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3414*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3415*6a54128fSAndroid Build Coastguard Worker 		}
3416*6a54128fSAndroid Build Coastguard Worker 	}
3417*6a54128fSAndroid Build Coastguard Worker 
3418*6a54128fSAndroid Build Coastguard Worker 	if (rewrite_checksums) {
3419*6a54128fSAndroid Build Coastguard Worker 		retval = rewrite_metadata_checksums(fs, rewrite_checksums);
3420*6a54128fSAndroid Build Coastguard Worker 		if (retval != 0) {
3421*6a54128fSAndroid Build Coastguard Worker 			printf("Failed to rewrite metadata checksums\n");
3422*6a54128fSAndroid Build Coastguard Worker 			rc = 1;
3423*6a54128fSAndroid Build Coastguard Worker 			goto closefs;
3424*6a54128fSAndroid Build Coastguard Worker 		}
3425*6a54128fSAndroid Build Coastguard Worker 	}
3426*6a54128fSAndroid Build Coastguard Worker 
3427*6a54128fSAndroid Build Coastguard Worker 	if (l_flag)
3428*6a54128fSAndroid Build Coastguard Worker 		list_super(sb);
3429*6a54128fSAndroid Build Coastguard Worker 	if (stride_set) {
3430*6a54128fSAndroid Build Coastguard Worker 		sb->s_raid_stride = stride;
3431*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3432*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting stride size to %d\n"), stride);
3433*6a54128fSAndroid Build Coastguard Worker 	}
3434*6a54128fSAndroid Build Coastguard Worker 	if (stripe_width_set) {
3435*6a54128fSAndroid Build Coastguard Worker 		sb->s_raid_stripe_width = stripe_width;
3436*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3437*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting stripe width to %d\n"), stripe_width);
3438*6a54128fSAndroid Build Coastguard Worker 	}
3439*6a54128fSAndroid Build Coastguard Worker 	if (ext_mount_opts) {
3440*6a54128fSAndroid Build Coastguard Worker 		strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
3441*6a54128fSAndroid Build Coastguard Worker 			sizeof(fs->super->s_mount_opts));
3442*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
3443*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
3444*6a54128fSAndroid Build Coastguard Worker 		printf(_("Setting extended default mount options to '%s'\n"),
3445*6a54128fSAndroid Build Coastguard Worker 		       ext_mount_opts);
3446*6a54128fSAndroid Build Coastguard Worker 		free(ext_mount_opts);
3447*6a54128fSAndroid Build Coastguard Worker 	}
3448*6a54128fSAndroid Build Coastguard Worker 
3449*6a54128fSAndroid Build Coastguard Worker 	free(device_name);
3450*6a54128fSAndroid Build Coastguard Worker 	remove_error_table(&et_ext2_error_table);
3451*6a54128fSAndroid Build Coastguard Worker 
3452*6a54128fSAndroid Build Coastguard Worker closefs:
3453*6a54128fSAndroid Build Coastguard Worker 	if (rc) {
3454*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mmp_stop(fs);
3455*6a54128fSAndroid Build Coastguard Worker #ifndef BUILD_AS_LIB
3456*6a54128fSAndroid Build Coastguard Worker 		exit(1);
3457*6a54128fSAndroid Build Coastguard Worker #endif
3458*6a54128fSAndroid Build Coastguard Worker 	}
3459*6a54128fSAndroid Build Coastguard Worker 
3460*6a54128fSAndroid Build Coastguard Worker 	if (feature_64bit)
3461*6a54128fSAndroid Build Coastguard Worker 		convert_64bit(fs, feature_64bit);
3462*6a54128fSAndroid Build Coastguard Worker 
3463*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_close_free(&fs);
3464*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
3465*6a54128fSAndroid Build Coastguard Worker 		com_err("tune2fs", retval,
3466*6a54128fSAndroid Build Coastguard Worker 			_("while writing out and closing file system"));
3467*6a54128fSAndroid Build Coastguard Worker 		rc = 1;
3468*6a54128fSAndroid Build Coastguard Worker 	}
3469*6a54128fSAndroid Build Coastguard Worker 
3470*6a54128fSAndroid Build Coastguard Worker 	return rc;
3471*6a54128fSAndroid Build Coastguard Worker }
3472