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
20 #ifdef HAVE_LINUX_MAJOR_H
21 #include <linux/major.h>
23 #ifdef HAVE_SYS_STAT_H
28 #include "et/com_err.h"
30 #include "ext2fs/ext2_fs.h"
31 #include "ext2fs/ext2fs.h"
32 #include "nls-enable.h"
33 #include "blkid/blkid.h"
36 #ifndef HAVE_STRCASECMP
37 int strcasecmp (char *s1, char *s2)
40 int ch1 = *s1++, ch2 = *s2++;
48 return *s1 ? 1 : *s2 ? -1 : 0;
53 * Given argv[0], return the program name.
55 char *get_progname(char *argv_zero)
59 cp = strrchr(argv_zero, '/');
66 void proceed_question(void)
69 const char *short_yes = _("yY");
73 fputs(_("Proceed anyway? (y,n) "), stdout);
75 if (!fgets(buf, sizeof(buf), stdin) ||
76 strchr(short_yes, buf[0]) == 0)
80 void check_plausibility(const char *device)
85 val = ext2fs_stat(device, &s);
88 fprintf(stderr, _("Could not stat %s --- %s\n"),
89 device, error_message(errno));
91 fputs(_("\nThe device apparently does not exist; "
92 "did you specify it correctly?\n"), stderr);
95 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
96 /* On FreeBSD, all disk devices are character specials */
97 if (!S_ISBLK(s.st_mode) && !S_ISCHR(s.st_mode))
99 if (!S_ISBLK(s.st_mode))
102 printf(_("%s is not a block special device.\n"), device);
107 #ifdef HAVE_LINUX_MAJOR_H
109 #define MAJOR(dev) ((dev)>>8)
110 #define MINOR(dev) ((dev) & 0xff)
112 #ifndef SCSI_BLK_MAJOR
113 #ifdef SCSI_DISK0_MAJOR
114 #ifdef SCSI_DISK8_MAJOR
115 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
116 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
117 ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
119 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
120 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
121 #endif /* defined(SCSI_DISK8_MAJOR) */
122 #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
124 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
125 #endif /* defined(SCSI_DISK0_MAJOR) */
126 #endif /* defined(SCSI_BLK_MAJOR) */
127 if (((MAJOR(s.st_rdev) == HD_MAJOR &&
128 MINOR(s.st_rdev)%64 == 0) ||
129 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
130 MINOR(s.st_rdev)%16 == 0))) {
131 printf(_("%s is entire device, not just one partition!\n"),
138 void check_mount(const char *device, int force, const char *type)
143 retval = ext2fs_check_if_mounted(device, &mount_flags);
145 com_err("ext2fs_check_if_mount", retval,
146 _("while determining whether %s is mounted."),
150 if (mount_flags & EXT2_MF_MOUNTED) {
151 fprintf(stderr, _("%s is mounted; "), device);
153 fputs(_("mke2fs forced anyway. Hope /etc/mtab is "
154 "incorrect.\n"), stderr);
158 fprintf(stderr, _("will not make a %s here!\n"), type);
161 if (mount_flags & EXT2_MF_BUSY) {
162 fprintf(stderr, _("%s is apparently in use by the system; "),
165 fputs(_("mke2fs forced anyway.\n"), stderr);
172 void parse_journal_opts(const char *opts)
174 char *buf, *token, *next, *p, *arg;
176 int journal_usage = 0;
181 fputs(_("Couldn't allocate memory to parse journal "
182 "options!\n"), stderr);
186 for (token = buf; token && *token; token = next) {
187 p = strchr(token, ',');
193 arg = strchr(token, '=');
199 printf("Journal option=%s, argument=%s\n", token,
202 if (strcmp(token, "device") == 0) {
203 journal_device = blkid_get_devname(NULL, arg, NULL);
204 if (!journal_device) {
206 fprintf(stderr, _("\nCould not find "
207 "journal device matching %s\n"),
212 } else if (strcmp(token, "size") == 0) {
217 journal_size = strtoul(arg, &p, 0);
220 } else if (strcmp(token, "v1_superblock") == 0) {
221 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
227 fputs(_("\nBad journal options specified.\n\n"
228 "Journal options are separated by commas, "
229 "and may take an argument which\n"
230 "\tis set off by an equals ('=') sign.\n\n"
231 "Valid journal options are:\n"
232 "\tsize=<journal size in megabytes>\n"
233 "\tdevice=<journal device>\n\n"
234 "The journal size must be between "
235 "1024 and 10240000 filesystem blocks.\n\n"), stderr);
243 * Determine the number of journal blocks to use, either via
244 * user-specified # of megabytes, or via some intelligently selected
247 * Find a reasonable journal file size (in blocks) given the number of blocks
248 * in the filesystem. For very small filesystems, it is not reasonable to
249 * have a journal that fills more than half of the filesystem.
251 unsigned int figure_journal_size(int size, ext2_filsys fs)
255 j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
257 fputs(_("\nFilesystem too small for a journal\n"), stderr);
262 j_blocks = size * 1024 / (fs->blocksize / 1024);
263 if (j_blocks < 1024 || j_blocks > 10240000) {
264 fprintf(stderr, _("\nThe requested journal "
265 "size is %d blocks; it must be\n"
266 "between 1024 and 10240000 blocks. "
271 if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
272 fputs(_("\nJournal size too big for filesystem.\n"),
280 void print_check_message(int mnt, unsigned int check)
286 printf(_("This filesystem will be automatically "
287 "checked every %d mounts or\n"
288 "%g days, whichever comes first. "
289 "Use tune2fs -c or -i to override.\n"),
290 mnt, ((double) check) / (3600 * 24));
293 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
297 printf("MMP check failed: %s\n", msg);
299 time_t t = mmp->mmp_time;
301 printf("MMP error info: last update: %s node: %s device: %s\n",
302 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);