blob: fafcc230e14bdf44b269492d3d76ceae49c0ba49 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersene494fdd1999-10-19 20:23:03 +00002/*
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)
38 *
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 *
52 * -c for readablility checking (SLOW!)
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
59 * enforced (but it's not much fun on a character device :-).
Erik Andersen330fd2b2000-05-19 05:35:19 +000060 *
61 * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
62 * removed getopt based parser and added a hand rolled one.
Eric Andersene494fdd1999-10-19 20:23:03 +000063 */
64
Eric Andersene674eb71999-10-19 20:52:57 +000065#include "internal.h"
Eric Andersene494fdd1999-10-19 20:23:03 +000066#include <stdio.h>
67#include <time.h>
68#include <unistd.h>
69#include <string.h>
70#include <signal.h>
71#include <fcntl.h>
72#include <ctype.h>
73#include <stdlib.h>
74#include <termios.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000075#include <sys/ioctl.h>
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000076#include <sys/param.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000077#include <mntent.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000078
Eric Andersen85c55202000-07-10 22:46:55 +000079
80typedef unsigned char u8;
81typedef unsigned short u16;
82typedef unsigned int u32;
83
84
85#define MINIX_ROOT_INO 1
86#define MINIX_LINK_MAX 250
87#define MINIX2_LINK_MAX 65530
88
89#define MINIX_I_MAP_SLOTS 8
90#define MINIX_Z_MAP_SLOTS 64
91#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
92#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
93#define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
94#define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
95#define MINIX_VALID_FS 0x0001 /* Clean fs. */
96#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
97
98#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
99#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
100
101#define MINIX_V1 0x0001 /* original minix fs */
102#define MINIX_V2 0x0002 /* minix V2 fs */
103
104#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
105
106/*
107 * This is the original minix inode layout on disk.
108 * Note the 8-bit gid and atime and ctime.
109 */
110struct minix_inode {
111 u16 i_mode;
112 u16 i_uid;
113 u32 i_size;
114 u32 i_time;
115 u8 i_gid;
116 u8 i_nlinks;
117 u16 i_zone[9];
118};
119
120/*
121 * The new minix inode has all the time entries, as well as
122 * long block numbers and a third indirect block (7+1+1+1
123 * instead of 7+1+1). Also, some previously 8-bit values are
124 * now 16-bit. The inode is now 64 bytes instead of 32.
125 */
126struct minix2_inode {
127 u16 i_mode;
128 u16 i_nlinks;
129 u16 i_uid;
130 u16 i_gid;
131 u32 i_size;
132 u32 i_atime;
133 u32 i_mtime;
134 u32 i_ctime;
135 u32 i_zone[10];
136};
137
138/*
139 * minix super-block data on disk
140 */
141struct minix_super_block {
142 u16 s_ninodes;
143 u16 s_nzones;
144 u16 s_imap_blocks;
145 u16 s_zmap_blocks;
146 u16 s_firstdatazone;
147 u16 s_log_zone_size;
148 u32 s_max_size;
149 u16 s_magic;
150 u16 s_state;
151 u32 s_zones;
152};
153
154struct minix_dir_entry {
155 u16 inode;
156 char name[0];
157};
158
159#define BLOCK_SIZE_BITS 10
160#define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
161
162#define NAME_MAX 255 /* # chars in a file name */
163
164#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
165
166#define MINIX_VALID_FS 0x0001 /* Clean fs. */
167#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
168
169#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
170#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
171
172#ifndef BLKGETSIZE
173#define BLKGETSIZE _IO(0x12,96) /* return device size */
174#endif
175
Eric Andersene494fdd1999-10-19 20:23:03 +0000176
Eric Andersene494fdd1999-10-19 20:23:03 +0000177#ifndef __linux__
178#define volatile
179#endif
180
181#define MINIX_ROOT_INO 1
182#define MINIX_BAD_INO 2
183
184#define TEST_BUFFER_BLOCKS 16
185#define MAX_GOOD_BLOCKS 512
186
187#define UPPER(size,n) (((size)+((n)-1))/(n))
188#define INODE_SIZE (sizeof(struct minix_inode))
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000189#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000190#define INODE_SIZE2 (sizeof(struct minix2_inode))
191#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
192 : MINIX_INODES_PER_BLOCK))
193#else
194#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
195#endif
196#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
197
198#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
199
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000201static int DEV = -1;
202static long BLOCKS = 0;
203static int check = 0;
204static int badblocks = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205static int namelen = 30; /* default (changed to 30, per Linus's
206
207 suggestion, Sun Nov 21 08:05:07 1993) */
Eric Andersene494fdd1999-10-19 20:23:03 +0000208static int dirsize = 32;
209static int magic = MINIX_SUPER_MAGIC2;
210static int version2 = 0;
211
212static char root_block[BLOCK_SIZE] = "\0";
213
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214static char *inode_buffer = NULL;
215
Eric Andersene494fdd1999-10-19 20:23:03 +0000216#define Inode (((struct minix_inode *) inode_buffer)-1)
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000217#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000218#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
219#endif
220static char super_block_buffer[BLOCK_SIZE];
221static char boot_block_buffer[512];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222
Eric Andersene494fdd1999-10-19 20:23:03 +0000223#define Super (*(struct minix_super_block *)super_block_buffer)
224#define INODES ((unsigned long)Super.s_ninodes)
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000225#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000226#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
227#else
228#define ZONES ((unsigned long)(Super.s_nzones))
229#endif
230#define IMAPS ((unsigned long)Super.s_imap_blocks)
231#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
232#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
233#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
234#define MAXSIZE ((unsigned long)Super.s_max_size)
235#define MAGIC (Super.s_magic)
236#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
237
238static char *inode_map;
239static char *zone_map;
240
241static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
242static int used_good_blocks = 0;
243static unsigned long req_nr_inodes = 0;
244
Eric Andersen56f3e352000-09-19 21:13:55 +0000245static inline int bit(char * a,unsigned int i)
246{
247 return (a[i >> 3] & (1<<(i & 7))) != 0;
248}
249#define inode_in_use(x) (bit(inode_map,(x)))
250#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000251
252#define mark_inode(x) (setbit(inode_map,(x)))
253#define unmark_inode(x) (clrbit(inode_map,(x)))
254
255#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
256#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
257
258/*
259 * Volatile to let gcc know that this doesn't return. When trying
260 * to compile this under minix, volatile gives a warning, as
261 * exit() isn't defined as volatile under minix.
262 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263static volatile void die(char *str)
264{
Matt Kraaid537a952000-07-14 01:51:25 +0000265 errorMsg("%s\n", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000266 exit(8);
267}
268
Erik Andersen330fd2b2000-05-19 05:35:19 +0000269static volatile void show_usage() __attribute__ ((noreturn));
Eric Andersene674eb71999-10-19 20:52:57 +0000270static volatile void show_usage()
Eric Andersene494fdd1999-10-19 20:23:03 +0000271{
Matt Kraai3bd8bd82000-07-14 23:28:47 +0000272 usage(mkfs_minix_usage);
Eric Andersene494fdd1999-10-19 20:23:03 +0000273}
274
275/*
276 * Check to make certain that our new filesystem won't be created on
277 * an already mounted partition. Code adapted from mke2fs, Copyright
278 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
279 */
280static void check_mount(void)
281{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282 FILE *f;
283 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000284
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000286 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 while ((mnt = getmntent(f)) != NULL)
288 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000289 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000291 if (!mnt)
292 return;
293
Eric Andersene674eb71999-10-19 20:52:57 +0000294 die("%s is mounted; will not make a filesystem here!");
Eric Andersene494fdd1999-10-19 20:23:03 +0000295}
296
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000298{
299 char ch;
300
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 if (lseek(fd, offset, 0) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000302 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000304 return 0;
305 return 1;
306}
307
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308static int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000309{
310 int high, low;
311
312 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000314 low = high;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000316 const int mid = (low + high) / 2;
317
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000319 low = mid;
320 else
321 high = mid;
322 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000324 return (low + 1);
325}
326
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327static int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000328{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 int fd;
330 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000331
332 fd = open(file, O_RDWR);
333 if (fd < 0) {
334 perror(file);
335 exit(1);
336 }
337 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
338 close(fd);
339 return (size * 512);
340 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341
Eric Andersene494fdd1999-10-19 20:23:03 +0000342 size = count_blocks(fd);
343 close(fd);
344 return size;
345}
346
Eric Andersene674eb71999-10-19 20:52:57 +0000347static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000348{
349 /* Mark the super block valid. */
350 Super.s_state |= MINIX_VALID_FS;
351 Super.s_state &= ~MINIX_ERROR_FS;
352
353 if (lseek(DEV, 0, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000354 die("seek to boot block failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000355 if (512 != write(DEV, boot_block_buffer, 512))
Eric Andersene674eb71999-10-19 20:52:57 +0000356 die("unable to clear boot sector");
Eric Andersene494fdd1999-10-19 20:23:03 +0000357 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000358 die("seek failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000359 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000360 die("unable to write super-block");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000362 die("unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000364 die("unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000366 die("unable to write inodes");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367
Eric Andersene494fdd1999-10-19 20:23:03 +0000368}
369
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000371{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000373 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000374 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000375 die("write failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000376}
377
Eric Andersene674eb71999-10-19 20:52:57 +0000378static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000379{
380 int blk;
381
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Eric Andersene674eb71999-10-19 20:52:57 +0000383 die("too many bad blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000384 if (used_good_blocks)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 blk = good_blocks_table[used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000386 else
387 blk = FIRSTZONE;
388 while (blk < ZONES && zone_in_use(blk))
389 blk++;
390 if (blk >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000391 die("not enough good blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000392 good_blocks_table[used_good_blocks] = blk;
393 used_good_blocks++;
394 return blk;
395}
396
Eric Andersene674eb71999-10-19 20:52:57 +0000397static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000398{
399 int blk;
400
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401 for (blk = 0; blk < used_good_blocks; blk++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000402 mark_zone(good_blocks_table[blk]);
403}
404
405inline int next(int zone)
406{
407 if (!zone)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 zone = FIRSTZONE - 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000409 while (++zone < ZONES)
410 if (zone_in_use(zone))
411 return zone;
412 return 0;
413}
414
Eric Andersene674eb71999-10-19 20:52:57 +0000415static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000416{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
418 int i, j, zone;
419 int ind = 0, dind = 0;
420 unsigned short ind_block[BLOCK_SIZE >> 1];
421 unsigned short dind_block[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000422
423#define NEXT_BAD (zone = next(zone))
424
425 if (!badblocks)
426 return;
427 mark_inode(MINIX_BAD_INO);
428 inode->i_nlinks = 1;
429 inode->i_time = time(NULL);
430 inode->i_mode = S_IFREG + 0000;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 inode->i_size = badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000432 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000434 inode->i_zone[i] = zone;
435 if (!NEXT_BAD)
436 goto end_bad;
437 }
438 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439 memset(ind_block, 0, BLOCK_SIZE);
440 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000441 ind_block[i] = zone;
442 if (!NEXT_BAD)
443 goto end_bad;
444 }
445 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 memset(dind_block, 0, BLOCK_SIZE);
447 for (i = 0; i < 512; i++) {
448 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000449 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 memset(ind_block, 0, BLOCK_SIZE);
451 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000452 ind_block[j] = zone;
453 if (!NEXT_BAD)
454 goto end_bad;
455 }
456 }
Eric Andersene674eb71999-10-19 20:52:57 +0000457 die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000459 if (ind)
460 write_block(ind, (char *) ind_block);
461 if (dind)
462 write_block(dind, (char *) dind_block);
463}
464
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000465#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000467{
468 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
469 int i, j, zone;
470 int ind = 0, dind = 0;
471 unsigned long ind_block[BLOCK_SIZE >> 2];
472 unsigned long dind_block[BLOCK_SIZE >> 2];
473
474 if (!badblocks)
475 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000477 inode->i_nlinks = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
Eric Andersene494fdd1999-10-19 20:23:03 +0000479 inode->i_mode = S_IFREG + 0000;
480 inode->i_size = badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000482 for (i = 0; i < 7; i++) {
483 inode->i_zone[i] = zone;
484 if (!NEXT_BAD)
485 goto end_bad;
486 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 inode->i_zone[7] = ind = get_free_block();
488 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000489 for (i = 0; i < 256; i++) {
490 ind_block[i] = zone;
491 if (!NEXT_BAD)
492 goto end_bad;
493 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 inode->i_zone[8] = dind = get_free_block();
495 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000496 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 write_block(ind, (char *) ind_block);
498 dind_block[i] = ind = get_free_block();
499 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000500 for (j = 0; j < 256; j++) {
501 ind_block[j] = zone;
502 if (!NEXT_BAD)
503 goto end_bad;
504 }
505 }
506 /* Could make triple indirect block here */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 die("too many bad blocks");
508 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000509 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000511 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 write_block(dind, (char *) dind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000513}
514#endif
515
Eric Andersene674eb71999-10-19 20:52:57 +0000516static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000517{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000518 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000519
520 mark_inode(MINIX_ROOT_INO);
521 inode->i_zone[0] = get_free_block();
522 inode->i_nlinks = 2;
523 inode->i_time = time(NULL);
524 if (badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000525 inode->i_size = 3 * dirsize;
526 else {
527 root_block[2 * dirsize] = '\0';
528 root_block[2 * dirsize + 1] = '\0';
529 inode->i_size = 2 * dirsize;
530 }
531 inode->i_mode = S_IFDIR + 0755;
532 inode->i_uid = getuid();
533 if (inode->i_uid)
534 inode->i_gid = getgid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 write_block(inode->i_zone[0], root_block);
536}
537
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000538#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539static void make_root_inode2(void)
540{
541 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
542
543 mark_inode(MINIX_ROOT_INO);
544 inode->i_zone[0] = get_free_block();
545 inode->i_nlinks = 2;
546 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
547 if (badblocks)
548 inode->i_size = 3 * dirsize;
549 else {
550 root_block[2 * dirsize] = '\0';
551 root_block[2 * dirsize + 1] = '\0';
552 inode->i_size = 2 * dirsize;
553 }
554 inode->i_mode = S_IFDIR + 0755;
555 inode->i_uid = getuid();
556 if (inode->i_uid)
557 inode->i_gid = getgid();
558 write_block(inode->i_zone[0], root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000559}
560#endif
561
Eric Andersene674eb71999-10-19 20:52:57 +0000562static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000563{
564 int i;
565 unsigned long inodes;
566
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 memset(super_block_buffer, 0, BLOCK_SIZE);
568 memset(boot_block_buffer, 0, 512);
Eric Andersene494fdd1999-10-19 20:23:03 +0000569 MAGIC = magic;
570 ZONESIZE = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
Eric Andersene494fdd1999-10-19 20:23:03 +0000572 ZONES = BLOCKS;
573/* some magic nrs: 1 inode / 3 blocks */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574 if (req_nr_inodes == 0)
575 inodes = BLOCKS / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000576 else
577 inodes = req_nr_inodes;
578 /* Round up inode count to fill block size */
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000579#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000580 if (version2)
581 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 ~(MINIX2_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000583 else
584#endif
585 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 ~(MINIX_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000587 if (inodes > 65535)
588 inodes = 65535;
589 INODES = inodes;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000591 ZMAPS = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000592 i = 0;
593 while (ZMAPS !=
594 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
595 BITS_PER_BLOCK) && i < 1000) {
596 ZMAPS =
597 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
598 BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000599 i++;
600 }
601 /* Real bad hack but overwise mkfs.minix can be thrown
602 * in infinite loop...
603 * try:
604 * dd if=/dev/zero of=test.fs count=10 bs=1024
605 * /sbin/mkfs.minix -i 200 test.fs
606 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 if (i >= 999) {
608 die("unable to allocate buffers for maps");
Eric Andersene494fdd1999-10-19 20:23:03 +0000609 }
610 FIRSTZONE = NORM_FIRSTZONE;
Erik Andersen0d068a22000-03-21 22:32:57 +0000611 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
612 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
614 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
615 for (i = FIRSTZONE; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000616 unmark_zone(i);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 for (i = MINIX_ROOT_INO; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000618 unmark_inode(i);
Erik Andersen0d068a22000-03-21 22:32:57 +0000619 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000620 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
621 printf("%ld inodes\n", INODES);
622 printf("%ld blocks\n", ZONES);
623 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
624 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
625 printf("Maxsize=%ld\n\n", MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000626}
627
628/*
629 * Perform a test of a block; return the number of
630 * blocks readable/writeable.
631 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632long do_check(char *buffer, int try, unsigned int current_block)
Eric Andersene494fdd1999-10-19 20:23:03 +0000633{
634 long got;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635
Eric Andersene494fdd1999-10-19 20:23:03 +0000636 /* Seek to the correct loc. */
637 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
Erik Andersene49d5ec2000-02-08 19:58:47 +0000638 current_block * BLOCK_SIZE) {
639 die("seek failed during testing of blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000640 }
641
642
643 /* Try the read */
644 got = read(DEV, buffer, try * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000645 if (got < 0)
646 got = 0;
647 if (got & (BLOCK_SIZE - 1)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000648 printf("Weird values in do_check: probably bugs\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000649 }
650 got /= BLOCK_SIZE;
651 return got;
652}
653
654static unsigned int currently_testing = 0;
655
Eric Andersene674eb71999-10-19 20:52:57 +0000656static void alarm_intr(int alnum)
Eric Andersene494fdd1999-10-19 20:23:03 +0000657{
658 if (currently_testing >= ZONES)
659 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000661 alarm(5);
662 if (!currently_testing)
663 return;
664 printf("%d ...", currently_testing);
665 fflush(stdout);
666}
667
Eric Andersene674eb71999-10-19 20:52:57 +0000668static void check_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000669{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670 int try, got;
Eric Andersene494fdd1999-10-19 20:23:03 +0000671 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
672
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673 currently_testing = 0;
674 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000675 alarm(5);
676 while (currently_testing < ZONES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
678 currently_testing * BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +0000679 die("seek failed in check_blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000680 try = TEST_BUFFER_BLOCKS;
681 if (currently_testing + try > ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 try = ZONES - currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000683 got = do_check(buffer, try, currently_testing);
684 currently_testing += got;
685 if (got == try)
686 continue;
687 if (currently_testing < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000688 die("bad blocks before data-area: cannot make fs");
Eric Andersene494fdd1999-10-19 20:23:03 +0000689 mark_zone(currently_testing);
690 badblocks++;
691 currently_testing++;
692 }
693 if (badblocks > 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000694 printf("%d bad blocks\n", badblocks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000695 else if (badblocks == 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000696 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000697}
698
Eric Andersene674eb71999-10-19 20:52:57 +0000699static void get_list_blocks(filename)
Eric Andersene494fdd1999-10-19 20:23:03 +0000700char *filename;
701
702{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 FILE *listfile;
704 unsigned long blockno;
Eric Andersene494fdd1999-10-19 20:23:03 +0000705
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706 listfile = fopen(filename, "r");
707 if (listfile == (FILE *) NULL) {
708 die("can't open file of bad blocks");
709 }
710 while (!feof(listfile)) {
711 fscanf(listfile, "%ld\n", &blockno);
712 mark_zone(blockno);
713 badblocks++;
714 }
715 if (badblocks > 1)
716 printf("%d bad blocks\n", badblocks);
717 else if (badblocks == 1)
718 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000719}
720
Erik Andersene49d5ec2000-02-08 19:58:47 +0000721extern int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000722{
Eric Andersen32322032000-06-06 22:12:35 +0000723 int i=1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000724 char *tmp;
725 struct stat statbuf;
726 char *listfile = NULL;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000727 int stopIt=FALSE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000728
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
730 die("bad inode size");
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000731#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
733 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000734#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000735
736 /* Parse options */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000737 argv++;
738 while (--argc >= 0 && *argv && **argv) {
739 if (**argv == '-') {
740 stopIt=FALSE;
741 while (i > 0 && *++(*argv) && stopIt==FALSE) {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000742 switch (**argv) {
743 case 'c':
744 check = 1;
745 break;
746 case 'i':
Eric Andersen32322032000-06-06 22:12:35 +0000747 {
748 char *cp=NULL;
749 if (*(*argv+1) != 0) {
750 cp = ++(*argv);
751 } else {
752 if (--argc == 0) {
753 goto goodbye;
754 }
755 cp = *(++argv);
756 }
757 req_nr_inodes = strtoul(cp, &tmp, 0);
758 if (*tmp)
759 show_usage();
Eric Andersen32322032000-06-06 22:12:35 +0000760 stopIt=TRUE;
761 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000762 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000763 case 'l':
764 if (--argc == 0) {
765 goto goodbye;
766 }
767 listfile = *(++argv);
768 break;
769 case 'n':
770 {
771 char *cp=NULL;
772
Erik Andersen330fd2b2000-05-19 05:35:19 +0000773 if (*(*argv+1) != 0) {
774 cp = ++(*argv);
775 } else {
Eric Andersen32322032000-06-06 22:12:35 +0000776 if (--argc == 0) {
777 goto goodbye;
778 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000779 cp = *(++argv);
780 }
781 i = strtoul(cp, &tmp, 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000782 if (*tmp)
783 show_usage();
784 if (i == 14)
785 magic = MINIX_SUPER_MAGIC;
786 else if (i == 30)
787 magic = MINIX_SUPER_MAGIC2;
788 else
789 show_usage();
790 namelen = i;
791 dirsize = i + 2;
792 stopIt=TRUE;
793 break;
794 }
795 case 'v':
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000796#ifdef BB_FEATURE_MINIX2
Erik Andersen330fd2b2000-05-19 05:35:19 +0000797 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000798#else
Matt Kraaid537a952000-07-14 01:51:25 +0000799 errorMsg("%s: not compiled with minix v2 support\n",
800 device_name);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000801 exit(-1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000802#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000803 break;
804 case '-':
805 case 'h':
806 default:
807goodbye:
808 show_usage();
809 }
810 }
811 } else {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000812 if (device_name == NULL)
813 device_name = *argv;
814 else if (BLOCKS == 0)
815 BLOCKS = strtol(*argv, &tmp, 0);
816 else {
817 goto goodbye;
818 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000819 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 argv++;
821 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000822
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 if (device_name && !BLOCKS)
824 BLOCKS = get_size(device_name) / 1024;
825 if (!device_name || BLOCKS < 10) {
826 show_usage();
827 }
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000828#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000829 if (version2) {
830 if (namelen == 14)
831 magic = MINIX2_SUPER_MAGIC;
832 else
833 magic = MINIX2_SUPER_MAGIC2;
834 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000835#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836 if (BLOCKS > 65535)
837 BLOCKS = 65535;
838 check_mount(); /* is it already mounted? */
839 tmp = root_block;
840 *(short *) tmp = 1;
841 strcpy(tmp + 2, ".");
842 tmp += dirsize;
843 *(short *) tmp = 1;
844 strcpy(tmp + 2, "..");
845 tmp += dirsize;
846 *(short *) tmp = 2;
847 strcpy(tmp + 2, ".badblocks");
848 DEV = open(device_name, O_RDWR);
849 if (DEV < 0)
850 die("unable to open %s");
851 if (fstat(DEV, &statbuf) < 0)
852 die("unable to stat %s");
853 if (!S_ISBLK(statbuf.st_mode))
854 check = 0;
855 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
856 die("will not try to make filesystem on '%s'");
857 setup_tables();
858 if (check)
859 check_blocks();
860 else if (listfile)
861 get_list_blocks(listfile);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000862#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 if (version2) {
864 make_root_inode2();
865 make_bad_inode2();
866 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000867#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868 {
869 make_root_inode();
870 make_bad_inode();
871 }
872 mark_good_blocks();
873 write_tables();
Eric Andersenbd4a75f2000-06-19 20:03:48 +0000874 return( 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000875
Eric Andersene494fdd1999-10-19 20:23:03 +0000876}