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 | * |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * (C) 1991 Linus Torvalds. |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * DD.MM.YY |
| 12 | * |
| 13 | * 24.11.91 - Time began. Used the fsck sources to get started. |
| 14 | * |
| 15 | * 25.11.91 - Corrected some bugs. Added support for ".badblocks" |
| 16 | * The algorithm for ".badblocks" is a bit weird, but |
| 17 | * it should work. Oh, well. |
| 18 | * |
| 19 | * 25.01.92 - Added the -l option for getting the list of bad blocks |
| 20 | * out of a named file. (Dave Rivers, rivers@ponds.uucp) |
| 21 | * |
| 22 | * 28.02.92 - Added %-information when using -c. |
| 23 | * |
| 24 | * 28.02.93 - Added support for other namelengths than the original |
| 25 | * 14 characters so that I can test the new kernel routines.. |
| 26 | * |
| 27 | * 09.10.93 - Make exit status conform to that required by fsutil |
| 28 | * (Rik Faith, faith@cs.unc.edu) |
| 29 | * |
| 30 | * 31.10.93 - Added inode request feature, for backup floppies: use |
| 31 | * 32 inodes, for a news partition use more. |
| 32 | * (Scott Heavner, sdh@po.cwru.edu) |
| 33 | * |
| 34 | * 03.01.94 - Added support for file system valid flag. |
| 35 | * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu) |
| 36 | * |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 37 | * 30.10.94 - added support for v2 filesystem |
| 38 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 39 | * |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 40 | * 09.11.94 - Added test to prevent overwrite of mounted fs adapted |
| 41 | * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs |
| 42 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) |
| 43 | * |
| 44 | * 03.20.95 - Clear first 512 bytes of filesystem to make certain that |
| 45 | * the filesystem is not misidentified as a MS-DOS FAT filesystem. |
| 46 | * (Daniel Quinlan, quinlan@yggdrasil.com) |
| 47 | * |
| 48 | * 02.07.96 - Added small patch from Russell King to make the program a |
| 49 | * good deal more portable (janl@math.uio.no) |
| 50 | * |
| 51 | * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks] |
| 52 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 53 | * -c for readability checking (SLOW!) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 54 | * -l for getting a list of bad blocks from a file. |
| 55 | * -n for namelength (currently the kernel only uses 14 or 30) |
| 56 | * -i for number of inodes |
| 57 | * -v for v2 filesystem |
| 58 | * |
| 59 | * 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] | 60 | * enforced (but it's not much fun on a character device :-). |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 61 | * |
| 62 | * Modified for BusyBox by Erik Andersen <andersen@debian.org> -- |
| 63 | * removed getopt based parser and added a hand rolled one. |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 64 | */ |
| 65 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 66 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 67 | #include <mntent.h> |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 69 | #include "minix.h" |
| 70 | |
Denis Vlasenko | 7ab3699 | 2007-12-02 08:55:34 +0000 | [diff] [blame] | 71 | /* Store the very same times/uids/gids for image consistency */ |
| 72 | #if 1 |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 73 | # define CUR_TIME 0 |
| 74 | # define GETUID 0 |
| 75 | # define GETGID 0 |
| 76 | #else |
Denis Vlasenko | 7ab3699 | 2007-12-02 08:55:34 +0000 | [diff] [blame] | 77 | /* Was using this. Is it useful? NB: this will break testsuite */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 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 | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 92 | enum { dev_fd = 3 }; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 94 | struct globals { |
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 | char root_block[BLOCK_SIZE]; |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 113 | char superblock_buffer[BLOCK_SIZE]; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 114 | char boot_block_buffer[512]; |
| 115 | unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; |
Denis Vlasenko | 83bb98f | 2007-03-18 14:43:21 +0000 | [diff] [blame] | 116 | /* check_blocks(): buffer[] was the biggest static in entire bbox */ |
| 117 | char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 119 | unsigned short ind_block1[BLOCK_SIZE >> 1]; |
| 120 | unsigned short dind_block1[BLOCK_SIZE >> 1]; |
| 121 | unsigned long ind_block2[BLOCK_SIZE >> 2]; |
| 122 | unsigned long dind_block2[BLOCK_SIZE >> 2]; |
| 123 | }; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 124 | #define G (*ptr_to_globals) |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 125 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 126 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 127 | } while (0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 129 | static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n) |
Eric Andersen | 56f3e35 | 2000-09-19 21:13:55 +0000 | [diff] [blame] | 130 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 131 | return (size + n-1) / n; |
Eric Andersen | 56f3e35 | 2000-09-19 21:13:55 +0000 | [diff] [blame] | 132 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 134 | #define INODE_BUF1 (((struct minix1_inode*)G.inode_buffer) - 1) |
| 135 | #define INODE_BUF2 (((struct minix2_inode*)G.inode_buffer) - 1) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 136 | |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 137 | #define SB (*(struct minix_superblock*)G.superblock_buffer) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 138 | |
| 139 | #define SB_INODES (SB.s_ninodes) |
| 140 | #define SB_IMAPS (SB.s_imap_blocks) |
| 141 | #define SB_ZMAPS (SB.s_zmap_blocks) |
| 142 | #define SB_FIRSTZONE (SB.s_firstdatazone) |
| 143 | #define SB_ZONE_SIZE (SB.s_log_zone_size) |
| 144 | #define SB_MAXSIZE (SB.s_max_size) |
| 145 | #define SB_MAGIC (SB.s_magic) |
| 146 | |
| 147 | #if !ENABLE_FEATURE_MINIX2 |
| 148 | # define SB_ZONES (SB.s_nzones) |
| 149 | # define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK) |
| 150 | #else |
| 151 | # define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones) |
| 152 | # define INODE_BLOCKS div_roundup(SB_INODES, \ |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 153 | (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK)) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
| 156 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) |
| 157 | #define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS) |
| 158 | |
Denis Vlasenko | cc24419 | 2007-02-06 00:37:40 +0000 | [diff] [blame] | 159 | /* Before you ask "where they come from?": */ |
| 160 | /* setbit/clrbit are supplied by sys/param.h */ |
| 161 | |
| 162 | static int minix_bit(const char* a, unsigned i) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 163 | { |
Denis Vlasenko | 12ff9dc | 2008-11-22 18:58:11 +0000 | [diff] [blame] | 164 | return a[i >> 3] & (1<<(i & 7)); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Denis Vlasenko | cc24419 | 2007-02-06 00:37:40 +0000 | [diff] [blame] | 167 | static void minix_setbit(char *a, unsigned i) |
| 168 | { |
| 169 | setbit(a, i); |
| 170 | } |
| 171 | static void minix_clrbit(char *a, unsigned i) |
| 172 | { |
| 173 | clrbit(a, i); |
| 174 | } |
Denis Vlasenko | dc485c9 | 2007-02-06 00:36:53 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 176 | /* Note: do not assume 0/1, it is 0/nonzero */ |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 177 | #define zone_in_use(x) minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1) |
| 178 | /*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 179 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 180 | #define mark_inode(x) minix_setbit(G.inode_map,(x)) |
| 181 | #define unmark_inode(x) minix_clrbit(G.inode_map,(x)) |
| 182 | #define mark_zone(x) minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1) |
| 183 | #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] | 184 | |
| 185 | #ifndef BLKGETSIZE |
| 186 | # define BLKGETSIZE _IO(0x12,96) /* return device size */ |
| 187 | #endif |
| 188 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 189 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 190 | static long valid_offset(int fd, int offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 191 | { |
| 192 | char ch; |
| 193 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 194 | if (lseek(fd, offset, SEEK_SET) < 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 195 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | if (read(fd, &ch, 1) < 1) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | return 1; |
| 199 | } |
| 200 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 201 | static int count_blocks(int fd) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 202 | { |
| 203 | int high, low; |
| 204 | |
| 205 | low = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | for (high = 1; valid_offset(fd, high); high *= 2) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 207 | low = high; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 208 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 209 | while (low < high - 1) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 210 | const int mid = (low + high) / 2; |
| 211 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 212 | if (valid_offset(fd, mid)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 213 | low = mid; |
| 214 | else |
| 215 | high = mid; |
| 216 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 217 | valid_offset(fd, 0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 218 | return (low + 1); |
| 219 | } |
| 220 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 221 | static int get_size(const char *file) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 222 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | int fd; |
| 224 | long size; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 225 | |
Bernhard Reutner-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 226 | fd = xopen(file, O_RDWR); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 227 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { |
| 228 | close(fd); |
| 229 | return (size * 512); |
| 230 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 231 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 232 | size = count_blocks(fd); |
| 233 | close(fd); |
| 234 | return size; |
| 235 | } |
| 236 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 237 | static void write_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 238 | { |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 239 | /* Mark the superblock valid. */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 240 | SB.s_state |= MINIX_VALID_FS; |
| 241 | SB.s_state &= ~MINIX_ERROR_FS; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 243 | msg_eol = "seek to 0 failed"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 244 | xlseek(dev_fd, 0, SEEK_SET); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 245 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 246 | msg_eol = "can't clear boot sector"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 247 | xwrite(dev_fd, G.boot_block_buffer, 512); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 248 | |
| 249 | msg_eol = "seek to BLOCK_SIZE failed"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 250 | xlseek(dev_fd, BLOCK_SIZE, SEEK_SET); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 251 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 252 | msg_eol = "can't write superblock"; |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 253 | xwrite(dev_fd, G.superblock_buffer, BLOCK_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 254 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 255 | msg_eol = "can't write inode map"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 256 | xwrite(dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 257 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 258 | msg_eol = "can't write zone map"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 259 | xwrite(dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 260 | |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 261 | msg_eol = "can't write inodes"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 262 | xwrite(dev_fd, G.inode_buffer, INODE_BUFFER_SIZE); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 263 | |
| 264 | msg_eol = "\n"; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 267 | static void write_block(int blk, char *buffer) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 268 | { |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 269 | xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET); |
| 270 | xwrite(dev_fd, buffer, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 273 | static int get_free_block(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 274 | { |
| 275 | int blk; |
| 276 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 277 | if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 278 | bb_error_msg_and_die("too many bad blocks"); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 279 | if (G.used_good_blocks) |
| 280 | blk = G.good_blocks_table[G.used_good_blocks - 1] + 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 281 | else |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 282 | blk = SB_FIRSTZONE; |
| 283 | while (blk < SB_ZONES && zone_in_use(blk)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 284 | blk++; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 285 | if (blk >= SB_ZONES) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 286 | bb_error_msg_and_die("not enough good blocks"); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 287 | G.good_blocks_table[G.used_good_blocks] = blk; |
| 288 | G.used_good_blocks++; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 289 | return blk; |
| 290 | } |
| 291 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 292 | static void mark_good_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 293 | { |
| 294 | int blk; |
| 295 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 296 | for (blk = 0; blk < G.used_good_blocks; blk++) |
| 297 | mark_zone(G.good_blocks_table[blk]); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Eric Andersen | 92d2324 | 2001-03-19 23:49:41 +0000 | [diff] [blame] | 300 | static int next(int zone) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 301 | { |
| 302 | if (!zone) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 303 | zone = SB_FIRSTZONE - 1; |
| 304 | while (++zone < SB_ZONES) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 305 | if (zone_in_use(zone)) |
| 306 | return zone; |
| 307 | return 0; |
| 308 | } |
| 309 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 310 | static void make_bad_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 311 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 312 | struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 313 | int i, j, zone; |
| 314 | int ind = 0, dind = 0; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 315 | /* moved to globals to reduce stack usage |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 316 | unsigned short ind_block[BLOCK_SIZE >> 1]; |
| 317 | unsigned short dind_block[BLOCK_SIZE >> 1]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 318 | */ |
| 319 | #define ind_block (G.ind_block1) |
| 320 | #define dind_block (G.dind_block1) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 321 | |
| 322 | #define NEXT_BAD (zone = next(zone)) |
| 323 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 324 | if (!G.badblocks) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 325 | return; |
| 326 | mark_inode(MINIX_BAD_INO); |
| 327 | inode->i_nlinks = 1; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 328 | /* BTW, setting this makes all images different */ |
| 329 | /* it's harder to check for bugs then - diff isn't helpful :(... */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 330 | inode->i_time = CUR_TIME; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 331 | inode->i_mode = S_IFREG + 0000; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 332 | inode->i_size = G.badblocks * BLOCK_SIZE; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 333 | zone = next(0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 334 | for (i = 0; i < 7; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 335 | inode->i_zone[i] = zone; |
| 336 | if (!NEXT_BAD) |
| 337 | goto end_bad; |
| 338 | } |
| 339 | inode->i_zone[7] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 340 | memset(ind_block, 0, BLOCK_SIZE); |
| 341 | for (i = 0; i < 512; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 342 | ind_block[i] = zone; |
| 343 | if (!NEXT_BAD) |
| 344 | goto end_bad; |
| 345 | } |
| 346 | inode->i_zone[8] = dind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 347 | memset(dind_block, 0, BLOCK_SIZE); |
| 348 | for (i = 0; i < 512; i++) { |
| 349 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 350 | dind_block[i] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 351 | memset(ind_block, 0, BLOCK_SIZE); |
| 352 | for (j = 0; j < 512; j++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 353 | ind_block[j] = zone; |
| 354 | if (!NEXT_BAD) |
| 355 | goto end_bad; |
| 356 | } |
| 357 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 358 | bb_error_msg_and_die("too many bad blocks"); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 359 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 360 | if (ind) |
| 361 | write_block(ind, (char *) ind_block); |
| 362 | if (dind) |
| 363 | write_block(dind, (char *) dind_block); |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 364 | #undef ind_block |
| 365 | #undef dind_block |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 368 | #if ENABLE_FEATURE_MINIX2 |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 369 | static void make_bad_inode2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 370 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 371 | struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 372 | int i, j, zone; |
| 373 | int ind = 0, dind = 0; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 374 | /* moved to globals to reduce stack usage |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 375 | unsigned long ind_block[BLOCK_SIZE >> 2]; |
| 376 | unsigned long dind_block[BLOCK_SIZE >> 2]; |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 377 | */ |
| 378 | #define ind_block (G.ind_block2) |
| 379 | #define dind_block (G.dind_block2) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 380 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 381 | if (!G.badblocks) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 382 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 383 | mark_inode(MINIX_BAD_INO); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 384 | inode->i_nlinks = 1; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 385 | inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 386 | inode->i_mode = S_IFREG + 0000; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 387 | inode->i_size = G.badblocks * BLOCK_SIZE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | zone = next(0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 389 | for (i = 0; i < 7; i++) { |
| 390 | inode->i_zone[i] = zone; |
| 391 | if (!NEXT_BAD) |
| 392 | goto end_bad; |
| 393 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 394 | inode->i_zone[7] = ind = get_free_block(); |
| 395 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 396 | for (i = 0; i < 256; i++) { |
| 397 | ind_block[i] = zone; |
| 398 | if (!NEXT_BAD) |
| 399 | goto end_bad; |
| 400 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 401 | inode->i_zone[8] = dind = get_free_block(); |
| 402 | memset(dind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 403 | for (i = 0; i < 256; i++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 404 | write_block(ind, (char *) ind_block); |
| 405 | dind_block[i] = ind = get_free_block(); |
| 406 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 407 | for (j = 0; j < 256; j++) { |
| 408 | ind_block[j] = zone; |
| 409 | if (!NEXT_BAD) |
| 410 | goto end_bad; |
| 411 | } |
| 412 | } |
| 413 | /* Could make triple indirect block here */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 414 | bb_error_msg_and_die("too many bad blocks"); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 415 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 416 | if (ind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 417 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 418 | if (dind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 419 | write_block(dind, (char *) dind_block); |
Denis Vlasenko | ab80187 | 2007-12-02 08:35:37 +0000 | [diff] [blame] | 420 | #undef ind_block |
| 421 | #undef dind_block |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 422 | } |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 423 | #else |
| 424 | void make_bad_inode2(void); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 425 | #endif |
| 426 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 427 | static void make_root_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 428 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 429 | struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 430 | |
| 431 | mark_inode(MINIX_ROOT_INO); |
| 432 | inode->i_zone[0] = get_free_block(); |
| 433 | inode->i_nlinks = 2; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 434 | inode->i_time = CUR_TIME; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 435 | if (G.badblocks) |
| 436 | inode->i_size = 3 * G.dirsize; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 437 | else { |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 438 | G.root_block[2 * G.dirsize] = '\0'; |
| 439 | G.root_block[2 * G.dirsize + 1] = '\0'; |
| 440 | inode->i_size = 2 * G.dirsize; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 441 | } |
| 442 | inode->i_mode = S_IFDIR + 0755; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 443 | inode->i_uid = GETUID; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 444 | if (inode->i_uid) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 445 | inode->i_gid = GETGID; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 446 | write_block(inode->i_zone[0], G.root_block); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 449 | #if ENABLE_FEATURE_MINIX2 |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 450 | static void make_root_inode2(void) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 451 | { |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 452 | struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 453 | |
| 454 | mark_inode(MINIX_ROOT_INO); |
| 455 | inode->i_zone[0] = get_free_block(); |
| 456 | inode->i_nlinks = 2; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 457 | inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 458 | if (G.badblocks) |
| 459 | inode->i_size = 3 * G.dirsize; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 460 | else { |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 461 | G.root_block[2 * G.dirsize] = '\0'; |
| 462 | G.root_block[2 * G.dirsize + 1] = '\0'; |
| 463 | inode->i_size = 2 * G.dirsize; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 464 | } |
| 465 | inode->i_mode = S_IFDIR + 0755; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 466 | inode->i_uid = GETUID; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 467 | if (inode->i_uid) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 468 | inode->i_gid = GETGID; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 469 | write_block(inode->i_zone[0], G.root_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 470 | } |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 471 | #else |
| 472 | void make_root_inode2(void); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 473 | #endif |
| 474 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 475 | /* |
| 476 | * Perform a test of a block; return the number of |
| 477 | * blocks readable. |
| 478 | */ |
| 479 | static size_t do_check(char *buffer, size_t try, unsigned current_block) |
| 480 | { |
| 481 | ssize_t got; |
| 482 | |
| 483 | /* Seek to the correct loc. */ |
| 484 | msg_eol = "seek failed during testing of blocks"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 485 | xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 486 | msg_eol = "\n"; |
| 487 | |
| 488 | /* Try the read */ |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 489 | got = read(dev_fd, buffer, try * BLOCK_SIZE); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 490 | if (got < 0) |
| 491 | got = 0; |
| 492 | try = ((size_t)got) / BLOCK_SIZE; |
| 493 | |
| 494 | if (got & (BLOCK_SIZE - 1)) |
| 495 | fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try)); |
| 496 | return try; |
| 497 | } |
| 498 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 499 | static void alarm_intr(int alnum UNUSED_PARAM) |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 500 | { |
| 501 | if (G.currently_testing >= SB_ZONES) |
| 502 | return; |
| 503 | signal(SIGALRM, alarm_intr); |
| 504 | alarm(5); |
| 505 | if (!G.currently_testing) |
| 506 | return; |
| 507 | printf("%d ...", G.currently_testing); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 508 | fflush_all(); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static void check_blocks(void) |
| 512 | { |
| 513 | size_t try, got; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 514 | |
| 515 | G.currently_testing = 0; |
| 516 | signal(SIGALRM, alarm_intr); |
| 517 | alarm(5); |
| 518 | while (G.currently_testing < SB_ZONES) { |
| 519 | msg_eol = "seek failed in check_blocks"; |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 520 | xlseek(dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 521 | msg_eol = "\n"; |
| 522 | try = TEST_BUFFER_BLOCKS; |
| 523 | if (G.currently_testing + try > SB_ZONES) |
| 524 | try = SB_ZONES - G.currently_testing; |
Denis Vlasenko | 83bb98f | 2007-03-18 14:43:21 +0000 | [diff] [blame] | 525 | got = do_check(G.check_blocks_buffer, try, G.currently_testing); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 526 | G.currently_testing += got; |
| 527 | if (got == try) |
| 528 | continue; |
| 529 | if (G.currently_testing < SB_FIRSTZONE) |
| 530 | bb_error_msg_and_die("bad blocks before data-area: cannot make fs"); |
| 531 | mark_zone(G.currently_testing); |
| 532 | G.badblocks++; |
| 533 | G.currently_testing++; |
| 534 | } |
| 535 | alarm(0); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 536 | printf("%d bad block(s)\n", G.badblocks); |
| 537 | } |
| 538 | |
| 539 | static void get_list_blocks(char *filename) |
| 540 | { |
| 541 | FILE *listfile; |
| 542 | unsigned long blockno; |
| 543 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 544 | listfile = xfopen_for_read(filename); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 545 | while (!feof(listfile)) { |
| 546 | fscanf(listfile, "%ld\n", &blockno); |
| 547 | mark_zone(blockno); |
| 548 | G.badblocks++; |
| 549 | } |
| 550 | printf("%d bad block(s)\n", G.badblocks); |
| 551 | } |
| 552 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 553 | static void setup_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 554 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 555 | unsigned long inodes; |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 556 | unsigned norm_firstzone; |
Denis Vlasenko | 28bb84b | 2006-11-29 23:15:04 +0000 | [diff] [blame] | 557 | unsigned sb_zmaps; |
| 558 | unsigned i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 559 | |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 560 | /* memset(G.superblock_buffer, 0, BLOCK_SIZE); */ |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 561 | /* memset(G.boot_block_buffer, 0, 512); */ |
| 562 | SB_MAGIC = G.magic; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 563 | SB_ZONE_SIZE = 0; |
| 564 | SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; |
| 565 | if (version2) |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 566 | SB.s_zones = G.total_blocks; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 567 | else |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 568 | SB.s_nzones = G.total_blocks; |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 569 | |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 570 | /* some magic nrs: 1 inode / 3 blocks */ |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 571 | if (G.req_nr_inodes == 0) |
| 572 | inodes = G.total_blocks / 3; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 573 | else |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 574 | inodes = G.req_nr_inodes; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 575 | /* Round up inode count to fill block size */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 576 | if (version2) |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 577 | inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) & |
| 578 | ~(MINIX2_INODES_PER_BLOCK - 1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 579 | else |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 580 | inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) & |
| 581 | ~(MINIX1_INODES_PER_BLOCK - 1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 582 | if (inodes > 65535) |
| 583 | inodes = 65535; |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 584 | SB_INODES = inodes; |
| 585 | SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK); |
| 586 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 587 | /* Real bad hack but overwise mkfs.minix can be thrown |
| 588 | * in infinite loop... |
| 589 | * try: |
| 590 | * dd if=/dev/zero of=test.fs count=10 bs=1024 |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 591 | * mkfs.minix -i 200 test.fs |
| 592 | */ |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 593 | /* This code is not insane: NORM_FIRSTZONE is not a constant, |
| 594 | * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 595 | i = 999; |
| 596 | SB_ZMAPS = 0; |
| 597 | do { |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 598 | norm_firstzone = NORM_FIRSTZONE; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 599 | 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] | 600 | if (SB_ZMAPS == sb_zmaps) goto got_it; |
| 601 | SB_ZMAPS = sb_zmaps; |
| 602 | /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */ |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 603 | } while (--i); |
| 604 | bb_error_msg_and_die("incompatible size/inode count, try different -i N"); |
| 605 | got_it: |
| 606 | |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 607 | SB_FIRSTZONE = norm_firstzone; |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 608 | G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE); |
| 609 | G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE); |
| 610 | memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE); |
| 611 | memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 612 | for (i = SB_FIRSTZONE; i < SB_ZONES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 613 | unmark_zone(i); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 614 | for (i = MINIX_ROOT_INO; i <= SB_INODES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 615 | unmark_inode(i); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 616 | G.inode_buffer = xzalloc(INODE_BUFFER_SIZE); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 617 | printf("%ld inodes\n", (long)SB_INODES); |
| 618 | printf("%ld blocks\n", (long)SB_ZONES); |
Denis Vlasenko | 016a82e | 2006-11-29 22:47:42 +0000 | [diff] [blame] | 619 | printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)norm_firstzone); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 620 | printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE); |
| 621 | printf("Maxsize=%ld\n", (long)SB_MAXSIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 624 | int mkfs_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 625 | int mkfs_minix_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 626 | { |
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 | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 630 | char *str_i; |
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 | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 646 | opt_complementary = "n+"; /* -n N */ |
| 647 | opt = getopt32(argv, "ci:l:n:v", &str_i, &listfile, &G.namelen); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 648 | argv += optind; |
| 649 | //if (opt & 1) -c |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 650 | if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 651 | //if (opt & 4) -l |
| 652 | if (opt & 8) { // -n |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 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 */ |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 685 | if (find_mount_point(G.device_name, 0)) |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame] | 686 | bb_error_msg_and_die("can't format mounted filesystem"); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 687 | |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 688 | xmove_fd(xopen(G.device_name, O_RDWR), dev_fd); |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 689 | xfstat(dev_fd, &statbuf, G.device_name); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 690 | if (!S_ISBLK(statbuf.st_mode)) |
| 691 | opt &= ~1; // clear -c (check) |
| 692 | |
| 693 | /* I don't know why someone has special code to prevent mkfs.minix |
| 694 | * on IDE devices. Why IDE but not SCSI, etc?... */ |
| 695 | #if 0 |
| 696 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) |
| 697 | /* what is this? */ |
| 698 | bb_error_msg_and_die("will not try " |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 699 | "to make filesystem on '%s'", G.device_name); |
Denis Vlasenko | 66b86c6 | 2006-11-29 22:33:47 +0000 | [diff] [blame] | 700 | #endif |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 701 | |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 702 | tmp = G.root_block; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 703 | *(short *) tmp = 1; |
| 704 | strcpy(tmp + 2, "."); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 705 | tmp += G.dirsize; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 706 | *(short *) tmp = 1; |
| 707 | strcpy(tmp + 2, ".."); |
Denis Vlasenko | 4efeaee | 2007-03-15 19:52:42 +0000 | [diff] [blame] | 708 | tmp += G.dirsize; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 709 | *(short *) tmp = 2; |
| 710 | strcpy(tmp + 2, ".badblocks"); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 711 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 712 | setup_tables(); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 713 | |
| 714 | if (opt & 1) // -c ? |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 715 | check_blocks(); |
| 716 | else if (listfile) |
| 717 | get_list_blocks(listfile); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 718 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 719 | if (version2) { |
| 720 | make_root_inode2(); |
| 721 | make_bad_inode2(); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 722 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 723 | make_root_inode(); |
| 724 | make_bad_inode(); |
| 725 | } |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 726 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 727 | mark_good_blocks(); |
| 728 | write_tables(); |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 729 | return 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 730 | } |