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