xref: /aosp_15_r20/external/e2fsprogs/misc/util.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1 /*
2  * util.c --- helper functions used by tune2fs and mke2fs
3  *
4  * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11 
12 #ifndef _LARGEFILE_SOURCE
13 #define _LARGEFILE_SOURCE
14 #endif
15 #ifndef _LARGEFILE64_SOURCE
16 #define _LARGEFILE64_SOURCE
17 #endif
18 
19 #ifdef _WIN32
20 #define _POSIX
21 #define __USE_MINGW_ALARM
22 #endif
23 
24 #include "config.h"
25 #include <fcntl.h>
26 #include <setjmp.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <string.h>
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_LINUX_MAJOR_H
37 #include <linux/major.h>
38 #endif
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_STAT_H
41 #include <sys/stat.h>
42 #endif
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #include <time.h>
47 
48 #include "et/com_err.h"
49 #include "e2p/e2p.h"
50 #include "ext2fs/ext2_fs.h"
51 #include "ext2fs/ext2fs.h"
52 #include "support/nls-enable.h"
53 #include "support/devname.h"
54 #include "blkid/blkid.h"
55 #include "util.h"
56 
57 char *journal_location_string = NULL;
58 
59 #ifndef HAVE_STRCASECMP
strcasecmp(char * s1,char * s2)60 int strcasecmp (char *s1, char *s2)
61 {
62 	while (*s1 && *s2) {
63 		int ch1 = *s1++, ch2 = *s2++;
64 		if (isupper (ch1))
65 			ch1 = tolower (ch1);
66 		if (isupper (ch2))
67 			ch2 = tolower (ch2);
68 		if (ch1 != ch2)
69 			return ch1 - ch2;
70 	}
71 	return *s1 ? 1 : *s2 ? -1 : 0;
72 }
73 #endif
74 
75 /*
76  * Given argv[0], return the program name.
77  */
get_progname(char * argv_zero)78 char *get_progname(char *argv_zero)
79 {
80 	char	*cp;
81 
82 	cp = strrchr(argv_zero, '/');
83 	if (!cp )
84 		return argv_zero;
85 	else
86 		return cp+1;
87 }
88 
89 static jmp_buf alarm_env;
90 
alarm_signal(int signal EXT2FS_ATTR ((unused)))91 static void alarm_signal(int signal EXT2FS_ATTR((unused)))
92 {
93 	longjmp(alarm_env, 1);
94 }
95 
proceed_question(int delay)96 void proceed_question(int delay)
97 {
98 	char buf[256];
99 	const char *short_yes = _("yY");
100 	const char *english_yes = "yY";
101 
102 	fflush(stdout);
103 	fflush(stderr);
104 	if (delay > 0) {
105 		if (setjmp(alarm_env)) {
106 			signal(SIGALRM, SIG_IGN);
107 			printf("%s", _("<proceeding>\n"));
108 			return;
109 		}
110 		signal(SIGALRM, alarm_signal);
111 		printf(_("Proceed anyway (or wait %d seconds to proceed) ? (y,N) "),
112 		       delay);
113 		alarm(delay);
114 	} else
115 		fputs(_("Proceed anyway? (y,N) "), stdout);
116 	buf[0] = 0;
117 	if (!fgets(buf, sizeof(buf), stdin) ||
118 	    strchr(_("nN"), buf[0]) ||
119 	    !(strchr(short_yes, buf[0]) ||
120 	      strchr(english_yes, buf[0]))) {
121 		putc('\n', stdout);
122 		exit(1);
123 	}
124 	signal(SIGALRM, SIG_IGN);
125 }
126 
check_mount(const char * device,int force,const char * type)127 void check_mount(const char *device, int force, const char *type)
128 {
129 	errcode_t	retval;
130 	int		mount_flags;
131 
132 	retval = ext2fs_check_if_mounted(device, &mount_flags);
133 	if (retval) {
134 		com_err("ext2fs_check_if_mount", retval,
135 			_("while determining whether %s is mounted."),
136 			device);
137 		return;
138 	}
139 	if (mount_flags & EXT2_MF_MOUNTED) {
140 		fprintf(stderr, _("%s is mounted; "), device);
141 		if (force >= 2) {
142 			fputs(_("mke2fs forced anyway.  Hope /etc/mtab is "
143 				"incorrect.\n"), stderr);
144 			return;
145 		}
146 	abort_mke2fs:
147 		fprintf(stderr, _("will not make a %s here!\n"), type);
148 		exit(1);
149 	}
150 	if (mount_flags & EXT2_MF_BUSY) {
151 		fprintf(stderr, _("%s is apparently in use by the system; "),
152 			device);
153 		if (force >= 2) {
154 			fputs(_("mke2fs forced anyway.\n"), stderr);
155 			return;
156 		}
157 		goto abort_mke2fs;
158 	}
159 }
160 
parse_journal_opts(const char * opts)161 void parse_journal_opts(const char *opts)
162 {
163 	char	*buf, *token, *next, *p, *arg;
164 	int	len;
165 	int	journal_usage = 0;
166 
167 	len = strlen(opts);
168 	buf = malloc(len+1);
169 	if (!buf) {
170 		fputs(_("Couldn't allocate memory to parse journal "
171 			"options!\n"), stderr);
172 		exit(1);
173 	}
174 	strcpy(buf, opts);
175 	for (token = buf; token && *token; token = next) {
176 		p = strchr(token, ',');
177 		next = 0;
178 		if (p) {
179 			*p = 0;
180 			next = p+1;
181 		}
182 		arg = strchr(token, '=');
183 		if (arg) {
184 			*arg = 0;
185 			arg++;
186 		}
187 #if 0
188 		printf("Journal option=%s, argument=%s\n", token,
189 		       arg ? arg : "NONE");
190 #endif
191 		if (strcmp(token, "device") == 0) {
192 			journal_device = get_devname(NULL, arg, NULL);
193 			if (!journal_device) {
194 				if (arg)
195 					fprintf(stderr, _("\nCould not find "
196 						"journal device matching %s\n"),
197 						arg);
198 				journal_usage++;
199 				continue;
200 			}
201 		} else if (strcmp(token, "size") == 0) {
202 			if (!arg) {
203 				journal_usage++;
204 				continue;
205 			}
206 			journal_size = strtoul(arg, &p, 0);
207 			if (*p)
208 				journal_usage++;
209 		} else if (strcmp(token, "fast_commit_size") == 0) {
210 			if (!arg) {
211 				journal_usage++;
212 				continue;
213 			}
214 			journal_fc_size = strtoul(arg, &p, 0);
215 			if (*p)
216 				journal_usage++;
217 		} else if (!strcmp(token, "location")) {
218 			if (!arg) {
219 				journal_usage++;
220 				continue;
221 			}
222 			journal_location_string = strdup(arg);
223 		} else if (strcmp(token, "v1_superblock") == 0) {
224 			journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
225 			continue;
226 		} else
227 			journal_usage++;
228 	}
229 	if (journal_usage) {
230 		fputs(_("\nBad journal options specified.\n\n"
231 			"Journal options are separated by commas, "
232 			"and may take an argument which\n"
233 			"\tis set off by an equals ('=') sign.\n\n"
234 			"Valid journal options are:\n"
235 			"\tsize=<journal size in megabytes>\n"
236 			"\tdevice=<journal device>\n"
237 			"\tlocation=<journal location>\n\n"
238 			"The journal size must be between "
239 			"1024 and 10240000 filesystem blocks.\n\n"), stderr);
240 		free(buf);
241 		exit(1);
242 	}
243 	free(buf);
244 }
245 
jsize_to_blks(ext2_filsys fs,int size)246 static inline int jsize_to_blks(ext2_filsys fs, int size)
247 {
248 	return (size * 1024) / (fs->blocksize / 1024);
249 }
250 
251 /* Fast commit size is in KBs */
fcsize_to_blks(ext2_filsys fs,int size)252 static inline int fcsize_to_blks(ext2_filsys fs, int size)
253 {
254 	return (size * 1024) / (fs->blocksize);
255 }
256 
257 /*
258  * Determine the number of journal blocks to use, either via
259  * user-specified # of megabytes, or via some intelligently selected
260  * defaults.
261  *
262  * Find a reasonable journal file size (in blocks) given the number of blocks in
263  * the filesystem. For very small filesystems, it is not reasonable to have a
264  * journal that fills more than half of the filesystem.
265  */
figure_journal_size(struct ext2fs_journal_params * jparams,int requested_j_size,int requested_fc_size,ext2_filsys fs)266 void figure_journal_size(struct ext2fs_journal_params *jparams,
267 		int requested_j_size, int requested_fc_size, ext2_filsys fs)
268 {
269 	int total_blocks, ret;
270 
271 	ret = ext2fs_get_journal_params(jparams, fs);
272 	if (ret) {
273 		fputs(_("\nFilesystem too small for a journal\n"), stderr);
274 		return;
275 	}
276 
277 	if (requested_j_size > 0 ||
278 		(ext2fs_has_feature_fast_commit(fs->super) && requested_fc_size > 0)) {
279 		if (requested_j_size > 0)
280 			jparams->num_journal_blocks =
281 				jsize_to_blks(fs, requested_j_size);
282 		if (ext2fs_has_feature_fast_commit(fs->super) &&
283 			requested_fc_size > 0)
284 			jparams->num_fc_blocks =
285 				fcsize_to_blks(fs, requested_fc_size);
286 		else if (!ext2fs_has_feature_fast_commit(fs->super))
287 			jparams->num_fc_blocks = 0;
288 		total_blocks = jparams->num_journal_blocks + jparams->num_fc_blocks;
289 		if (total_blocks < 1024 || total_blocks > 10240000) {
290 			fprintf(stderr, _("\nThe total requested journal "
291 				"size is %d blocks; it must be\n"
292 				"between 1024 and 10240000 blocks.  "
293 				"Aborting.\n"),
294 				total_blocks);
295 			exit(1);
296 		}
297 		if ((unsigned int) total_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
298 			fputs(_("\nTotal journal size too big for filesystem.\n"),
299 			      stderr);
300 			exit(1);
301 		}
302 	}
303 }
304 
print_check_message(int mnt,unsigned int check)305 void print_check_message(int mnt, unsigned int check)
306 {
307 	if (mnt < 0)
308 		mnt = 0;
309 	if (!mnt && !check)
310 		return;
311 	printf(_("This filesystem will be automatically "
312 		 "checked every %d mounts or\n"
313 		 "%g days, whichever comes first.  "
314 		 "Use tune2fs -c or -i to override.\n"),
315 	       mnt, ((double) check) / (3600 * 24));
316 }
317 
dump_mmp_msg(struct mmp_struct * mmp,const char * msg)318 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
319 {
320 
321 	if (msg)
322 		printf("MMP check failed: %s\n", msg);
323 	if (mmp) {
324 		time_t t = mmp->mmp_time;
325 
326 		printf("MMP error info: node: %.*s, device: %.*s, updated: %s",
327 		       EXT2_LEN_STR(mmp->mmp_nodename),
328 		       EXT2_LEN_STR(mmp->mmp_bdevname), ctime(&t));
329 	}
330 }
331