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