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