util.c revision 2537b6d0c1aa9710a05cdfafc281b4884c67bdb2
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#include <stdio.h> 13#include <string.h> 14#ifdef HAVE_ERRNO_H 15#include <errno.h> 16#endif 17#include <linux/ext2_fs.h> 18#ifdef HAVE_LINUX_MAJOR_H 19#include <linux/major.h> 20#include <sys/stat.h> 21#endif 22 23#include "et/com_err.h" 24#include "e2p/e2p.h" 25#include "ext2fs/ext2fs.h" 26#include "nls-enable.h" 27#include "util.h" 28 29#ifndef HAVE_STRCASECMP 30int strcasecmp (char *s1, char *s2) 31{ 32 while (*s1 && *s2) { 33 int ch1 = *s1++, ch2 = *s2++; 34 if (isupper (ch1)) 35 ch1 = tolower (ch1); 36 if (isupper (ch2)) 37 ch2 = tolower (ch2); 38 if (ch1 != ch2) 39 return ch1 - ch2; 40 } 41 return *s1 ? 1 : *s2 ? -1 : 0; 42} 43#endif 44 45void proceed_question(void) 46{ 47 char buf[256]; 48 const char *short_yes = _("yY"); 49 50 fflush(stdout); 51 fflush(stderr); 52 printf(_("Proceed anyway? (y,n) ")); 53 buf[0] = 0; 54 fgets(buf, sizeof(buf), stdin); 55 if (strchr(short_yes, buf[0]) == 0) 56 exit(1); 57} 58 59void check_plausibility(const char *device) 60{ 61 int val; 62 struct stat s; 63 64 val = stat(device, &s); 65 66 if(val == -1) { 67 fprintf(stderr, _("Could not stat %s --- %s\n"), 68 device, error_message(errno)); 69 if (errno == ENOENT) 70 fprintf(stderr, _("\nThe device apparently does " 71 "not exist; did you specify it correctly?\n")); 72 exit(1); 73 } 74 if (!S_ISBLK(s.st_mode)) { 75 printf(_("%s is not a block special device.\n"), device); 76 proceed_question(); 77 return; 78 } 79 80#ifdef HAVE_LINUX_MAJOR_H 81#ifndef MAJOR 82#define MAJOR(dev) ((dev)>>8) 83#define MINOR(dev) ((dev) & 0xff) 84#endif 85#ifndef SCSI_BLK_MAJOR 86#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR) 87#endif 88 if (((MAJOR(s.st_rdev) == HD_MAJOR && 89 MINOR(s.st_rdev)%64 == 0) || 90 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) && 91 MINOR(s.st_rdev)%16 == 0))) { 92 printf(_("%s is entire device, not just one partition!\n"), 93 device); 94 proceed_question(); 95 } 96#endif 97} 98 99void check_mount(const char *device, int force, const char *type) 100{ 101 errcode_t retval; 102 int mount_flags; 103 104 retval = ext2fs_check_if_mounted(device, &mount_flags); 105 if (retval) { 106 com_err("ext2fs_check_if_mount", retval, 107 _("while determining whether %s is mounted."), 108 device); 109 return; 110 } 111 if (!(mount_flags & EXT2_MF_MOUNTED)) 112 return; 113 114 fprintf(stderr, _("%s is mounted; "), device); 115 if (force) { 116 fprintf(stderr, _("mke2fs forced anyway. " 117 "Hope /etc/mtab is incorrect.\n")); 118 } else { 119 fprintf(stderr, _("will not make a %s here!\n"), type); 120 exit(1); 121 } 122} 123 124void parse_journal_opts(const char *opts) 125{ 126 char *buf, *token, *next, *p, *arg; 127 int len; 128 int journal_usage = 0; 129 130 len = strlen(opts); 131 buf = malloc(len+1); 132 if (!buf) { 133 fprintf(stderr, _("Couldn't allocate memory to parse " 134 "journal options!\n")); 135 exit(1); 136 } 137 strcpy(buf, opts); 138 for (token = buf; token && *token; token = next) { 139 p = strchr(token, ','); 140 next = 0; 141 if (p) { 142 *p = 0; 143 next = p+1; 144 } 145 arg = strchr(token, '='); 146 if (arg) { 147 *arg = 0; 148 arg++; 149 } 150#if 0 151 printf("Journal option=%s, argument=%s\n", token, 152 arg ? arg : "NONE"); 153#endif 154 if (strcmp(token, "device") == 0) { 155 if (!arg) { 156 journal_usage++; 157 continue; 158 } 159 journal_device = arg; 160 } else if (strcmp(token, "size") == 0) { 161 if (!arg) { 162 journal_usage++; 163 continue; 164 } 165 journal_size = strtoul(arg, &p, 0); 166 if (*p) 167 journal_usage++; 168 } else if (strcmp(token, "v1_superblock") == 0) { 169 journal_flags |= EXT2_MKJOURNAL_V1_SUPER; 170 continue; 171 } else { 172 journal_size = strtoul(token, &p, 0); 173 if (*p) 174 journal_usage++; 175 } 176 } 177 if (journal_usage) { 178 fprintf(stderr, _("\nBad journal options specified.\n\n" 179 "Journal options are separated by commas, " 180 "and may take an argument which\n" 181 "\tis set off by an equals ('=') sign.\n\n" 182 "Valid raid options are:\n" 183 "\tsize=<journal size in megabytes>\n" 184 "\tdevice=<journal device>\n\n" 185 "The journal size must be between " 186 "1024 and 102400 filesystem blocks.\n\n" )); 187 exit(1); 188 } 189} 190 191/* 192 * Determine the number of journal blocks to use, either via 193 * user-specified # of megabytes, or via some intelligently selected 194 * defaults. 195 * 196 * Find a reasonable journal file size (in blocks) given the number of blocks 197 * in the filesystem. For very small filesystems, it is not reasonable to 198 * have a journal that fills more than half of the filesystem. 199 */ 200int figure_journal_size(int journal_size, ext2_filsys fs) 201{ 202 blk_t j_blocks; 203 204 if (fs->super->s_blocks_count < 2048) { 205 fprintf(stderr, _("\nFilesystem too small for a journal\n")); 206 return 0; 207 } 208 209 if (journal_size >= 0) { 210 j_blocks = journal_size * 1024 / 211 (fs->blocksize / 1024); 212 if (j_blocks < 1024 || j_blocks > 102400) { 213 fprintf(stderr, _("\nThe requested journal " 214 "size is %d blocks; it must be\n" 215 "between 1024 and 102400 blocks. " 216 "Aborting.\n"), 217 j_blocks); 218 exit(1); 219 } 220 if (j_blocks > fs->super->s_free_blocks_count) { 221 fprintf(stderr, _("\nJournal size too big " 222 "for filesystem.\n")); 223 exit(1); 224 } 225 return j_blocks; 226 } 227 228 if (fs->super->s_blocks_count < 32768) 229 j_blocks = 1024; 230 else if (fs->super->s_blocks_count < 262144) 231 j_blocks = 4096; 232 else 233 j_blocks = 8192; 234 235 return j_blocks; 236} 237 238