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 #define _LARGEFILE_SOURCE
13 #define _LARGEFILE64_SOURCE
24 #ifdef HAVE_LINUX_MAJOR_H
25 #include <linux/major.h>
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_STAT_H
36 #include "et/com_err.h"
38 #include "ext2fs/ext2_fs.h"
39 #include "ext2fs/ext2fs.h"
40 #include "nls-enable.h"
41 #include "blkid/blkid.h"
44 char *journal_location_string = NULL;
46 #ifndef HAVE_STRCASECMP
47 int strcasecmp (char *s1, char *s2)
50 int ch1 = *s1++, ch2 = *s2++;
58 return *s1 ? 1 : *s2 ? -1 : 0;
63 * Given argv[0], return the program name.
65 char *get_progname(char *argv_zero)
69 cp = strrchr(argv_zero, '/');
76 static jmp_buf alarm_env;
78 static void alarm_signal(int signal)
80 longjmp(alarm_env, 1);
83 void proceed_question(int delay)
86 const char *short_yes = _("yY");
91 if (setjmp(alarm_env)) {
92 signal(SIGALRM, SIG_IGN);
93 printf("%s", _("<proceeding>\n"));
96 signal(SIGALRM, alarm_signal);
97 printf(_("Proceed anyway (or wait %d seconds) ? (y,n) "),
101 fputs(_("Proceed anyway? (y,n) "), stdout);
103 if (!fgets(buf, sizeof(buf), stdin) ||
104 strchr(short_yes, buf[0]) == 0) {
108 signal(SIGALRM, SIG_IGN);
111 void check_mount(const char *device, int force, const char *type)
116 retval = ext2fs_check_if_mounted(device, &mount_flags);
118 com_err("ext2fs_check_if_mount", retval,
119 _("while determining whether %s is mounted."),
123 if (mount_flags & EXT2_MF_MOUNTED) {
124 fprintf(stderr, _("%s is mounted; "), device);
126 fputs(_("mke2fs forced anyway. Hope /etc/mtab is "
127 "incorrect.\n"), stderr);
131 fprintf(stderr, _("will not make a %s here!\n"), type);
134 if (mount_flags & EXT2_MF_BUSY) {
135 fprintf(stderr, _("%s is apparently in use by the system; "),
138 fputs(_("mke2fs forced anyway.\n"), stderr);
145 void parse_journal_opts(const char *opts)
147 char *buf, *token, *next, *p, *arg;
149 int journal_usage = 0;
154 fputs(_("Couldn't allocate memory to parse journal "
155 "options!\n"), stderr);
159 for (token = buf; token && *token; token = next) {
160 p = strchr(token, ',');
166 arg = strchr(token, '=');
172 printf("Journal option=%s, argument=%s\n", token,
175 if (strcmp(token, "device") == 0) {
176 journal_device = blkid_get_devname(NULL, arg, NULL);
177 if (!journal_device) {
179 fprintf(stderr, _("\nCould not find "
180 "journal device matching %s\n"),
185 } else if (strcmp(token, "size") == 0) {
190 journal_size = strtoul(arg, &p, 0);
193 } else if (!strcmp(token, "location")) {
198 journal_location_string = strdup(arg);
199 } else if (strcmp(token, "v1_superblock") == 0) {
200 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
206 fputs(_("\nBad journal options specified.\n\n"
207 "Journal options are separated by commas, "
208 "and may take an argument which\n"
209 "\tis set off by an equals ('=') sign.\n\n"
210 "Valid journal options are:\n"
211 "\tsize=<journal size in megabytes>\n"
212 "\tdevice=<journal device>\n"
213 "\tlocation=<journal location>\n\n"
214 "The journal size must be between "
215 "1024 and 10240000 filesystem blocks.\n\n"), stderr);
223 * Determine the number of journal blocks to use, either via
224 * user-specified # of megabytes, or via some intelligently selected
227 * Find a reasonable journal file size (in blocks) given the number of blocks
228 * in the filesystem. For very small filesystems, it is not reasonable to
229 * have a journal that fills more than half of the filesystem.
231 unsigned int figure_journal_size(int size, ext2_filsys fs)
235 j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
237 fputs(_("\nFilesystem too small for a journal\n"), stderr);
242 j_blocks = size * 1024 / (fs->blocksize / 1024);
243 if (j_blocks < 1024 || j_blocks > 10240000) {
244 fprintf(stderr, _("\nThe requested journal "
245 "size is %d blocks; it must be\n"
246 "between 1024 and 10240000 blocks. "
251 if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
252 fputs(_("\nJournal size too big for filesystem.\n"),
260 void print_check_message(int mnt, unsigned int check)
266 printf(_("This filesystem will be automatically "
267 "checked every %d mounts or\n"
268 "%g days, whichever comes first. "
269 "Use tune2fs -c or -i to override.\n"),
270 mnt, ((double) check) / (3600 * 24));
273 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
277 printf("MMP check failed: %s\n", msg);
279 time_t t = mmp->mmp_time;
281 printf("MMP error info: last update: %s node: %s device: %s\n",
282 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);