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