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