blob: 3c72e5419a53370672253e04f7e89db28e2d804e [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 *
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 * (C) 1991 Linus Torvalds.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersene494fdd1999-10-19 20:23:03 +00008 */
9
10/*
11 * DD.MM.YY
12 *
13 * 24.11.91 - Time began. Used the fsck sources to get started.
14 *
15 * 25.11.91 - Corrected some bugs. Added support for ".badblocks"
16 * The algorithm for ".badblocks" is a bit weird, but
17 * it should work. Oh, well.
18 *
19 * 25.01.92 - Added the -l option for getting the list of bad blocks
20 * out of a named file. (Dave Rivers, rivers@ponds.uucp)
21 *
22 * 28.02.92 - Added %-information when using -c.
23 *
24 * 28.02.93 - Added support for other namelengths than the original
25 * 14 characters so that I can test the new kernel routines..
26 *
27 * 09.10.93 - Make exit status conform to that required by fsutil
28 * (Rik Faith, faith@cs.unc.edu)
29 *
30 * 31.10.93 - Added inode request feature, for backup floppies: use
31 * 32 inodes, for a news partition use more.
32 * (Scott Heavner, sdh@po.cwru.edu)
33 *
34 * 03.01.94 - Added support for file system valid flag.
35 * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
36 *
Denis Vlasenko66b86c62006-11-29 22:33:47 +000037 * 30.10.94 - added support for v2 filesystem
38 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000039 *
Eric Andersene494fdd1999-10-19 20:23:03 +000040 * 09.11.94 - Added test to prevent overwrite of mounted fs adapted
41 * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
42 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
43 *
44 * 03.20.95 - Clear first 512 bytes of filesystem to make certain that
45 * the filesystem is not misidentified as a MS-DOS FAT filesystem.
46 * (Daniel Quinlan, quinlan@yggdrasil.com)
47 *
48 * 02.07.96 - Added small patch from Russell King to make the program a
49 * good deal more portable (janl@math.uio.no)
50 *
51 * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
52 *
Eric Andersenaff114c2004-04-14 17:51:38 +000053 * -c for readability checking (SLOW!)
Eric Andersene494fdd1999-10-19 20:23:03 +000054 * -l for getting a list of bad blocks from a file.
55 * -n for namelength (currently the kernel only uses 14 or 30)
56 * -i for number of inodes
57 * -v for v2 filesystem
58 *
59 * The device may be a block device or a image of one, but this isn't
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060 * enforced (but it's not much fun on a character device :-).
Erik Andersen330fd2b2000-05-19 05:35:19 +000061 *
62 * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
63 * removed getopt based parser and added a hand rolled one.
Eric Andersene494fdd1999-10-19 20:23:03 +000064 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010065//config:config MKFS_MINIX
Denys Vlasenkodf1ff102018-12-28 13:37:44 +010066//config: bool "mkfs.minix (10 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010067//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010068//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020069//config: The minix filesystem is a nice, small, compact, read-write filesystem
70//config: with little overhead. If you wish to be able to create minix
71//config: filesystems this utility will do the job for you.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010072//config:
73//config:config FEATURE_MINIX2
74//config: bool "Support Minix fs v2 (fsck_minix/mkfs_minix)"
75//config: default y
76//config: depends on FSCK_MINIX || MKFS_MINIX
77//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020078//config: If you wish to be able to create version 2 minix filesystems, enable
79//config: this. If you enabled 'mkfs_minix' then you almost certainly want to
80//config: be using the version 2 filesystem support.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010081
Denys Vlasenko205d48e2017-01-29 14:57:33 +010082// APPLET_ODDNAME:name main location suid_type help
Denys Vlasenkodd898c92016-11-23 11:46:32 +010083//applet:IF_MKFS_MINIX(APPLET_ODDNAME(mkfs.minix, mkfs_minix, BB_DIR_SBIN, BB_SUID_DROP, mkfs_minix))
84
85//kbuild:lib-$(CONFIG_MKFS_MINIX) += mkfs_minix.o
Eric Andersene494fdd1999-10-19 20:23:03 +000086
Pere Orga5bc8c002011-04-11 03:29:49 +020087//usage:#define mkfs_minix_trivial_usage
88//usage: "[-c | -l FILE] [-nXX] [-iXX] BLOCKDEV [KBYTES]"
89//usage:#define mkfs_minix_full_usage "\n\n"
90//usage: "Make a MINIX filesystem\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020091//usage: "\n -c Check device for bad blocks"
92//usage: "\n -n [14|30] Maximum length of filenames"
93//usage: "\n -i INODES Number of inodes for the filesystem"
94//usage: "\n -l FILE Read bad blocks list from FILE"
95//usage: "\n -v Make version 2 filesystem"
96
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000097#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000098#include <mntent.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000099
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000100#include "minix.h"
101
Denis Vlasenko7ab36992007-12-02 08:55:34 +0000102/* Store the very same times/uids/gids for image consistency */
103#if 1
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000104# define CUR_TIME 0
105# define GETUID 0
106# define GETGID 0
107#else
Denis Vlasenko7ab36992007-12-02 08:55:34 +0000108/* Was using this. Is it useful? NB: this will break testsuite */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000109# define CUR_TIME time(NULL)
110# define GETUID getuid()
111# define GETGID getgid()
112#endif
Eric Andersen85c55202000-07-10 22:46:55 +0000113
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000114enum {
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000115 MAX_GOOD_BLOCKS = 512,
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000116 TEST_BUFFER_BLOCKS = 16,
117};
Eric Andersen85c55202000-07-10 22:46:55 +0000118
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000119#if !ENABLE_FEATURE_MINIX2
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000120enum { version2 = 0 };
121#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000122
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000123enum { dev_fd = 3 };
Eric Andersene494fdd1999-10-19 20:23:03 +0000124
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000125struct globals {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000126#if ENABLE_FEATURE_MINIX2
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000127 smallint version2;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000128#define version2 G.version2
129#endif
130 char *device_name;
131 uint32_t total_blocks;
132 int badblocks;
133 int namelen;
134 int dirsize;
135 int magic;
136 char *inode_buffer;
137 char *inode_map;
138 char *zone_map;
139 int used_good_blocks;
140 unsigned long req_nr_inodes;
141 unsigned currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000142
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000143 char root_block[BLOCK_SIZE];
Denys Vlasenkodce39c92017-08-16 11:53:48 +0200144 union {
145 char superblock_buffer[BLOCK_SIZE];
146 struct minix_superblock SB;
147 } u;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000148 char boot_block_buffer[512];
149 unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
Denis Vlasenko83bb98f2007-03-18 14:43:21 +0000150 /* check_blocks(): buffer[] was the biggest static in entire bbox */
151 char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000152
Denis Vlasenkoab801872007-12-02 08:35:37 +0000153 unsigned short ind_block1[BLOCK_SIZE >> 1];
154 unsigned short dind_block1[BLOCK_SIZE >> 1];
155 unsigned long ind_block2[BLOCK_SIZE >> 2];
156 unsigned long dind_block2[BLOCK_SIZE >> 2];
157};
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000158#define G (*ptr_to_globals)
Denis Vlasenkoab801872007-12-02 08:35:37 +0000159#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000160 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkoab801872007-12-02 08:35:37 +0000161} while (0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000162
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000163static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
Eric Andersen56f3e352000-09-19 21:13:55 +0000164{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000165 return (size + n-1) / n;
Eric Andersen56f3e352000-09-19 21:13:55 +0000166}
Eric Andersene494fdd1999-10-19 20:23:03 +0000167
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000168#define INODE_BUF1 (((struct minix1_inode*)G.inode_buffer) - 1)
169#define INODE_BUF2 (((struct minix2_inode*)G.inode_buffer) - 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000170
Denys Vlasenkodce39c92017-08-16 11:53:48 +0200171#define SB (G.u.SB)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000172
173#define SB_INODES (SB.s_ninodes)
174#define SB_IMAPS (SB.s_imap_blocks)
175#define SB_ZMAPS (SB.s_zmap_blocks)
176#define SB_FIRSTZONE (SB.s_firstdatazone)
177#define SB_ZONE_SIZE (SB.s_log_zone_size)
178#define SB_MAXSIZE (SB.s_max_size)
179#define SB_MAGIC (SB.s_magic)
180
181#if !ENABLE_FEATURE_MINIX2
182# define SB_ZONES (SB.s_nzones)
183# define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
184#else
185# define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones)
186# define INODE_BLOCKS div_roundup(SB_INODES, \
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000187 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000188#endif
189
190#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
191#define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
192
Denis Vlasenkocc244192007-02-06 00:37:40 +0000193/* Before you ask "where they come from?": */
194/* setbit/clrbit are supplied by sys/param.h */
195
196static int minix_bit(const char* a, unsigned i)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000197{
Denis Vlasenko12ff9dc2008-11-22 18:58:11 +0000198 return a[i >> 3] & (1<<(i & 7));
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000199}
200
Denis Vlasenkocc244192007-02-06 00:37:40 +0000201static void minix_setbit(char *a, unsigned i)
202{
203 setbit(a, i);
204}
205static void minix_clrbit(char *a, unsigned i)
206{
207 clrbit(a, i);
208}
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000209
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000210/* Note: do not assume 0/1, it is 0/nonzero */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000211#define zone_in_use(x) minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
212/*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000213
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000214#define mark_inode(x) minix_setbit(G.inode_map,(x))
215#define unmark_inode(x) minix_clrbit(G.inode_map,(x))
216#define mark_zone(x) minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
217#define unmark_zone(x) minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000218
219#ifndef BLKGETSIZE
220# define BLKGETSIZE _IO(0x12,96) /* return device size */
221#endif
222
Rob Landley88621d72006-08-29 19:41:06 +0000223static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000224{
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000225 /* Mark the superblock valid. */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000226 SB.s_state |= MINIX_VALID_FS;
227 SB.s_state &= ~MINIX_ERROR_FS;
Eric Andersene494fdd1999-10-19 20:23:03 +0000228
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000229 msg_eol = "seek to 0 failed";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000230 xlseek(dev_fd, 0, SEEK_SET);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100232 msg_eol = "can't clear boot sector";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000233 xwrite(dev_fd, G.boot_block_buffer, 512);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000234
235 msg_eol = "seek to BLOCK_SIZE failed";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000236 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000237
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100238 msg_eol = "can't write superblock";
Denys Vlasenkodce39c92017-08-16 11:53:48 +0200239 xwrite(dev_fd, G.u.superblock_buffer, BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000240
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100241 msg_eol = "can't write inode map";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000242 xwrite(dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000243
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100244 msg_eol = "can't write zone map";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000245 xwrite(dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000246
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100247 msg_eol = "can't write inodes";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000248 xwrite(dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000249
250 msg_eol = "\n";
Eric Andersene494fdd1999-10-19 20:23:03 +0000251}
252
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000254{
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000255 xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET);
256 xwrite(dev_fd, buffer, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000257}
258
Eric Andersene674eb71999-10-19 20:52:57 +0000259static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000260{
261 int blk;
262
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000263 if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
James Byrne69374872019-07-02 11:35:03 +0200264 bb_simple_error_msg_and_die("too many bad blocks");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000265 if (G.used_good_blocks)
266 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000267 else
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000268 blk = SB_FIRSTZONE;
269 while (blk < SB_ZONES && zone_in_use(blk))
Eric Andersene494fdd1999-10-19 20:23:03 +0000270 blk++;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000271 if (blk >= SB_ZONES)
James Byrne69374872019-07-02 11:35:03 +0200272 bb_simple_error_msg_and_die("not enough good blocks");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000273 G.good_blocks_table[G.used_good_blocks] = blk;
274 G.used_good_blocks++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000275 return blk;
276}
277
Rob Landley88621d72006-08-29 19:41:06 +0000278static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000279{
280 int blk;
281
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000282 for (blk = 0; blk < G.used_good_blocks; blk++)
283 mark_zone(G.good_blocks_table[blk]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000284}
285
Eric Andersen92d23242001-03-19 23:49:41 +0000286static int next(int zone)
Eric Andersene494fdd1999-10-19 20:23:03 +0000287{
288 if (!zone)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000289 zone = SB_FIRSTZONE - 1;
290 while (++zone < SB_ZONES)
Eric Andersene494fdd1999-10-19 20:23:03 +0000291 if (zone_in_use(zone))
292 return zone;
293 return 0;
294}
295
Rob Landley88621d72006-08-29 19:41:06 +0000296static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000297{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000298 struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 int i, j, zone;
300 int ind = 0, dind = 0;
Denis Vlasenkoab801872007-12-02 08:35:37 +0000301 /* moved to globals to reduce stack usage
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 unsigned short ind_block[BLOCK_SIZE >> 1];
303 unsigned short dind_block[BLOCK_SIZE >> 1];
Denis Vlasenkoab801872007-12-02 08:35:37 +0000304 */
305#define ind_block (G.ind_block1)
306#define dind_block (G.dind_block1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000307
308#define NEXT_BAD (zone = next(zone))
309
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000310 if (!G.badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000311 return;
312 mark_inode(MINIX_BAD_INO);
313 inode->i_nlinks = 1;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000314 /* BTW, setting this makes all images different */
315 /* it's harder to check for bugs then - diff isn't helpful :(... */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000316 inode->i_time = CUR_TIME;
Eric Andersene494fdd1999-10-19 20:23:03 +0000317 inode->i_mode = S_IFREG + 0000;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000318 inode->i_size = G.badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000319 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000321 inode->i_zone[i] = zone;
322 if (!NEXT_BAD)
323 goto end_bad;
324 }
325 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 memset(ind_block, 0, BLOCK_SIZE);
327 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000328 ind_block[i] = zone;
329 if (!NEXT_BAD)
330 goto end_bad;
331 }
332 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 memset(dind_block, 0, BLOCK_SIZE);
334 for (i = 0; i < 512; i++) {
335 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000336 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 memset(ind_block, 0, BLOCK_SIZE);
338 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000339 ind_block[j] = zone;
340 if (!NEXT_BAD)
341 goto end_bad;
342 }
343 }
James Byrne69374872019-07-02 11:35:03 +0200344 bb_simple_error_msg_and_die("too many bad blocks");
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000345 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000346 if (ind)
347 write_block(ind, (char *) ind_block);
348 if (dind)
349 write_block(dind, (char *) dind_block);
Denis Vlasenkoab801872007-12-02 08:35:37 +0000350#undef ind_block
351#undef dind_block
Eric Andersene494fdd1999-10-19 20:23:03 +0000352}
353
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000354#if ENABLE_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000355static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000356{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000357 struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000358 int i, j, zone;
359 int ind = 0, dind = 0;
Denis Vlasenkoab801872007-12-02 08:35:37 +0000360 /* moved to globals to reduce stack usage
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 unsigned long ind_block[BLOCK_SIZE >> 2];
362 unsigned long dind_block[BLOCK_SIZE >> 2];
Denis Vlasenkoab801872007-12-02 08:35:37 +0000363 */
364#define ind_block (G.ind_block2)
365#define dind_block (G.dind_block2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000366
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000367 if (!G.badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000368 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000370 inode->i_nlinks = 1;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000371 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
Eric Andersene494fdd1999-10-19 20:23:03 +0000372 inode->i_mode = S_IFREG + 0000;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000373 inode->i_size = G.badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000375 for (i = 0; i < 7; i++) {
376 inode->i_zone[i] = zone;
377 if (!NEXT_BAD)
378 goto end_bad;
379 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 inode->i_zone[7] = ind = get_free_block();
381 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000382 for (i = 0; i < 256; i++) {
383 ind_block[i] = zone;
384 if (!NEXT_BAD)
385 goto end_bad;
386 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 inode->i_zone[8] = dind = get_free_block();
388 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000389 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 write_block(ind, (char *) ind_block);
391 dind_block[i] = ind = get_free_block();
392 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000393 for (j = 0; j < 256; j++) {
394 ind_block[j] = zone;
395 if (!NEXT_BAD)
396 goto end_bad;
397 }
398 }
399 /* Could make triple indirect block here */
James Byrne69374872019-07-02 11:35:03 +0200400 bb_simple_error_msg_and_die("too many bad blocks");
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000401 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000402 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000404 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 write_block(dind, (char *) dind_block);
Denis Vlasenkoab801872007-12-02 08:35:37 +0000406#undef ind_block
407#undef dind_block
Eric Andersene494fdd1999-10-19 20:23:03 +0000408}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000409#else
410void make_bad_inode2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000411#endif
412
Rob Landley88621d72006-08-29 19:41:06 +0000413static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000414{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000415 struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000416
417 mark_inode(MINIX_ROOT_INO);
418 inode->i_zone[0] = get_free_block();
419 inode->i_nlinks = 2;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000420 inode->i_time = CUR_TIME;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000421 if (G.badblocks)
422 inode->i_size = 3 * G.dirsize;
Eric Andersene494fdd1999-10-19 20:23:03 +0000423 else {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000424 G.root_block[2 * G.dirsize] = '\0';
425 G.root_block[2 * G.dirsize + 1] = '\0';
426 inode->i_size = 2 * G.dirsize;
Eric Andersene494fdd1999-10-19 20:23:03 +0000427 }
428 inode->i_mode = S_IFDIR + 0755;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000429 inode->i_uid = GETUID;
Eric Andersene494fdd1999-10-19 20:23:03 +0000430 if (inode->i_uid)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000431 inode->i_gid = GETGID;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000432 write_block(inode->i_zone[0], G.root_block);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433}
434
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000435#if ENABLE_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000436static void make_root_inode2(void)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000438 struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439
440 mark_inode(MINIX_ROOT_INO);
441 inode->i_zone[0] = get_free_block();
442 inode->i_nlinks = 2;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000443 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000444 if (G.badblocks)
445 inode->i_size = 3 * G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 else {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000447 G.root_block[2 * G.dirsize] = '\0';
448 G.root_block[2 * G.dirsize + 1] = '\0';
449 inode->i_size = 2 * G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 }
451 inode->i_mode = S_IFDIR + 0755;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000452 inode->i_uid = GETUID;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 if (inode->i_uid)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000454 inode->i_gid = GETGID;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000455 write_block(inode->i_zone[0], G.root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000456}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000457#else
458void make_root_inode2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000459#endif
460
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000461/*
462 * Perform a test of a block; return the number of
463 * blocks readable.
464 */
465static size_t do_check(char *buffer, size_t try, unsigned current_block)
466{
467 ssize_t got;
468
469 /* Seek to the correct loc. */
470 msg_eol = "seek failed during testing of blocks";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000471 xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000472 msg_eol = "\n";
473
474 /* Try the read */
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000475 got = read(dev_fd, buffer, try * BLOCK_SIZE);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000476 if (got < 0)
477 got = 0;
478 try = ((size_t)got) / BLOCK_SIZE;
479
480 if (got & (BLOCK_SIZE - 1))
481 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
482 return try;
483}
484
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000485static void alarm_intr(int alnum UNUSED_PARAM)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000486{
487 if (G.currently_testing >= SB_ZONES)
488 return;
489 signal(SIGALRM, alarm_intr);
490 alarm(5);
491 if (!G.currently_testing)
492 return;
493 printf("%d ...", G.currently_testing);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100494 fflush_all();
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000495}
496
497static void check_blocks(void)
498{
499 size_t try, got;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000500
501 G.currently_testing = 0;
502 signal(SIGALRM, alarm_intr);
503 alarm(5);
504 while (G.currently_testing < SB_ZONES) {
505 msg_eol = "seek failed in check_blocks";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000506 xlseek(dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000507 msg_eol = "\n";
508 try = TEST_BUFFER_BLOCKS;
509 if (G.currently_testing + try > SB_ZONES)
510 try = SB_ZONES - G.currently_testing;
Denis Vlasenko83bb98f2007-03-18 14:43:21 +0000511 got = do_check(G.check_blocks_buffer, try, G.currently_testing);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000512 G.currently_testing += got;
513 if (got == try)
514 continue;
515 if (G.currently_testing < SB_FIRSTZONE)
James Byrne69374872019-07-02 11:35:03 +0200516 bb_simple_error_msg_and_die("bad blocks before data-area: cannot make fs");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000517 mark_zone(G.currently_testing);
518 G.badblocks++;
519 G.currently_testing++;
520 }
521 alarm(0);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000522 printf("%d bad block(s)\n", G.badblocks);
523}
524
525static void get_list_blocks(char *filename)
526{
527 FILE *listfile;
528 unsigned long blockno;
529
Denis Vlasenko5415c852008-07-21 23:05:26 +0000530 listfile = xfopen_for_read(filename);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000531 while (!feof(listfile)) {
Denys Vlasenko327f5502013-11-29 16:45:45 +0100532 fscanf(listfile, "%lu\n", &blockno);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000533 mark_zone(blockno);
534 G.badblocks++;
535 }
536 printf("%d bad block(s)\n", G.badblocks);
537}
538
Rob Landley88621d72006-08-29 19:41:06 +0000539static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000540{
Eric Andersene494fdd1999-10-19 20:23:03 +0000541 unsigned long inodes;
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000542 unsigned norm_firstzone;
Denis Vlasenko28bb84b2006-11-29 23:15:04 +0000543 unsigned sb_zmaps;
544 unsigned i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000545
Denys Vlasenkodce39c92017-08-16 11:53:48 +0200546 /* memset(G.u.superblock_buffer, 0, BLOCK_SIZE); */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000547 /* memset(G.boot_block_buffer, 0, 512); */
548 SB_MAGIC = G.magic;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000549 SB_ZONE_SIZE = 0;
550 SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
551 if (version2)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000552 SB.s_zones = G.total_blocks;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000553 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000554 SB.s_nzones = G.total_blocks;
Eric Andersenb225e2a2004-08-28 00:43:07 +0000555
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000556 /* some magic nrs: 1 inode / 3 blocks */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000557 if (G.req_nr_inodes == 0)
558 inodes = G.total_blocks / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000559 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000560 inodes = G.req_nr_inodes;
Eric Andersene494fdd1999-10-19 20:23:03 +0000561 /* Round up inode count to fill block size */
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 if (version2)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000563 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
564 ~(MINIX2_INODES_PER_BLOCK - 1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000565 else
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000566 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
567 ~(MINIX1_INODES_PER_BLOCK - 1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000568 if (inodes > 65535)
569 inodes = 65535;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000570 SB_INODES = inodes;
571 SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
572
Eric Andersene494fdd1999-10-19 20:23:03 +0000573 /* Real bad hack but overwise mkfs.minix can be thrown
574 * in infinite loop...
575 * try:
576 * dd if=/dev/zero of=test.fs count=10 bs=1024
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000577 * mkfs.minix -i 200 test.fs
578 */
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000579 /* This code is not insane: NORM_FIRSTZONE is not a constant,
580 * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000581 i = 999;
582 SB_ZMAPS = 0;
583 do {
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000584 norm_firstzone = NORM_FIRSTZONE;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000585 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000586 if (SB_ZMAPS == sb_zmaps) goto got_it;
587 SB_ZMAPS = sb_zmaps;
588 /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000589 } while (--i);
James Byrne69374872019-07-02 11:35:03 +0200590 bb_simple_error_msg_and_die("incompatible size/inode count, try different -i N");
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000591 got_it:
592
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000593 SB_FIRSTZONE = norm_firstzone;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000594 G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
595 G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
596 memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
597 memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000598 for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000599 unmark_zone(i);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000600 for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000601 unmark_inode(i);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000602 G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200603 printf("%lu inodes\n", (unsigned long)SB_INODES);
604 printf("%lu blocks\n", (unsigned long)SB_ZONES);
605 printf("Firstdatazone=%lu (%lu)\n", (unsigned long)SB_FIRSTZONE, (unsigned long)norm_firstzone);
606 printf("Zonesize=%u\n", BLOCK_SIZE << SB_ZONE_SIZE);
607 printf("Maxsize=%lu\n", (unsigned long)SB_MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000608}
609
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000610int mkfs_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000611int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000612{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000613 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614 char *tmp;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000615 char *str_i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 char *listfile = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000617
Denis Vlasenkoab801872007-12-02 08:35:37 +0000618 INIT_G();
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000619/* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
620 G.namelen = 30;
621 G.dirsize = 32;
622 G.magic = MINIX1_SUPER_MAGIC2;
623
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000624 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
James Byrne69374872019-07-02 11:35:03 +0200625 bb_simple_error_msg_and_die("bad inode size");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000626#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
James Byrne69374872019-07-02 11:35:03 +0200628 bb_simple_error_msg_and_die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000629#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000630
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200631 opt = getopt32(argv, "ci:l:n:+v", &str_i, &listfile, &G.namelen);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000632 argv += optind;
633 //if (opt & 1) -c
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000634 if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000635 //if (opt & 4) -l
636 if (opt & 8) { // -n
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000637 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
638 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000639 else bb_show_usage();
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000640 G.dirsize = G.namelen + 2;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000641 }
642 if (opt & 0x10) { // -v
643#if ENABLE_FEATURE_MINIX2
644 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000645#else
James Byrne69374872019-07-02 11:35:03 +0200646 bb_simple_error_msg_and_die("not compiled with minix v2 support");
Eric Andersene494fdd1999-10-19 20:23:03 +0000647#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000648 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000649
Denys Vlasenko7c6f2d42013-03-18 02:26:58 +0100650 G.device_name = argv[0];
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000651 if (!G.device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000652 bb_show_usage();
Denys Vlasenko7c6f2d42013-03-18 02:26:58 +0100653
654 /* Check if it is mounted */
655 if (find_mount_point(G.device_name, 0))
James Byrne69374872019-07-02 11:35:03 +0200656 bb_simple_error_msg_and_die("can't format mounted filesystem");
Denys Vlasenko7c6f2d42013-03-18 02:26:58 +0100657
658 xmove_fd(xopen(G.device_name, O_RDWR), dev_fd);
659
660 G.total_blocks = get_volume_size_in_bytes(dev_fd, argv[1], 1024, /*extend:*/ 1) / 1024;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000661
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000662 if (G.total_blocks < 10)
James Byrne69374872019-07-02 11:35:03 +0200663 bb_simple_error_msg_and_die("must have at least 10 blocks");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000664
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 if (version2) {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000666 G.magic = MINIX2_SUPER_MAGIC2;
667 if (G.namelen == 14)
668 G.magic = MINIX2_SUPER_MAGIC;
669 } else if (G.total_blocks > 65535)
670 G.total_blocks = 65535;
Denys Vlasenko7c6f2d42013-03-18 02:26:58 +0100671#if 0
672 struct stat statbuf;
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200673 xfstat(dev_fd, &statbuf, G.device_name);
Denys Vlasenko7c6f2d42013-03-18 02:26:58 +0100674/* why? */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000675 if (!S_ISBLK(statbuf.st_mode))
676 opt &= ~1; // clear -c (check)
Denys Vlasenko7c6f2d42013-03-18 02:26:58 +0100677#if 0
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000678/* I don't know why someone has special code to prevent mkfs.minix
679 * on IDE devices. Why IDE but not SCSI, etc?... */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000680 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
681 /* what is this? */
682 bb_error_msg_and_die("will not try "
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000683 "to make filesystem on '%s'", G.device_name);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000684#endif
Denys Vlasenko7c6f2d42013-03-18 02:26:58 +0100685#endif
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000686 tmp = G.root_block;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 *(short *) tmp = 1;
688 strcpy(tmp + 2, ".");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000689 tmp += G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 *(short *) tmp = 1;
691 strcpy(tmp + 2, "..");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000692 tmp += G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 *(short *) tmp = 2;
694 strcpy(tmp + 2, ".badblocks");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000695
Erik Andersene49d5ec2000-02-08 19:58:47 +0000696 setup_tables();
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000697
698 if (opt & 1) // -c ?
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 check_blocks();
700 else if (listfile)
701 get_list_blocks(listfile);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000702
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 if (version2) {
704 make_root_inode2();
705 make_bad_inode2();
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000706 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 make_root_inode();
708 make_bad_inode();
709 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000710
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 mark_good_blocks();
712 write_tables();
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000713 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000714}