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 | * |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 68 | #define DEBUG 0 |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 70 | /* If debugging, store the very same times/uids/gids for image consistency */ |
| 71 | #if DEBUG |
| 72 | # define CUR_TIME 0 |
| 73 | # define GETUID 0 |
| 74 | # define GETGID 0 |
| 75 | #else |
| 76 | # define CUR_TIME time(NULL) |
| 77 | # define GETUID getuid() |
| 78 | # define GETGID getgid() |
| 79 | #endif |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * This is the original minix inode layout on disk. |
| 83 | * Note the 8-bit gid and atime and ctime. |
| 84 | */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 85 | struct minix1_inode { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 86 | uint16_t i_mode; |
| 87 | uint16_t i_uid; |
| 88 | uint32_t i_size; |
| 89 | uint32_t i_time; |
| 90 | uint8_t i_gid; |
| 91 | uint8_t i_nlinks; |
| 92 | uint16_t i_zone[9]; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | /* |
| 96 | * The new minix inode has all the time entries, as well as |
| 97 | * long block numbers and a third indirect block (7+1+1+1 |
| 98 | * instead of 7+1+1). Also, some previously 8-bit values are |
| 99 | * now 16-bit. The inode is now 64 bytes instead of 32. |
| 100 | */ |
| 101 | struct minix2_inode { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 102 | uint16_t i_mode; |
| 103 | uint16_t i_nlinks; |
| 104 | uint16_t i_uid; |
| 105 | uint16_t i_gid; |
| 106 | uint32_t i_size; |
| 107 | uint32_t i_atime; |
| 108 | uint32_t i_mtime; |
| 109 | uint32_t i_ctime; |
| 110 | uint32_t i_zone[10]; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * minix super-block data on disk |
| 115 | */ |
| 116 | struct minix_super_block { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 117 | uint16_t s_ninodes; |
| 118 | uint16_t s_nzones; |
| 119 | uint16_t s_imap_blocks; |
| 120 | uint16_t s_zmap_blocks; |
| 121 | uint16_t s_firstdatazone; |
| 122 | uint16_t s_log_zone_size; |
| 123 | uint32_t s_max_size; |
| 124 | uint16_t s_magic; |
| 125 | uint16_t s_state; |
| 126 | uint32_t s_zones; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | struct minix_dir_entry { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 130 | uint16_t inode; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 131 | char name[0]; |
| 132 | }; |
| 133 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 134 | /* Believe it or not, but mount.h has this one */ |
| 135 | #undef BLOCK_SIZE |
| 136 | enum { |
| 137 | BLOCK_SIZE = 1024, |
| 138 | BITS_PER_BLOCK = BLOCK_SIZE << 3, |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 140 | MINIX_ROOT_INO = 1, |
| 141 | MINIX_BAD_INO = 2, |
| 142 | MAX_GOOD_BLOCKS = 512, |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 143 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 144 | MINIX1_SUPER_MAGIC = 0x137F, /* original minix fs */ |
| 145 | MINIX1_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */ |
| 146 | MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */ |
| 147 | MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */ |
| 148 | MINIX_VALID_FS = 0x0001, /* clean fs */ |
| 149 | MINIX_ERROR_FS = 0x0002, /* fs has errors */ |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 151 | INODE_SIZE1 = sizeof(struct minix1_inode), |
| 152 | INODE_SIZE2 = sizeof(struct minix2_inode), |
| 153 | MINIX1_INODES_PER_BLOCK = BLOCK_SIZE / sizeof(struct minix1_inode), |
| 154 | MINIX2_INODES_PER_BLOCK = BLOCK_SIZE / sizeof(struct minix2_inode), |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 156 | TEST_BUFFER_BLOCKS = 16, |
| 157 | }; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 159 | #if ENABLE_FEATURE_MINIX2 |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 160 | static int version2; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 161 | #else |
| 162 | enum { version2 = 0 }; |
| 163 | #endif |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 165 | static char *device_name; |
| 166 | static int dev_fd = -1; |
| 167 | static uint32_t total_blocks; |
| 168 | static int badblocks; |
| 169 | /* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */ |
| 170 | static int namelen = 30; |
| 171 | static int dirsize = 32; |
| 172 | static int magic = MINIX1_SUPER_MAGIC2; |
| 173 | |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 174 | static char root_block[BLOCK_SIZE]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 175 | static char super_block_buffer[BLOCK_SIZE]; |
| 176 | static char boot_block_buffer[512]; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 177 | static char *inode_buffer; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 178 | |
| 179 | static char *inode_map; |
| 180 | static char *zone_map; |
| 181 | |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 182 | static int used_good_blocks; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 183 | static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 184 | static unsigned long req_nr_inodes; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | a597aad | 2006-12-16 23:48:13 +0000 | [diff] [blame] | 186 | static ATTRIBUTE_ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n) |
Eric Andersen | 56f3e35 | 2000-09-19 21:13:55 +0000 | [diff] [blame] | 187 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 188 | return (size + n-1) / n; |
Eric Andersen | 56f3e35 | 2000-09-19 21:13:55 +0000 | [diff] [blame] | 189 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 191 | #define INODE_BUF1 (((struct minix1_inode*)inode_buffer) - 1) |
| 192 | #define INODE_BUF2 (((struct minix2_inode*)inode_buffer) - 1) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 193 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 194 | #define SB (*(struct minix_super_block*)super_block_buffer) |
| 195 | |
| 196 | #define SB_INODES (SB.s_ninodes) |
| 197 | #define SB_IMAPS (SB.s_imap_blocks) |
| 198 | #define SB_ZMAPS (SB.s_zmap_blocks) |
| 199 | #define SB_FIRSTZONE (SB.s_firstdatazone) |
| 200 | #define SB_ZONE_SIZE (SB.s_log_zone_size) |
| 201 | #define SB_MAXSIZE (SB.s_max_size) |
| 202 | #define SB_MAGIC (SB.s_magic) |
| 203 | |
| 204 | #if !ENABLE_FEATURE_MINIX2 |
| 205 | # define SB_ZONES (SB.s_nzones) |
| 206 | # define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK) |
| 207 | #else |
| 208 | # define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones) |
| 209 | # define INODE_BLOCKS div_roundup(SB_INODES, \ |
| 210 | version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK) |
| 211 | #endif |
| 212 | |
| 213 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) |
| 214 | #define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS) |
| 215 | |
| 216 | static int bit(const char* a, unsigned i) |
| 217 | { |
| 218 | return a[i >> 3] & (1<<(i & 7)); |
| 219 | } |
| 220 | |
| 221 | /* Note: do not assume 0/1, it is 0/nonzero */ |
| 222 | #define inode_in_use(x) bit(inode_map,(x)) |
| 223 | #define zone_in_use(x) bit(zone_map,(x)-SB_FIRSTZONE+1) |
| 224 | |
| 225 | #define mark_inode(x) setbit(inode_map,(x)) |
| 226 | #define unmark_inode(x) clrbit(inode_map,(x)) |
| 227 | #define mark_zone(x) setbit(zone_map,(x)-SB_FIRSTZONE+1) |
| 228 | #define unmark_zone(x) clrbit(zone_map,(x)-SB_FIRSTZONE+1) |
| 229 | |
| 230 | #ifndef BLKGETSIZE |
| 231 | # define BLKGETSIZE _IO(0x12,96) /* return device size */ |
| 232 | #endif |
| 233 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 234 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 235 | static long valid_offset(int fd, int offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 236 | { |
| 237 | char ch; |
| 238 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 239 | if (lseek(fd, offset, SEEK_SET) < 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 240 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | if (read(fd, &ch, 1) < 1) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | return 1; |
| 244 | } |
| 245 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 246 | static int count_blocks(int fd) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 247 | { |
| 248 | int high, low; |
| 249 | |
| 250 | low = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | for (high = 1; valid_offset(fd, high); high *= 2) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 252 | low = high; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 253 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 254 | while (low < high - 1) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 255 | const int mid = (low + high) / 2; |
| 256 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | if (valid_offset(fd, mid)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 258 | low = mid; |
| 259 | else |
| 260 | high = mid; |
| 261 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 262 | valid_offset(fd, 0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 263 | return (low + 1); |
| 264 | } |
| 265 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 266 | static int get_size(const char *file) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 267 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 268 | int fd; |
| 269 | long size; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 270 | |
Bernhard Reutner-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 271 | fd = xopen(file, O_RDWR); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 272 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { |
| 273 | close(fd); |
| 274 | return (size * 512); |
| 275 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 276 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 277 | size = count_blocks(fd); |
| 278 | close(fd); |
| 279 | return size; |
| 280 | } |
| 281 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 282 | static void write_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 283 | { |
| 284 | /* Mark the super block valid. */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 285 | SB.s_state |= MINIX_VALID_FS; |
| 286 | SB.s_state &= ~MINIX_ERROR_FS; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 288 | msg_eol = "seek to 0 failed"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 289 | xlseek(dev_fd, 0, SEEK_SET); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 290 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 291 | msg_eol = "cannot clear boot sector"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 292 | xwrite(dev_fd, boot_block_buffer, 512); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 293 | |
| 294 | msg_eol = "seek to BLOCK_SIZE failed"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 295 | xlseek(dev_fd, BLOCK_SIZE, SEEK_SET); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 296 | |
| 297 | msg_eol = "cannot write superblock"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 298 | xwrite(dev_fd, super_block_buffer, BLOCK_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 299 | |
| 300 | msg_eol = "cannot write inode map"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 301 | xwrite(dev_fd, inode_map, SB_IMAPS * BLOCK_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 302 | |
| 303 | msg_eol = "cannot write zone map"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 304 | xwrite(dev_fd, zone_map, SB_ZMAPS * BLOCK_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 305 | |
| 306 | msg_eol = "cannot write inodes"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 307 | xwrite(dev_fd, inode_buffer, INODE_BUFFER_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 308 | |
| 309 | msg_eol = "\n"; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | static void write_block(int blk, char *buffer) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 313 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 314 | xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET); |
| 315 | xwrite(dev_fd, buffer, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 318 | static int get_free_block(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 319 | { |
| 320 | int blk; |
| 321 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 322 | if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 323 | bb_error_msg_and_die("too many bad blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 324 | if (used_good_blocks) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 325 | blk = good_blocks_table[used_good_blocks - 1] + 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 326 | else |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 327 | blk = SB_FIRSTZONE; |
| 328 | while (blk < SB_ZONES && zone_in_use(blk)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 329 | blk++; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 330 | if (blk >= SB_ZONES) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 331 | bb_error_msg_and_die("not enough good blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 332 | good_blocks_table[used_good_blocks] = blk; |
| 333 | used_good_blocks++; |
| 334 | return blk; |
| 335 | } |
| 336 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 337 | static void mark_good_blocks(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 | for (blk = 0; blk < used_good_blocks; blk++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 342 | mark_zone(good_blocks_table[blk]); |
| 343 | } |
| 344 | |
Eric Andersen | 92d2324 | 2001-03-19 23:49:41 +0000 | [diff] [blame] | 345 | static int next(int zone) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 346 | { |
| 347 | if (!zone) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 348 | zone = SB_FIRSTZONE - 1; |
| 349 | while (++zone < SB_ZONES) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 350 | if (zone_in_use(zone)) |
| 351 | return zone; |
| 352 | return 0; |
| 353 | } |
| 354 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 355 | static void make_bad_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 356 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 357 | struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 358 | int i, j, zone; |
| 359 | int ind = 0, dind = 0; |
| 360 | unsigned short ind_block[BLOCK_SIZE >> 1]; |
| 361 | unsigned short dind_block[BLOCK_SIZE >> 1]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 362 | |
| 363 | #define NEXT_BAD (zone = next(zone)) |
| 364 | |
| 365 | if (!badblocks) |
| 366 | return; |
| 367 | mark_inode(MINIX_BAD_INO); |
| 368 | inode->i_nlinks = 1; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 369 | /* BTW, setting this makes all images different */ |
| 370 | /* it's harder to check for bugs then - diff isn't helpful :(... */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 371 | inode->i_time = CUR_TIME; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 372 | inode->i_mode = S_IFREG + 0000; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 373 | inode->i_size = badblocks * BLOCK_SIZE; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 374 | zone = next(0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 375 | for (i = 0; i < 7; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 376 | inode->i_zone[i] = zone; |
| 377 | if (!NEXT_BAD) |
| 378 | goto end_bad; |
| 379 | } |
| 380 | inode->i_zone[7] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 381 | memset(ind_block, 0, BLOCK_SIZE); |
| 382 | for (i = 0; i < 512; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 383 | ind_block[i] = zone; |
| 384 | if (!NEXT_BAD) |
| 385 | goto end_bad; |
| 386 | } |
| 387 | inode->i_zone[8] = dind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | memset(dind_block, 0, BLOCK_SIZE); |
| 389 | for (i = 0; i < 512; i++) { |
| 390 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 391 | dind_block[i] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 392 | memset(ind_block, 0, BLOCK_SIZE); |
| 393 | for (j = 0; j < 512; j++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 394 | ind_block[j] = zone; |
| 395 | if (!NEXT_BAD) |
| 396 | goto end_bad; |
| 397 | } |
| 398 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 399 | bb_error_msg_and_die("too many bad blocks"); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 400 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 401 | if (ind) |
| 402 | write_block(ind, (char *) ind_block); |
| 403 | if (dind) |
| 404 | write_block(dind, (char *) dind_block); |
| 405 | } |
| 406 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 407 | #if ENABLE_FEATURE_MINIX2 |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 408 | static void make_bad_inode2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 409 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 410 | struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 411 | int i, j, zone; |
| 412 | int ind = 0, dind = 0; |
| 413 | unsigned long ind_block[BLOCK_SIZE >> 2]; |
| 414 | unsigned long dind_block[BLOCK_SIZE >> 2]; |
| 415 | |
| 416 | if (!badblocks) |
| 417 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 418 | mark_inode(MINIX_BAD_INO); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 419 | inode->i_nlinks = 1; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 420 | inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 421 | inode->i_mode = S_IFREG + 0000; |
| 422 | inode->i_size = badblocks * BLOCK_SIZE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | zone = next(0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 424 | for (i = 0; i < 7; i++) { |
| 425 | inode->i_zone[i] = zone; |
| 426 | if (!NEXT_BAD) |
| 427 | goto end_bad; |
| 428 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 429 | inode->i_zone[7] = ind = get_free_block(); |
| 430 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 431 | for (i = 0; i < 256; i++) { |
| 432 | ind_block[i] = zone; |
| 433 | if (!NEXT_BAD) |
| 434 | goto end_bad; |
| 435 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 436 | inode->i_zone[8] = dind = get_free_block(); |
| 437 | memset(dind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 438 | for (i = 0; i < 256; i++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 439 | write_block(ind, (char *) ind_block); |
| 440 | dind_block[i] = ind = get_free_block(); |
| 441 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 442 | for (j = 0; j < 256; j++) { |
| 443 | ind_block[j] = zone; |
| 444 | if (!NEXT_BAD) |
| 445 | goto end_bad; |
| 446 | } |
| 447 | } |
| 448 | /* Could make triple indirect block here */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 449 | bb_error_msg_and_die("too many bad blocks"); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 450 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 451 | if (ind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 453 | if (dind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 454 | write_block(dind, (char *) dind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 455 | } |
| 456 | #endif |
| 457 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 458 | static void make_root_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 459 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 460 | struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 461 | |
| 462 | mark_inode(MINIX_ROOT_INO); |
| 463 | inode->i_zone[0] = get_free_block(); |
| 464 | inode->i_nlinks = 2; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 465 | inode->i_time = CUR_TIME; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 466 | if (badblocks) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 467 | inode->i_size = 3 * dirsize; |
| 468 | else { |
| 469 | root_block[2 * dirsize] = '\0'; |
| 470 | root_block[2 * dirsize + 1] = '\0'; |
| 471 | inode->i_size = 2 * dirsize; |
| 472 | } |
| 473 | inode->i_mode = S_IFDIR + 0755; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 474 | inode->i_uid = GETUID; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 475 | if (inode->i_uid) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 476 | inode->i_gid = GETGID; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 477 | write_block(inode->i_zone[0], root_block); |
| 478 | } |
| 479 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 480 | #if ENABLE_FEATURE_MINIX2 |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 481 | static void make_root_inode2(void) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 482 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 483 | struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 484 | |
| 485 | mark_inode(MINIX_ROOT_INO); |
| 486 | inode->i_zone[0] = get_free_block(); |
| 487 | inode->i_nlinks = 2; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 488 | inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 489 | if (badblocks) |
| 490 | inode->i_size = 3 * dirsize; |
| 491 | else { |
| 492 | root_block[2 * dirsize] = '\0'; |
| 493 | root_block[2 * dirsize + 1] = '\0'; |
| 494 | inode->i_size = 2 * dirsize; |
| 495 | } |
| 496 | inode->i_mode = S_IFDIR + 0755; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 497 | inode->i_uid = GETUID; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 498 | if (inode->i_uid) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 499 | inode->i_gid = GETGID; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 500 | write_block(inode->i_zone[0], root_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 501 | } |
| 502 | #endif |
| 503 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 504 | static void setup_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 505 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 506 | unsigned long inodes; |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 507 | unsigned norm_firstzone; |
Denis Vlasenko | 28bb84b | 2006-11-29 23:15:04 +0000 | [diff] [blame] | 508 | unsigned sb_zmaps; |
| 509 | unsigned i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 510 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 511 | memset(super_block_buffer, 0, BLOCK_SIZE); |
| 512 | memset(boot_block_buffer, 0, 512); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 513 | SB_MAGIC = magic; |
| 514 | SB_ZONE_SIZE = 0; |
| 515 | SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; |
| 516 | if (version2) |
| 517 | SB.s_zones = total_blocks; |
| 518 | else |
| 519 | SB.s_nzones = total_blocks; |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 520 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 521 | /* some magic nrs: 1 inode / 3 blocks */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 522 | if (req_nr_inodes == 0) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 523 | inodes = total_blocks / 3; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 524 | else |
| 525 | inodes = req_nr_inodes; |
| 526 | /* Round up inode count to fill block size */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 527 | if (version2) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 528 | inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) & |
| 529 | ~(MINIX2_INODES_PER_BLOCK - 1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 530 | else |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 531 | inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) & |
| 532 | ~(MINIX1_INODES_PER_BLOCK - 1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 533 | if (inodes > 65535) |
| 534 | inodes = 65535; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 535 | SB_INODES = inodes; |
| 536 | SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK); |
| 537 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 538 | /* Real bad hack but overwise mkfs.minix can be thrown |
| 539 | * in infinite loop... |
| 540 | * try: |
| 541 | * dd if=/dev/zero of=test.fs count=10 bs=1024 |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 542 | * mkfs.minix -i 200 test.fs |
| 543 | */ |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 544 | /* This code is not insane: NORM_FIRSTZONE is not a constant, |
| 545 | * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 546 | i = 999; |
| 547 | SB_ZMAPS = 0; |
| 548 | do { |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 549 | norm_firstzone = NORM_FIRSTZONE; |
| 550 | sb_zmaps = div_roundup(total_blocks - norm_firstzone + 1, BITS_PER_BLOCK); |
| 551 | if (SB_ZMAPS == sb_zmaps) goto got_it; |
| 552 | SB_ZMAPS = sb_zmaps; |
| 553 | /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 554 | } while (--i); |
| 555 | bb_error_msg_and_die("incompatible size/inode count, try different -i N"); |
| 556 | got_it: |
| 557 | |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 558 | SB_FIRSTZONE = norm_firstzone; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 559 | inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE); |
| 560 | zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE); |
| 561 | memset(inode_map, 0xff, SB_IMAPS * BLOCK_SIZE); |
| 562 | memset(zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE); |
| 563 | for (i = SB_FIRSTZONE; i < SB_ZONES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 564 | unmark_zone(i); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 565 | for (i = MINIX_ROOT_INO; i <= SB_INODES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 566 | unmark_inode(i); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 567 | inode_buffer = xzalloc(INODE_BUFFER_SIZE); |
| 568 | printf("%ld inodes\n", (long)SB_INODES); |
| 569 | printf("%ld blocks\n", (long)SB_ZONES); |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 570 | printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)norm_firstzone); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 571 | printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE); |
| 572 | printf("Maxsize=%ld\n", (long)SB_MAXSIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Perform a test of a block; return the number of |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 577 | * blocks readable/writable. |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 578 | */ |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 579 | static long do_check(char *buffer, int try, unsigned current_block) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 580 | { |
| 581 | long got; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 582 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 583 | /* Seek to the correct loc. */ |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 584 | msg_eol = "seek failed during testing of blocks"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 585 | xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 586 | msg_eol = "\n"; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 587 | |
| 588 | /* Try the read */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 589 | got = read(dev_fd, buffer, try * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 590 | if (got < 0) |
| 591 | got = 0; |
| 592 | if (got & (BLOCK_SIZE - 1)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 593 | printf("Weird values in do_check: probably bugs\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 594 | } |
| 595 | got /= BLOCK_SIZE; |
| 596 | return got; |
| 597 | } |
| 598 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 599 | static unsigned currently_testing; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 600 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 601 | static void alarm_intr(int alnum) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 602 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 603 | if (currently_testing >= SB_ZONES) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 604 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 605 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 606 | alarm(5); |
| 607 | if (!currently_testing) |
| 608 | return; |
| 609 | printf("%d ...", currently_testing); |
| 610 | fflush(stdout); |
| 611 | } |
| 612 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 613 | static void check_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 614 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 615 | int try, got; |
Denis Vlasenko | 93f6aa6 | 2006-11-30 21:11:01 +0000 | [diff] [blame] | 616 | /* buffer[] was the biggest static in entire bbox */ |
| 617 | char *buffer = xmalloc(BLOCK_SIZE * TEST_BUFFER_BLOCKS); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 618 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 619 | currently_testing = 0; |
| 620 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 621 | alarm(5); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 622 | while (currently_testing < SB_ZONES) { |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 623 | msg_eol = "seek failed in check_blocks"; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 624 | xlseek(dev_fd, currently_testing * BLOCK_SIZE, SEEK_SET); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 625 | msg_eol = "\n"; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 626 | try = TEST_BUFFER_BLOCKS; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 627 | if (currently_testing + try > SB_ZONES) |
| 628 | try = SB_ZONES - currently_testing; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 629 | got = do_check(buffer, try, currently_testing); |
| 630 | currently_testing += got; |
| 631 | if (got == try) |
| 632 | continue; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 633 | if (currently_testing < SB_FIRSTZONE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 634 | 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] | 635 | mark_zone(currently_testing); |
| 636 | badblocks++; |
| 637 | currently_testing++; |
| 638 | } |
Denis Vlasenko | 93f6aa6 | 2006-11-30 21:11:01 +0000 | [diff] [blame] | 639 | free(buffer); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 640 | printf("%d bad block(s)\n", badblocks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 643 | static void get_list_blocks(char *filename) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 644 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 645 | FILE *listfile; |
| 646 | unsigned long blockno; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 647 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 648 | listfile = xfopen(filename, "r"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 649 | while (!feof(listfile)) { |
| 650 | fscanf(listfile, "%ld\n", &blockno); |
| 651 | mark_zone(blockno); |
| 652 | badblocks++; |
| 653 | } |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 654 | printf("%d bad block(s)\n", badblocks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 657 | int mkfs_minix_main(int argc, char **argv) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 658 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 659 | struct mntent *mp; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 660 | unsigned opt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 661 | char *tmp; |
| 662 | struct stat statbuf; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 663 | char *str_i, *str_n; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 664 | char *listfile = NULL; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 665 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 666 | if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 667 | bb_error_msg_and_die("bad inode size"); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 668 | #if ENABLE_FEATURE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 669 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 670 | bb_error_msg_and_die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 671 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 672 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 673 | opt = getopt32(argc, argv, "ci:l:n:v", &str_i, &listfile, &str_n); |
| 674 | argv += optind; |
| 675 | //if (opt & 1) -c |
| 676 | if (opt & 2) req_nr_inodes = xatoul(str_i); // -i |
| 677 | //if (opt & 4) -l |
| 678 | if (opt & 8) { // -n |
| 679 | namelen = xatoi_u(str_n); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 680 | if (namelen == 14) magic = MINIX1_SUPER_MAGIC; |
| 681 | else if (namelen == 30) magic = MINIX1_SUPER_MAGIC2; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 682 | else bb_show_usage(); |
| 683 | dirsize = namelen + 2; |
| 684 | } |
| 685 | if (opt & 0x10) { // -v |
| 686 | #if ENABLE_FEATURE_MINIX2 |
| 687 | version2 = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 688 | #else |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 689 | bb_error_msg_and_die("%s: not compiled with minix v2 support", |
| 690 | device_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 691 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 692 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 693 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 694 | device_name = *argv++; |
| 695 | if (!device_name) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 696 | bb_show_usage(); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 697 | if (*argv) |
| 698 | total_blocks = xatou32(*argv); |
| 699 | else |
| 700 | total_blocks = get_size(device_name) / 1024; |
| 701 | |
| 702 | if (total_blocks < 10) |
| 703 | bb_error_msg_and_die("must have at least 10 blocks"); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 704 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 705 | if (version2) { |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 706 | magic = MINIX2_SUPER_MAGIC2; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 707 | if (namelen == 14) |
| 708 | magic = MINIX2_SUPER_MAGIC; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 709 | } else if (total_blocks > 65535) |
| 710 | total_blocks = 65535; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 711 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 712 | /* Check if it is mounted */ |
| 713 | mp = find_mount_point(device_name, NULL); |
| 714 | if (mp && strcmp(device_name, mp->mnt_fsname) == 0) |
| 715 | bb_error_msg_and_die("%s is mounted on %s; " |
| 716 | "refusing to make a filesystem", |
| 717 | device_name, mp->mnt_dir); |
| 718 | |
| 719 | dev_fd = xopen(device_name, O_RDWR); |
| 720 | if (fstat(dev_fd, &statbuf) < 0) |
| 721 | bb_error_msg_and_die("cannot stat %s", device_name); |
| 722 | if (!S_ISBLK(statbuf.st_mode)) |
| 723 | opt &= ~1; // clear -c (check) |
| 724 | |
| 725 | /* I don't know why someone has special code to prevent mkfs.minix |
| 726 | * on IDE devices. Why IDE but not SCSI, etc?... */ |
| 727 | #if 0 |
| 728 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) |
| 729 | /* what is this? */ |
| 730 | bb_error_msg_and_die("will not try " |
| 731 | "to make filesystem on '%s'", device_name); |
| 732 | #endif |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 733 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 734 | tmp = root_block; |
| 735 | *(short *) tmp = 1; |
| 736 | strcpy(tmp + 2, "."); |
| 737 | tmp += dirsize; |
| 738 | *(short *) tmp = 1; |
| 739 | strcpy(tmp + 2, ".."); |
| 740 | tmp += dirsize; |
| 741 | *(short *) tmp = 2; |
| 742 | strcpy(tmp + 2, ".badblocks"); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 743 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 744 | setup_tables(); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 745 | |
| 746 | if (opt & 1) // -c ? |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 747 | check_blocks(); |
| 748 | else if (listfile) |
| 749 | get_list_blocks(listfile); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 750 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 751 | if (version2) { |
| 752 | make_root_inode2(); |
| 753 | make_bad_inode2(); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 754 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 755 | make_root_inode(); |
| 756 | make_bad_inode(); |
| 757 | } |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 758 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 759 | mark_good_blocks(); |
| 760 | write_tables(); |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 761 | return 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 762 | } |