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