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
27 #include "et/com_err.h"
29 #include "ext2fs/ext2_fs.h"
30 #include "ext2fs/ext2fs.h"
31 #include "nls-enable.h"
32 #include "blkid/blkid.h"
35 #ifndef HAVE_STRCASECMP
36 int strcasecmp (char *s1, char *s2)
39 int ch1 = *s1++, ch2 = *s2++;
47 return *s1 ? 1 : *s2 ? -1 : 0;
52 * Given argv[0], return the program name.
54 char *get_progname(char *argv_zero)
58 cp = strrchr(argv_zero, '/');
65 void proceed_question(void)
68 const char *short_yes = _("yY");
72 printf(_("Proceed anyway? (y,n) "));
74 fgets(buf, sizeof(buf), stdin);
75 if (strchr(short_yes, buf[0]) == 0)
79 void check_plausibility(const char *device)
85 val = stat64(device, &s);
89 val = stat(device, &s);
93 fprintf(stderr, _("Could not stat %s --- %s\n"),
94 device, error_message(errno));
96 fprintf(stderr, _("\nThe device apparently does "
97 "not exist; did you specify it correctly?\n"));
100 if (!S_ISBLK(s.st_mode)) {
101 printf(_("%s is not a block special device.\n"), device);
106 #ifdef HAVE_LINUX_MAJOR_H
108 #define MAJOR(dev) ((dev)>>8)
109 #define MINOR(dev) ((dev) & 0xff)
111 #ifndef SCSI_BLK_MAJOR
112 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
114 if (((MAJOR(s.st_rdev) == HD_MAJOR &&
115 MINOR(s.st_rdev)%64 == 0) ||
116 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
117 MINOR(s.st_rdev)%16 == 0))) {
118 printf(_("%s is entire device, not just one partition!\n"),
125 void check_mount(const char *device, int force, const char *type)
130 retval = ext2fs_check_if_mounted(device, &mount_flags);
132 com_err("ext2fs_check_if_mount", retval,
133 _("while determining whether %s is mounted."),
137 if (!(mount_flags & EXT2_MF_MOUNTED))
140 fprintf(stderr, _("%s is mounted; "), device);
142 fprintf(stderr, _("mke2fs forced anyway. "
143 "Hope /etc/mtab is incorrect.\n"));
145 fprintf(stderr, _("will not make a %s here!\n"), type);
150 void parse_journal_opts(const char *opts)
152 char *buf, *token, *next, *p, *arg;
154 int journal_usage = 0;
159 fprintf(stderr, _("Couldn't allocate memory to parse "
160 "journal options!\n"));
164 for (token = buf; token && *token; token = next) {
165 p = strchr(token, ',');
171 arg = strchr(token, '=');
177 printf("Journal option=%s, argument=%s\n", token,
180 if (strcmp(token, "device") == 0) {
181 journal_device = blkid_get_devname(NULL, arg, NULL);
182 if (!journal_device) {
186 } else if (strcmp(token, "size") == 0) {
191 journal_size = strtoul(arg, &p, 0);
194 } else if (strcmp(token, "v1_superblock") == 0) {
195 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
201 fprintf(stderr, _("\nBad journal options specified.\n\n"
202 "Journal options are separated by commas, "
203 "and may take an argument which\n"
204 "\tis set off by an equals ('=') sign.\n\n"
205 "Valid journal options are:\n"
206 "\tsize=<journal size in megabytes>\n"
207 "\tdevice=<journal device>\n\n"
208 "The journal size must be between "
209 "1024 and 102400 filesystem blocks.\n\n" ));
215 * Determine the number of journal blocks to use, either via
216 * user-specified # of megabytes, or via some intelligently selected
219 * Find a reasonable journal file size (in blocks) given the number of blocks
220 * in the filesystem. For very small filesystems, it is not reasonable to
221 * have a journal that fills more than half of the filesystem.
223 int figure_journal_size(int size, ext2_filsys fs)
227 if (fs->super->s_blocks_count < 2048) {
228 fprintf(stderr, _("\nFilesystem too small for a journal\n"));
233 j_blocks = size * 1024 / (fs->blocksize / 1024);
234 if (j_blocks < 1024 || j_blocks > 102400) {
235 fprintf(stderr, _("\nThe requested journal "
236 "size is %d blocks; it must be\n"
237 "between 1024 and 102400 blocks. "
242 if (j_blocks > fs->super->s_free_blocks_count) {
243 fprintf(stderr, _("\nJournal size too big "
244 "for filesystem.\n"));
250 if (fs->super->s_blocks_count < 32768)
252 else if (fs->super->s_blocks_count < 262144)
260 void print_check_message(ext2_filsys fs)
262 printf(_("This filesystem will be automatically "
263 "checked every %d mounts or\n"
264 "%g days, whichever comes first. "
265 "Use tune2fs -c or -i to override.\n"),
266 fs->super->s_max_mnt_count,
267 (double)fs->super->s_checkinterval / (3600 * 24));