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; |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 533 | inode_map = xmalloc(IMAPS * BLOCK_SIZE); |
| 534 | zone_map = xmalloc(ZMAPS * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 535 | memset(inode_map, 0xff, IMAPS * BLOCK_SIZE); |
| 536 | memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE); |
| 537 | for (i = FIRSTZONE; i < ZONES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 538 | unmark_zone(i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 539 | for (i = MINIX_ROOT_INO; i <= INODES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 540 | unmark_inode(i); |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 541 | inode_buffer = xmalloc(INODE_BUFFER_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 542 | memset(inode_buffer, 0, INODE_BUFFER_SIZE); |
| 543 | printf("%ld inodes\n", INODES); |
| 544 | printf("%ld blocks\n", ZONES); |
| 545 | printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE); |
| 546 | printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE); |
| 547 | printf("Maxsize=%ld\n\n", MAXSIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Perform a test of a block; return the number of |
| 552 | * blocks readable/writeable. |
| 553 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 554 | long do_check(char *buffer, int try, unsigned int current_block) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 555 | { |
| 556 | long got; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 557 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 558 | /* Seek to the correct loc. */ |
| 559 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 560 | current_block * BLOCK_SIZE) { |
| 561 | die("seek failed during testing of blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | |
| 565 | /* Try the read */ |
| 566 | got = read(DEV, buffer, try * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 567 | if (got < 0) |
| 568 | got = 0; |
| 569 | if (got & (BLOCK_SIZE - 1)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 570 | printf("Weird values in do_check: probably bugs\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 571 | } |
| 572 | got /= BLOCK_SIZE; |
| 573 | return got; |
| 574 | } |
| 575 | |
| 576 | static unsigned int currently_testing = 0; |
| 577 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 578 | static void alarm_intr(int alnum) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 579 | { |
| 580 | if (currently_testing >= ZONES) |
| 581 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 582 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 583 | alarm(5); |
| 584 | if (!currently_testing) |
| 585 | return; |
| 586 | printf("%d ...", currently_testing); |
| 587 | fflush(stdout); |
| 588 | } |
| 589 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 590 | static void check_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 591 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 592 | int try, got; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 593 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; |
| 594 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 595 | currently_testing = 0; |
| 596 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 597 | alarm(5); |
| 598 | while (currently_testing < ZONES) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 599 | if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) != |
| 600 | currently_testing * BLOCK_SIZE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 601 | die("seek failed in check_blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 602 | try = TEST_BUFFER_BLOCKS; |
| 603 | if (currently_testing + try > ZONES) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 604 | try = ZONES - currently_testing; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 605 | got = do_check(buffer, try, currently_testing); |
| 606 | currently_testing += got; |
| 607 | if (got == try) |
| 608 | continue; |
| 609 | if (currently_testing < FIRSTZONE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 610 | die("bad blocks before data-area: cannot make fs"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 611 | mark_zone(currently_testing); |
| 612 | badblocks++; |
| 613 | currently_testing++; |
| 614 | } |
| 615 | if (badblocks > 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 616 | printf("%d bad blocks\n", badblocks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 617 | else if (badblocks == 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 618 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 621 | static void get_list_blocks(filename) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 622 | char *filename; |
| 623 | |
| 624 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 625 | FILE *listfile; |
| 626 | unsigned long blockno; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 627 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 628 | listfile = fopen(filename, "r"); |
| 629 | if (listfile == (FILE *) NULL) { |
| 630 | die("can't open file of bad blocks"); |
| 631 | } |
| 632 | while (!feof(listfile)) { |
| 633 | fscanf(listfile, "%ld\n", &blockno); |
| 634 | mark_zone(blockno); |
| 635 | badblocks++; |
| 636 | } |
| 637 | if (badblocks > 1) |
| 638 | printf("%d bad blocks\n", badblocks); |
| 639 | else if (badblocks == 1) |
| 640 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 643 | extern int mkfs_minix_main(int argc, char **argv) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 644 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 645 | int i; |
| 646 | char *tmp; |
| 647 | struct stat statbuf; |
| 648 | char *listfile = NULL; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 649 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 650 | if (argc && *argv) |
| 651 | program_name = *argv; |
| 652 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) |
| 653 | die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 654 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 655 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) |
| 656 | die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 657 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 658 | opterr = 0; |
| 659 | while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF) |
| 660 | switch (i) { |
| 661 | case 'c': |
| 662 | check = 1; |
| 663 | break; |
| 664 | case 'i': |
| 665 | req_nr_inodes = (unsigned long) atol(optarg); |
| 666 | break; |
| 667 | case 'l': |
| 668 | listfile = optarg; |
| 669 | break; |
| 670 | case 'n': |
| 671 | i = strtoul(optarg, &tmp, 0); |
| 672 | if (*tmp) |
| 673 | show_usage(); |
| 674 | if (i == 14) |
| 675 | magic = MINIX_SUPER_MAGIC; |
| 676 | else if (i == 30) |
| 677 | magic = MINIX_SUPER_MAGIC2; |
| 678 | else |
| 679 | show_usage(); |
| 680 | namelen = i; |
| 681 | dirsize = i + 2; |
| 682 | break; |
| 683 | case 'v': |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 684 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 685 | version2 = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 686 | #else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 687 | fprintf(stderr, "%s: not compiled with minix v2 support\n", |
| 688 | program_name, device_name); |
| 689 | exit(-1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 690 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 691 | break; |
| 692 | default: |
| 693 | show_usage(); |
| 694 | } |
| 695 | argc -= optind; |
| 696 | argv += optind; |
| 697 | if (argc > 0 && !device_name) { |
| 698 | device_name = argv[0]; |
| 699 | argc--; |
| 700 | argv++; |
| 701 | } |
| 702 | if (argc > 0) { |
| 703 | BLOCKS = strtol(argv[0], &tmp, 0); |
| 704 | if (*tmp) { |
| 705 | printf("strtol error: number of blocks not specified"); |
| 706 | show_usage(); |
| 707 | } |
| 708 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 709 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 710 | if (device_name && !BLOCKS) |
| 711 | BLOCKS = get_size(device_name) / 1024; |
| 712 | if (!device_name || BLOCKS < 10) { |
| 713 | show_usage(); |
| 714 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 715 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 716 | if (version2) { |
| 717 | if (namelen == 14) |
| 718 | magic = MINIX2_SUPER_MAGIC; |
| 719 | else |
| 720 | magic = MINIX2_SUPER_MAGIC2; |
| 721 | } else |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 722 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 723 | if (BLOCKS > 65535) |
| 724 | BLOCKS = 65535; |
| 725 | check_mount(); /* is it already mounted? */ |
| 726 | tmp = root_block; |
| 727 | *(short *) tmp = 1; |
| 728 | strcpy(tmp + 2, "."); |
| 729 | tmp += dirsize; |
| 730 | *(short *) tmp = 1; |
| 731 | strcpy(tmp + 2, ".."); |
| 732 | tmp += dirsize; |
| 733 | *(short *) tmp = 2; |
| 734 | strcpy(tmp + 2, ".badblocks"); |
| 735 | DEV = open(device_name, O_RDWR); |
| 736 | if (DEV < 0) |
| 737 | die("unable to open %s"); |
| 738 | if (fstat(DEV, &statbuf) < 0) |
| 739 | die("unable to stat %s"); |
| 740 | if (!S_ISBLK(statbuf.st_mode)) |
| 741 | check = 0; |
| 742 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) |
| 743 | die("will not try to make filesystem on '%s'"); |
| 744 | setup_tables(); |
| 745 | if (check) |
| 746 | check_blocks(); |
| 747 | else if (listfile) |
| 748 | get_list_blocks(listfile); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 749 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 750 | if (version2) { |
| 751 | make_root_inode2(); |
| 752 | make_bad_inode2(); |
| 753 | } else |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 754 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 755 | { |
| 756 | make_root_inode(); |
| 757 | make_bad_inode(); |
| 758 | } |
| 759 | mark_good_blocks(); |
| 760 | write_tables(); |
| 761 | return 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 762 | } |