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