Whamcloud - gitweb
tune2fs: fix an error message
[tools/e2fsprogs.git] / misc / util.c
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 "support/devname.h"
49 #include "blkid/blkid.h"
50 #include "util.h"
51
52 char *journal_location_string = NULL;
53
54 #ifndef HAVE_STRCASECMP
55 int strcasecmp (char *s1, char *s2)
56 {
57         while (*s1 && *s2) {
58                 int ch1 = *s1++, ch2 = *s2++;
59                 if (isupper (ch1))
60                         ch1 = tolower (ch1);
61                 if (isupper (ch2))
62                         ch2 = tolower (ch2);
63                 if (ch1 != ch2)
64                         return ch1 - ch2;
65         }
66         return *s1 ? 1 : *s2 ? -1 : 0;
67 }
68 #endif
69
70 /*
71  * Given argv[0], return the program name.
72  */
73 char *get_progname(char *argv_zero)
74 {
75         char    *cp;
76
77         cp = strrchr(argv_zero, '/');
78         if (!cp )
79                 return argv_zero;
80         else
81                 return cp+1;
82 }
83
84 static jmp_buf alarm_env;
85
86 static void alarm_signal(int signal EXT2FS_ATTR((unused)))
87 {
88         longjmp(alarm_env, 1);
89 }
90
91 void proceed_question(int delay)
92 {
93         char buf[256];
94         const char *short_yes = _("yY");
95         const char *english_yes = "yY";
96
97         fflush(stdout);
98         fflush(stderr);
99         if (delay > 0) {
100                 if (setjmp(alarm_env)) {
101                         signal(SIGALRM, SIG_IGN);
102                         printf("%s", _("<proceeding>\n"));
103                         return;
104                 }
105                 signal(SIGALRM, alarm_signal);
106                 printf(_("Proceed anyway (or wait %d seconds to proceed) ? (y,N) "),
107                        delay);
108                 alarm(delay);
109         } else
110                 fputs(_("Proceed anyway? (y,N) "), stdout);
111         buf[0] = 0;
112         if (!fgets(buf, sizeof(buf), stdin) ||
113             strchr(_("nN"), buf[0]) ||
114             !(strchr(short_yes, buf[0]) ||
115               strchr(english_yes, buf[0]))) {
116                 putc('\n', stdout);
117                 exit(1);
118         }
119         signal(SIGALRM, SIG_IGN);
120 }
121
122 void check_mount(const char *device, int force, const char *type)
123 {
124         errcode_t       retval;
125         int             mount_flags;
126
127         retval = ext2fs_check_if_mounted(device, &mount_flags);
128         if (retval) {
129                 com_err("ext2fs_check_if_mount", retval,
130                         _("while determining whether %s is mounted."),
131                         device);
132                 return;
133         }
134         if (mount_flags & EXT2_MF_MOUNTED) {
135                 fprintf(stderr, _("%s is mounted; "), device);
136                 if (force >= 2) {
137                         fputs(_("mke2fs forced anyway.  Hope /etc/mtab is "
138                                 "incorrect.\n"), stderr);
139                         return;
140                 }
141         abort_mke2fs:
142                 fprintf(stderr, _("will not make a %s here!\n"), type);
143                 exit(1);
144         }
145         if (mount_flags & EXT2_MF_BUSY) {
146                 fprintf(stderr, _("%s is apparently in use by the system; "),
147                         device);
148                 if (force >= 2) {
149                         fputs(_("mke2fs forced anyway.\n"), stderr);
150                         return;
151                 }
152                 goto abort_mke2fs;
153         }
154 }
155
156 void parse_journal_opts(const char *opts)
157 {
158         char    *buf, *token, *next, *p, *arg;
159         int     len;
160         int     journal_usage = 0;
161
162         len = strlen(opts);
163         buf = malloc(len+1);
164         if (!buf) {
165                 fputs(_("Couldn't allocate memory to parse journal "
166                         "options!\n"), stderr);
167                 exit(1);
168         }
169         strcpy(buf, opts);
170         for (token = buf; token && *token; token = next) {
171                 p = strchr(token, ',');
172                 next = 0;
173                 if (p) {
174                         *p = 0;
175                         next = p+1;
176                 }
177                 arg = strchr(token, '=');
178                 if (arg) {
179                         *arg = 0;
180                         arg++;
181                 }
182 #if 0
183                 printf("Journal option=%s, argument=%s\n", token,
184                        arg ? arg : "NONE");
185 #endif
186                 if (strcmp(token, "device") == 0) {
187                         journal_device = get_devname(NULL, arg, NULL);
188                         if (!journal_device) {
189                                 if (arg)
190                                         fprintf(stderr, _("\nCould not find "
191                                                 "journal device matching %s\n"),
192                                                 arg);
193                                 journal_usage++;
194                                 continue;
195                         }
196                 } else if (strcmp(token, "size") == 0) {
197                         if (!arg) {
198                                 journal_usage++;
199                                 continue;
200                         }
201                         journal_size = strtoul(arg, &p, 0);
202                         if (*p)
203                                 journal_usage++;
204                 } else if (strcmp(token, "fast_commit_size") == 0) {
205                         if (!arg) {
206                                 journal_usage++;
207                                 continue;
208                         }
209                         journal_fc_size = strtoul(arg, &p, 0);
210                         if (*p)
211                                 journal_usage++;
212                 } else if (!strcmp(token, "location")) {
213                         if (!arg) {
214                                 journal_usage++;
215                                 continue;
216                         }
217                         journal_location_string = strdup(arg);
218                 } else if (strcmp(token, "v1_superblock") == 0) {
219                         journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
220                         continue;
221                 } else
222                         journal_usage++;
223         }
224         if (journal_usage) {
225                 fputs(_("\nBad journal options specified.\n\n"
226                         "Journal options are separated by commas, "
227                         "and may take an argument which\n"
228                         "\tis set off by an equals ('=') sign.\n\n"
229                         "Valid journal options are:\n"
230                         "\tsize=<journal size in megabytes>\n"
231                         "\tdevice=<journal device>\n"
232                         "\tlocation=<journal location>\n\n"
233                         "The journal size must be between "
234                         "1024 and 10240000 filesystem blocks.\n\n"), stderr);
235                 free(buf);
236                 exit(1);
237         }
238         free(buf);
239 }
240
241 static inline int jsize_to_blks(ext2_filsys fs, int size)
242 {
243         return (size * 1024) / (fs->blocksize / 1024);
244 }
245
246 /* Fast commit size is in KBs */
247 static inline int fcsize_to_blks(ext2_filsys fs, int size)
248 {
249         return (size * 1024) / (fs->blocksize);
250 }
251
252 /*
253  * Determine the number of journal blocks to use, either via
254  * user-specified # of megabytes, or via some intelligently selected
255  * defaults.
256  *
257  * Find a reasonable journal file size (in blocks) given the number of blocks in
258  * the filesystem. For very small filesystems, it is not reasonable to have a
259  * journal that fills more than half of the filesystem.
260  */
261 void figure_journal_size(struct ext2fs_journal_params *jparams,
262                 int requested_j_size, int requested_fc_size, ext2_filsys fs)
263 {
264         int total_blocks, ret;
265
266         ret = ext2fs_get_journal_params(jparams, fs);
267         if (ret) {
268                 fputs(_("\nFilesystem too small for a journal\n"), stderr);
269                 return;
270         }
271
272         if (requested_j_size > 0 ||
273                 (ext2fs_has_feature_fast_commit(fs->super) && requested_fc_size > 0)) {
274                 if (requested_j_size > 0)
275                         jparams->num_journal_blocks =
276                                 jsize_to_blks(fs, requested_j_size);
277                 if (ext2fs_has_feature_fast_commit(fs->super) &&
278                         requested_fc_size > 0)
279                         jparams->num_fc_blocks =
280                                 fcsize_to_blks(fs, requested_fc_size);
281                 else if (!ext2fs_has_feature_fast_commit(fs->super))
282                         jparams->num_fc_blocks = 0;
283                 total_blocks = jparams->num_journal_blocks + jparams->num_fc_blocks;
284                 if (total_blocks < 1024 || total_blocks > 10240000) {
285                         fprintf(stderr, _("\nThe total requested journal "
286                                 "size is %d blocks; it must be\n"
287                                 "between 1024 and 10240000 blocks.  "
288                                 "Aborting.\n"),
289                                 total_blocks);
290                         exit(1);
291                 }
292                 if ((unsigned int) total_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
293                         fputs(_("\nTotal journal size too big for filesystem.\n"),
294                               stderr);
295                         exit(1);
296                 }
297         }
298 }
299
300 void print_check_message(int mnt, unsigned int check)
301 {
302         if (mnt < 0)
303                 mnt = 0;
304         if (!mnt && !check)
305                 return;
306         printf(_("This filesystem will be automatically "
307                  "checked every %d mounts or\n"
308                  "%g days, whichever comes first.  "
309                  "Use tune2fs -c or -i to override.\n"),
310                mnt, ((double) check) / (3600 * 24));
311 }
312
313 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
314 {
315
316         if (msg)
317                 printf("MMP check failed: %s\n", msg);
318         if (mmp) {
319                 time_t t = mmp->mmp_time;
320
321                 printf("MMP error info: node: %.*s, device: %.*s, updated: %s",
322                        EXT2_LEN_STR(mmp->mmp_nodename),
323                        EXT2_LEN_STR(mmp->mmp_bdevname), ctime(&t));
324         }
325 }