Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mkfs.c - make a linux (minix) file-system. |
| 3 | * |
| 4 | * (C) 1991 Linus Torvalds. This file may be redistributed as per |
| 5 | * the Linux copyright. |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * DD.MM.YY |
| 10 | * |
| 11 | * 24.11.91 - Time began. Used the fsck sources to get started. |
| 12 | * |
| 13 | * 25.11.91 - Corrected some bugs. Added support for ".badblocks" |
| 14 | * The algorithm for ".badblocks" is a bit weird, but |
| 15 | * it should work. Oh, well. |
| 16 | * |
| 17 | * 25.01.92 - Added the -l option for getting the list of bad blocks |
| 18 | * out of a named file. (Dave Rivers, rivers@ponds.uucp) |
| 19 | * |
| 20 | * 28.02.92 - Added %-information when using -c. |
| 21 | * |
| 22 | * 28.02.93 - Added support for other namelengths than the original |
| 23 | * 14 characters so that I can test the new kernel routines.. |
| 24 | * |
| 25 | * 09.10.93 - Make exit status conform to that required by fsutil |
| 26 | * (Rik Faith, faith@cs.unc.edu) |
| 27 | * |
| 28 | * 31.10.93 - Added inode request feature, for backup floppies: use |
| 29 | * 32 inodes, for a news partition use more. |
| 30 | * (Scott Heavner, sdh@po.cwru.edu) |
| 31 | * |
| 32 | * 03.01.94 - Added support for file system valid flag. |
| 33 | * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu) |
| 34 | * |
| 35 | * 30.10.94 - added support for v2 filesystem |
| 36 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) |
| 37 | * |
| 38 | * 09.11.94 - Added test to prevent overwrite of mounted fs adapted |
| 39 | * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs |
| 40 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) |
| 41 | * |
| 42 | * 03.20.95 - Clear first 512 bytes of filesystem to make certain that |
| 43 | * the filesystem is not misidentified as a MS-DOS FAT filesystem. |
| 44 | * (Daniel Quinlan, quinlan@yggdrasil.com) |
| 45 | * |
| 46 | * 02.07.96 - Added small patch from Russell King to make the program a |
| 47 | * good deal more portable (janl@math.uio.no) |
| 48 | * |
| 49 | * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks] |
| 50 | * |
| 51 | * -c for readablility checking (SLOW!) |
| 52 | * -l for getting a list of bad blocks from a file. |
| 53 | * -n for namelength (currently the kernel only uses 14 or 30) |
| 54 | * -i for number of inodes |
| 55 | * -v for v2 filesystem |
| 56 | * |
| 57 | * The device may be a block device or a image of one, but this isn't |
| 58 | * enforced (but it's not much fun on a character device :-). |
| 59 | */ |
| 60 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 61 | #include "internal.h" |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 62 | #include <stdio.h> |
| 63 | #include <time.h> |
| 64 | #include <unistd.h> |
| 65 | #include <string.h> |
| 66 | #include <signal.h> |
| 67 | #include <fcntl.h> |
| 68 | #include <ctype.h> |
| 69 | #include <stdlib.h> |
| 70 | #include <termios.h> |
| 71 | #include <sys/stat.h> |
| 72 | #include <sys/ioctl.h> |
| 73 | #include <mntent.h> |
| 74 | #include <getopt.h> |
| 75 | |
| 76 | #include <linux/fs.h> |
| 77 | #include <linux/minix_fs.h> |
| 78 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 79 | #ifdef MINIX2_SUPER_MAGIC2 |
| 80 | #define HAVE_MINIX2 1 |
| 81 | #endif |
| 82 | |
| 83 | #ifndef __GNUC__ |
| 84 | #error "needs gcc for the bitop-__asm__'s" |
| 85 | #endif |
| 86 | |
| 87 | #ifndef __linux__ |
| 88 | #define volatile |
| 89 | #endif |
| 90 | |
| 91 | #define MINIX_ROOT_INO 1 |
| 92 | #define MINIX_BAD_INO 2 |
| 93 | |
| 94 | #define TEST_BUFFER_BLOCKS 16 |
| 95 | #define MAX_GOOD_BLOCKS 512 |
| 96 | |
| 97 | #define UPPER(size,n) (((size)+((n)-1))/(n)) |
| 98 | #define INODE_SIZE (sizeof(struct minix_inode)) |
| 99 | #ifdef HAVE_MINIX2 |
| 100 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) |
| 101 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ |
| 102 | : MINIX_INODES_PER_BLOCK)) |
| 103 | #else |
| 104 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) |
| 105 | #endif |
| 106 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) |
| 107 | |
| 108 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) |
| 109 | |
| 110 | static char * program_name = "mkfs"; |
| 111 | static char * device_name = NULL; |
| 112 | static int DEV = -1; |
| 113 | static long BLOCKS = 0; |
| 114 | static int check = 0; |
| 115 | static int badblocks = 0; |
| 116 | static int namelen = 30; /* default (changed to 30, per Linus's |
| 117 | suggestion, Sun Nov 21 08:05:07 1993) */ |
| 118 | static int dirsize = 32; |
| 119 | static int magic = MINIX_SUPER_MAGIC2; |
| 120 | static int version2 = 0; |
| 121 | |
| 122 | static char root_block[BLOCK_SIZE] = "\0"; |
| 123 | |
| 124 | static char * inode_buffer = NULL; |
| 125 | #define Inode (((struct minix_inode *) inode_buffer)-1) |
| 126 | #ifdef HAVE_MINIX2 |
| 127 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) |
| 128 | #endif |
| 129 | static char super_block_buffer[BLOCK_SIZE]; |
| 130 | static char boot_block_buffer[512]; |
| 131 | #define Super (*(struct minix_super_block *)super_block_buffer) |
| 132 | #define INODES ((unsigned long)Super.s_ninodes) |
| 133 | #ifdef HAVE_MINIX2 |
| 134 | #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones)) |
| 135 | #else |
| 136 | #define ZONES ((unsigned long)(Super.s_nzones)) |
| 137 | #endif |
| 138 | #define IMAPS ((unsigned long)Super.s_imap_blocks) |
| 139 | #define ZMAPS ((unsigned long)Super.s_zmap_blocks) |
| 140 | #define FIRSTZONE ((unsigned long)Super.s_firstdatazone) |
| 141 | #define ZONESIZE ((unsigned long)Super.s_log_zone_size) |
| 142 | #define MAXSIZE ((unsigned long)Super.s_max_size) |
| 143 | #define MAGIC (Super.s_magic) |
| 144 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) |
| 145 | |
| 146 | static char *inode_map; |
| 147 | static char *zone_map; |
| 148 | |
| 149 | static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; |
| 150 | static int used_good_blocks = 0; |
| 151 | static unsigned long req_nr_inodes = 0; |
| 152 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 153 | #define inode_in_use(x) (bit(inode_map,(x))) |
| 154 | #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1)) |
| 155 | |
| 156 | #define mark_inode(x) (setbit(inode_map,(x))) |
| 157 | #define unmark_inode(x) (clrbit(inode_map,(x))) |
| 158 | |
| 159 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1)) |
| 160 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1)) |
| 161 | |
| 162 | /* |
| 163 | * Volatile to let gcc know that this doesn't return. When trying |
| 164 | * to compile this under minix, volatile gives a warning, as |
| 165 | * exit() isn't defined as volatile under minix. |
| 166 | */ |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 167 | static volatile void die(char *str) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 168 | fprintf(stderr, "%s: %s\n", program_name, str); |
| 169 | exit(8); |
| 170 | } |
| 171 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 172 | static volatile void show_usage() |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 173 | { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 174 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT); |
| 175 | fprintf(stderr, "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n\n", program_name); |
| 176 | fprintf(stderr, "Make a MINIX filesystem.\n\n"); |
| 177 | fprintf(stderr, "OPTIONS:\n"); |
| 178 | fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n"); |
| 179 | fprintf(stderr, "\t-n [14|30]\tSpecify the maximum length of filenames\n"); |
| 180 | fprintf(stderr, "\t-i\t\tSpecify the number of inodes for the filesystem\n"); |
| 181 | fprintf(stderr, "\t-l FILENAME\tRead the bad blocks list from FILENAME\n"); |
| 182 | fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 183 | exit(16); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Check to make certain that our new filesystem won't be created on |
| 188 | * an already mounted partition. Code adapted from mke2fs, Copyright |
| 189 | * (C) 1994 Theodore Ts'o. Also licensed under GPL. |
| 190 | */ |
| 191 | static void check_mount(void) |
| 192 | { |
| 193 | FILE * f; |
| 194 | struct mntent * mnt; |
| 195 | |
| 196 | if ((f = setmntent (MOUNTED, "r")) == NULL) |
| 197 | return; |
| 198 | while ((mnt = getmntent (f)) != NULL) |
| 199 | if (strcmp (device_name, mnt->mnt_fsname) == 0) |
| 200 | break; |
| 201 | endmntent (f); |
| 202 | if (!mnt) |
| 203 | return; |
| 204 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 205 | die("%s is mounted; will not make a filesystem here!"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static long valid_offset (int fd, int offset) |
| 209 | { |
| 210 | char ch; |
| 211 | |
| 212 | if (lseek (fd, offset, 0) < 0) |
| 213 | return 0; |
| 214 | if (read (fd, &ch, 1) < 1) |
| 215 | return 0; |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | static int count_blocks (int fd) |
| 220 | { |
| 221 | int high, low; |
| 222 | |
| 223 | low = 0; |
| 224 | for (high = 1; valid_offset (fd, high); high *= 2) |
| 225 | low = high; |
| 226 | while (low < high - 1) |
| 227 | { |
| 228 | const int mid = (low + high) / 2; |
| 229 | |
| 230 | if (valid_offset (fd, mid)) |
| 231 | low = mid; |
| 232 | else |
| 233 | high = mid; |
| 234 | } |
| 235 | valid_offset (fd, 0); |
| 236 | return (low + 1); |
| 237 | } |
| 238 | |
| 239 | static int get_size(const char *file) |
| 240 | { |
| 241 | int fd; |
| 242 | long size; |
| 243 | |
| 244 | fd = open(file, O_RDWR); |
| 245 | if (fd < 0) { |
| 246 | perror(file); |
| 247 | exit(1); |
| 248 | } |
| 249 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { |
| 250 | close(fd); |
| 251 | return (size * 512); |
| 252 | } |
| 253 | |
| 254 | size = count_blocks(fd); |
| 255 | close(fd); |
| 256 | return size; |
| 257 | } |
| 258 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 259 | static void write_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 260 | { |
| 261 | /* Mark the super block valid. */ |
| 262 | Super.s_state |= MINIX_VALID_FS; |
| 263 | Super.s_state &= ~MINIX_ERROR_FS; |
| 264 | |
| 265 | if (lseek(DEV, 0, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 266 | die("seek to boot block failed in write_tables"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 267 | if (512 != write(DEV, boot_block_buffer, 512)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 268 | die("unable to clear boot sector"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 269 | if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 270 | die("seek failed in write_tables"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 271 | if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 272 | die("unable to write super-block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 273 | if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 274 | die("unable to write inode map"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 275 | if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 276 | die("unable to write zone map"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 277 | if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 278 | die("unable to write inodes"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 279 | |
| 280 | } |
| 281 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 282 | static void write_block(int blk, char * buffer) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 283 | { |
| 284 | if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 285 | die("seek failed in write_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 286 | if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 287 | die("write failed in write_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 290 | static int get_free_block(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 291 | { |
| 292 | int blk; |
| 293 | |
| 294 | if (used_good_blocks+1 >= MAX_GOOD_BLOCKS) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 295 | die("too many bad blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 296 | if (used_good_blocks) |
| 297 | blk = good_blocks_table[used_good_blocks-1]+1; |
| 298 | else |
| 299 | blk = FIRSTZONE; |
| 300 | while (blk < ZONES && zone_in_use(blk)) |
| 301 | blk++; |
| 302 | if (blk >= ZONES) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 303 | die("not enough good blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 304 | good_blocks_table[used_good_blocks] = blk; |
| 305 | used_good_blocks++; |
| 306 | return blk; |
| 307 | } |
| 308 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 309 | static void mark_good_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 310 | { |
| 311 | int blk; |
| 312 | |
| 313 | for (blk=0 ; blk < used_good_blocks ; blk++) |
| 314 | mark_zone(good_blocks_table[blk]); |
| 315 | } |
| 316 | |
| 317 | inline int next(int zone) |
| 318 | { |
| 319 | if (!zone) |
| 320 | zone = FIRSTZONE-1; |
| 321 | while (++zone < ZONES) |
| 322 | if (zone_in_use(zone)) |
| 323 | return zone; |
| 324 | return 0; |
| 325 | } |
| 326 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 327 | static void make_bad_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 328 | { |
| 329 | struct minix_inode * inode = &Inode[MINIX_BAD_INO]; |
| 330 | int i,j,zone; |
| 331 | int ind=0,dind=0; |
| 332 | unsigned short ind_block[BLOCK_SIZE>>1]; |
| 333 | unsigned short dind_block[BLOCK_SIZE>>1]; |
| 334 | |
| 335 | #define NEXT_BAD (zone = next(zone)) |
| 336 | |
| 337 | if (!badblocks) |
| 338 | return; |
| 339 | mark_inode(MINIX_BAD_INO); |
| 340 | inode->i_nlinks = 1; |
| 341 | inode->i_time = time(NULL); |
| 342 | inode->i_mode = S_IFREG + 0000; |
| 343 | inode->i_size = badblocks*BLOCK_SIZE; |
| 344 | zone = next(0); |
| 345 | for (i=0 ; i<7 ; i++) { |
| 346 | inode->i_zone[i] = zone; |
| 347 | if (!NEXT_BAD) |
| 348 | goto end_bad; |
| 349 | } |
| 350 | inode->i_zone[7] = ind = get_free_block(); |
| 351 | memset(ind_block,0,BLOCK_SIZE); |
| 352 | for (i=0 ; i<512 ; i++) { |
| 353 | ind_block[i] = zone; |
| 354 | if (!NEXT_BAD) |
| 355 | goto end_bad; |
| 356 | } |
| 357 | inode->i_zone[8] = dind = get_free_block(); |
| 358 | memset(dind_block,0,BLOCK_SIZE); |
| 359 | for (i=0 ; i<512 ; i++) { |
| 360 | write_block(ind,(char *) ind_block); |
| 361 | dind_block[i] = ind = get_free_block(); |
| 362 | memset(ind_block,0,BLOCK_SIZE); |
| 363 | for (j=0 ; j<512 ; j++) { |
| 364 | ind_block[j] = zone; |
| 365 | if (!NEXT_BAD) |
| 366 | goto end_bad; |
| 367 | } |
| 368 | } |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 369 | die("too many bad blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 370 | end_bad: |
| 371 | if (ind) |
| 372 | write_block(ind, (char *) ind_block); |
| 373 | if (dind) |
| 374 | write_block(dind, (char *) dind_block); |
| 375 | } |
| 376 | |
| 377 | #ifdef HAVE_MINIX2 |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 378 | static void |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 379 | make_bad_inode2 (void) |
| 380 | { |
| 381 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; |
| 382 | int i, j, zone; |
| 383 | int ind = 0, dind = 0; |
| 384 | unsigned long ind_block[BLOCK_SIZE >> 2]; |
| 385 | unsigned long dind_block[BLOCK_SIZE >> 2]; |
| 386 | |
| 387 | if (!badblocks) |
| 388 | return; |
| 389 | mark_inode (MINIX_BAD_INO); |
| 390 | inode->i_nlinks = 1; |
| 391 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); |
| 392 | inode->i_mode = S_IFREG + 0000; |
| 393 | inode->i_size = badblocks * BLOCK_SIZE; |
| 394 | zone = next (0); |
| 395 | for (i = 0; i < 7; i++) { |
| 396 | inode->i_zone[i] = zone; |
| 397 | if (!NEXT_BAD) |
| 398 | goto end_bad; |
| 399 | } |
| 400 | inode->i_zone[7] = ind = get_free_block (); |
| 401 | memset (ind_block, 0, BLOCK_SIZE); |
| 402 | for (i = 0; i < 256; i++) { |
| 403 | ind_block[i] = zone; |
| 404 | if (!NEXT_BAD) |
| 405 | goto end_bad; |
| 406 | } |
| 407 | inode->i_zone[8] = dind = get_free_block (); |
| 408 | memset (dind_block, 0, BLOCK_SIZE); |
| 409 | for (i = 0; i < 256; i++) { |
| 410 | write_block (ind, (char *) ind_block); |
| 411 | dind_block[i] = ind = get_free_block (); |
| 412 | memset (ind_block, 0, BLOCK_SIZE); |
| 413 | for (j = 0; j < 256; j++) { |
| 414 | ind_block[j] = zone; |
| 415 | if (!NEXT_BAD) |
| 416 | goto end_bad; |
| 417 | } |
| 418 | } |
| 419 | /* Could make triple indirect block here */ |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 420 | die ("too many bad blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 421 | end_bad: |
| 422 | if (ind) |
| 423 | write_block (ind, (char *) ind_block); |
| 424 | if (dind) |
| 425 | write_block (dind, (char *) dind_block); |
| 426 | } |
| 427 | #endif |
| 428 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 429 | static void make_root_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 430 | { |
| 431 | struct minix_inode * inode = &Inode[MINIX_ROOT_INO]; |
| 432 | |
| 433 | mark_inode(MINIX_ROOT_INO); |
| 434 | inode->i_zone[0] = get_free_block(); |
| 435 | inode->i_nlinks = 2; |
| 436 | inode->i_time = time(NULL); |
| 437 | if (badblocks) |
| 438 | inode->i_size = 3*dirsize; |
| 439 | else { |
| 440 | root_block[2*dirsize] = '\0'; |
| 441 | root_block[2*dirsize+1] = '\0'; |
| 442 | inode->i_size = 2*dirsize; |
| 443 | } |
| 444 | inode->i_mode = S_IFDIR + 0755; |
| 445 | inode->i_uid = getuid(); |
| 446 | if (inode->i_uid) |
| 447 | inode->i_gid = getgid(); |
| 448 | write_block(inode->i_zone[0],root_block); |
| 449 | } |
| 450 | |
| 451 | #ifdef HAVE_MINIX2 |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 452 | static void |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 453 | make_root_inode2 (void) |
| 454 | { |
| 455 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; |
| 456 | |
| 457 | mark_inode (MINIX_ROOT_INO); |
| 458 | inode->i_zone[0] = get_free_block (); |
| 459 | inode->i_nlinks = 2; |
| 460 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); |
| 461 | if (badblocks) |
| 462 | inode->i_size = 3 * dirsize; |
| 463 | else { |
| 464 | root_block[2 * dirsize] = '\0'; |
| 465 | root_block[2 * dirsize + 1] = '\0'; |
| 466 | inode->i_size = 2 * dirsize; |
| 467 | } |
| 468 | inode->i_mode = S_IFDIR + 0755; |
| 469 | inode->i_uid = getuid(); |
| 470 | if (inode->i_uid) |
| 471 | inode->i_gid = getgid(); |
| 472 | write_block (inode->i_zone[0], root_block); |
| 473 | } |
| 474 | #endif |
| 475 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 476 | static void setup_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 477 | { |
| 478 | int i; |
| 479 | unsigned long inodes; |
| 480 | |
| 481 | memset(super_block_buffer,0,BLOCK_SIZE); |
| 482 | memset(boot_block_buffer,0,512); |
| 483 | MAGIC = magic; |
| 484 | ZONESIZE = 0; |
| 485 | MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024; |
| 486 | ZONES = BLOCKS; |
| 487 | /* some magic nrs: 1 inode / 3 blocks */ |
| 488 | if ( req_nr_inodes == 0 ) |
| 489 | inodes = BLOCKS/3; |
| 490 | else |
| 491 | inodes = req_nr_inodes; |
| 492 | /* Round up inode count to fill block size */ |
| 493 | #ifdef HAVE_MINIX2 |
| 494 | if (version2) |
| 495 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & |
| 496 | ~(MINIX2_INODES_PER_BLOCK - 1)); |
| 497 | else |
| 498 | #endif |
| 499 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & |
| 500 | ~(MINIX_INODES_PER_BLOCK - 1)); |
| 501 | if (inodes > 65535) |
| 502 | inodes = 65535; |
| 503 | INODES = inodes; |
| 504 | IMAPS = UPPER(INODES + 1,BITS_PER_BLOCK); |
| 505 | ZMAPS = 0; |
| 506 | i=0; |
| 507 | while (ZMAPS != UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK) && i<1000) { |
| 508 | ZMAPS = UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK); |
| 509 | i++; |
| 510 | } |
| 511 | /* Real bad hack but overwise mkfs.minix can be thrown |
| 512 | * in infinite loop... |
| 513 | * try: |
| 514 | * dd if=/dev/zero of=test.fs count=10 bs=1024 |
| 515 | * /sbin/mkfs.minix -i 200 test.fs |
| 516 | * */ |
| 517 | if (i>=999) { |
| 518 | die ("unable to allocate buffers for maps"); |
| 519 | } |
| 520 | FIRSTZONE = NORM_FIRSTZONE; |
| 521 | inode_map = malloc(IMAPS * BLOCK_SIZE); |
| 522 | zone_map = malloc(ZMAPS * BLOCK_SIZE); |
| 523 | if (!inode_map || !zone_map) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 524 | die("unable to allocate buffers for maps"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 525 | memset(inode_map,0xff,IMAPS * BLOCK_SIZE); |
| 526 | memset(zone_map,0xff,ZMAPS * BLOCK_SIZE); |
| 527 | for (i = FIRSTZONE ; i<ZONES ; i++) |
| 528 | unmark_zone(i); |
| 529 | for (i = MINIX_ROOT_INO ; i<=INODES ; i++) |
| 530 | unmark_inode(i); |
| 531 | inode_buffer = malloc(INODE_BUFFER_SIZE); |
| 532 | if (!inode_buffer) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 533 | die("unable to allocate buffer for inodes"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 534 | memset(inode_buffer,0,INODE_BUFFER_SIZE); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 535 | printf("%ld inodes\n",INODES); |
| 536 | printf("%ld blocks\n",ZONES); |
| 537 | printf("Firstdatazone=%ld (%ld)\n",FIRSTZONE,NORM_FIRSTZONE); |
| 538 | printf("Zonesize=%d\n",BLOCK_SIZE<<ZONESIZE); |
| 539 | printf("Maxsize=%ld\n\n",MAXSIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Perform a test of a block; return the number of |
| 544 | * blocks readable/writeable. |
| 545 | */ |
| 546 | long do_check(char * buffer, int try, unsigned int current_block) |
| 547 | { |
| 548 | long got; |
| 549 | |
| 550 | /* Seek to the correct loc. */ |
| 551 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != |
| 552 | current_block * BLOCK_SIZE ) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 553 | die("seek failed during testing of blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
| 557 | /* Try the read */ |
| 558 | got = read(DEV, buffer, try * BLOCK_SIZE); |
| 559 | if (got < 0) got = 0; |
| 560 | if (got & (BLOCK_SIZE - 1 )) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 561 | printf("Weird values in do_check: probably bugs\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 562 | } |
| 563 | got /= BLOCK_SIZE; |
| 564 | return got; |
| 565 | } |
| 566 | |
| 567 | static unsigned int currently_testing = 0; |
| 568 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 569 | static void alarm_intr(int alnum) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 570 | { |
| 571 | if (currently_testing >= ZONES) |
| 572 | return; |
| 573 | signal(SIGALRM,alarm_intr); |
| 574 | alarm(5); |
| 575 | if (!currently_testing) |
| 576 | return; |
| 577 | printf("%d ...", currently_testing); |
| 578 | fflush(stdout); |
| 579 | } |
| 580 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 581 | static void check_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 582 | { |
| 583 | int try,got; |
| 584 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; |
| 585 | |
| 586 | currently_testing=0; |
| 587 | signal(SIGALRM,alarm_intr); |
| 588 | alarm(5); |
| 589 | while (currently_testing < ZONES) { |
| 590 | if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) != |
| 591 | currently_testing*BLOCK_SIZE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 592 | die("seek failed in check_blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 593 | try = TEST_BUFFER_BLOCKS; |
| 594 | if (currently_testing + try > ZONES) |
| 595 | try = ZONES-currently_testing; |
| 596 | got = do_check(buffer, try, currently_testing); |
| 597 | currently_testing += got; |
| 598 | if (got == try) |
| 599 | continue; |
| 600 | if (currently_testing < FIRSTZONE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 601 | die("bad blocks before data-area: cannot make fs"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 602 | mark_zone(currently_testing); |
| 603 | badblocks++; |
| 604 | currently_testing++; |
| 605 | } |
| 606 | if (badblocks > 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 607 | printf("%d bad blocks\n", badblocks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 608 | else if (badblocks == 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 609 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 612 | static void get_list_blocks(filename) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 613 | char *filename; |
| 614 | |
| 615 | { |
| 616 | FILE *listfile; |
| 617 | unsigned long blockno; |
| 618 | |
| 619 | listfile=fopen(filename,"r"); |
| 620 | if(listfile == (FILE *)NULL) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 621 | die("can't open file of bad blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 622 | } |
| 623 | while(!feof(listfile)) { |
| 624 | fscanf(listfile,"%ld\n", &blockno); |
| 625 | mark_zone(blockno); |
| 626 | badblocks++; |
| 627 | } |
| 628 | if(badblocks > 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 629 | printf("%d bad blocks\n", badblocks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 630 | else if (badblocks == 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 631 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | extern int |
| 635 | mkfs_minix_main(int argc, char ** argv) |
| 636 | { |
| 637 | int i; |
| 638 | char * tmp; |
| 639 | struct stat statbuf; |
| 640 | char * listfile = NULL; |
| 641 | |
| 642 | if (argc && *argv) |
| 643 | program_name = *argv; |
| 644 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 645 | die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 646 | #ifdef HAVE_MINIX2 |
| 647 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 648 | die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 649 | #endif |
| 650 | opterr = 0; |
| 651 | while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF) |
| 652 | switch (i) { |
| 653 | case 'c': |
| 654 | check=1; break; |
| 655 | case 'i': |
| 656 | req_nr_inodes = (unsigned long) atol(optarg); |
| 657 | break; |
| 658 | case 'l': |
| 659 | listfile = optarg; break; |
| 660 | case 'n': |
| 661 | i = strtoul(optarg,&tmp,0); |
| 662 | if (*tmp) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 663 | show_usage(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 664 | if (i == 14) |
| 665 | magic = MINIX_SUPER_MAGIC; |
| 666 | else if (i == 30) |
| 667 | magic = MINIX_SUPER_MAGIC2; |
| 668 | else |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 669 | show_usage(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 670 | namelen = i; |
| 671 | dirsize = i+2; |
| 672 | break; |
| 673 | case 'v': |
| 674 | #ifdef HAVE_MINIX2 |
| 675 | version2 = 1; |
| 676 | #else |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 677 | fprintf(stderr,"%s: not compiled with minix v2 support\n",program_name,device_name); |
| 678 | exit(-1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 679 | #endif |
| 680 | break; |
| 681 | default: |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 682 | show_usage(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 683 | } |
| 684 | argc -= optind; |
| 685 | argv += optind; |
| 686 | if (argc > 0 && !device_name) { |
| 687 | device_name = argv[0]; |
| 688 | argc--; |
| 689 | argv++; |
| 690 | } |
| 691 | if (argc > 0) { |
| 692 | BLOCKS = strtol(argv[0],&tmp,0); |
| 693 | if (*tmp) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 694 | printf("strtol error: number of blocks not specified"); |
| 695 | show_usage(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | if (device_name && !BLOCKS) |
| 700 | BLOCKS = get_size (device_name) / 1024; |
| 701 | if (!device_name || BLOCKS<10) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 702 | show_usage(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 703 | } |
| 704 | #ifdef HAVE_MINIX2 |
| 705 | if (version2) { |
| 706 | if (namelen == 14) |
| 707 | magic = MINIX2_SUPER_MAGIC; |
| 708 | else |
| 709 | magic = MINIX2_SUPER_MAGIC2; |
| 710 | } else |
| 711 | #endif |
| 712 | if (BLOCKS > 65535) |
| 713 | BLOCKS = 65535; |
| 714 | check_mount(); /* is it already mounted? */ |
| 715 | tmp = root_block; |
| 716 | *(short *)tmp = 1; |
| 717 | strcpy(tmp+2,"."); |
| 718 | tmp += dirsize; |
| 719 | *(short *)tmp = 1; |
| 720 | strcpy(tmp+2,".."); |
| 721 | tmp += dirsize; |
| 722 | *(short *)tmp = 2; |
| 723 | strcpy(tmp+2,".badblocks"); |
| 724 | DEV = open(device_name,O_RDWR ); |
| 725 | if (DEV<0) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 726 | die("unable to open %s"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 727 | if (fstat(DEV,&statbuf)<0) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 728 | die("unable to stat %s"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 729 | if (!S_ISBLK(statbuf.st_mode)) |
| 730 | check=0; |
| 731 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 732 | die("will not try to make filesystem on '%s'"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 733 | setup_tables(); |
| 734 | if (check) |
| 735 | check_blocks(); |
| 736 | else if (listfile) |
| 737 | get_list_blocks(listfile); |
| 738 | #ifdef HAVE_MINIX2 |
| 739 | if (version2) { |
| 740 | make_root_inode2 (); |
| 741 | make_bad_inode2 (); |
| 742 | } else |
| 743 | #endif |
| 744 | { |
| 745 | make_root_inode(); |
| 746 | make_bad_inode(); |
| 747 | } |
| 748 | mark_good_blocks(); |
| 749 | write_tables(); |
| 750 | return 0; |
| 751 | } |