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