2 * util.c --- helper functions used by tune2fs and mke2fs
4 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
12 #ifndef _LARGEFILE_SOURCE
13 #define _LARGEFILE_SOURCE
15 #ifndef _LARGEFILE64_SOURCE
16 #define _LARGEFILE64_SOURCE
31 #ifdef HAVE_LINUX_MAJOR_H
32 #include <linux/major.h>
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
43 #include "et/com_err.h"
45 #include "ext2fs/ext2_fs.h"
46 #include "ext2fs/ext2fs.h"
47 #include "support/nls-enable.h"
48 #include "blkid/blkid.h"
51 char *journal_location_string = NULL;
53 #ifndef HAVE_STRCASECMP
54 int strcasecmp (char *s1, char *s2)
57 int ch1 = *s1++, ch2 = *s2++;
65 return *s1 ? 1 : *s2 ? -1 : 0;
70 * Given argv[0], return the program name.
72 char *get_progname(char *argv_zero)
76 cp = strrchr(argv_zero, '/');
83 static jmp_buf alarm_env;
85 static void alarm_signal(int signal EXT2FS_ATTR((unused)))
87 longjmp(alarm_env, 1);
90 void proceed_question(int delay)
93 const char *short_yes = _("yY");
94 const char *english_yes = "yY";
99 if (setjmp(alarm_env)) {
100 signal(SIGALRM, SIG_IGN);
101 printf("%s", _("<proceeding>\n"));
104 signal(SIGALRM, alarm_signal);
105 printf(_("Proceed anyway (or wait %d seconds to proceed) ? (y,N) "),
109 fputs(_("Proceed anyway? (y,N) "), stdout);
111 if (!fgets(buf, sizeof(buf), stdin) ||
112 strchr(_("nN"), buf[0]) ||
113 !(strchr(short_yes, buf[0]) ||
114 strchr(english_yes, buf[0]))) {
118 signal(SIGALRM, SIG_IGN);
121 void check_mount(const char *device, int force, const char *type)
126 retval = ext2fs_check_if_mounted(device, &mount_flags);
128 com_err("ext2fs_check_if_mount", retval,
129 _("while determining whether %s is mounted."),
133 if (mount_flags & EXT2_MF_MOUNTED) {
134 fprintf(stderr, _("%s is mounted; "), device);
136 fputs(_("mke2fs forced anyway. Hope /etc/mtab is "
137 "incorrect.\n"), stderr);
141 fprintf(stderr, _("will not make a %s here!\n"), type);
144 if (mount_flags & EXT2_MF_BUSY) {
145 fprintf(stderr, _("%s is apparently in use by the system; "),
148 fputs(_("mke2fs forced anyway.\n"), stderr);
155 void parse_journal_opts(const char *opts)
157 char *buf, *token, *next, *p, *arg;
159 int journal_usage = 0;
164 fputs(_("Couldn't allocate memory to parse journal "
165 "options!\n"), stderr);
169 for (token = buf; token && *token; token = next) {
170 p = strchr(token, ',');
176 arg = strchr(token, '=');
182 printf("Journal option=%s, argument=%s\n", token,
185 if (strcmp(token, "device") == 0) {
186 journal_device = blkid_get_devname(NULL, arg, NULL);
187 if (!journal_device) {
189 fprintf(stderr, _("\nCould not find "
190 "journal device matching %s\n"),
195 } else if (strcmp(token, "size") == 0) {
200 journal_size = strtoul(arg, &p, 0);
203 } else if (strcmp(token, "fast_commit_size") == 0) {
208 journal_fc_size = strtoul(arg, &p, 0);
211 } else if (!strcmp(token, "location")) {
216 journal_location_string = strdup(arg);
217 } else if (strcmp(token, "v1_superblock") == 0) {
218 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
224 fputs(_("\nBad journal options specified.\n\n"
225 "Journal options are separated by commas, "
226 "and may take an argument which\n"
227 "\tis set off by an equals ('=') sign.\n\n"
228 "Valid journal options are:\n"
229 "\tsize=<journal size in megabytes>\n"
230 "\tdevice=<journal device>\n"
231 "\tlocation=<journal location>\n\n"
232 "The journal size must be between "
233 "1024 and 10240000 filesystem blocks.\n\n"), stderr);
240 static inline int jsize_to_blks(ext2_filsys fs, int size)
242 return (size * 1024) / (fs->blocksize / 1024);
245 /* Fast commit size is in KBs */
246 static inline int fcsize_to_blks(ext2_filsys fs, int size)
248 return (size * 1024) / (fs->blocksize);
252 * Determine the number of journal blocks to use, either via
253 * user-specified # of megabytes, or via some intelligently selected
256 * Find a reasonable journal file size (in blocks) given the number of blocks in
257 * the filesystem. For very small filesystems, it is not reasonable to have a
258 * journal that fills more than half of the filesystem.
260 void figure_journal_size(struct ext2fs_journal_params *jparams,
261 int requested_j_size, int requested_fc_size, ext2_filsys fs)
263 int total_blocks, ret;
265 ret = ext2fs_get_journal_params(jparams, fs);
267 fputs(_("\nFilesystem too small for a journal\n"), stderr);
271 if (requested_j_size > 0 ||
272 (ext2fs_has_feature_fast_commit(fs->super) && requested_fc_size > 0)) {
273 if (requested_j_size > 0)
274 jparams->num_journal_blocks =
275 jsize_to_blks(fs, requested_j_size);
276 if (ext2fs_has_feature_fast_commit(fs->super) &&
277 requested_fc_size > 0)
278 jparams->num_fc_blocks =
279 fcsize_to_blks(fs, requested_fc_size);
280 else if (!ext2fs_has_feature_fast_commit(fs->super))
281 jparams->num_fc_blocks = 0;
282 total_blocks = jparams->num_journal_blocks + jparams->num_fc_blocks;
283 if (total_blocks < 1024 || total_blocks > 10240000) {
284 fprintf(stderr, _("\nThe total requested journal "
285 "size is %d blocks; it must be\n"
286 "between 1024 and 10240000 blocks. "
291 if ((unsigned int) total_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
292 fputs(_("\nTotal journal size too big for filesystem.\n"),
299 void print_check_message(int mnt, unsigned int check)
305 printf(_("This filesystem will be automatically "
306 "checked every %d mounts or\n"
307 "%g days, whichever comes first. "
308 "Use tune2fs -c or -i to override.\n"),
309 mnt, ((double) check) / (3600 * 24));
312 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
316 printf("MMP check failed: %s\n", msg);
318 time_t t = mmp->mmp_time;
320 printf("MMP error info: node: %.*s, device: %.*s, updated: %s",
321 EXT2_LEN_STR(mmp->mmp_nodename),
322 EXT2_LEN_STR(mmp->mmp_bdevname), ctime(&t));