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 | |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 80 | |
| 81 | typedef unsigned char u8; |
| 82 | typedef unsigned short u16; |
| 83 | typedef unsigned int u32; |
| 84 | |
| 85 | |
| 86 | #define MINIX_ROOT_INO 1 |
| 87 | #define MINIX_LINK_MAX 250 |
| 88 | #define MINIX2_LINK_MAX 65530 |
| 89 | |
| 90 | #define MINIX_I_MAP_SLOTS 8 |
| 91 | #define MINIX_Z_MAP_SLOTS 64 |
| 92 | #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */ |
| 93 | #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */ |
| 94 | #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */ |
| 95 | #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */ |
| 96 | #define MINIX_VALID_FS 0x0001 /* Clean fs. */ |
| 97 | #define MINIX_ERROR_FS 0x0002 /* fs has errors. */ |
| 98 | |
| 99 | #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode))) |
| 100 | #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode))) |
| 101 | |
| 102 | #define MINIX_V1 0x0001 /* original minix fs */ |
| 103 | #define MINIX_V2 0x0002 /* minix V2 fs */ |
| 104 | |
| 105 | #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version |
| 106 | |
| 107 | /* |
| 108 | * This is the original minix inode layout on disk. |
| 109 | * Note the 8-bit gid and atime and ctime. |
| 110 | */ |
| 111 | struct minix_inode { |
| 112 | u16 i_mode; |
| 113 | u16 i_uid; |
| 114 | u32 i_size; |
| 115 | u32 i_time; |
| 116 | u8 i_gid; |
| 117 | u8 i_nlinks; |
| 118 | u16 i_zone[9]; |
| 119 | }; |
| 120 | |
| 121 | /* |
| 122 | * The new minix inode has all the time entries, as well as |
| 123 | * long block numbers and a third indirect block (7+1+1+1 |
| 124 | * instead of 7+1+1). Also, some previously 8-bit values are |
| 125 | * now 16-bit. The inode is now 64 bytes instead of 32. |
| 126 | */ |
| 127 | struct minix2_inode { |
| 128 | u16 i_mode; |
| 129 | u16 i_nlinks; |
| 130 | u16 i_uid; |
| 131 | u16 i_gid; |
| 132 | u32 i_size; |
| 133 | u32 i_atime; |
| 134 | u32 i_mtime; |
| 135 | u32 i_ctime; |
| 136 | u32 i_zone[10]; |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * minix super-block data on disk |
| 141 | */ |
| 142 | struct minix_super_block { |
| 143 | u16 s_ninodes; |
| 144 | u16 s_nzones; |
| 145 | u16 s_imap_blocks; |
| 146 | u16 s_zmap_blocks; |
| 147 | u16 s_firstdatazone; |
| 148 | u16 s_log_zone_size; |
| 149 | u32 s_max_size; |
| 150 | u16 s_magic; |
| 151 | u16 s_state; |
| 152 | u32 s_zones; |
| 153 | }; |
| 154 | |
| 155 | struct minix_dir_entry { |
| 156 | u16 inode; |
| 157 | char name[0]; |
| 158 | }; |
| 159 | |
| 160 | #define BLOCK_SIZE_BITS 10 |
| 161 | #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS) |
| 162 | |
| 163 | #define NAME_MAX 255 /* # chars in a file name */ |
| 164 | |
| 165 | #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode))) |
| 166 | |
| 167 | #define MINIX_VALID_FS 0x0001 /* Clean fs. */ |
| 168 | #define MINIX_ERROR_FS 0x0002 /* fs has errors. */ |
| 169 | |
| 170 | #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */ |
| 171 | #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */ |
| 172 | |
| 173 | #ifndef BLKGETSIZE |
| 174 | #define BLKGETSIZE _IO(0x12,96) /* return device size */ |
| 175 | #endif |
| 176 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 177 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 178 | #ifdef MINIX2_SUPER_MAGIC2 |
| 179 | #define HAVE_MINIX2 1 |
| 180 | #endif |
| 181 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 182 | #ifndef __linux__ |
| 183 | #define volatile |
| 184 | #endif |
| 185 | |
| 186 | #define MINIX_ROOT_INO 1 |
| 187 | #define MINIX_BAD_INO 2 |
| 188 | |
| 189 | #define TEST_BUFFER_BLOCKS 16 |
| 190 | #define MAX_GOOD_BLOCKS 512 |
| 191 | |
| 192 | #define UPPER(size,n) (((size)+((n)-1))/(n)) |
| 193 | #define INODE_SIZE (sizeof(struct minix_inode)) |
| 194 | #ifdef HAVE_MINIX2 |
| 195 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) |
| 196 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ |
| 197 | : MINIX_INODES_PER_BLOCK)) |
| 198 | #else |
| 199 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) |
| 200 | #endif |
| 201 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) |
| 202 | |
| 203 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) |
| 204 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 205 | static char *program_name = "mkfs"; |
| 206 | static char *device_name = NULL; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 207 | static int DEV = -1; |
| 208 | static long BLOCKS = 0; |
| 209 | static int check = 0; |
| 210 | static int badblocks = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 211 | static int namelen = 30; /* default (changed to 30, per Linus's |
| 212 | |
| 213 | suggestion, Sun Nov 21 08:05:07 1993) */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 214 | static int dirsize = 32; |
| 215 | static int magic = MINIX_SUPER_MAGIC2; |
| 216 | static int version2 = 0; |
| 217 | |
| 218 | static char root_block[BLOCK_SIZE] = "\0"; |
| 219 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | static char *inode_buffer = NULL; |
| 221 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 222 | #define Inode (((struct minix_inode *) inode_buffer)-1) |
| 223 | #ifdef HAVE_MINIX2 |
| 224 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) |
| 225 | #endif |
| 226 | static char super_block_buffer[BLOCK_SIZE]; |
| 227 | static char boot_block_buffer[512]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 228 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 229 | #define Super (*(struct minix_super_block *)super_block_buffer) |
| 230 | #define INODES ((unsigned long)Super.s_ninodes) |
| 231 | #ifdef HAVE_MINIX2 |
| 232 | #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones)) |
| 233 | #else |
| 234 | #define ZONES ((unsigned long)(Super.s_nzones)) |
| 235 | #endif |
| 236 | #define IMAPS ((unsigned long)Super.s_imap_blocks) |
| 237 | #define ZMAPS ((unsigned long)Super.s_zmap_blocks) |
| 238 | #define FIRSTZONE ((unsigned long)Super.s_firstdatazone) |
| 239 | #define ZONESIZE ((unsigned long)Super.s_log_zone_size) |
| 240 | #define MAXSIZE ((unsigned long)Super.s_max_size) |
| 241 | #define MAGIC (Super.s_magic) |
| 242 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) |
| 243 | |
| 244 | static char *inode_map; |
| 245 | static char *zone_map; |
| 246 | |
| 247 | static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; |
| 248 | static int used_good_blocks = 0; |
| 249 | static unsigned long req_nr_inodes = 0; |
| 250 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 251 | #define inode_in_use(x) (isset(inode_map,(x))) |
| 252 | #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 253 | |
| 254 | #define mark_inode(x) (setbit(inode_map,(x))) |
| 255 | #define unmark_inode(x) (clrbit(inode_map,(x))) |
| 256 | |
| 257 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1)) |
| 258 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1)) |
| 259 | |
| 260 | /* |
| 261 | * Volatile to let gcc know that this doesn't return. When trying |
| 262 | * to compile this under minix, volatile gives a warning, as |
| 263 | * exit() isn't defined as volatile under minix. |
| 264 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | static volatile void die(char *str) |
| 266 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 267 | fprintf(stderr, "%s: %s\n", program_name, str); |
| 268 | exit(8); |
| 269 | } |
| 270 | |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 271 | static volatile void show_usage() __attribute__ ((noreturn)); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 272 | static volatile void show_usage() |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 273 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 274 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", |
| 275 | BB_VER, BB_BT); |
| 276 | fprintf(stderr, |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 277 | "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 278 | program_name); |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 279 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 280 | fprintf(stderr, "\nMake a MINIX filesystem.\n\n"); |
Eric Andersen | 2086e3d | 2000-07-04 22:17:01 +0000 | [diff] [blame] | 281 | fprintf(stderr, "Options:\n"); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 282 | fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 283 | fprintf(stderr, |
| 284 | "\t-n [14|30]\tSpecify the maximum length of filenames\n"); |
| 285 | fprintf(stderr, |
Eric Andersen | 815e904 | 2000-06-06 16:15:23 +0000 | [diff] [blame] | 286 | "\t-i INODES\tSpecify the number of inodes for the filesystem\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 287 | fprintf(stderr, |
| 288 | "\t-l FILENAME\tRead the bad blocks list from FILENAME\n"); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 289 | 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] | 290 | #endif |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 291 | exit(16); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Check to make certain that our new filesystem won't be created on |
| 296 | * an already mounted partition. Code adapted from mke2fs, Copyright |
| 297 | * (C) 1994 Theodore Ts'o. Also licensed under GPL. |
| 298 | */ |
| 299 | static void check_mount(void) |
| 300 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 301 | FILE *f; |
| 302 | struct mntent *mnt; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 303 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 304 | if ((f = setmntent(MOUNTED, "r")) == NULL) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 305 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 306 | while ((mnt = getmntent(f)) != NULL) |
| 307 | if (strcmp(device_name, mnt->mnt_fsname) == 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 308 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 309 | endmntent(f); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 310 | if (!mnt) |
| 311 | return; |
| 312 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 313 | die("%s is mounted; will not make a filesystem here!"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 316 | static long valid_offset(int fd, int offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 317 | { |
| 318 | char ch; |
| 319 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 320 | if (lseek(fd, offset, 0) < 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 321 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 322 | if (read(fd, &ch, 1) < 1) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 323 | return 0; |
| 324 | return 1; |
| 325 | } |
| 326 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 327 | static int count_blocks(int fd) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 328 | { |
| 329 | int high, low; |
| 330 | |
| 331 | low = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 332 | for (high = 1; valid_offset(fd, high); high *= 2) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 333 | low = high; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 334 | while (low < high - 1) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 335 | const int mid = (low + high) / 2; |
| 336 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 337 | if (valid_offset(fd, mid)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 338 | low = mid; |
| 339 | else |
| 340 | high = mid; |
| 341 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 342 | valid_offset(fd, 0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 343 | return (low + 1); |
| 344 | } |
| 345 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 346 | static int get_size(const char *file) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 347 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 348 | int fd; |
| 349 | long size; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 350 | |
| 351 | fd = open(file, O_RDWR); |
| 352 | if (fd < 0) { |
| 353 | perror(file); |
| 354 | exit(1); |
| 355 | } |
| 356 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { |
| 357 | close(fd); |
| 358 | return (size * 512); |
| 359 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 360 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 361 | size = count_blocks(fd); |
| 362 | close(fd); |
| 363 | return size; |
| 364 | } |
| 365 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 366 | static void write_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 367 | { |
| 368 | /* Mark the super block valid. */ |
| 369 | Super.s_state |= MINIX_VALID_FS; |
| 370 | Super.s_state &= ~MINIX_ERROR_FS; |
| 371 | |
| 372 | if (lseek(DEV, 0, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 373 | die("seek to boot block failed in write_tables"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 374 | if (512 != write(DEV, boot_block_buffer, 512)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 375 | die("unable to clear boot sector"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 376 | if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 377 | die("seek failed in write_tables"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 378 | if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 379 | die("unable to write super-block"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 380 | if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 381 | die("unable to write inode map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 382 | if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 383 | die("unable to write zone map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 384 | if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 385 | die("unable to write inodes"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 389 | static void write_block(int blk, char *buffer) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 390 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 391 | if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 392 | die("seek failed in write_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 393 | if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 394 | die("write failed in write_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 397 | static int get_free_block(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 398 | { |
| 399 | int blk; |
| 400 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 401 | if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 402 | die("too many bad blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 403 | if (used_good_blocks) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 404 | blk = good_blocks_table[used_good_blocks - 1] + 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 405 | else |
| 406 | blk = FIRSTZONE; |
| 407 | while (blk < ZONES && zone_in_use(blk)) |
| 408 | blk++; |
| 409 | if (blk >= ZONES) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 410 | die("not enough good blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 411 | good_blocks_table[used_good_blocks] = blk; |
| 412 | used_good_blocks++; |
| 413 | return blk; |
| 414 | } |
| 415 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 416 | static void mark_good_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 417 | { |
| 418 | int blk; |
| 419 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 420 | for (blk = 0; blk < used_good_blocks; blk++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 421 | mark_zone(good_blocks_table[blk]); |
| 422 | } |
| 423 | |
| 424 | inline int next(int zone) |
| 425 | { |
| 426 | if (!zone) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 427 | zone = FIRSTZONE - 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 428 | while (++zone < ZONES) |
| 429 | if (zone_in_use(zone)) |
| 430 | return zone; |
| 431 | return 0; |
| 432 | } |
| 433 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 434 | static void make_bad_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 435 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 436 | struct minix_inode *inode = &Inode[MINIX_BAD_INO]; |
| 437 | int i, j, zone; |
| 438 | int ind = 0, dind = 0; |
| 439 | unsigned short ind_block[BLOCK_SIZE >> 1]; |
| 440 | unsigned short dind_block[BLOCK_SIZE >> 1]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 441 | |
| 442 | #define NEXT_BAD (zone = next(zone)) |
| 443 | |
| 444 | if (!badblocks) |
| 445 | return; |
| 446 | mark_inode(MINIX_BAD_INO); |
| 447 | inode->i_nlinks = 1; |
| 448 | inode->i_time = time(NULL); |
| 449 | inode->i_mode = S_IFREG + 0000; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 450 | inode->i_size = badblocks * BLOCK_SIZE; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 451 | zone = next(0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | for (i = 0; i < 7; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 453 | inode->i_zone[i] = zone; |
| 454 | if (!NEXT_BAD) |
| 455 | goto end_bad; |
| 456 | } |
| 457 | inode->i_zone[7] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | memset(ind_block, 0, BLOCK_SIZE); |
| 459 | for (i = 0; i < 512; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 460 | ind_block[i] = zone; |
| 461 | if (!NEXT_BAD) |
| 462 | goto end_bad; |
| 463 | } |
| 464 | inode->i_zone[8] = dind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 465 | memset(dind_block, 0, BLOCK_SIZE); |
| 466 | for (i = 0; i < 512; i++) { |
| 467 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 468 | dind_block[i] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 469 | memset(ind_block, 0, BLOCK_SIZE); |
| 470 | for (j = 0; j < 512; j++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 471 | ind_block[j] = zone; |
| 472 | if (!NEXT_BAD) |
| 473 | goto end_bad; |
| 474 | } |
| 475 | } |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 476 | die("too many bad blocks"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 477 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 478 | if (ind) |
| 479 | write_block(ind, (char *) ind_block); |
| 480 | if (dind) |
| 481 | write_block(dind, (char *) dind_block); |
| 482 | } |
| 483 | |
| 484 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 485 | static void make_bad_inode2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 486 | { |
| 487 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; |
| 488 | int i, j, zone; |
| 489 | int ind = 0, dind = 0; |
| 490 | unsigned long ind_block[BLOCK_SIZE >> 2]; |
| 491 | unsigned long dind_block[BLOCK_SIZE >> 2]; |
| 492 | |
| 493 | if (!badblocks) |
| 494 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 495 | mark_inode(MINIX_BAD_INO); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 496 | inode->i_nlinks = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 497 | inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 498 | inode->i_mode = S_IFREG + 0000; |
| 499 | inode->i_size = badblocks * BLOCK_SIZE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 500 | zone = next(0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 501 | for (i = 0; i < 7; i++) { |
| 502 | inode->i_zone[i] = zone; |
| 503 | if (!NEXT_BAD) |
| 504 | goto end_bad; |
| 505 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 506 | inode->i_zone[7] = ind = get_free_block(); |
| 507 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 508 | for (i = 0; i < 256; i++) { |
| 509 | ind_block[i] = zone; |
| 510 | if (!NEXT_BAD) |
| 511 | goto end_bad; |
| 512 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 513 | inode->i_zone[8] = dind = get_free_block(); |
| 514 | memset(dind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 515 | for (i = 0; i < 256; i++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 516 | write_block(ind, (char *) ind_block); |
| 517 | dind_block[i] = ind = get_free_block(); |
| 518 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 519 | for (j = 0; j < 256; j++) { |
| 520 | ind_block[j] = zone; |
| 521 | if (!NEXT_BAD) |
| 522 | goto end_bad; |
| 523 | } |
| 524 | } |
| 525 | /* Could make triple indirect block here */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 526 | die("too many bad blocks"); |
| 527 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 528 | if (ind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 529 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 530 | if (dind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 531 | write_block(dind, (char *) dind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 532 | } |
| 533 | #endif |
| 534 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 535 | static void make_root_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 536 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 537 | struct minix_inode *inode = &Inode[MINIX_ROOT_INO]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 538 | |
| 539 | mark_inode(MINIX_ROOT_INO); |
| 540 | inode->i_zone[0] = get_free_block(); |
| 541 | inode->i_nlinks = 2; |
| 542 | inode->i_time = time(NULL); |
| 543 | if (badblocks) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 544 | inode->i_size = 3 * dirsize; |
| 545 | else { |
| 546 | root_block[2 * dirsize] = '\0'; |
| 547 | root_block[2 * dirsize + 1] = '\0'; |
| 548 | inode->i_size = 2 * dirsize; |
| 549 | } |
| 550 | inode->i_mode = S_IFDIR + 0755; |
| 551 | inode->i_uid = getuid(); |
| 552 | if (inode->i_uid) |
| 553 | inode->i_gid = getgid(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 554 | write_block(inode->i_zone[0], root_block); |
| 555 | } |
| 556 | |
| 557 | #ifdef HAVE_MINIX2 |
| 558 | static void make_root_inode2(void) |
| 559 | { |
| 560 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; |
| 561 | |
| 562 | mark_inode(MINIX_ROOT_INO); |
| 563 | inode->i_zone[0] = get_free_block(); |
| 564 | inode->i_nlinks = 2; |
| 565 | inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); |
| 566 | if (badblocks) |
| 567 | inode->i_size = 3 * dirsize; |
| 568 | else { |
| 569 | root_block[2 * dirsize] = '\0'; |
| 570 | root_block[2 * dirsize + 1] = '\0'; |
| 571 | inode->i_size = 2 * dirsize; |
| 572 | } |
| 573 | inode->i_mode = S_IFDIR + 0755; |
| 574 | inode->i_uid = getuid(); |
| 575 | if (inode->i_uid) |
| 576 | inode->i_gid = getgid(); |
| 577 | write_block(inode->i_zone[0], root_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 578 | } |
| 579 | #endif |
| 580 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 581 | static void setup_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 582 | { |
| 583 | int i; |
| 584 | unsigned long inodes; |
| 585 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 586 | memset(super_block_buffer, 0, BLOCK_SIZE); |
| 587 | memset(boot_block_buffer, 0, 512); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 588 | MAGIC = magic; |
| 589 | ZONESIZE = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 590 | MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 591 | ZONES = BLOCKS; |
| 592 | /* some magic nrs: 1 inode / 3 blocks */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 593 | if (req_nr_inodes == 0) |
| 594 | inodes = BLOCKS / 3; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 595 | else |
| 596 | inodes = req_nr_inodes; |
| 597 | /* Round up inode count to fill block size */ |
| 598 | #ifdef HAVE_MINIX2 |
| 599 | if (version2) |
| 600 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 601 | ~(MINIX2_INODES_PER_BLOCK - 1)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 602 | else |
| 603 | #endif |
| 604 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 605 | ~(MINIX_INODES_PER_BLOCK - 1)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 606 | if (inodes > 65535) |
| 607 | inodes = 65535; |
| 608 | INODES = inodes; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 609 | IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 610 | ZMAPS = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 611 | i = 0; |
| 612 | while (ZMAPS != |
| 613 | UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, |
| 614 | BITS_PER_BLOCK) && i < 1000) { |
| 615 | ZMAPS = |
| 616 | UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, |
| 617 | BITS_PER_BLOCK); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 618 | i++; |
| 619 | } |
| 620 | /* Real bad hack but overwise mkfs.minix can be thrown |
| 621 | * in infinite loop... |
| 622 | * try: |
| 623 | * dd if=/dev/zero of=test.fs count=10 bs=1024 |
| 624 | * /sbin/mkfs.minix -i 200 test.fs |
| 625 | * */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 626 | if (i >= 999) { |
| 627 | die("unable to allocate buffers for maps"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 628 | } |
| 629 | FIRSTZONE = NORM_FIRSTZONE; |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 630 | inode_map = xmalloc(IMAPS * BLOCK_SIZE); |
| 631 | zone_map = xmalloc(ZMAPS * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 632 | memset(inode_map, 0xff, IMAPS * BLOCK_SIZE); |
| 633 | memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE); |
| 634 | for (i = FIRSTZONE; i < ZONES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 635 | unmark_zone(i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 636 | for (i = MINIX_ROOT_INO; i <= INODES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 637 | unmark_inode(i); |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 638 | inode_buffer = xmalloc(INODE_BUFFER_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 639 | memset(inode_buffer, 0, INODE_BUFFER_SIZE); |
| 640 | printf("%ld inodes\n", INODES); |
| 641 | printf("%ld blocks\n", ZONES); |
| 642 | printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE); |
| 643 | printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE); |
| 644 | printf("Maxsize=%ld\n\n", MAXSIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Perform a test of a block; return the number of |
| 649 | * blocks readable/writeable. |
| 650 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 651 | long do_check(char *buffer, int try, unsigned int current_block) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 652 | { |
| 653 | long got; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 654 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 655 | /* Seek to the correct loc. */ |
| 656 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 657 | current_block * BLOCK_SIZE) { |
| 658 | die("seek failed during testing of blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | |
| 662 | /* Try the read */ |
| 663 | got = read(DEV, buffer, try * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 664 | if (got < 0) |
| 665 | got = 0; |
| 666 | if (got & (BLOCK_SIZE - 1)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 667 | printf("Weird values in do_check: probably bugs\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 668 | } |
| 669 | got /= BLOCK_SIZE; |
| 670 | return got; |
| 671 | } |
| 672 | |
| 673 | static unsigned int currently_testing = 0; |
| 674 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 675 | static void alarm_intr(int alnum) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 676 | { |
| 677 | if (currently_testing >= ZONES) |
| 678 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 679 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 680 | alarm(5); |
| 681 | if (!currently_testing) |
| 682 | return; |
| 683 | printf("%d ...", currently_testing); |
| 684 | fflush(stdout); |
| 685 | } |
| 686 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 687 | static void check_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 688 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 689 | int try, got; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 690 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; |
| 691 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 692 | currently_testing = 0; |
| 693 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 694 | alarm(5); |
| 695 | while (currently_testing < ZONES) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 696 | if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) != |
| 697 | currently_testing * BLOCK_SIZE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 698 | die("seek failed in check_blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 699 | try = TEST_BUFFER_BLOCKS; |
| 700 | if (currently_testing + try > ZONES) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 701 | try = ZONES - currently_testing; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 702 | got = do_check(buffer, try, currently_testing); |
| 703 | currently_testing += got; |
| 704 | if (got == try) |
| 705 | continue; |
| 706 | if (currently_testing < FIRSTZONE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 707 | die("bad blocks before data-area: cannot make fs"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 708 | mark_zone(currently_testing); |
| 709 | badblocks++; |
| 710 | currently_testing++; |
| 711 | } |
| 712 | if (badblocks > 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 713 | printf("%d bad blocks\n", badblocks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 714 | else if (badblocks == 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 715 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 718 | static void get_list_blocks(filename) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 719 | char *filename; |
| 720 | |
| 721 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 722 | FILE *listfile; |
| 723 | unsigned long blockno; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 724 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 725 | listfile = fopen(filename, "r"); |
| 726 | if (listfile == (FILE *) NULL) { |
| 727 | die("can't open file of bad blocks"); |
| 728 | } |
| 729 | while (!feof(listfile)) { |
| 730 | fscanf(listfile, "%ld\n", &blockno); |
| 731 | mark_zone(blockno); |
| 732 | badblocks++; |
| 733 | } |
| 734 | if (badblocks > 1) |
| 735 | printf("%d bad blocks\n", badblocks); |
| 736 | else if (badblocks == 1) |
| 737 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 740 | extern int mkfs_minix_main(int argc, char **argv) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 741 | { |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 742 | int i=1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 743 | char *tmp; |
| 744 | struct stat statbuf; |
| 745 | char *listfile = NULL; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 746 | int stopIt=FALSE; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 747 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 748 | if (argc && *argv) |
| 749 | program_name = *argv; |
| 750 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) |
| 751 | die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 752 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 753 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) |
| 754 | die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 755 | #endif |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 756 | |
| 757 | /* Parse options */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 758 | argv++; |
| 759 | while (--argc >= 0 && *argv && **argv) { |
| 760 | if (**argv == '-') { |
| 761 | stopIt=FALSE; |
| 762 | while (i > 0 && *++(*argv) && stopIt==FALSE) { |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 763 | switch (**argv) { |
| 764 | case 'c': |
| 765 | check = 1; |
| 766 | break; |
| 767 | case 'i': |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 768 | { |
| 769 | char *cp=NULL; |
| 770 | if (*(*argv+1) != 0) { |
| 771 | cp = ++(*argv); |
| 772 | } else { |
| 773 | if (--argc == 0) { |
| 774 | goto goodbye; |
| 775 | } |
| 776 | cp = *(++argv); |
| 777 | } |
| 778 | req_nr_inodes = strtoul(cp, &tmp, 0); |
| 779 | if (*tmp) |
| 780 | show_usage(); |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 781 | stopIt=TRUE; |
| 782 | break; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 783 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 784 | case 'l': |
| 785 | if (--argc == 0) { |
| 786 | goto goodbye; |
| 787 | } |
| 788 | listfile = *(++argv); |
| 789 | break; |
| 790 | case 'n': |
| 791 | { |
| 792 | char *cp=NULL; |
| 793 | |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 794 | if (*(*argv+1) != 0) { |
| 795 | cp = ++(*argv); |
| 796 | } else { |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 797 | if (--argc == 0) { |
| 798 | goto goodbye; |
| 799 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 800 | cp = *(++argv); |
| 801 | } |
| 802 | i = strtoul(cp, &tmp, 0); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 803 | if (*tmp) |
| 804 | show_usage(); |
| 805 | if (i == 14) |
| 806 | magic = MINIX_SUPER_MAGIC; |
| 807 | else if (i == 30) |
| 808 | magic = MINIX_SUPER_MAGIC2; |
| 809 | else |
| 810 | show_usage(); |
| 811 | namelen = i; |
| 812 | dirsize = i + 2; |
| 813 | stopIt=TRUE; |
| 814 | break; |
| 815 | } |
| 816 | case 'v': |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 817 | #ifdef HAVE_MINIX2 |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 818 | version2 = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 819 | #else |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 820 | fprintf(stderr, "%s: not compiled with minix v2 support\n", |
| 821 | program_name, device_name); |
| 822 | exit(-1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 823 | #endif |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 824 | break; |
| 825 | case '-': |
| 826 | case 'h': |
| 827 | default: |
| 828 | goodbye: |
| 829 | show_usage(); |
| 830 | } |
| 831 | } |
| 832 | } else { |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 833 | if (device_name == NULL) |
| 834 | device_name = *argv; |
| 835 | else if (BLOCKS == 0) |
| 836 | BLOCKS = strtol(*argv, &tmp, 0); |
| 837 | else { |
| 838 | goto goodbye; |
| 839 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 840 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 841 | argv++; |
| 842 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 843 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 844 | if (device_name && !BLOCKS) |
| 845 | BLOCKS = get_size(device_name) / 1024; |
| 846 | if (!device_name || BLOCKS < 10) { |
| 847 | show_usage(); |
| 848 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 849 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 850 | if (version2) { |
| 851 | if (namelen == 14) |
| 852 | magic = MINIX2_SUPER_MAGIC; |
| 853 | else |
| 854 | magic = MINIX2_SUPER_MAGIC2; |
| 855 | } else |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 856 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 857 | if (BLOCKS > 65535) |
| 858 | BLOCKS = 65535; |
| 859 | check_mount(); /* is it already mounted? */ |
| 860 | tmp = root_block; |
| 861 | *(short *) tmp = 1; |
| 862 | strcpy(tmp + 2, "."); |
| 863 | tmp += dirsize; |
| 864 | *(short *) tmp = 1; |
| 865 | strcpy(tmp + 2, ".."); |
| 866 | tmp += dirsize; |
| 867 | *(short *) tmp = 2; |
| 868 | strcpy(tmp + 2, ".badblocks"); |
| 869 | DEV = open(device_name, O_RDWR); |
| 870 | if (DEV < 0) |
| 871 | die("unable to open %s"); |
| 872 | if (fstat(DEV, &statbuf) < 0) |
| 873 | die("unable to stat %s"); |
| 874 | if (!S_ISBLK(statbuf.st_mode)) |
| 875 | check = 0; |
| 876 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) |
| 877 | die("will not try to make filesystem on '%s'"); |
| 878 | setup_tables(); |
| 879 | if (check) |
| 880 | check_blocks(); |
| 881 | else if (listfile) |
| 882 | get_list_blocks(listfile); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 883 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 884 | if (version2) { |
| 885 | make_root_inode2(); |
| 886 | make_bad_inode2(); |
| 887 | } else |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 888 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 889 | { |
| 890 | make_root_inode(); |
| 891 | make_bad_inode(); |
| 892 | } |
| 893 | mark_good_blocks(); |
| 894 | write_tables(); |
Eric Andersen | bd4a75f | 2000-06-19 20:03:48 +0000 | [diff] [blame] | 895 | return( 0); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 896 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 897 | } |