blob: 9b40faac666e4e18a9d80eb48a251ccb4fcd039e [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 Andersen3570a342000-09-25 21:45:58 +000065#include "busybox.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
Eric Andersenbd018b12001-01-31 23:00:46 +0000258static void show_usage() __attribute__ ((noreturn));
259
260static void show_usage()
Eric Andersene494fdd1999-10-19 20:23:03 +0000261{
Matt Kraai3bd8bd82000-07-14 23:28:47 +0000262 usage(mkfs_minix_usage);
Eric Andersene494fdd1999-10-19 20:23:03 +0000263}
264
265/*
266 * Check to make certain that our new filesystem won't be created on
267 * an already mounted partition. Code adapted from mke2fs, Copyright
268 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
269 */
270static void check_mount(void)
271{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 FILE *f;
273 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000274
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000276 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 while ((mnt = getmntent(f)) != NULL)
278 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000279 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000281 if (!mnt)
282 return;
283
Matt Kraaidd19c692001-01-31 19:00:21 +0000284 error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000285}
286
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000288{
289 char ch;
290
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 if (lseek(fd, offset, 0) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000292 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000294 return 0;
295 return 1;
296}
297
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298static int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000299{
300 int high, low;
301
302 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000304 low = high;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000306 const int mid = (low + high) / 2;
307
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000309 low = mid;
310 else
311 high = mid;
312 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000314 return (low + 1);
315}
316
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317static int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000318{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 int fd;
320 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000321
Matt Kraaia9819b22000-12-22 01:48:07 +0000322 if ((fd = open(file, O_RDWR)) < 0)
323 perror_msg_and_die("%s", file);
Eric Andersene494fdd1999-10-19 20:23:03 +0000324 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
325 close(fd);
326 return (size * 512);
327 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328
Eric Andersene494fdd1999-10-19 20:23:03 +0000329 size = count_blocks(fd);
330 close(fd);
331 return size;
332}
333
Eric Andersene674eb71999-10-19 20:52:57 +0000334static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000335{
336 /* Mark the super block valid. */
337 Super.s_state |= MINIX_VALID_FS;
338 Super.s_state &= ~MINIX_ERROR_FS;
339
340 if (lseek(DEV, 0, SEEK_SET))
Matt Kraaidd19c692001-01-31 19:00:21 +0000341 error_msg_and_die("seek to boot block failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000342 if (512 != write(DEV, boot_block_buffer, 512))
Matt Kraaidd19c692001-01-31 19:00:21 +0000343 error_msg_and_die("unable to clear boot sector");
Eric Andersene494fdd1999-10-19 20:23:03 +0000344 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
Matt Kraaidd19c692001-01-31 19:00:21 +0000345 error_msg_and_die("seek failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000346 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
Matt Kraaidd19c692001-01-31 19:00:21 +0000347 error_msg_and_die("unable to write super-block");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000348 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
Matt Kraaidd19c692001-01-31 19:00:21 +0000349 error_msg_and_die("unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
Matt Kraaidd19c692001-01-31 19:00:21 +0000351 error_msg_and_die("unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
Matt Kraaidd19c692001-01-31 19:00:21 +0000353 error_msg_and_die("unable to write inodes");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354
Eric Andersene494fdd1999-10-19 20:23:03 +0000355}
356
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000358{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
Matt Kraaidd19c692001-01-31 19:00:21 +0000360 error_msg_and_die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
Matt Kraaidd19c692001-01-31 19:00:21 +0000362 error_msg_and_die("write failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000363}
364
Eric Andersene674eb71999-10-19 20:52:57 +0000365static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000366{
367 int blk;
368
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Matt Kraaidd19c692001-01-31 19:00:21 +0000370 error_msg_and_die("too many bad blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000371 if (used_good_blocks)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 blk = good_blocks_table[used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000373 else
374 blk = FIRSTZONE;
375 while (blk < ZONES && zone_in_use(blk))
376 blk++;
377 if (blk >= ZONES)
Matt Kraaidd19c692001-01-31 19:00:21 +0000378 error_msg_and_die("not enough good blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000379 good_blocks_table[used_good_blocks] = blk;
380 used_good_blocks++;
381 return blk;
382}
383
Eric Andersene674eb71999-10-19 20:52:57 +0000384static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000385{
386 int blk;
387
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 for (blk = 0; blk < used_good_blocks; blk++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000389 mark_zone(good_blocks_table[blk]);
390}
391
392inline int next(int zone)
393{
394 if (!zone)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000395 zone = FIRSTZONE - 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000396 while (++zone < ZONES)
397 if (zone_in_use(zone))
398 return zone;
399 return 0;
400}
401
Eric Andersene674eb71999-10-19 20:52:57 +0000402static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000403{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
405 int i, j, zone;
406 int ind = 0, dind = 0;
407 unsigned short ind_block[BLOCK_SIZE >> 1];
408 unsigned short dind_block[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000409
410#define NEXT_BAD (zone = next(zone))
411
412 if (!badblocks)
413 return;
414 mark_inode(MINIX_BAD_INO);
415 inode->i_nlinks = 1;
416 inode->i_time = time(NULL);
417 inode->i_mode = S_IFREG + 0000;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000418 inode->i_size = badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000419 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000421 inode->i_zone[i] = zone;
422 if (!NEXT_BAD)
423 goto end_bad;
424 }
425 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000426 memset(ind_block, 0, BLOCK_SIZE);
427 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000428 ind_block[i] = zone;
429 if (!NEXT_BAD)
430 goto end_bad;
431 }
432 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 memset(dind_block, 0, BLOCK_SIZE);
434 for (i = 0; i < 512; i++) {
435 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000436 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437 memset(ind_block, 0, BLOCK_SIZE);
438 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000439 ind_block[j] = zone;
440 if (!NEXT_BAD)
441 goto end_bad;
442 }
443 }
Matt Kraaidd19c692001-01-31 19:00:21 +0000444 error_msg_and_die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000446 if (ind)
447 write_block(ind, (char *) ind_block);
448 if (dind)
449 write_block(dind, (char *) dind_block);
450}
451
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000452#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000454{
455 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
456 int i, j, zone;
457 int ind = 0, dind = 0;
458 unsigned long ind_block[BLOCK_SIZE >> 2];
459 unsigned long dind_block[BLOCK_SIZE >> 2];
460
461 if (!badblocks)
462 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000464 inode->i_nlinks = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
Eric Andersene494fdd1999-10-19 20:23:03 +0000466 inode->i_mode = S_IFREG + 0000;
467 inode->i_size = badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000469 for (i = 0; i < 7; i++) {
470 inode->i_zone[i] = zone;
471 if (!NEXT_BAD)
472 goto end_bad;
473 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 inode->i_zone[7] = ind = get_free_block();
475 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000476 for (i = 0; i < 256; i++) {
477 ind_block[i] = zone;
478 if (!NEXT_BAD)
479 goto end_bad;
480 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 inode->i_zone[8] = dind = get_free_block();
482 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000483 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 write_block(ind, (char *) ind_block);
485 dind_block[i] = ind = get_free_block();
486 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000487 for (j = 0; j < 256; j++) {
488 ind_block[j] = zone;
489 if (!NEXT_BAD)
490 goto end_bad;
491 }
492 }
493 /* Could make triple indirect block here */
Matt Kraaidd19c692001-01-31 19:00:21 +0000494 error_msg_and_die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000496 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000498 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499 write_block(dind, (char *) dind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000500}
501#endif
502
Eric Andersene674eb71999-10-19 20:52:57 +0000503static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000504{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000506
507 mark_inode(MINIX_ROOT_INO);
508 inode->i_zone[0] = get_free_block();
509 inode->i_nlinks = 2;
510 inode->i_time = time(NULL);
511 if (badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000512 inode->i_size = 3 * dirsize;
513 else {
514 root_block[2 * dirsize] = '\0';
515 root_block[2 * dirsize + 1] = '\0';
516 inode->i_size = 2 * dirsize;
517 }
518 inode->i_mode = S_IFDIR + 0755;
519 inode->i_uid = getuid();
520 if (inode->i_uid)
521 inode->i_gid = getgid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 write_block(inode->i_zone[0], root_block);
523}
524
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000525#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526static void make_root_inode2(void)
527{
528 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
529
530 mark_inode(MINIX_ROOT_INO);
531 inode->i_zone[0] = get_free_block();
532 inode->i_nlinks = 2;
533 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
534 if (badblocks)
535 inode->i_size = 3 * dirsize;
536 else {
537 root_block[2 * dirsize] = '\0';
538 root_block[2 * dirsize + 1] = '\0';
539 inode->i_size = 2 * dirsize;
540 }
541 inode->i_mode = S_IFDIR + 0755;
542 inode->i_uid = getuid();
543 if (inode->i_uid)
544 inode->i_gid = getgid();
545 write_block(inode->i_zone[0], root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000546}
547#endif
548
Eric Andersene674eb71999-10-19 20:52:57 +0000549static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000550{
551 int i;
552 unsigned long inodes;
553
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 memset(super_block_buffer, 0, BLOCK_SIZE);
555 memset(boot_block_buffer, 0, 512);
Eric Andersene494fdd1999-10-19 20:23:03 +0000556 MAGIC = magic;
557 ZONESIZE = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
Eric Andersene494fdd1999-10-19 20:23:03 +0000559 ZONES = BLOCKS;
560/* some magic nrs: 1 inode / 3 blocks */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 if (req_nr_inodes == 0)
562 inodes = BLOCKS / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000563 else
564 inodes = req_nr_inodes;
565 /* Round up inode count to fill block size */
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000566#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000567 if (version2)
568 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 ~(MINIX2_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000570 else
571#endif
572 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 ~(MINIX_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000574 if (inodes > 65535)
575 inodes = 65535;
576 INODES = inodes;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000578 ZMAPS = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 i = 0;
580 while (ZMAPS !=
581 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
582 BITS_PER_BLOCK) && i < 1000) {
583 ZMAPS =
584 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
585 BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000586 i++;
587 }
588 /* Real bad hack but overwise mkfs.minix can be thrown
589 * in infinite loop...
590 * try:
591 * dd if=/dev/zero of=test.fs count=10 bs=1024
592 * /sbin/mkfs.minix -i 200 test.fs
593 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 if (i >= 999) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000595 error_msg_and_die("unable to allocate buffers for maps");
Eric Andersene494fdd1999-10-19 20:23:03 +0000596 }
597 FIRSTZONE = NORM_FIRSTZONE;
Erik Andersen0d068a22000-03-21 22:32:57 +0000598 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
599 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
601 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
602 for (i = FIRSTZONE; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000603 unmark_zone(i);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604 for (i = MINIX_ROOT_INO; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000605 unmark_inode(i);
Erik Andersen0d068a22000-03-21 22:32:57 +0000606 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
608 printf("%ld inodes\n", INODES);
609 printf("%ld blocks\n", ZONES);
610 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
611 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
612 printf("Maxsize=%ld\n\n", MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000613}
614
615/*
616 * Perform a test of a block; return the number of
617 * blocks readable/writeable.
618 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000619long do_check(char *buffer, int try, unsigned int current_block)
Eric Andersene494fdd1999-10-19 20:23:03 +0000620{
621 long got;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000622
Eric Andersene494fdd1999-10-19 20:23:03 +0000623 /* Seek to the correct loc. */
624 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 current_block * BLOCK_SIZE) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000626 error_msg_and_die("seek failed during testing of blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000627 }
628
629
630 /* Try the read */
631 got = read(DEV, buffer, try * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632 if (got < 0)
633 got = 0;
634 if (got & (BLOCK_SIZE - 1)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000635 printf("Weird values in do_check: probably bugs\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000636 }
637 got /= BLOCK_SIZE;
638 return got;
639}
640
641static unsigned int currently_testing = 0;
642
Eric Andersene674eb71999-10-19 20:52:57 +0000643static void alarm_intr(int alnum)
Eric Andersene494fdd1999-10-19 20:23:03 +0000644{
645 if (currently_testing >= ZONES)
646 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000648 alarm(5);
649 if (!currently_testing)
650 return;
651 printf("%d ...", currently_testing);
652 fflush(stdout);
653}
654
Eric Andersene674eb71999-10-19 20:52:57 +0000655static void check_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000656{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 int try, got;
Eric Andersene494fdd1999-10-19 20:23:03 +0000658 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
659
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660 currently_testing = 0;
661 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000662 alarm(5);
663 while (currently_testing < ZONES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000664 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
665 currently_testing * BLOCK_SIZE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000666 error_msg_and_die("seek failed in check_blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000667 try = TEST_BUFFER_BLOCKS;
668 if (currently_testing + try > ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 try = ZONES - currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000670 got = do_check(buffer, try, currently_testing);
671 currently_testing += got;
672 if (got == try)
673 continue;
674 if (currently_testing < FIRSTZONE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000675 error_msg_and_die("bad blocks before data-area: cannot make fs");
Eric Andersene494fdd1999-10-19 20:23:03 +0000676 mark_zone(currently_testing);
677 badblocks++;
678 currently_testing++;
679 }
680 if (badblocks > 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000681 printf("%d bad blocks\n", badblocks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000682 else if (badblocks == 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000683 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000684}
685
Eric Andersene674eb71999-10-19 20:52:57 +0000686static void get_list_blocks(filename)
Eric Andersene494fdd1999-10-19 20:23:03 +0000687char *filename;
688
689{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 FILE *listfile;
691 unsigned long blockno;
Eric Andersene494fdd1999-10-19 20:23:03 +0000692
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 listfile = fopen(filename, "r");
694 if (listfile == (FILE *) NULL) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000695 error_msg_and_die("can't open file of bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000696 }
697 while (!feof(listfile)) {
698 fscanf(listfile, "%ld\n", &blockno);
699 mark_zone(blockno);
700 badblocks++;
701 }
702 if (badblocks > 1)
703 printf("%d bad blocks\n", badblocks);
704 else if (badblocks == 1)
705 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000706}
707
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708extern int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000709{
Eric Andersen32322032000-06-06 22:12:35 +0000710 int i=1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 char *tmp;
712 struct stat statbuf;
713 char *listfile = NULL;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000714 int stopIt=FALSE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000715
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000717 error_msg_and_die("bad inode size");
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000718#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000720 error_msg_and_die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000721#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000722
723 /* Parse options */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000724 argv++;
725 while (--argc >= 0 && *argv && **argv) {
726 if (**argv == '-') {
727 stopIt=FALSE;
728 while (i > 0 && *++(*argv) && stopIt==FALSE) {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000729 switch (**argv) {
730 case 'c':
731 check = 1;
732 break;
733 case 'i':
Eric Andersen32322032000-06-06 22:12:35 +0000734 {
735 char *cp=NULL;
736 if (*(*argv+1) != 0) {
737 cp = ++(*argv);
738 } else {
739 if (--argc == 0) {
740 goto goodbye;
741 }
742 cp = *(++argv);
743 }
744 req_nr_inodes = strtoul(cp, &tmp, 0);
745 if (*tmp)
746 show_usage();
Eric Andersen32322032000-06-06 22:12:35 +0000747 stopIt=TRUE;
748 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000749 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000750 case 'l':
751 if (--argc == 0) {
752 goto goodbye;
753 }
754 listfile = *(++argv);
755 break;
756 case 'n':
757 {
758 char *cp=NULL;
759
Erik Andersen330fd2b2000-05-19 05:35:19 +0000760 if (*(*argv+1) != 0) {
761 cp = ++(*argv);
762 } else {
Eric Andersen32322032000-06-06 22:12:35 +0000763 if (--argc == 0) {
764 goto goodbye;
765 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000766 cp = *(++argv);
767 }
768 i = strtoul(cp, &tmp, 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000769 if (*tmp)
770 show_usage();
771 if (i == 14)
772 magic = MINIX_SUPER_MAGIC;
773 else if (i == 30)
774 magic = MINIX_SUPER_MAGIC2;
775 else
776 show_usage();
777 namelen = i;
778 dirsize = i + 2;
779 stopIt=TRUE;
780 break;
781 }
782 case 'v':
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000783#ifdef BB_FEATURE_MINIX2
Erik Andersen330fd2b2000-05-19 05:35:19 +0000784 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000785#else
Matt Kraaidd19c692001-01-31 19:00:21 +0000786 error_msg("%s: not compiled with minix v2 support",
Matt Kraaid537a952000-07-14 01:51:25 +0000787 device_name);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000788 exit(-1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000789#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000790 break;
791 case '-':
792 case 'h':
793 default:
794goodbye:
795 show_usage();
796 }
797 }
798 } else {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000799 if (device_name == NULL)
800 device_name = *argv;
801 else if (BLOCKS == 0)
802 BLOCKS = strtol(*argv, &tmp, 0);
803 else {
804 goto goodbye;
805 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000806 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 argv++;
808 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000809
Erik Andersene49d5ec2000-02-08 19:58:47 +0000810 if (device_name && !BLOCKS)
811 BLOCKS = get_size(device_name) / 1024;
812 if (!device_name || BLOCKS < 10) {
813 show_usage();
814 }
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000815#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000816 if (version2) {
817 if (namelen == 14)
818 magic = MINIX2_SUPER_MAGIC;
819 else
820 magic = MINIX2_SUPER_MAGIC2;
821 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000822#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 if (BLOCKS > 65535)
824 BLOCKS = 65535;
825 check_mount(); /* is it already mounted? */
826 tmp = root_block;
827 *(short *) tmp = 1;
828 strcpy(tmp + 2, ".");
829 tmp += dirsize;
830 *(short *) tmp = 1;
831 strcpy(tmp + 2, "..");
832 tmp += dirsize;
833 *(short *) tmp = 2;
834 strcpy(tmp + 2, ".badblocks");
835 DEV = open(device_name, O_RDWR);
836 if (DEV < 0)
Matt Kraaidd19c692001-01-31 19:00:21 +0000837 error_msg_and_die("unable to open %s", device_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000838 if (fstat(DEV, &statbuf) < 0)
Matt Kraaidd19c692001-01-31 19:00:21 +0000839 error_msg_and_die("unable to stat %s", device_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000840 if (!S_ISBLK(statbuf.st_mode))
841 check = 0;
842 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
Matt Kraaidd19c692001-01-31 19:00:21 +0000843 error_msg_and_die("will not try to make filesystem on '%s'", device_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000844 setup_tables();
845 if (check)
846 check_blocks();
847 else if (listfile)
848 get_list_blocks(listfile);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000849#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000850 if (version2) {
851 make_root_inode2();
852 make_bad_inode2();
853 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000854#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 {
856 make_root_inode();
857 make_bad_inode();
858 }
859 mark_good_blocks();
860 write_tables();
Eric Andersenbd4a75f2000-06-19 20:03:48 +0000861 return( 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000862
Eric Andersene494fdd1999-10-19 20:23:03 +0000863}