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