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