]>
Commit | Line | Data |
---|---|---|
63985320 TT |
1 | /* |
2 | * util.c --- helper functions used by tune2fs and mke2fs | |
efc6f628 | 3 | * |
63985320 TT |
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 | ||
6ef3920a TT |
12 | #define _LARGEFILE_SOURCE |
13 | #define _LARGEFILE64_SOURCE | |
14 | ||
d1154eb4 | 15 | #include "config.h" |
63985320 TT |
16 | #include <stdio.h> |
17 | #include <string.h> | |
18 | #ifdef HAVE_ERRNO_H | |
19 | #include <errno.h> | |
20 | #endif | |
63985320 TT |
21 | #ifdef HAVE_LINUX_MAJOR_H |
22 | #include <linux/major.h> | |
d71a495d TT |
23 | #endif |
24 | #ifdef HAVE_SYS_STAT_H | |
63985320 TT |
25 | #include <sys/stat.h> |
26 | #endif | |
0f5eba75 | 27 | #include <time.h> |
63985320 TT |
28 | |
29 | #include "et/com_err.h" | |
30 | #include "e2p/e2p.h" | |
54c637d4 | 31 | #include "ext2fs/ext2_fs.h" |
63985320 TT |
32 | #include "ext2fs/ext2fs.h" |
33 | #include "nls-enable.h" | |
ed1b33e8 | 34 | #include "blkid/blkid.h" |
63985320 TT |
35 | #include "util.h" |
36 | ||
37 | #ifndef HAVE_STRCASECMP | |
38 | int strcasecmp (char *s1, char *s2) | |
39 | { | |
40 | while (*s1 && *s2) { | |
41 | int ch1 = *s1++, ch2 = *s2++; | |
42 | if (isupper (ch1)) | |
43 | ch1 = tolower (ch1); | |
44 | if (isupper (ch2)) | |
45 | ch2 = tolower (ch2); | |
46 | if (ch1 != ch2) | |
47 | return ch1 - ch2; | |
48 | } | |
49 | return *s1 ? 1 : *s2 ? -1 : 0; | |
50 | } | |
51 | #endif | |
52 | ||
0072f8de AD |
53 | /* |
54 | * Given argv[0], return the program name. | |
55 | */ | |
56 | char *get_progname(char *argv_zero) | |
57 | { | |
58 | char *cp; | |
59 | ||
60 | cp = strrchr(argv_zero, '/'); | |
61 | if (!cp ) | |
62 | return argv_zero; | |
63 | else | |
64 | return cp+1; | |
65 | } | |
66 | ||
63985320 TT |
67 | void proceed_question(void) |
68 | { | |
69 | char buf[256]; | |
c8c071a0 | 70 | const char *short_yes = _("yY"); |
63985320 TT |
71 | |
72 | fflush(stdout); | |
73 | fflush(stderr); | |
54434927 | 74 | fputs(_("Proceed anyway? (y,n) "), stdout); |
63985320 | 75 | buf[0] = 0; |
d9039ae0 DL |
76 | if (!fgets(buf, sizeof(buf), stdin) || |
77 | strchr(short_yes, buf[0]) == 0) | |
63985320 TT |
78 | exit(1); |
79 | } | |
80 | ||
81 | void check_plausibility(const char *device) | |
82 | { | |
83 | int val; | |
c859cb1d | 84 | ext2fs_struct_stat s; |
efc6f628 | 85 | |
c859cb1d | 86 | val = ext2fs_stat(device, &s); |
efc6f628 | 87 | |
63985320 TT |
88 | if(val == -1) { |
89 | fprintf(stderr, _("Could not stat %s --- %s\n"), | |
90 | device, error_message(errno)); | |
91 | if (errno == ENOENT) | |
54434927 TT |
92 | fputs(_("\nThe device apparently does not exist; " |
93 | "did you specify it correctly?\n"), stderr); | |
63985320 TT |
94 | exit(1); |
95 | } | |
289e0557 | 96 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
b34cbddb MA |
97 | /* On FreeBSD, all disk devices are character specials */ |
98 | if (!S_ISBLK(s.st_mode) && !S_ISCHR(s.st_mode)) | |
99 | #else | |
100 | if (!S_ISBLK(s.st_mode)) | |
101 | #endif | |
102 | { | |
63985320 TT |
103 | printf(_("%s is not a block special device.\n"), device); |
104 | proceed_question(); | |
105 | return; | |
106 | } | |
107 | ||
108 | #ifdef HAVE_LINUX_MAJOR_H | |
109 | #ifndef MAJOR | |
110 | #define MAJOR(dev) ((dev)>>8) | |
111 | #define MINOR(dev) ((dev) & 0xff) | |
112 | #endif | |
113 | #ifndef SCSI_BLK_MAJOR | |
d07b1503 TT |
114 | #ifdef SCSI_DISK0_MAJOR |
115 | #ifdef SCSI_DISK8_MAJOR | |
116 | #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \ | |
117 | ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \ | |
118 | ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR)) | |
119 | #else | |
120 | #define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \ | |
121 | ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR)) | |
122 | #endif /* defined(SCSI_DISK8_MAJOR) */ | |
123 | #define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR) | |
124 | #else | |
63985320 | 125 | #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR) |
d07b1503 TT |
126 | #endif /* defined(SCSI_DISK0_MAJOR) */ |
127 | #endif /* defined(SCSI_BLK_MAJOR) */ | |
63985320 TT |
128 | if (((MAJOR(s.st_rdev) == HD_MAJOR && |
129 | MINOR(s.st_rdev)%64 == 0) || | |
130 | (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) && | |
131 | MINOR(s.st_rdev)%16 == 0))) { | |
132 | printf(_("%s is entire device, not just one partition!\n"), | |
133 | device); | |
134 | proceed_question(); | |
135 | } | |
136 | #endif | |
137 | } | |
138 | ||
139 | void check_mount(const char *device, int force, const char *type) | |
140 | { | |
141 | errcode_t retval; | |
142 | int mount_flags; | |
143 | ||
144 | retval = ext2fs_check_if_mounted(device, &mount_flags); | |
145 | if (retval) { | |
146 | com_err("ext2fs_check_if_mount", retval, | |
147 | _("while determining whether %s is mounted."), | |
148 | device); | |
149 | return; | |
150 | } | |
2fa8f37f TT |
151 | if (mount_flags & EXT2_MF_MOUNTED) { |
152 | fprintf(stderr, _("%s is mounted; "), device); | |
82896cbe | 153 | if (force >= 2) { |
2fa8f37f TT |
154 | fputs(_("mke2fs forced anyway. Hope /etc/mtab is " |
155 | "incorrect.\n"), stderr); | |
156 | return; | |
157 | } | |
158 | abort_mke2fs: | |
63985320 TT |
159 | fprintf(stderr, _("will not make a %s here!\n"), type); |
160 | exit(1); | |
161 | } | |
2fa8f37f TT |
162 | if (mount_flags & EXT2_MF_BUSY) { |
163 | fprintf(stderr, _("%s is apparently in use by the system; "), | |
164 | device); | |
82896cbe | 165 | if (force >= 2) { |
2fa8f37f TT |
166 | fputs(_("mke2fs forced anyway.\n"), stderr); |
167 | return; | |
168 | } | |
169 | goto abort_mke2fs; | |
170 | } | |
63985320 TT |
171 | } |
172 | ||
173 | void parse_journal_opts(const char *opts) | |
174 | { | |
175 | char *buf, *token, *next, *p, *arg; | |
176 | int len; | |
177 | int journal_usage = 0; | |
178 | ||
179 | len = strlen(opts); | |
180 | buf = malloc(len+1); | |
181 | if (!buf) { | |
54434927 TT |
182 | fputs(_("Couldn't allocate memory to parse journal " |
183 | "options!\n"), stderr); | |
63985320 TT |
184 | exit(1); |
185 | } | |
186 | strcpy(buf, opts); | |
187 | for (token = buf; token && *token; token = next) { | |
188 | p = strchr(token, ','); | |
189 | next = 0; | |
190 | if (p) { | |
191 | *p = 0; | |
192 | next = p+1; | |
efc6f628 | 193 | } |
63985320 TT |
194 | arg = strchr(token, '='); |
195 | if (arg) { | |
196 | *arg = 0; | |
197 | arg++; | |
198 | } | |
199 | #if 0 | |
200 | printf("Journal option=%s, argument=%s\n", token, | |
201 | arg ? arg : "NONE"); | |
202 | #endif | |
203 | if (strcmp(token, "device") == 0) { | |
ed1b33e8 | 204 | journal_device = blkid_get_devname(NULL, arg, NULL); |
2d15576d | 205 | if (!journal_device) { |
1bb14a22 ES |
206 | if (arg) |
207 | fprintf(stderr, _("\nCould not find " | |
208 | "journal device matching %s\n"), | |
209 | arg); | |
63985320 TT |
210 | journal_usage++; |
211 | continue; | |
212 | } | |
63985320 TT |
213 | } else if (strcmp(token, "size") == 0) { |
214 | if (!arg) { | |
215 | journal_usage++; | |
216 | continue; | |
217 | } | |
218 | journal_size = strtoul(arg, &p, 0); | |
2537b6d0 | 219 | if (*p) |
63985320 | 220 | journal_usage++; |
63985320 TT |
221 | } else if (strcmp(token, "v1_superblock") == 0) { |
222 | journal_flags |= EXT2_MKJOURNAL_V1_SUPER; | |
223 | continue; | |
d6a27e00 TT |
224 | } else |
225 | journal_usage++; | |
63985320 TT |
226 | } |
227 | if (journal_usage) { | |
54434927 | 228 | fputs(_("\nBad journal options specified.\n\n" |
63985320 TT |
229 | "Journal options are separated by commas, " |
230 | "and may take an argument which\n" | |
231 | "\tis set off by an equals ('=') sign.\n\n" | |
ddc32a04 | 232 | "Valid journal options are:\n" |
63985320 TT |
233 | "\tsize=<journal size in megabytes>\n" |
234 | "\tdevice=<journal device>\n\n" | |
2537b6d0 | 235 | "The journal size must be between " |
7087bbec | 236 | "1024 and 10240000 filesystem blocks.\n\n"), stderr); |
45415c2d | 237 | free(buf); |
63985320 TT |
238 | exit(1); |
239 | } | |
45415c2d | 240 | free(buf); |
efc6f628 | 241 | } |
63985320 | 242 | |
5683e6e2 | 243 | /* |
2537b6d0 TT |
244 | * Determine the number of journal blocks to use, either via |
245 | * user-specified # of megabytes, or via some intelligently selected | |
246 | * defaults. | |
efc6f628 | 247 | * |
5683e6e2 TT |
248 | * Find a reasonable journal file size (in blocks) given the number of blocks |
249 | * in the filesystem. For very small filesystems, it is not reasonable to | |
250 | * have a journal that fills more than half of the filesystem. | |
251 | */ | |
d4e0b1c6 | 252 | unsigned int figure_journal_size(int size, ext2_filsys fs) |
5683e6e2 | 253 | { |
56d12367 | 254 | int j_blocks; |
5683e6e2 | 255 | |
4efbac6f | 256 | j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super)); |
56d12367 | 257 | if (j_blocks < 0) { |
54434927 | 258 | fputs(_("\nFilesystem too small for a journal\n"), stderr); |
2537b6d0 TT |
259 | return 0; |
260 | } | |
efc6f628 | 261 | |
9dc6ad1e | 262 | if (size > 0) { |
4ea7bd04 | 263 | j_blocks = size * 1024 / (fs->blocksize / 1024); |
f331acbe | 264 | if (j_blocks < 1024 || j_blocks > 10240000) { |
2537b6d0 TT |
265 | fprintf(stderr, _("\nThe requested journal " |
266 | "size is %d blocks; it must be\n" | |
f331acbe | 267 | "between 1024 and 10240000 blocks. " |
2537b6d0 TT |
268 | "Aborting.\n"), |
269 | j_blocks); | |
270 | exit(1); | |
271 | } | |
4efbac6f | 272 | if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) { |
54434927 TT |
273 | fputs(_("\nJournal size too big for filesystem.\n"), |
274 | stderr); | |
2537b6d0 TT |
275 | exit(1); |
276 | } | |
2537b6d0 | 277 | } |
5683e6e2 TT |
278 | return j_blocks; |
279 | } | |
2537b6d0 | 280 | |
14b283ae | 281 | void print_check_message(int mnt, unsigned int check) |
66cf2f60 | 282 | { |
14b283ae TT |
283 | if (mnt < 0) |
284 | mnt = 0; | |
285 | if (!mnt && !check) | |
286 | return; | |
66cf2f60 TT |
287 | printf(_("This filesystem will be automatically " |
288 | "checked every %d mounts or\n" | |
289 | "%g days, whichever comes first. " | |
290 | "Use tune2fs -c or -i to override.\n"), | |
faa2dcda | 291 | mnt, ((double) check) / (3600 * 24)); |
66cf2f60 | 292 | } |
0f5eba75 AD |
293 | |
294 | void dump_mmp_msg(struct mmp_struct *mmp, const char *msg) | |
295 | { | |
296 | ||
297 | if (msg) | |
298 | printf("MMP check failed: %s\n", msg); | |
299 | if (mmp) { | |
300 | time_t t = mmp->mmp_time; | |
301 | ||
302 | printf("MMP error info: last update: %s node: %s device: %s\n", | |
303 | ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname); | |
304 | } | |
305 | } |