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