blob: b29bf5a486687723d0bc24bd3a0264868f21a599 [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 *
Denis Vlasenko66b86c62006-11-29 22:33:47 +000036 * 30.10.94 - added support for v2 filesystem
37 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
Eric Andersenc7bda1c2004-03-15 08:29:22 +000038 *
Eric Andersene494fdd1999-10-19 20:23:03 +000039 * 09.11.94 - Added test to prevent overwrite of mounted fs adapted
40 * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
41 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
42 *
43 * 03.20.95 - Clear first 512 bytes of filesystem to make certain that
44 * the filesystem is not misidentified as a MS-DOS FAT filesystem.
45 * (Daniel Quinlan, quinlan@yggdrasil.com)
46 *
47 * 02.07.96 - Added small patch from Russell King to make the program a
48 * good deal more portable (janl@math.uio.no)
49 *
50 * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
51 *
Eric Andersenaff114c2004-04-14 17:51:38 +000052 * -c for readability checking (SLOW!)
Eric Andersene494fdd1999-10-19 20:23:03 +000053 * -l for getting a list of bad blocks from a file.
54 * -n for namelength (currently the kernel only uses 14 or 30)
55 * -i for number of inodes
56 * -v for v2 filesystem
57 *
58 * The device may be a block device or a image of one, but this isn't
Eric Andersenc7bda1c2004-03-15 08:29:22 +000059 * 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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000065#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000066#include <mntent.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000067
Denis Vlasenkoaa959592007-01-03 00:43:19 +000068#include "minix.h"
69
Denis Vlasenko7ab36992007-12-02 08:55:34 +000070/* Store the very same times/uids/gids for image consistency */
71#if 1
Denis Vlasenko66b86c62006-11-29 22:33:47 +000072# define CUR_TIME 0
73# define GETUID 0
74# define GETGID 0
75#else
Denis Vlasenko7ab36992007-12-02 08:55:34 +000076/* Was using this. Is it useful? NB: this will break testsuite */
Denis Vlasenko66b86c62006-11-29 22:33:47 +000077# define CUR_TIME time(NULL)
78# define GETUID getuid()
79# define GETGID getgid()
80#endif
Eric Andersen85c55202000-07-10 22:46:55 +000081
Denis Vlasenko66b86c62006-11-29 22:33:47 +000082enum {
Denis Vlasenko66b86c62006-11-29 22:33:47 +000083 MAX_GOOD_BLOCKS = 512,
Denis Vlasenko66b86c62006-11-29 22:33:47 +000084 TEST_BUFFER_BLOCKS = 16,
85};
Eric Andersen85c55202000-07-10 22:46:55 +000086
Denis Vlasenko4efeaee2007-03-15 19:52:42 +000087#if !ENABLE_FEATURE_MINIX2
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000088enum { version2 = 0 };
89#endif
Eric Andersene494fdd1999-10-19 20:23:03 +000090
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +000091enum { dev_fd = 3 };
Eric Andersene494fdd1999-10-19 20:23:03 +000092
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +000093struct globals {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +000094#if ENABLE_FEATURE_MINIX2
Denis Vlasenkoca525b42007-06-13 12:27:17 +000095 smallint version2;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +000096#define version2 G.version2
97#endif
98 char *device_name;
99 uint32_t total_blocks;
100 int badblocks;
101 int namelen;
102 int dirsize;
103 int magic;
104 char *inode_buffer;
105 char *inode_map;
106 char *zone_map;
107 int used_good_blocks;
108 unsigned long req_nr_inodes;
109 unsigned currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000110
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000111 char root_block[BLOCK_SIZE];
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000112 char superblock_buffer[BLOCK_SIZE];
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000113 char boot_block_buffer[512];
114 unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
Denis Vlasenko83bb98f2007-03-18 14:43:21 +0000115 /* check_blocks(): buffer[] was the biggest static in entire bbox */
116 char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000117
Denis Vlasenkoab801872007-12-02 08:35:37 +0000118 unsigned short ind_block1[BLOCK_SIZE >> 1];
119 unsigned short dind_block1[BLOCK_SIZE >> 1];
120 unsigned long ind_block2[BLOCK_SIZE >> 2];
121 unsigned long dind_block2[BLOCK_SIZE >> 2];
122};
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000123#define G (*ptr_to_globals)
Denis Vlasenkoab801872007-12-02 08:35:37 +0000124#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000125 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkoab801872007-12-02 08:35:37 +0000126} while (0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000127
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000128static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
Eric Andersen56f3e352000-09-19 21:13:55 +0000129{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000130 return (size + n-1) / n;
Eric Andersen56f3e352000-09-19 21:13:55 +0000131}
Eric Andersene494fdd1999-10-19 20:23:03 +0000132
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000133#define INODE_BUF1 (((struct minix1_inode*)G.inode_buffer) - 1)
134#define INODE_BUF2 (((struct minix2_inode*)G.inode_buffer) - 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000135
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000136#define SB (*(struct minix_superblock*)G.superblock_buffer)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000137
138#define SB_INODES (SB.s_ninodes)
139#define SB_IMAPS (SB.s_imap_blocks)
140#define SB_ZMAPS (SB.s_zmap_blocks)
141#define SB_FIRSTZONE (SB.s_firstdatazone)
142#define SB_ZONE_SIZE (SB.s_log_zone_size)
143#define SB_MAXSIZE (SB.s_max_size)
144#define SB_MAGIC (SB.s_magic)
145
146#if !ENABLE_FEATURE_MINIX2
147# define SB_ZONES (SB.s_nzones)
148# define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
149#else
150# define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones)
151# define INODE_BLOCKS div_roundup(SB_INODES, \
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000152 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000153#endif
154
155#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
156#define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
157
Denis Vlasenkocc244192007-02-06 00:37:40 +0000158/* Before you ask "where they come from?": */
159/* setbit/clrbit are supplied by sys/param.h */
160
161static int minix_bit(const char* a, unsigned i)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000162{
163 return a[i >> 3] & (1<<(i & 7));
164}
165
Denis Vlasenkocc244192007-02-06 00:37:40 +0000166static void minix_setbit(char *a, unsigned i)
167{
168 setbit(a, i);
169}
170static void minix_clrbit(char *a, unsigned i)
171{
172 clrbit(a, i);
173}
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000174
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000175/* Note: do not assume 0/1, it is 0/nonzero */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000176#define zone_in_use(x) minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
177/*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000178
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000179#define mark_inode(x) minix_setbit(G.inode_map,(x))
180#define unmark_inode(x) minix_clrbit(G.inode_map,(x))
181#define mark_zone(x) minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
182#define unmark_zone(x) minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000183
184#ifndef BLKGETSIZE
185# define BLKGETSIZE _IO(0x12,96) /* return device size */
186#endif
187
Eric Andersene494fdd1999-10-19 20:23:03 +0000188
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000190{
191 char ch;
192
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000193 if (lseek(fd, offset, SEEK_SET) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000194 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000196 return 0;
197 return 1;
198}
199
Rob Landley88621d72006-08-29 19:41:06 +0000200static int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000201{
202 int high, low;
203
204 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000206 low = high;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000207
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000209 const int mid = (low + high) / 2;
210
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000212 low = mid;
213 else
214 high = mid;
215 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000216 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000217 return (low + 1);
218}
219
Rob Landley88621d72006-08-29 19:41:06 +0000220static int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000221{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 int fd;
223 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000224
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000225 fd = xopen(file, O_RDWR);
Eric Andersene494fdd1999-10-19 20:23:03 +0000226 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
227 close(fd);
228 return (size * 512);
229 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230
Eric Andersene494fdd1999-10-19 20:23:03 +0000231 size = count_blocks(fd);
232 close(fd);
233 return size;
234}
235
Rob Landley88621d72006-08-29 19:41:06 +0000236static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000237{
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000238 /* Mark the superblock valid. */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000239 SB.s_state |= MINIX_VALID_FS;
240 SB.s_state &= ~MINIX_ERROR_FS;
Eric Andersene494fdd1999-10-19 20:23:03 +0000241
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000242 msg_eol = "seek to 0 failed";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000243 xlseek(dev_fd, 0, SEEK_SET);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000245 msg_eol = "cannot clear boot sector";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000246 xwrite(dev_fd, G.boot_block_buffer, 512);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000247
248 msg_eol = "seek to BLOCK_SIZE failed";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000249 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000250
251 msg_eol = "cannot write superblock";
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000252 xwrite(dev_fd, G.superblock_buffer, BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000253
254 msg_eol = "cannot write inode map";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000255 xwrite(dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000256
257 msg_eol = "cannot write zone map";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000258 xwrite(dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000259
260 msg_eol = "cannot write inodes";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000261 xwrite(dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000262
263 msg_eol = "\n";
Eric Andersene494fdd1999-10-19 20:23:03 +0000264}
265
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000267{
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000268 xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET);
269 xwrite(dev_fd, buffer, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000270}
271
Eric Andersene674eb71999-10-19 20:52:57 +0000272static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000273{
274 int blk;
275
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000276 if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000277 bb_error_msg_and_die("too many bad blocks");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000278 if (G.used_good_blocks)
279 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000280 else
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000281 blk = SB_FIRSTZONE;
282 while (blk < SB_ZONES && zone_in_use(blk))
Eric Andersene494fdd1999-10-19 20:23:03 +0000283 blk++;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000284 if (blk >= SB_ZONES)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000285 bb_error_msg_and_die("not enough good blocks");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000286 G.good_blocks_table[G.used_good_blocks] = blk;
287 G.used_good_blocks++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000288 return blk;
289}
290
Rob Landley88621d72006-08-29 19:41:06 +0000291static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000292{
293 int blk;
294
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000295 for (blk = 0; blk < G.used_good_blocks; blk++)
296 mark_zone(G.good_blocks_table[blk]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000297}
298
Eric Andersen92d23242001-03-19 23:49:41 +0000299static int next(int zone)
Eric Andersene494fdd1999-10-19 20:23:03 +0000300{
301 if (!zone)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000302 zone = SB_FIRSTZONE - 1;
303 while (++zone < SB_ZONES)
Eric Andersene494fdd1999-10-19 20:23:03 +0000304 if (zone_in_use(zone))
305 return zone;
306 return 0;
307}
308
Rob Landley88621d72006-08-29 19:41:06 +0000309static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000310{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000311 struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 int i, j, zone;
313 int ind = 0, dind = 0;
Denis Vlasenkoab801872007-12-02 08:35:37 +0000314 /* moved to globals to reduce stack usage
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 unsigned short ind_block[BLOCK_SIZE >> 1];
316 unsigned short dind_block[BLOCK_SIZE >> 1];
Denis Vlasenkoab801872007-12-02 08:35:37 +0000317 */
318#define ind_block (G.ind_block1)
319#define dind_block (G.dind_block1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000320
321#define NEXT_BAD (zone = next(zone))
322
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000323 if (!G.badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000324 return;
325 mark_inode(MINIX_BAD_INO);
326 inode->i_nlinks = 1;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000327 /* BTW, setting this makes all images different */
328 /* it's harder to check for bugs then - diff isn't helpful :(... */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000329 inode->i_time = CUR_TIME;
Eric Andersene494fdd1999-10-19 20:23:03 +0000330 inode->i_mode = S_IFREG + 0000;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000331 inode->i_size = G.badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000332 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000334 inode->i_zone[i] = zone;
335 if (!NEXT_BAD)
336 goto end_bad;
337 }
338 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 memset(ind_block, 0, BLOCK_SIZE);
340 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000341 ind_block[i] = zone;
342 if (!NEXT_BAD)
343 goto end_bad;
344 }
345 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 memset(dind_block, 0, BLOCK_SIZE);
347 for (i = 0; i < 512; i++) {
348 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000349 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 memset(ind_block, 0, BLOCK_SIZE);
351 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000352 ind_block[j] = zone;
353 if (!NEXT_BAD)
354 goto end_bad;
355 }
356 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000357 bb_error_msg_and_die("too many bad blocks");
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000358 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000359 if (ind)
360 write_block(ind, (char *) ind_block);
361 if (dind)
362 write_block(dind, (char *) dind_block);
Denis Vlasenkoab801872007-12-02 08:35:37 +0000363#undef ind_block
364#undef dind_block
Eric Andersene494fdd1999-10-19 20:23:03 +0000365}
366
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000367#if ENABLE_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000368static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000369{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000370 struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000371 int i, j, zone;
372 int ind = 0, dind = 0;
Denis Vlasenkoab801872007-12-02 08:35:37 +0000373 /* moved to globals to reduce stack usage
Eric Andersene494fdd1999-10-19 20:23:03 +0000374 unsigned long ind_block[BLOCK_SIZE >> 2];
375 unsigned long dind_block[BLOCK_SIZE >> 2];
Denis Vlasenkoab801872007-12-02 08:35:37 +0000376 */
377#define ind_block (G.ind_block2)
378#define dind_block (G.dind_block2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000379
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000380 if (!G.badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000381 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000383 inode->i_nlinks = 1;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000384 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
Eric Andersene494fdd1999-10-19 20:23:03 +0000385 inode->i_mode = S_IFREG + 0000;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000386 inode->i_size = G.badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000388 for (i = 0; i < 7; i++) {
389 inode->i_zone[i] = zone;
390 if (!NEXT_BAD)
391 goto end_bad;
392 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000393 inode->i_zone[7] = ind = get_free_block();
394 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000395 for (i = 0; i < 256; i++) {
396 ind_block[i] = zone;
397 if (!NEXT_BAD)
398 goto end_bad;
399 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 inode->i_zone[8] = dind = get_free_block();
401 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000402 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 write_block(ind, (char *) ind_block);
404 dind_block[i] = ind = get_free_block();
405 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000406 for (j = 0; j < 256; j++) {
407 ind_block[j] = zone;
408 if (!NEXT_BAD)
409 goto end_bad;
410 }
411 }
412 /* Could make triple indirect block here */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000413 bb_error_msg_and_die("too many bad blocks");
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000414 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000415 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000416 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000417 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000418 write_block(dind, (char *) dind_block);
Denis Vlasenkoab801872007-12-02 08:35:37 +0000419#undef ind_block
420#undef dind_block
Eric Andersene494fdd1999-10-19 20:23:03 +0000421}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000422#else
423void make_bad_inode2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000424#endif
425
Rob Landley88621d72006-08-29 19:41:06 +0000426static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000427{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000428 struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000429
430 mark_inode(MINIX_ROOT_INO);
431 inode->i_zone[0] = get_free_block();
432 inode->i_nlinks = 2;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000433 inode->i_time = CUR_TIME;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000434 if (G.badblocks)
435 inode->i_size = 3 * G.dirsize;
Eric Andersene494fdd1999-10-19 20:23:03 +0000436 else {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000437 G.root_block[2 * G.dirsize] = '\0';
438 G.root_block[2 * G.dirsize + 1] = '\0';
439 inode->i_size = 2 * G.dirsize;
Eric Andersene494fdd1999-10-19 20:23:03 +0000440 }
441 inode->i_mode = S_IFDIR + 0755;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000442 inode->i_uid = GETUID;
Eric Andersene494fdd1999-10-19 20:23:03 +0000443 if (inode->i_uid)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000444 inode->i_gid = GETGID;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000445 write_block(inode->i_zone[0], G.root_block);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446}
447
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000448#if ENABLE_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000449static void make_root_inode2(void)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000451 struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452
453 mark_inode(MINIX_ROOT_INO);
454 inode->i_zone[0] = get_free_block();
455 inode->i_nlinks = 2;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000456 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000457 if (G.badblocks)
458 inode->i_size = 3 * G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 else {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000460 G.root_block[2 * G.dirsize] = '\0';
461 G.root_block[2 * G.dirsize + 1] = '\0';
462 inode->i_size = 2 * G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 }
464 inode->i_mode = S_IFDIR + 0755;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000465 inode->i_uid = GETUID;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466 if (inode->i_uid)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000467 inode->i_gid = GETGID;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000468 write_block(inode->i_zone[0], G.root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000469}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000470#else
471void make_root_inode2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000472#endif
473
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000474/*
475 * Perform a test of a block; return the number of
476 * blocks readable.
477 */
478static size_t do_check(char *buffer, size_t try, unsigned current_block)
479{
480 ssize_t got;
481
482 /* Seek to the correct loc. */
483 msg_eol = "seek failed during testing of blocks";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000484 xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000485 msg_eol = "\n";
486
487 /* Try the read */
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000488 got = read(dev_fd, buffer, try * BLOCK_SIZE);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000489 if (got < 0)
490 got = 0;
491 try = ((size_t)got) / BLOCK_SIZE;
492
493 if (got & (BLOCK_SIZE - 1))
494 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
495 return try;
496}
497
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000498static void alarm_intr(int alnum UNUSED_PARAM)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000499{
500 if (G.currently_testing >= SB_ZONES)
501 return;
502 signal(SIGALRM, alarm_intr);
503 alarm(5);
504 if (!G.currently_testing)
505 return;
506 printf("%d ...", G.currently_testing);
507 fflush(stdout);
508}
509
510static void check_blocks(void)
511{
512 size_t try, got;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000513
514 G.currently_testing = 0;
515 signal(SIGALRM, alarm_intr);
516 alarm(5);
517 while (G.currently_testing < SB_ZONES) {
518 msg_eol = "seek failed in check_blocks";
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000519 xlseek(dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000520 msg_eol = "\n";
521 try = TEST_BUFFER_BLOCKS;
522 if (G.currently_testing + try > SB_ZONES)
523 try = SB_ZONES - G.currently_testing;
Denis Vlasenko83bb98f2007-03-18 14:43:21 +0000524 got = do_check(G.check_blocks_buffer, try, G.currently_testing);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000525 G.currently_testing += got;
526 if (got == try)
527 continue;
528 if (G.currently_testing < SB_FIRSTZONE)
529 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
530 mark_zone(G.currently_testing);
531 G.badblocks++;
532 G.currently_testing++;
533 }
534 alarm(0);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000535 printf("%d bad block(s)\n", G.badblocks);
536}
537
538static void get_list_blocks(char *filename)
539{
540 FILE *listfile;
541 unsigned long blockno;
542
Denis Vlasenko5415c852008-07-21 23:05:26 +0000543 listfile = xfopen_for_read(filename);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000544 while (!feof(listfile)) {
545 fscanf(listfile, "%ld\n", &blockno);
546 mark_zone(blockno);
547 G.badblocks++;
548 }
549 printf("%d bad block(s)\n", G.badblocks);
550}
551
Rob Landley88621d72006-08-29 19:41:06 +0000552static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000553{
Eric Andersene494fdd1999-10-19 20:23:03 +0000554 unsigned long inodes;
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000555 unsigned norm_firstzone;
Denis Vlasenko28bb84b2006-11-29 23:15:04 +0000556 unsigned sb_zmaps;
557 unsigned i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000558
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000559 /* memset(G.superblock_buffer, 0, BLOCK_SIZE); */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000560 /* memset(G.boot_block_buffer, 0, 512); */
561 SB_MAGIC = G.magic;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000562 SB_ZONE_SIZE = 0;
563 SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
564 if (version2)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000565 SB.s_zones = G.total_blocks;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000566 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000567 SB.s_nzones = G.total_blocks;
Eric Andersenb225e2a2004-08-28 00:43:07 +0000568
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000569 /* some magic nrs: 1 inode / 3 blocks */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000570 if (G.req_nr_inodes == 0)
571 inodes = G.total_blocks / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000572 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000573 inodes = G.req_nr_inodes;
Eric Andersene494fdd1999-10-19 20:23:03 +0000574 /* Round up inode count to fill block size */
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 if (version2)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000576 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
577 ~(MINIX2_INODES_PER_BLOCK - 1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000578 else
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000579 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
580 ~(MINIX1_INODES_PER_BLOCK - 1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000581 if (inodes > 65535)
582 inodes = 65535;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000583 SB_INODES = inodes;
584 SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
585
Eric Andersene494fdd1999-10-19 20:23:03 +0000586 /* Real bad hack but overwise mkfs.minix can be thrown
587 * in infinite loop...
588 * try:
589 * dd if=/dev/zero of=test.fs count=10 bs=1024
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000590 * mkfs.minix -i 200 test.fs
591 */
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000592 /* This code is not insane: NORM_FIRSTZONE is not a constant,
593 * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000594 i = 999;
595 SB_ZMAPS = 0;
596 do {
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000597 norm_firstzone = NORM_FIRSTZONE;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000598 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000599 if (SB_ZMAPS == sb_zmaps) goto got_it;
600 SB_ZMAPS = sb_zmaps;
601 /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000602 } while (--i);
603 bb_error_msg_and_die("incompatible size/inode count, try different -i N");
604 got_it:
605
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000606 SB_FIRSTZONE = norm_firstzone;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000607 G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
608 G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
609 memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
610 memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000611 for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000612 unmark_zone(i);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000613 for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000614 unmark_inode(i);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000615 G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000616 printf("%ld inodes\n", (long)SB_INODES);
617 printf("%ld blocks\n", (long)SB_ZONES);
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000618 printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)norm_firstzone);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000619 printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE);
620 printf("Maxsize=%ld\n", (long)SB_MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000621}
622
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000623int mkfs_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000624int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000625{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000626 struct mntent *mp;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000627 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000628 char *tmp;
629 struct stat statbuf;
Denis Vlasenko1d426652008-03-17 09:09:09 +0000630 char *str_i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000631 char *listfile = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000632
Denis Vlasenkoab801872007-12-02 08:35:37 +0000633 INIT_G();
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000634/* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
635 G.namelen = 30;
636 G.dirsize = 32;
637 G.magic = MINIX1_SUPER_MAGIC2;
638
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000639 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000640 bb_error_msg_and_die("bad inode size");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000641#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000642 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000643 bb_error_msg_and_die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000644#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000645
Denis Vlasenko1d426652008-03-17 09:09:09 +0000646 opt_complementary = "n+"; /* -n N */
647 opt = getopt32(argv, "ci:l:n:v", &str_i, &listfile, &G.namelen);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000648 argv += optind;
649 //if (opt & 1) -c
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000650 if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000651 //if (opt & 4) -l
652 if (opt & 8) { // -n
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000653 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
654 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000655 else bb_show_usage();
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000656 G.dirsize = G.namelen + 2;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000657 }
658 if (opt & 0x10) { // -v
659#if ENABLE_FEATURE_MINIX2
660 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000661#else
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000662 bb_error_msg_and_die("not compiled with minix v2 support");
Eric Andersene494fdd1999-10-19 20:23:03 +0000663#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000664 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000665
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000666 G.device_name = *argv++;
667 if (!G.device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000668 bb_show_usage();
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000669 if (*argv)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000670 G.total_blocks = xatou32(*argv);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000671 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000672 G.total_blocks = get_size(G.device_name) / 1024;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000673
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000674 if (G.total_blocks < 10)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000675 bb_error_msg_and_die("must have at least 10 blocks");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000676
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677 if (version2) {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000678 G.magic = MINIX2_SUPER_MAGIC2;
679 if (G.namelen == 14)
680 G.magic = MINIX2_SUPER_MAGIC;
681 } else if (G.total_blocks > 65535)
682 G.total_blocks = 65535;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000683
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000684 /* Check if it is mounted */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000685 mp = find_mount_point(G.device_name, NULL);
686 if (mp && strcmp(G.device_name, mp->mnt_fsname) == 0)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000687 bb_error_msg_and_die("%s is mounted on %s; "
688 "refusing to make a filesystem",
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000689 G.device_name, mp->mnt_dir);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000690
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000691 xmove_fd(xopen(G.device_name, O_RDWR), dev_fd);
692 if (fstat(dev_fd, &statbuf) < 0)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000693 bb_error_msg_and_die("cannot stat %s", G.device_name);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000694 if (!S_ISBLK(statbuf.st_mode))
695 opt &= ~1; // clear -c (check)
696
697/* I don't know why someone has special code to prevent mkfs.minix
698 * on IDE devices. Why IDE but not SCSI, etc?... */
699#if 0
700 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
701 /* what is this? */
702 bb_error_msg_and_die("will not try "
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000703 "to make filesystem on '%s'", G.device_name);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000704#endif
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000705
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000706 tmp = G.root_block;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 *(short *) tmp = 1;
708 strcpy(tmp + 2, ".");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000709 tmp += G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000710 *(short *) tmp = 1;
711 strcpy(tmp + 2, "..");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000712 tmp += G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 *(short *) tmp = 2;
714 strcpy(tmp + 2, ".badblocks");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000715
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 setup_tables();
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000717
718 if (opt & 1) // -c ?
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 check_blocks();
720 else if (listfile)
721 get_list_blocks(listfile);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000722
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 if (version2) {
724 make_root_inode2();
725 make_bad_inode2();
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000726 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 make_root_inode();
728 make_bad_inode();
729 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000730
Erik Andersene49d5ec2000-02-08 19:58:47 +0000731 mark_good_blocks();
732 write_tables();
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000733 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000734}