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