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
33 #include "et/com_err.h"
35 #include "ext2fs/ext2_fs.h"
36 #include "ext2fs/ext2fs.h"
37 #include "nls-enable.h"
38 #include "blkid/blkid.h"
41 char *journal_location_string = NULL;
43 #ifndef HAVE_STRCASECMP
44 int strcasecmp (char *s1, char *s2)
47 int ch1 = *s1++, ch2 = *s2++;
55 return *s1 ? 1 : *s2 ? -1 : 0;
60 * Given argv[0], return the program name.
62 char *get_progname(char *argv_zero)
66 cp = strrchr(argv_zero, '/');
73 static jmp_buf alarm_env;
75 static void alarm_signal(int signal)
77 longjmp(alarm_env, 1);
80 void proceed_question(int delay)
83 const char *short_yes = _("yY");
88 if (setjmp(alarm_env)) {
89 signal(SIGALRM, SIG_IGN);
90 printf(_("<proceeding>\n"));
93 signal(SIGALRM, alarm_signal);
94 printf(_("Proceed anyway (or wait %d seconds) ? (y,n) "),
98 fputs(_("Proceed anyway? (y,n) "), stdout);
100 if (!fgets(buf, sizeof(buf), stdin) ||
101 strchr(short_yes, buf[0]) == 0) {
105 signal(SIGALRM, SIG_IGN);
108 static void print_ext2_info(const char *device)
111 struct ext2_super_block *sb;
117 retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0,
118 unix_io_manager, &fs);
125 if (sb->s_last_mounted[0]) {
126 memset(buf, 0, sizeof(buf));
127 strncpy(buf, sb->s_last_mounted,
128 sizeof(sb->s_last_mounted));
129 printf(_("\tlast mounted on %s on %s"), buf,
132 printf(_("\tlast mounted on %s"), ctime(&tm));
133 } else if (sb->s_mkfs_time) {
134 tm = sb->s_mkfs_time;
135 printf(_("\tcreated on %s"), ctime(&tm));
136 } else if (sb->s_wtime) {
138 printf(_("\tlast modified on %s"), ctime(&tm));
144 * return 1 if there is no partition table, 0 if a partition table is
145 * detected, and -1 on an error.
147 static int check_partition_table(const char *device)
149 #ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS
154 pr = blkid_new_probe_from_filename(device);
158 ret = blkid_probe_enable_partitions(pr, 1);
162 ret = blkid_probe_enable_superblocks(pr, 0);
166 ret = blkid_do_fullprobe(pr);
170 ret = blkid_probe_lookup_value(pr, "PTTYPE", &value, NULL);
172 fprintf(stderr, _("Found a %s partition table in %s\n"),
178 blkid_free_probe(pr);
186 * return 1 if the device looks plausible, creating the file if necessary
188 int check_plausibility(const char *device, int flags, int *ret_is_dev)
190 int fd, ret, is_dev = 0;
191 ext2fs_struct_stat s;
193 blkid_cache cache = NULL;
194 char *fs_type = NULL;
195 char *fs_label = NULL;
197 fd = open(device, fl, 0666);
198 if ((fd < 0) && (errno == ENOENT) && (flags & CREATE_FILE)) {
200 fd = open(device, fl, 0666);
201 if (fd >= 0 && (flags & VERBOSE_CREATE))
202 printf(_("Creating regular file %s\n"), device);
205 fprintf(stderr, _("Could not open %s: %s\n"),
206 device, error_message(errno));
208 fputs(_("\nThe device apparently does not exist; "
209 "did you specify it correctly?\n"), stderr);
213 if (ext2fs_fstat(fd, &s) < 0) {
219 if (S_ISBLK(s.st_mode))
221 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
222 /* On FreeBSD, all disk devices are character specials */
223 if (S_ISCHR(s.st_mode))
227 *ret_is_dev = is_dev;
229 if ((flags & CHECK_BLOCK_DEV) && !is_dev) {
230 printf(_("%s is not a block special device.\n"), device);
235 * Note: we use the older-style blkid API's here because we
236 * want as much functionality to be available when using the
237 * internal blkid library, when e2fsprogs is compiled for
238 * non-Linux systems that will probably not have the libraries
239 * from util-linux available. We only use the newer
240 * blkid-probe interfaces to access functionality not
241 * available in the original blkid library.
243 if ((flags & CHECK_FS_EXIST) && blkid_get_cache(&cache, NULL) >= 0) {
244 fs_type = blkid_get_tag_value(cache, "TYPE", device);
246 fs_label = blkid_get_tag_value(cache, "LABEL", device);
247 blkid_put_cache(cache);
252 printf(_("%s contains a %s file system "
253 "labelled '%s'\n"), device, fs_type, fs_label);
255 printf(_("%s contains a %s file system\n"), device,
257 if (strncmp(fs_type, "ext", 3) == 0)
258 print_ext2_info(device);
264 ret = check_partition_table(device);
268 #ifdef HAVE_LINUX_MAJOR_H
270 #define MAJOR(dev) ((dev)>>8)
271 #define MINOR(dev) ((dev) & 0xff)
273 #ifndef SCSI_BLK_MAJOR
274 #ifdef SCSI_DISK0_MAJOR
275 #ifdef SCSI_DISK8_MAJOR
276 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
277 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
278 ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
280 #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
281 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
282 #endif /* defined(SCSI_DISK8_MAJOR) */
283 #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
285 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
286 #endif /* defined(SCSI_DISK0_MAJOR) */
287 #endif /* defined(SCSI_BLK_MAJOR) */
288 if (((MAJOR(s.st_rdev) == HD_MAJOR &&
289 MINOR(s.st_rdev)%64 == 0) ||
290 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
291 MINOR(s.st_rdev)%16 == 0))) {
292 printf(_("%s is entire device, not just one partition!\n"),
300 void check_mount(const char *device, int force, const char *type)
305 retval = ext2fs_check_if_mounted(device, &mount_flags);
307 com_err("ext2fs_check_if_mount", retval,
308 _("while determining whether %s is mounted."),
312 if (mount_flags & EXT2_MF_MOUNTED) {
313 fprintf(stderr, _("%s is mounted; "), device);
315 fputs(_("mke2fs forced anyway. Hope /etc/mtab is "
316 "incorrect.\n"), stderr);
320 fprintf(stderr, _("will not make a %s here!\n"), type);
323 if (mount_flags & EXT2_MF_BUSY) {
324 fprintf(stderr, _("%s is apparently in use by the system; "),
327 fputs(_("mke2fs forced anyway.\n"), stderr);
334 void parse_journal_opts(const char *opts)
336 char *buf, *token, *next, *p, *arg;
338 int journal_usage = 0;
343 fputs(_("Couldn't allocate memory to parse journal "
344 "options!\n"), stderr);
348 for (token = buf; token && *token; token = next) {
349 p = strchr(token, ',');
355 arg = strchr(token, '=');
361 printf("Journal option=%s, argument=%s\n", token,
364 if (strcmp(token, "device") == 0) {
365 journal_device = blkid_get_devname(NULL, arg, NULL);
366 if (!journal_device) {
368 fprintf(stderr, _("\nCould not find "
369 "journal device matching %s\n"),
374 } else if (strcmp(token, "size") == 0) {
379 journal_size = strtoul(arg, &p, 0);
382 } else if (!strcmp(token, "location")) {
387 journal_location_string = strdup(arg);
388 } else if (strcmp(token, "v1_superblock") == 0) {
389 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
395 fputs(_("\nBad journal options specified.\n\n"
396 "Journal options are separated by commas, "
397 "and may take an argument which\n"
398 "\tis set off by an equals ('=') sign.\n\n"
399 "Valid journal options are:\n"
400 "\tsize=<journal size in megabytes>\n"
401 "\tdevice=<journal device>\n"
402 "\tlocation=<journal location>\n\n"
403 "The journal size must be between "
404 "1024 and 10240000 filesystem blocks.\n\n"), stderr);
412 * Determine the number of journal blocks to use, either via
413 * user-specified # of megabytes, or via some intelligently selected
416 * Find a reasonable journal file size (in blocks) given the number of blocks
417 * in the filesystem. For very small filesystems, it is not reasonable to
418 * have a journal that fills more than half of the filesystem.
420 unsigned int figure_journal_size(int size, ext2_filsys fs)
424 j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
426 fputs(_("\nFilesystem too small for a journal\n"), stderr);
431 j_blocks = size * 1024 / (fs->blocksize / 1024);
432 if (j_blocks < 1024 || j_blocks > 10240000) {
433 fprintf(stderr, _("\nThe requested journal "
434 "size is %d blocks; it must be\n"
435 "between 1024 and 10240000 blocks. "
440 if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
441 fputs(_("\nJournal size too big for filesystem.\n"),
449 void print_check_message(int mnt, unsigned int check)
455 printf(_("This filesystem will be automatically "
456 "checked every %d mounts or\n"
457 "%g days, whichever comes first. "
458 "Use tune2fs -c or -i to override.\n"),
459 mnt, ((double) check) / (3600 * 24));
462 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
466 printf("MMP check failed: %s\n", msg);
468 time_t t = mmp->mmp_time;
470 printf("MMP error info: last update: %s node: %s device: %s\n",
471 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);