Home | History | Annotate | Download | only in misc
      1 /*
      2  * util.c --- helper functions used by tune2fs and mke2fs
      3  *
      4  * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Public
      8  * License.
      9  * %End-Header%
     10  */
     11 
     12 #ifndef _LARGEFILE_SOURCE
     13 #define _LARGEFILE_SOURCE
     14 #endif
     15 #ifndef _LARGEFILE64_SOURCE
     16 #define _LARGEFILE64_SOURCE
     17 #endif
     18 
     19 #include "config.h"
     20 #include <fcntl.h>
     21 #include <setjmp.h>
     22 #include <signal.h>
     23 #include <stdio.h>
     24 #include <string.h>
     25 #ifdef HAVE_ERRNO_H
     26 #include <errno.h>
     27 #endif
     28 #if HAVE_UNISTD_H
     29 #include <unistd.h>
     30 #endif
     31 #ifdef HAVE_LINUX_MAJOR_H
     32 #include <linux/major.h>
     33 #endif
     34 #include <sys/types.h>
     35 #ifdef HAVE_SYS_STAT_H
     36 #include <sys/stat.h>
     37 #endif
     38 #ifdef HAVE_UNISTD_H
     39 #include <unistd.h>
     40 #endif
     41 #include <time.h>
     42 
     43 #include "et/com_err.h"
     44 #include "e2p/e2p.h"
     45 #include "ext2fs/ext2_fs.h"
     46 #include "ext2fs/ext2fs.h"
     47 #include "support/nls-enable.h"
     48 #include "blkid/blkid.h"
     49 #include "util.h"
     50 
     51 char *journal_location_string = NULL;
     52 
     53 #ifndef HAVE_STRCASECMP
     54 int strcasecmp (char *s1, char *s2)
     55 {
     56 	while (*s1 && *s2) {
     57 		int ch1 = *s1++, ch2 = *s2++;
     58 		if (isupper (ch1))
     59 			ch1 = tolower (ch1);
     60 		if (isupper (ch2))
     61 			ch2 = tolower (ch2);
     62 		if (ch1 != ch2)
     63 			return ch1 - ch2;
     64 	}
     65 	return *s1 ? 1 : *s2 ? -1 : 0;
     66 }
     67 #endif
     68 
     69 /*
     70  * Given argv[0], return the program name.
     71  */
     72 char *get_progname(char *argv_zero)
     73 {
     74 	char	*cp;
     75 
     76 	cp = strrchr(argv_zero, '/');
     77 	if (!cp )
     78 		return argv_zero;
     79 	else
     80 		return cp+1;
     81 }
     82 
     83 static jmp_buf alarm_env;
     84 
     85 static void alarm_signal(int signal EXT2FS_ATTR((unused)))
     86 {
     87 	longjmp(alarm_env, 1);
     88 }
     89 
     90 void proceed_question(int delay)
     91 {
     92 	char buf[256];
     93 	const char *short_yes = _("yY");
     94 
     95 	fflush(stdout);
     96 	fflush(stderr);
     97 	if (delay > 0) {
     98 		if (setjmp(alarm_env)) {
     99 			signal(SIGALRM, SIG_IGN);
    100 			printf("%s", _("<proceeding>\n"));
    101 			return;
    102 		}
    103 		signal(SIGALRM, alarm_signal);
    104 		printf(_("Proceed anyway (or wait %d seconds) ? (y,n) "),
    105 		       delay);
    106 		alarm(delay);
    107 	} else
    108 		fputs(_("Proceed anyway? (y,n) "), stdout);
    109 	buf[0] = 0;
    110 	if (!fgets(buf, sizeof(buf), stdin) ||
    111 	    strchr(short_yes, buf[0]) == 0) {
    112 		putc('\n', stdout);
    113 		exit(1);
    114 	}
    115 	signal(SIGALRM, SIG_IGN);
    116 }
    117 
    118 void check_mount(const char *device, int force, const char *type)
    119 {
    120 	errcode_t	retval;
    121 	int		mount_flags;
    122 
    123 	retval = ext2fs_check_if_mounted(device, &mount_flags);
    124 	if (retval) {
    125 		com_err("ext2fs_check_if_mount", retval,
    126 			_("while determining whether %s is mounted."),
    127 			device);
    128 		return;
    129 	}
    130 	if (mount_flags & EXT2_MF_MOUNTED) {
    131 		fprintf(stderr, _("%s is mounted; "), device);
    132 		if (force >= 2) {
    133 			fputs(_("mke2fs forced anyway.  Hope /etc/mtab is "
    134 				"incorrect.\n"), stderr);
    135 			return;
    136 		}
    137 	abort_mke2fs:
    138 		fprintf(stderr, _("will not make a %s here!\n"), type);
    139 		exit(1);
    140 	}
    141 	if (mount_flags & EXT2_MF_BUSY) {
    142 		fprintf(stderr, _("%s is apparently in use by the system; "),
    143 			device);
    144 		if (force >= 2) {
    145 			fputs(_("mke2fs forced anyway.\n"), stderr);
    146 			return;
    147 		}
    148 		goto abort_mke2fs;
    149 	}
    150 }
    151 
    152 void parse_journal_opts(const char *opts)
    153 {
    154 	char	*buf, *token, *next, *p, *arg;
    155 	int	len;
    156 	int	journal_usage = 0;
    157 
    158 	len = strlen(opts);
    159 	buf = malloc(len+1);
    160 	if (!buf) {
    161 		fputs(_("Couldn't allocate memory to parse journal "
    162 			"options!\n"), stderr);
    163 		exit(1);
    164 	}
    165 	strcpy(buf, opts);
    166 	for (token = buf; token && *token; token = next) {
    167 		p = strchr(token, ',');
    168 		next = 0;
    169 		if (p) {
    170 			*p = 0;
    171 			next = p+1;
    172 		}
    173 		arg = strchr(token, '=');
    174 		if (arg) {
    175 			*arg = 0;
    176 			arg++;
    177 		}
    178 #if 0
    179 		printf("Journal option=%s, argument=%s\n", token,
    180 		       arg ? arg : "NONE");
    181 #endif
    182 		if (strcmp(token, "device") == 0) {
    183 			journal_device = blkid_get_devname(NULL, arg, NULL);
    184 			if (!journal_device) {
    185 				if (arg)
    186 					fprintf(stderr, _("\nCould not find "
    187 						"journal device matching %s\n"),
    188 						arg);
    189 				journal_usage++;
    190 				continue;
    191 			}
    192 		} else if (strcmp(token, "size") == 0) {
    193 			if (!arg) {
    194 				journal_usage++;
    195 				continue;
    196 			}
    197 			journal_size = strtoul(arg, &p, 0);
    198 			if (*p)
    199 				journal_usage++;
    200 		} else if (!strcmp(token, "location")) {
    201 			if (!arg) {
    202 				journal_usage++;
    203 				continue;
    204 			}
    205 			journal_location_string = strdup(arg);
    206 		} else if (strcmp(token, "v1_superblock") == 0) {
    207 			journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
    208 			continue;
    209 		} else
    210 			journal_usage++;
    211 	}
    212 	if (journal_usage) {
    213 		fputs(_("\nBad journal options specified.\n\n"
    214 			"Journal options are separated by commas, "
    215 			"and may take an argument which\n"
    216 			"\tis set off by an equals ('=') sign.\n\n"
    217 			"Valid journal options are:\n"
    218 			"\tsize=<journal size in megabytes>\n"
    219 			"\tdevice=<journal device>\n"
    220 			"\tlocation=<journal location>\n\n"
    221 			"The journal size must be between "
    222 			"1024 and 10240000 filesystem blocks.\n\n"), stderr);
    223 		free(buf);
    224 		exit(1);
    225 	}
    226 	free(buf);
    227 }
    228 
    229 /*
    230  * Determine the number of journal blocks to use, either via
    231  * user-specified # of megabytes, or via some intelligently selected
    232  * defaults.
    233  *
    234  * Find a reasonable journal file size (in blocks) given the number of blocks
    235  * in the filesystem.  For very small filesystems, it is not reasonable to
    236  * have a journal that fills more than half of the filesystem.
    237  */
    238 unsigned int figure_journal_size(int size, ext2_filsys fs)
    239 {
    240 	int j_blocks;
    241 
    242 	j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
    243 	if (j_blocks < 0) {
    244 		fputs(_("\nFilesystem too small for a journal\n"), stderr);
    245 		return 0;
    246 	}
    247 
    248 	if (size > 0) {
    249 		j_blocks = size * 1024 / (fs->blocksize	/ 1024);
    250 		if (j_blocks < 1024 || j_blocks > 10240000) {
    251 			fprintf(stderr, _("\nThe requested journal "
    252 				"size is %d blocks; it must be\n"
    253 				"between 1024 and 10240000 blocks.  "
    254 				"Aborting.\n"),
    255 				j_blocks);
    256 			exit(1);
    257 		}
    258 		if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
    259 			fputs(_("\nJournal size too big for filesystem.\n"),
    260 			      stderr);
    261 			exit(1);
    262 		}
    263 	}
    264 	return j_blocks;
    265 }
    266 
    267 void print_check_message(int mnt, unsigned int check)
    268 {
    269 	if (mnt < 0)
    270 		mnt = 0;
    271 	if (!mnt && !check)
    272 		return;
    273 	printf(_("This filesystem will be automatically "
    274 		 "checked every %d mounts or\n"
    275 		 "%g days, whichever comes first.  "
    276 		 "Use tune2fs -c or -i to override.\n"),
    277 	       mnt, ((double) check) / (3600 * 24));
    278 }
    279 
    280 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
    281 {
    282 
    283 	if (msg)
    284 		printf("MMP check failed: %s\n", msg);
    285 	if (mmp) {
    286 		time_t t = mmp->mmp_time;
    287 
    288 		printf("MMP error info: last update: %s node: %s device: %s\n",
    289 		       ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
    290 	}
    291 }
    292