util.c revision 4ea7bd04390935e1f8b473c8b857e518df2e226b
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#define _LARGEFILE_SOURCE
13#define _LARGEFILE64_SOURCE
14
15#include <stdio.h>
16#include <string.h>
17#ifdef HAVE_ERRNO_H
18#include <errno.h>
19#endif
20#ifdef HAVE_LINUX_MAJOR_H
21#include <linux/major.h>
22#endif
23#ifdef HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26
27#include "et/com_err.h"
28#include "e2p/e2p.h"
29#include "ext2fs/ext2_fs.h"
30#include "ext2fs/ext2fs.h"
31#include "nls-enable.h"
32#include "get_device_by_label.h"
33#include "util.h"
34
35#ifndef HAVE_STRCASECMP
36int strcasecmp (char *s1, char *s2)
37{
38	while (*s1 && *s2) {
39		int ch1 = *s1++, ch2 = *s2++;
40		if (isupper (ch1))
41			ch1 = tolower (ch1);
42		if (isupper (ch2))
43			ch2 = tolower (ch2);
44		if (ch1 != ch2)
45			return ch1 - ch2;
46	}
47	return *s1 ? 1 : *s2 ? -1 : 0;
48}
49#endif
50
51void proceed_question(void)
52{
53	char buf[256];
54	const char *short_yes = _("yY");
55
56	fflush(stdout);
57	fflush(stderr);
58	printf(_("Proceed anyway? (y,n) "));
59	buf[0] = 0;
60	fgets(buf, sizeof(buf), stdin);
61	if (strchr(short_yes, buf[0]) == 0)
62		exit(1);
63}
64
65void check_plausibility(const char *device)
66{
67	int val;
68#ifdef HAVE_OPEN64
69	struct stat64 s;
70
71	val = stat64(device, &s);
72#else
73	struct stat s;
74
75	val = stat(device, &s);
76#endif
77
78	if(val == -1) {
79		fprintf(stderr, _("Could not stat %s --- %s\n"),
80			device, error_message(errno));
81		if (errno == ENOENT)
82			fprintf(stderr, _("\nThe device apparently does "
83			       "not exist; did you specify it correctly?\n"));
84		exit(1);
85	}
86	if (!S_ISBLK(s.st_mode)) {
87		printf(_("%s is not a block special device.\n"), device);
88		proceed_question();
89		return;
90	}
91
92#ifdef HAVE_LINUX_MAJOR_H
93#ifndef MAJOR
94#define MAJOR(dev)	((dev)>>8)
95#define MINOR(dev)	((dev) & 0xff)
96#endif
97#ifndef SCSI_BLK_MAJOR
98#define SCSI_BLK_MAJOR(M)  ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
99#endif
100	if (((MAJOR(s.st_rdev) == HD_MAJOR &&
101	      MINOR(s.st_rdev)%64 == 0) ||
102	     (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
103	      MINOR(s.st_rdev)%16 == 0))) {
104		printf(_("%s is entire device, not just one partition!\n"),
105		       device);
106		proceed_question();
107	}
108#endif
109}
110
111void check_mount(const char *device, int force, const char *type)
112{
113	errcode_t	retval;
114	int		mount_flags;
115
116	retval = ext2fs_check_if_mounted(device, &mount_flags);
117	if (retval) {
118		com_err("ext2fs_check_if_mount", retval,
119			_("while determining whether %s is mounted."),
120			device);
121		return;
122	}
123	if (!(mount_flags & EXT2_MF_MOUNTED))
124		return;
125
126	fprintf(stderr, _("%s is mounted; "), device);
127	if (force) {
128		fprintf(stderr, _("mke2fs forced anyway.  "
129			"Hope /etc/mtab is incorrect.\n"));
130	} else {
131		fprintf(stderr, _("will not make a %s here!\n"), type);
132		exit(1);
133	}
134}
135
136void parse_journal_opts(const char *opts)
137{
138	char	*buf, *token, *next, *p, *arg;
139	int	len;
140	int	journal_usage = 0;
141
142	len = strlen(opts);
143	buf = malloc(len+1);
144	if (!buf) {
145		fprintf(stderr, _("Couldn't allocate memory to parse "
146			"journal options!\n"));
147		exit(1);
148	}
149	strcpy(buf, opts);
150	for (token = buf; token && *token; token = next) {
151		p = strchr(token, ',');
152		next = 0;
153		if (p) {
154			*p = 0;
155			next = p+1;
156		}
157		arg = strchr(token, '=');
158		if (arg) {
159			*arg = 0;
160			arg++;
161		}
162#if 0
163		printf("Journal option=%s, argument=%s\n", token,
164		       arg ? arg : "NONE");
165#endif
166		if (strcmp(token, "device") == 0) {
167			journal_device = interpret_spec(arg);
168			if (!journal_device) {
169				journal_usage++;
170				continue;
171			}
172		} else if (strcmp(token, "size") == 0) {
173			if (!arg) {
174				journal_usage++;
175				continue;
176			}
177			journal_size = strtoul(arg, &p, 0);
178			if (*p)
179				journal_usage++;
180		} else if (strcmp(token, "v1_superblock") == 0) {
181			journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
182			continue;
183		} else
184			journal_usage++;
185	}
186	if (journal_usage) {
187		fprintf(stderr, _("\nBad journal options specified.\n\n"
188			"Journal options are separated by commas, "
189			"and may take an argument which\n"
190			"\tis set off by an equals ('=') sign.\n\n"
191			"Valid raid options are:\n"
192			"\tsize=<journal size in megabytes>\n"
193			"\tdevice=<journal device>\n\n"
194			"The journal size must be between "
195			"1024 and 102400 filesystem blocks.\n\n" ));
196		exit(1);
197	}
198}
199
200/*
201 * Determine the number of journal blocks to use, either via
202 * user-specified # of megabytes, or via some intelligently selected
203 * defaults.
204 *
205 * Find a reasonable journal file size (in blocks) given the number of blocks
206 * in the filesystem.  For very small filesystems, it is not reasonable to
207 * have a journal that fills more than half of the filesystem.
208 */
209int figure_journal_size(int size, ext2_filsys fs)
210{
211	blk_t j_blocks;
212
213	if (fs->super->s_blocks_count < 2048) {
214		fprintf(stderr, _("\nFilesystem too small for a journal\n"));
215		return 0;
216	}
217
218	if (size >= 0) {
219		j_blocks = size * 1024 / (fs->blocksize	/ 1024);
220		if (j_blocks < 1024 || j_blocks > 102400) {
221			fprintf(stderr, _("\nThe requested journal "
222				"size is %d blocks; it must be\n"
223				"between 1024 and 102400 blocks.  "
224				"Aborting.\n"),
225				j_blocks);
226			exit(1);
227		}
228		if (j_blocks > fs->super->s_free_blocks_count) {
229			fprintf(stderr, _("\nJournal size too big "
230					  "for filesystem.\n"));
231			exit(1);
232		}
233		return j_blocks;
234	}
235
236	if (fs->super->s_blocks_count < 32768)
237		j_blocks = 1024;
238	else if (fs->super->s_blocks_count < 262144)
239		j_blocks = 4096;
240	else
241		j_blocks = 8192;
242
243	return j_blocks;
244}
245
246void print_check_message(ext2_filsys fs)
247{
248	printf(_("This filesystem will be automatically "
249		 "checked every %d mounts or\n"
250		 "%g days, whichever comes first.  "
251		 "Use tune2fs -c or -i to override.\n"),
252	       fs->super->s_max_mnt_count,
253	       (double)fs->super->s_checkinterval / (3600 * 24));
254}
255