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