Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 2 | /* |
| 3 | * mkfs.c - make a linux (minix) file-system. |
| 4 | * |
| 5 | * (C) 1991 Linus Torvalds. This file may be redistributed as per |
| 6 | * the Linux copyright. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * DD.MM.YY |
| 11 | * |
| 12 | * 24.11.91 - Time began. Used the fsck sources to get started. |
| 13 | * |
| 14 | * 25.11.91 - Corrected some bugs. Added support for ".badblocks" |
| 15 | * The algorithm for ".badblocks" is a bit weird, but |
| 16 | * it should work. Oh, well. |
| 17 | * |
| 18 | * 25.01.92 - Added the -l option for getting the list of bad blocks |
| 19 | * out of a named file. (Dave Rivers, rivers@ponds.uucp) |
| 20 | * |
| 21 | * 28.02.92 - Added %-information when using -c. |
| 22 | * |
| 23 | * 28.02.93 - Added support for other namelengths than the original |
| 24 | * 14 characters so that I can test the new kernel routines.. |
| 25 | * |
| 26 | * 09.10.93 - Make exit status conform to that required by fsutil |
| 27 | * (Rik Faith, faith@cs.unc.edu) |
| 28 | * |
| 29 | * 31.10.93 - Added inode request feature, for backup floppies: use |
| 30 | * 32 inodes, for a news partition use more. |
| 31 | * (Scott Heavner, sdh@po.cwru.edu) |
| 32 | * |
| 33 | * 03.01.94 - Added support for file system valid flag. |
| 34 | * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu) |
| 35 | * |
| 36 | * 30.10.94 - added support for v2 filesystem |
| 37 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 38 | * |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 39 | * 09.11.94 - Added test to prevent overwrite of mounted fs adapted |
| 40 | * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs |
| 41 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) |
| 42 | * |
| 43 | * 03.20.95 - Clear first 512 bytes of filesystem to make certain that |
| 44 | * the filesystem is not misidentified as a MS-DOS FAT filesystem. |
| 45 | * (Daniel Quinlan, quinlan@yggdrasil.com) |
| 46 | * |
| 47 | * 02.07.96 - Added small patch from Russell King to make the program a |
| 48 | * good deal more portable (janl@math.uio.no) |
| 49 | * |
| 50 | * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks] |
| 51 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 52 | * -c for readability checking (SLOW!) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 53 | * -l for getting a list of bad blocks from a file. |
| 54 | * -n for namelength (currently the kernel only uses 14 or 30) |
| 55 | * -i for number of inodes |
| 56 | * -v for v2 filesystem |
| 57 | * |
| 58 | * The device may be a block device or a image of one, but this isn't |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 59 | * enforced (but it's not much fun on a character device :-). |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 60 | * |
| 61 | * Modified for BusyBox by Erik Andersen <andersen@debian.org> -- |
| 62 | * removed getopt based parser and added a hand rolled one. |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 63 | */ |
| 64 | |
| 65 | #include <stdio.h> |
| 66 | #include <time.h> |
| 67 | #include <unistd.h> |
| 68 | #include <string.h> |
| 69 | #include <signal.h> |
| 70 | #include <fcntl.h> |
| 71 | #include <ctype.h> |
| 72 | #include <stdlib.h> |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 73 | #include <stdint.h> |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 74 | #include <termios.h> |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 75 | #include <sys/ioctl.h> |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 76 | #include <sys/param.h> |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 77 | #include <mntent.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 78 | #include "busybox.h" |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 79 | |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 80 | #define MINIX_ROOT_INO 1 |
| 81 | #define MINIX_LINK_MAX 250 |
| 82 | #define MINIX2_LINK_MAX 65530 |
| 83 | |
| 84 | #define MINIX_I_MAP_SLOTS 8 |
| 85 | #define MINIX_Z_MAP_SLOTS 64 |
| 86 | #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */ |
| 87 | #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */ |
| 88 | #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */ |
| 89 | #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */ |
| 90 | #define MINIX_VALID_FS 0x0001 /* Clean fs. */ |
| 91 | #define MINIX_ERROR_FS 0x0002 /* fs has errors. */ |
| 92 | |
| 93 | #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode))) |
| 94 | #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode))) |
| 95 | |
| 96 | #define MINIX_V1 0x0001 /* original minix fs */ |
| 97 | #define MINIX_V2 0x0002 /* minix V2 fs */ |
| 98 | |
| 99 | #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version |
| 100 | |
| 101 | /* |
| 102 | * This is the original minix inode layout on disk. |
| 103 | * Note the 8-bit gid and atime and ctime. |
| 104 | */ |
| 105 | struct minix_inode { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 106 | uint16_t i_mode; |
| 107 | uint16_t i_uid; |
| 108 | uint32_t i_size; |
| 109 | uint32_t i_time; |
| 110 | uint8_t i_gid; |
| 111 | uint8_t i_nlinks; |
| 112 | uint16_t i_zone[9]; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /* |
| 116 | * The new minix inode has all the time entries, as well as |
| 117 | * long block numbers and a third indirect block (7+1+1+1 |
| 118 | * instead of 7+1+1). Also, some previously 8-bit values are |
| 119 | * now 16-bit. The inode is now 64 bytes instead of 32. |
| 120 | */ |
| 121 | struct minix2_inode { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 122 | uint16_t i_mode; |
| 123 | uint16_t i_nlinks; |
| 124 | uint16_t i_uid; |
| 125 | uint16_t i_gid; |
| 126 | uint32_t i_size; |
| 127 | uint32_t i_atime; |
| 128 | uint32_t i_mtime; |
| 129 | uint32_t i_ctime; |
| 130 | uint32_t i_zone[10]; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * minix super-block data on disk |
| 135 | */ |
| 136 | struct minix_super_block { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 137 | uint16_t s_ninodes; |
| 138 | uint16_t s_nzones; |
| 139 | uint16_t s_imap_blocks; |
| 140 | uint16_t s_zmap_blocks; |
| 141 | uint16_t s_firstdatazone; |
| 142 | uint16_t s_log_zone_size; |
| 143 | uint32_t s_max_size; |
| 144 | uint16_t s_magic; |
| 145 | uint16_t s_state; |
| 146 | uint32_t s_zones; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | struct minix_dir_entry { |
Eric Andersen | 39cdf4e | 2004-01-30 22:40:05 +0000 | [diff] [blame] | 150 | uint16_t inode; |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 151 | char name[0]; |
| 152 | }; |
| 153 | |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 154 | #define NAME_MAX 255 /* # chars in a file name */ |
| 155 | |
| 156 | #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode))) |
| 157 | |
| 158 | #define MINIX_VALID_FS 0x0001 /* Clean fs. */ |
| 159 | #define MINIX_ERROR_FS 0x0002 /* fs has errors. */ |
| 160 | |
| 161 | #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */ |
| 162 | #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */ |
| 163 | |
| 164 | #ifndef BLKGETSIZE |
| 165 | #define BLKGETSIZE _IO(0x12,96) /* return device size */ |
| 166 | #endif |
| 167 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 168 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 169 | #ifndef __linux__ |
| 170 | #define volatile |
| 171 | #endif |
| 172 | |
| 173 | #define MINIX_ROOT_INO 1 |
| 174 | #define MINIX_BAD_INO 2 |
| 175 | |
| 176 | #define TEST_BUFFER_BLOCKS 16 |
| 177 | #define MAX_GOOD_BLOCKS 512 |
| 178 | |
| 179 | #define UPPER(size,n) (((size)+((n)-1))/(n)) |
| 180 | #define INODE_SIZE (sizeof(struct minix_inode)) |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 181 | #ifdef CONFIG_FEATURE_MINIX2 |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 182 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) |
| 183 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ |
| 184 | : MINIX_INODES_PER_BLOCK)) |
| 185 | #else |
| 186 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) |
| 187 | #endif |
| 188 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) |
| 189 | |
| 190 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) |
| 191 | |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 192 | static char *device_name; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 193 | static int DEV = -1; |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 194 | static uint32_t BLOCKS; |
| 195 | static int check; |
| 196 | static int badblocks; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 197 | static int namelen = 30; /* default (changed to 30, per Linus's |
| 198 | |
| 199 | suggestion, Sun Nov 21 08:05:07 1993) */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 200 | static int dirsize = 32; |
| 201 | static int magic = MINIX_SUPER_MAGIC2; |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 202 | static int version2; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 203 | |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 204 | static char root_block[BLOCK_SIZE]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 205 | |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 206 | static char *inode_buffer; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 208 | #define Inode (((struct minix_inode *) inode_buffer)-1) |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 209 | #ifdef CONFIG_FEATURE_MINIX2 |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 210 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) |
| 211 | #endif |
| 212 | static char super_block_buffer[BLOCK_SIZE]; |
| 213 | static char boot_block_buffer[512]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 214 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 215 | #define Super (*(struct minix_super_block *)super_block_buffer) |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 216 | #define INODES (Super.s_ninodes) |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 217 | #ifdef CONFIG_FEATURE_MINIX2 |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 218 | #define ZONES (version2 ? Super.s_zones : Super.s_nzones) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 219 | #else |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 220 | #define ZONES (Super.s_nzones) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 221 | #endif |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 222 | #define IMAPS (Super.s_imap_blocks) |
| 223 | #define ZMAPS (Super.s_zmap_blocks) |
| 224 | #define FIRSTZONE (Super.s_firstdatazone) |
| 225 | #define ZONESIZE (Super.s_log_zone_size) |
| 226 | #define MAXSIZE (Super.s_max_size) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 227 | #define MAGIC (Super.s_magic) |
| 228 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) |
| 229 | |
| 230 | static char *inode_map; |
| 231 | static char *zone_map; |
| 232 | |
| 233 | static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 234 | static int used_good_blocks; |
| 235 | static unsigned long req_nr_inodes; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 236 | |
Eric Andersen | 56f3e35 | 2000-09-19 21:13:55 +0000 | [diff] [blame] | 237 | static inline int bit(char * a,unsigned int i) |
| 238 | { |
| 239 | return (a[i >> 3] & (1<<(i & 7))) != 0; |
| 240 | } |
| 241 | #define inode_in_use(x) (bit(inode_map,(x))) |
| 242 | #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 243 | |
| 244 | #define mark_inode(x) (setbit(inode_map,(x))) |
| 245 | #define unmark_inode(x) (clrbit(inode_map,(x))) |
| 246 | |
| 247 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1)) |
| 248 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1)) |
| 249 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 250 | /* |
| 251 | * Check to make certain that our new filesystem won't be created on |
| 252 | * an already mounted partition. Code adapted from mke2fs, Copyright |
| 253 | * (C) 1994 Theodore Ts'o. Also licensed under GPL. |
| 254 | */ |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 255 | static inline void check_mount(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 256 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | FILE *f; |
| 258 | struct mntent *mnt; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 259 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 260 | if ((f = setmntent(MOUNTED, "r")) == NULL) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 261 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 262 | while ((mnt = getmntent(f)) != NULL) |
| 263 | if (strcmp(device_name, mnt->mnt_fsname) == 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 264 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | endmntent(f); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 266 | if (!mnt) |
| 267 | return; |
| 268 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 269 | bb_error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 272 | static long valid_offset(int fd, int offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 273 | { |
| 274 | char ch; |
| 275 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 276 | if (lseek(fd, offset, 0) < 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 277 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 278 | if (read(fd, &ch, 1) < 1) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 279 | return 0; |
| 280 | return 1; |
| 281 | } |
| 282 | |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 283 | static inline int count_blocks(int fd) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 284 | { |
| 285 | int high, low; |
| 286 | |
| 287 | low = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 288 | for (high = 1; valid_offset(fd, high); high *= 2) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 289 | low = high; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 290 | while (low < high - 1) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 291 | const int mid = (low + high) / 2; |
| 292 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 293 | if (valid_offset(fd, mid)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 294 | low = mid; |
| 295 | else |
| 296 | high = mid; |
| 297 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 298 | valid_offset(fd, 0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 299 | return (low + 1); |
| 300 | } |
| 301 | |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 302 | static inline int get_size(const char *file) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 303 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 304 | int fd; |
| 305 | long size; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 306 | |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 307 | fd = bb_xopen3(file, O_RDWR, 0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 308 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) { |
| 309 | close(fd); |
| 310 | return (size * 512); |
| 311 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 313 | size = count_blocks(fd); |
| 314 | close(fd); |
| 315 | return size; |
| 316 | } |
| 317 | |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 318 | static inline void write_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 319 | { |
| 320 | /* Mark the super block valid. */ |
| 321 | Super.s_state |= MINIX_VALID_FS; |
| 322 | Super.s_state &= ~MINIX_ERROR_FS; |
| 323 | |
| 324 | if (lseek(DEV, 0, SEEK_SET)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 325 | bb_error_msg_and_die("seek to boot block failed in write_tables"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 326 | if (512 != write(DEV, boot_block_buffer, 512)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 327 | bb_error_msg_and_die("unable to clear boot sector"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 328 | if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 329 | bb_error_msg_and_die("seek failed in write_tables"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 330 | if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 331 | bb_error_msg_and_die("unable to write super-block"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 332 | if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 333 | bb_error_msg_and_die("unable to write inode map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 334 | if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 335 | bb_error_msg_and_die("unable to write zone map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 336 | if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 337 | bb_error_msg_and_die("unable to write inodes"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 338 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 341 | static void write_block(int blk, char *buffer) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 342 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 343 | if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 344 | bb_error_msg_and_die("seek failed in write_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 345 | if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 346 | bb_error_msg_and_die("write failed in write_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 349 | static int get_free_block(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 350 | { |
| 351 | int blk; |
| 352 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 353 | if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 354 | bb_error_msg_and_die("too many bad blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 355 | if (used_good_blocks) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 356 | blk = good_blocks_table[used_good_blocks - 1] + 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 357 | else |
| 358 | blk = FIRSTZONE; |
| 359 | while (blk < ZONES && zone_in_use(blk)) |
| 360 | blk++; |
| 361 | if (blk >= ZONES) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 362 | bb_error_msg_and_die("not enough good blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 363 | good_blocks_table[used_good_blocks] = blk; |
| 364 | used_good_blocks++; |
| 365 | return blk; |
| 366 | } |
| 367 | |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 368 | static inline void mark_good_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 369 | { |
| 370 | int blk; |
| 371 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 372 | for (blk = 0; blk < used_good_blocks; blk++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 373 | mark_zone(good_blocks_table[blk]); |
| 374 | } |
| 375 | |
Eric Andersen | 92d2324 | 2001-03-19 23:49:41 +0000 | [diff] [blame] | 376 | static int next(int zone) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 377 | { |
| 378 | if (!zone) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 379 | zone = FIRSTZONE - 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 380 | while (++zone < ZONES) |
| 381 | if (zone_in_use(zone)) |
| 382 | return zone; |
| 383 | return 0; |
| 384 | } |
| 385 | |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 386 | static inline void make_bad_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 387 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | struct minix_inode *inode = &Inode[MINIX_BAD_INO]; |
| 389 | int i, j, zone; |
| 390 | int ind = 0, dind = 0; |
| 391 | unsigned short ind_block[BLOCK_SIZE >> 1]; |
| 392 | unsigned short dind_block[BLOCK_SIZE >> 1]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 393 | |
| 394 | #define NEXT_BAD (zone = next(zone)) |
| 395 | |
| 396 | if (!badblocks) |
| 397 | return; |
| 398 | mark_inode(MINIX_BAD_INO); |
| 399 | inode->i_nlinks = 1; |
| 400 | inode->i_time = time(NULL); |
| 401 | inode->i_mode = S_IFREG + 0000; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 402 | inode->i_size = badblocks * BLOCK_SIZE; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 403 | zone = next(0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 404 | for (i = 0; i < 7; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 405 | inode->i_zone[i] = zone; |
| 406 | if (!NEXT_BAD) |
| 407 | goto end_bad; |
| 408 | } |
| 409 | inode->i_zone[7] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 410 | memset(ind_block, 0, BLOCK_SIZE); |
| 411 | for (i = 0; i < 512; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 412 | ind_block[i] = zone; |
| 413 | if (!NEXT_BAD) |
| 414 | goto end_bad; |
| 415 | } |
| 416 | inode->i_zone[8] = dind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 417 | memset(dind_block, 0, BLOCK_SIZE); |
| 418 | for (i = 0; i < 512; i++) { |
| 419 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 420 | dind_block[i] = ind = get_free_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 421 | memset(ind_block, 0, BLOCK_SIZE); |
| 422 | for (j = 0; j < 512; j++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 423 | ind_block[j] = zone; |
| 424 | if (!NEXT_BAD) |
| 425 | goto end_bad; |
| 426 | } |
| 427 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 428 | bb_error_msg_and_die("too many bad blocks"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 429 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 430 | if (ind) |
| 431 | write_block(ind, (char *) ind_block); |
| 432 | if (dind) |
| 433 | write_block(dind, (char *) dind_block); |
| 434 | } |
| 435 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 436 | #ifdef CONFIG_FEATURE_MINIX2 |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 437 | static inline void make_bad_inode2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 438 | { |
| 439 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; |
| 440 | int i, j, zone; |
| 441 | int ind = 0, dind = 0; |
| 442 | unsigned long ind_block[BLOCK_SIZE >> 2]; |
| 443 | unsigned long dind_block[BLOCK_SIZE >> 2]; |
| 444 | |
| 445 | if (!badblocks) |
| 446 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 447 | mark_inode(MINIX_BAD_INO); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 448 | inode->i_nlinks = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 449 | inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 450 | inode->i_mode = S_IFREG + 0000; |
| 451 | inode->i_size = badblocks * BLOCK_SIZE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | zone = next(0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 453 | for (i = 0; i < 7; i++) { |
| 454 | inode->i_zone[i] = zone; |
| 455 | if (!NEXT_BAD) |
| 456 | goto end_bad; |
| 457 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | inode->i_zone[7] = ind = get_free_block(); |
| 459 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 460 | for (i = 0; i < 256; i++) { |
| 461 | ind_block[i] = zone; |
| 462 | if (!NEXT_BAD) |
| 463 | goto end_bad; |
| 464 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 465 | inode->i_zone[8] = dind = get_free_block(); |
| 466 | memset(dind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 467 | for (i = 0; i < 256; i++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 468 | write_block(ind, (char *) ind_block); |
| 469 | dind_block[i] = ind = get_free_block(); |
| 470 | memset(ind_block, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 471 | for (j = 0; j < 256; j++) { |
| 472 | ind_block[j] = zone; |
| 473 | if (!NEXT_BAD) |
| 474 | goto end_bad; |
| 475 | } |
| 476 | } |
| 477 | /* Could make triple indirect block here */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 478 | bb_error_msg_and_die("too many bad blocks"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 479 | end_bad: |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 480 | if (ind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 481 | write_block(ind, (char *) ind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 482 | if (dind) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 483 | write_block(dind, (char *) dind_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 484 | } |
| 485 | #endif |
| 486 | |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 487 | static inline void make_root_inode(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 488 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 489 | struct minix_inode *inode = &Inode[MINIX_ROOT_INO]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 490 | |
| 491 | mark_inode(MINIX_ROOT_INO); |
| 492 | inode->i_zone[0] = get_free_block(); |
| 493 | inode->i_nlinks = 2; |
| 494 | inode->i_time = time(NULL); |
| 495 | if (badblocks) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 496 | inode->i_size = 3 * dirsize; |
| 497 | else { |
| 498 | root_block[2 * dirsize] = '\0'; |
| 499 | root_block[2 * dirsize + 1] = '\0'; |
| 500 | inode->i_size = 2 * dirsize; |
| 501 | } |
| 502 | inode->i_mode = S_IFDIR + 0755; |
| 503 | inode->i_uid = getuid(); |
| 504 | if (inode->i_uid) |
| 505 | inode->i_gid = getgid(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 506 | write_block(inode->i_zone[0], root_block); |
| 507 | } |
| 508 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 509 | #ifdef CONFIG_FEATURE_MINIX2 |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 510 | static inline void make_root_inode2(void) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 511 | { |
| 512 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; |
| 513 | |
| 514 | mark_inode(MINIX_ROOT_INO); |
| 515 | inode->i_zone[0] = get_free_block(); |
| 516 | inode->i_nlinks = 2; |
| 517 | inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); |
| 518 | if (badblocks) |
| 519 | inode->i_size = 3 * dirsize; |
| 520 | else { |
| 521 | root_block[2 * dirsize] = '\0'; |
| 522 | root_block[2 * dirsize + 1] = '\0'; |
| 523 | inode->i_size = 2 * dirsize; |
| 524 | } |
| 525 | inode->i_mode = S_IFDIR + 0755; |
| 526 | inode->i_uid = getuid(); |
| 527 | if (inode->i_uid) |
| 528 | inode->i_gid = getgid(); |
| 529 | write_block(inode->i_zone[0], root_block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 530 | } |
| 531 | #endif |
| 532 | |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 533 | static inline void setup_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 534 | { |
| 535 | int i; |
| 536 | unsigned long inodes; |
| 537 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 538 | memset(super_block_buffer, 0, BLOCK_SIZE); |
| 539 | memset(boot_block_buffer, 0, 512); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 540 | MAGIC = magic; |
| 541 | ZONESIZE = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 542 | MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 543 | #ifdef CONFIG_FEATURE_MINIX2 |
| 544 | if (version2) { |
| 545 | Super.s_zones = BLOCKS; |
| 546 | } else |
| 547 | #endif |
| 548 | Super.s_nzones = BLOCKS; |
| 549 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 550 | /* some magic nrs: 1 inode / 3 blocks */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 551 | if (req_nr_inodes == 0) |
| 552 | inodes = BLOCKS / 3; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 553 | else |
| 554 | inodes = req_nr_inodes; |
| 555 | /* Round up inode count to fill block size */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 556 | #ifdef CONFIG_FEATURE_MINIX2 |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 557 | if (version2) |
| 558 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 559 | ~(MINIX2_INODES_PER_BLOCK - 1)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 560 | else |
| 561 | #endif |
| 562 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 563 | ~(MINIX_INODES_PER_BLOCK - 1)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 564 | if (inodes > 65535) |
| 565 | inodes = 65535; |
| 566 | INODES = inodes; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 567 | IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 568 | ZMAPS = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 569 | i = 0; |
| 570 | while (ZMAPS != |
| 571 | UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, |
| 572 | BITS_PER_BLOCK) && i < 1000) { |
| 573 | ZMAPS = |
| 574 | UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, |
| 575 | BITS_PER_BLOCK); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 576 | i++; |
| 577 | } |
| 578 | /* Real bad hack but overwise mkfs.minix can be thrown |
| 579 | * in infinite loop... |
| 580 | * try: |
| 581 | * dd if=/dev/zero of=test.fs count=10 bs=1024 |
| 582 | * /sbin/mkfs.minix -i 200 test.fs |
| 583 | * */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 584 | if (i >= 999) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 585 | bb_error_msg_and_die("unable to allocate buffers for maps"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 586 | } |
| 587 | FIRSTZONE = NORM_FIRSTZONE; |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 588 | inode_map = xmalloc(IMAPS * BLOCK_SIZE); |
| 589 | zone_map = xmalloc(ZMAPS * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 590 | memset(inode_map, 0xff, IMAPS * BLOCK_SIZE); |
| 591 | memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE); |
| 592 | for (i = FIRSTZONE; i < ZONES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 593 | unmark_zone(i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 594 | for (i = MINIX_ROOT_INO; i <= INODES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 595 | unmark_inode(i); |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 596 | inode_buffer = xmalloc(INODE_BUFFER_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 597 | memset(inode_buffer, 0, INODE_BUFFER_SIZE); |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 598 | printf("%ld inodes\n", (long)INODES); |
| 599 | printf("%ld blocks\n", (long)ZONES); |
| 600 | printf("Firstdatazone=%ld (%ld)\n", (long)FIRSTZONE, (long)NORM_FIRSTZONE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 601 | printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE); |
Eric Andersen | b225e2a | 2004-08-28 00:43:07 +0000 | [diff] [blame] | 602 | printf("Maxsize=%ld\n\n", (long)MAXSIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Perform a test of a block; return the number of |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 607 | * blocks readable/writable. |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 608 | */ |
Eric Andersen | c774efe | 2003-08-06 07:34:19 +0000 | [diff] [blame] | 609 | static inline long do_check(char *buffer, int try, unsigned int current_block) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 610 | { |
| 611 | long got; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 612 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 613 | /* Seek to the correct loc. */ |
| 614 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 615 | current_block * BLOCK_SIZE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 616 | bb_error_msg_and_die("seek failed during testing of blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | |
| 620 | /* Try the read */ |
| 621 | got = read(DEV, buffer, try * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 622 | if (got < 0) |
| 623 | got = 0; |
| 624 | if (got & (BLOCK_SIZE - 1)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 625 | printf("Weird values in do_check: probably bugs\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 626 | } |
| 627 | got /= BLOCK_SIZE; |
| 628 | return got; |
| 629 | } |
| 630 | |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 631 | static unsigned int currently_testing; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 632 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 633 | static void alarm_intr(int alnum) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 634 | { |
| 635 | if (currently_testing >= ZONES) |
| 636 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 637 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 638 | alarm(5); |
| 639 | if (!currently_testing) |
| 640 | return; |
| 641 | printf("%d ...", currently_testing); |
| 642 | fflush(stdout); |
| 643 | } |
| 644 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 645 | static void check_blocks(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 646 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 647 | int try, got; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 648 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; |
| 649 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 650 | currently_testing = 0; |
| 651 | signal(SIGALRM, alarm_intr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 652 | alarm(5); |
| 653 | while (currently_testing < ZONES) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 654 | if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) != |
| 655 | currently_testing * BLOCK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 656 | bb_error_msg_and_die("seek failed in check_blocks"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 657 | try = TEST_BUFFER_BLOCKS; |
| 658 | if (currently_testing + try > ZONES) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 659 | try = ZONES - currently_testing; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 660 | got = do_check(buffer, try, currently_testing); |
| 661 | currently_testing += got; |
| 662 | if (got == try) |
| 663 | continue; |
| 664 | if (currently_testing < FIRSTZONE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 665 | bb_error_msg_and_die("bad blocks before data-area: cannot make fs"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 666 | mark_zone(currently_testing); |
| 667 | badblocks++; |
| 668 | currently_testing++; |
| 669 | } |
| 670 | if (badblocks > 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 671 | printf("%d bad blocks\n", badblocks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 672 | else if (badblocks == 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 673 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 676 | static void get_list_blocks(char *filename) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 677 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 678 | FILE *listfile; |
| 679 | unsigned long blockno; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 680 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 681 | listfile = bb_xfopen(filename, "r"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 682 | while (!feof(listfile)) { |
| 683 | fscanf(listfile, "%ld\n", &blockno); |
| 684 | mark_zone(blockno); |
| 685 | badblocks++; |
| 686 | } |
| 687 | if (badblocks > 1) |
| 688 | printf("%d bad blocks\n", badblocks); |
| 689 | else if (badblocks == 1) |
| 690 | printf("one bad block\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 693 | int mkfs_minix_main(int argc, char **argv) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 694 | { |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 695 | int i=1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 696 | char *tmp; |
| 697 | struct stat statbuf; |
| 698 | char *listfile = NULL; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 699 | int stopIt=FALSE; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 700 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 701 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 702 | bb_error_msg_and_die("bad inode size"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 703 | #ifdef CONFIG_FEATURE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 704 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 705 | bb_error_msg_and_die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 706 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 707 | |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 708 | /* Parse options */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 709 | argv++; |
| 710 | while (--argc >= 0 && *argv && **argv) { |
| 711 | if (**argv == '-') { |
| 712 | stopIt=FALSE; |
| 713 | while (i > 0 && *++(*argv) && stopIt==FALSE) { |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 714 | switch (**argv) { |
| 715 | case 'c': |
| 716 | check = 1; |
| 717 | break; |
| 718 | case 'i': |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 719 | { |
| 720 | char *cp=NULL; |
| 721 | if (*(*argv+1) != 0) { |
| 722 | cp = ++(*argv); |
| 723 | } else { |
| 724 | if (--argc == 0) { |
| 725 | goto goodbye; |
| 726 | } |
| 727 | cp = *(++argv); |
| 728 | } |
| 729 | req_nr_inodes = strtoul(cp, &tmp, 0); |
| 730 | if (*tmp) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 731 | bb_show_usage(); |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 732 | stopIt=TRUE; |
| 733 | break; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 734 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 735 | case 'l': |
| 736 | if (--argc == 0) { |
| 737 | goto goodbye; |
| 738 | } |
| 739 | listfile = *(++argv); |
| 740 | break; |
| 741 | case 'n': |
| 742 | { |
| 743 | char *cp=NULL; |
| 744 | |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 745 | if (*(*argv+1) != 0) { |
| 746 | cp = ++(*argv); |
| 747 | } else { |
Eric Andersen | 3232203 | 2000-06-06 22:12:35 +0000 | [diff] [blame] | 748 | if (--argc == 0) { |
| 749 | goto goodbye; |
| 750 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 751 | cp = *(++argv); |
| 752 | } |
| 753 | i = strtoul(cp, &tmp, 0); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 754 | if (*tmp) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 755 | bb_show_usage(); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 756 | if (i == 14) |
| 757 | magic = MINIX_SUPER_MAGIC; |
| 758 | else if (i == 30) |
| 759 | magic = MINIX_SUPER_MAGIC2; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 760 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 761 | bb_show_usage(); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 762 | namelen = i; |
| 763 | dirsize = i + 2; |
| 764 | stopIt=TRUE; |
| 765 | break; |
| 766 | } |
| 767 | case 'v': |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 768 | #ifdef CONFIG_FEATURE_MINIX2 |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 769 | version2 = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 770 | #else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 771 | bb_error_msg("%s: not compiled with minix v2 support", |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 772 | device_name); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 773 | exit(-1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 774 | #endif |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 775 | break; |
| 776 | case '-': |
| 777 | case 'h': |
| 778 | default: |
| 779 | goodbye: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 780 | bb_show_usage(); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } else { |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 784 | if (device_name == NULL) |
| 785 | device_name = *argv; |
| 786 | else if (BLOCKS == 0) |
| 787 | BLOCKS = strtol(*argv, &tmp, 0); |
| 788 | else { |
| 789 | goto goodbye; |
| 790 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 791 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 792 | argv++; |
| 793 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 794 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 795 | if (device_name && !BLOCKS) |
| 796 | BLOCKS = get_size(device_name) / 1024; |
| 797 | if (!device_name || BLOCKS < 10) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 798 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 799 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 800 | #ifdef CONFIG_FEATURE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 801 | if (version2) { |
| 802 | if (namelen == 14) |
| 803 | magic = MINIX2_SUPER_MAGIC; |
| 804 | else |
| 805 | magic = MINIX2_SUPER_MAGIC2; |
| 806 | } else |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 807 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 808 | if (BLOCKS > 65535) |
| 809 | BLOCKS = 65535; |
| 810 | check_mount(); /* is it already mounted? */ |
| 811 | tmp = root_block; |
| 812 | *(short *) tmp = 1; |
| 813 | strcpy(tmp + 2, "."); |
| 814 | tmp += dirsize; |
| 815 | *(short *) tmp = 1; |
| 816 | strcpy(tmp + 2, ".."); |
| 817 | tmp += dirsize; |
| 818 | *(short *) tmp = 2; |
| 819 | strcpy(tmp + 2, ".badblocks"); |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 820 | DEV = bb_xopen3(device_name, O_RDWR, 0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 821 | if (fstat(DEV, &statbuf) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 822 | bb_error_msg_and_die("unable to stat %s", device_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 823 | if (!S_ISBLK(statbuf.st_mode)) |
| 824 | check = 0; |
| 825 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 826 | bb_error_msg_and_die("will not try to make filesystem on '%s'", device_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 827 | setup_tables(); |
| 828 | if (check) |
| 829 | check_blocks(); |
| 830 | else if (listfile) |
| 831 | get_list_blocks(listfile); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 832 | #ifdef CONFIG_FEATURE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 833 | if (version2) { |
| 834 | make_root_inode2(); |
| 835 | make_bad_inode2(); |
| 836 | } else |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 837 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 838 | { |
| 839 | make_root_inode(); |
| 840 | make_bad_inode(); |
| 841 | } |
| 842 | mark_good_blocks(); |
| 843 | write_tables(); |
"Vladimir N. Oleynik" | 1f2dc40 | 2005-10-15 13:56:17 +0000 | [diff] [blame] | 844 | return 0; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 845 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 846 | } |