Whamcloud - gitweb
LU-13197 e2fsck: avoid mallinfo() if over 2GB allocated
[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 "ext2fs/ext2fsP.h"
48 #include "support/nls-enable.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 = blkid_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, "location")) {
205                         if (!arg) {
206                                 journal_usage++;
207                                 continue;
208                         }
209                         journal_location_string = strdup(arg);
210                 } else if (strcmp(token, "v1_superblock") == 0) {
211                         journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
212                         continue;
213                 } else
214                         journal_usage++;
215         }
216         if (journal_usage) {
217                 fputs(_("\nBad journal options specified.\n\n"
218                         "Journal options are separated by commas, "
219                         "and may take an argument which\n"
220                         "\tis set off by an equals ('=') sign.\n\n"
221                         "Valid journal options are:\n"
222                         "\tsize=<journal size in megabytes>\n"
223                         "\tdevice=<journal device>\n"
224                         "\tlocation=<journal location>\n\n"
225                         "The journal size must be between "
226                         "1024 and 10240000 filesystem blocks.\n\n"), stderr);
227                 free(buf);
228                 exit(1);
229         }
230         free(buf);
231 }
232
233 /*
234  * Determine the number of journal blocks to use, either via
235  * user-specified # of megabytes, or via some intelligently selected
236  * defaults.
237  *
238  * Find a reasonable journal file size (in blocks) given the number of blocks
239  * in the filesystem.  For very small filesystems, it is not reasonable to
240  * have a journal that fills more than half of the filesystem.
241  */
242 unsigned int figure_journal_size(int size, ext2_filsys fs)
243 {
244         int j_blocks;
245
246         j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
247         if (j_blocks < 0) {
248                 fputs(_("\nFilesystem too small for a journal\n"), stderr);
249                 return 0;
250         }
251
252         if (size > 0) {
253                 int min_size;
254
255                 if (ext2fs_is_before_linux_ver(3, 10, 0) ||
256                     ext2fs_blocks_count(fs->super) <= 8192)
257                         min_size = 1024;
258                 else
259                         min_size = 2048;
260
261                 j_blocks = size * 1024 / (fs->blocksize / 1024);
262                 if (j_blocks < min_size || j_blocks > 10240000) {
263                         fprintf(stderr, _("\nThe requested journal "
264                                 "size is %d blocks; it must be\n"
265                                 "between %d and 10240000 blocks.  "
266                                 "Aborting.\n"), j_blocks, min_size);
267                         exit(1);
268                 }
269                 if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
270                         fputs(_("\nJournal size too big for filesystem.\n"),
271                               stderr);
272                         exit(1);
273                 }
274         }
275         return j_blocks;
276 }
277
278 void print_check_message(int mnt, unsigned int check)
279 {
280         if (mnt < 0)
281                 mnt = 0;
282         if (!mnt && !check)
283                 return;
284         printf(_("This filesystem will be automatically "
285                  "checked every %d mounts or\n"
286                  "%g days, whichever comes first.  "
287                  "Use tune2fs -c or -i to override.\n"),
288                mnt, ((double) check) / (3600 * 24));
289 }
290
291 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
292 {
293
294         if (msg)
295                 printf("MMP check failed: %s\n", msg);
296         if (mmp) {
297                 time_t t = mmp->mmp_time;
298
299                 printf("MMP error info: last update: %s node: %s device: %s\n",
300                        ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
301         }
302 }