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 "ext2fs/ext2fsP.h"
48 #include "support/nls-enable.h"
49 #include "blkid/blkid.h"
52 char *journal_location_string = NULL;
54 #ifndef HAVE_STRCASECMP
55 int strcasecmp (char *s1, char *s2)
58 int ch1 = *s1++, ch2 = *s2++;
66 return *s1 ? 1 : *s2 ? -1 : 0;
71 * Given argv[0], return the program name.
73 char *get_progname(char *argv_zero)
77 cp = strrchr(argv_zero, '/');
84 static jmp_buf alarm_env;
86 static void alarm_signal(int signal EXT2FS_ATTR((unused)))
88 longjmp(alarm_env, 1);
91 void proceed_question(int delay)
94 const char *short_yes = _("yY");
95 const char *english_yes = "yY";
100 if (setjmp(alarm_env)) {
101 signal(SIGALRM, SIG_IGN);
102 printf("%s", _("<proceeding>\n"));
105 signal(SIGALRM, alarm_signal);
106 printf(_("Proceed anyway (or wait %d seconds to proceed) ? (y,N) "),
110 fputs(_("Proceed anyway? (y,N) "), stdout);
112 if (!fgets(buf, sizeof(buf), stdin) ||
113 strchr(_("nN"), buf[0]) ||
114 !(strchr(short_yes, buf[0]) ||
115 strchr(english_yes, buf[0]))) {
119 signal(SIGALRM, SIG_IGN);
122 void check_mount(const char *device, int force, const char *type)
127 retval = ext2fs_check_if_mounted(device, &mount_flags);
129 com_err("ext2fs_check_if_mount", retval,
130 _("while determining whether %s is mounted."),
134 if (mount_flags & EXT2_MF_MOUNTED) {
135 fprintf(stderr, _("%s is mounted; "), device);
137 fputs(_("mke2fs forced anyway. Hope /etc/mtab is "
138 "incorrect.\n"), stderr);
142 fprintf(stderr, _("will not make a %s here!\n"), type);
145 if (mount_flags & EXT2_MF_BUSY) {
146 fprintf(stderr, _("%s is apparently in use by the system; "),
149 fputs(_("mke2fs forced anyway.\n"), stderr);
156 void parse_journal_opts(const char *opts)
158 char *buf, *token, *next, *p, *arg;
160 int journal_usage = 0;
165 fputs(_("Couldn't allocate memory to parse journal "
166 "options!\n"), stderr);
170 for (token = buf; token && *token; token = next) {
171 p = strchr(token, ',');
177 arg = strchr(token, '=');
183 printf("Journal option=%s, argument=%s\n", token,
186 if (strcmp(token, "device") == 0) {
187 journal_device = blkid_get_devname(NULL, arg, NULL);
188 if (!journal_device) {
190 fprintf(stderr, _("\nCould not find "
191 "journal device matching %s\n"),
196 } else if (strcmp(token, "size") == 0) {
201 journal_size = strtoul(arg, &p, 0);
204 } else if (!strcmp(token, "location")) {
209 journal_location_string = strdup(arg);
210 } else if (strcmp(token, "v1_superblock") == 0) {
211 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
217 fputs(_("\nBad journal options specified.\n\n"
218 "Journal options are separated by commas, "
219 "and may take an argument which\n"
220 "\tis set off by an equals ('=') sign.\n\n"
221 "Valid journal options are:\n"
222 "\tsize=<journal size in megabytes>\n"
223 "\tdevice=<journal device>\n"
224 "\tlocation=<journal location>\n\n"
225 "The journal size must be between "
226 "1024 and 10240000 filesystem blocks.\n\n"), stderr);
234 * Determine the number of journal blocks to use, either via
235 * user-specified # of megabytes, or via some intelligently selected
238 * Find a reasonable journal file size (in blocks) given the number of blocks
239 * in the filesystem. For very small filesystems, it is not reasonable to
240 * have a journal that fills more than half of the filesystem.
242 unsigned int figure_journal_size(int size, ext2_filsys fs)
246 j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
248 fputs(_("\nFilesystem too small for a journal\n"), stderr);
255 if (ext2fs_is_before_linux_ver(3, 10, 0) ||
256 ext2fs_blocks_count(fs->super) <= 8192)
261 j_blocks = size * 1024 / (fs->blocksize / 1024);
262 if (j_blocks < min_size || j_blocks > 10240000) {
263 fprintf(stderr, _("\nThe requested journal "
264 "size is %d blocks; it must be\n"
265 "between %d and 10240000 blocks. "
266 "Aborting.\n"), j_blocks, min_size);
269 if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
270 fputs(_("\nJournal size too big for filesystem.\n"),
278 void print_check_message(int mnt, unsigned int check)
284 printf(_("This filesystem will be automatically "
285 "checked every %d mounts or\n"
286 "%g days, whichever comes first. "
287 "Use tune2fs -c or -i to override.\n"),
288 mnt, ((double) check) / (3600 * 24));
291 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
295 printf("MMP check failed: %s\n", msg);
297 time_t t = mmp->mmp_time;
299 printf("MMP error info: last update: %s node: %s device: %s\n",
300 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);