blob: b154f4ab9b0b5ec0babd26195e7fc7764104c086 [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)
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
Eric Andersencbe31da2001-02-20 06:14:08 +000065#include "busybox.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000066#include <mntent.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000067
Eric Andersen85c55202000-07-10 22:46:55 +000068#define MINIX_ROOT_INO 1
69#define MINIX_LINK_MAX 250
70#define MINIX2_LINK_MAX 65530
71
72#define MINIX_I_MAP_SLOTS 8
73#define MINIX_Z_MAP_SLOTS 64
74#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
75#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
76#define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
77#define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
78#define MINIX_VALID_FS 0x0001 /* Clean fs. */
79#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
80
81#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
82#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
83
84#define MINIX_V1 0x0001 /* original minix fs */
85#define MINIX_V2 0x0002 /* minix V2 fs */
86
87#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
88
89/*
90 * This is the original minix inode layout on disk.
91 * Note the 8-bit gid and atime and ctime.
92 */
93struct minix_inode {
Eric Andersen39cdf4e2004-01-30 22:40:05 +000094 uint16_t i_mode;
95 uint16_t i_uid;
96 uint32_t i_size;
97 uint32_t i_time;
98 uint8_t i_gid;
99 uint8_t i_nlinks;
100 uint16_t i_zone[9];
Eric Andersen85c55202000-07-10 22:46:55 +0000101};
102
103/*
104 * The new minix inode has all the time entries, as well as
105 * long block numbers and a third indirect block (7+1+1+1
106 * instead of 7+1+1). Also, some previously 8-bit values are
107 * now 16-bit. The inode is now 64 bytes instead of 32.
108 */
109struct minix2_inode {
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000110 uint16_t i_mode;
111 uint16_t i_nlinks;
112 uint16_t i_uid;
113 uint16_t i_gid;
114 uint32_t i_size;
115 uint32_t i_atime;
116 uint32_t i_mtime;
117 uint32_t i_ctime;
118 uint32_t i_zone[10];
Eric Andersen85c55202000-07-10 22:46:55 +0000119};
120
121/*
122 * minix super-block data on disk
123 */
124struct minix_super_block {
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000125 uint16_t s_ninodes;
126 uint16_t s_nzones;
127 uint16_t s_imap_blocks;
128 uint16_t s_zmap_blocks;
129 uint16_t s_firstdatazone;
130 uint16_t s_log_zone_size;
131 uint32_t s_max_size;
132 uint16_t s_magic;
133 uint16_t s_state;
134 uint32_t s_zones;
Eric Andersen85c55202000-07-10 22:46:55 +0000135};
136
137struct minix_dir_entry {
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000138 uint16_t inode;
Eric Andersen85c55202000-07-10 22:46:55 +0000139 char name[0];
140};
141
Eric Andersen85c55202000-07-10 22:46:55 +0000142#define NAME_MAX 255 /* # chars in a file name */
143
144#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
145
146#define MINIX_VALID_FS 0x0001 /* Clean fs. */
147#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
148
149#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
150#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
151
152#ifndef BLKGETSIZE
153#define BLKGETSIZE _IO(0x12,96) /* return device size */
154#endif
155
Eric Andersene494fdd1999-10-19 20:23:03 +0000156
Eric Andersene494fdd1999-10-19 20:23:03 +0000157#ifndef __linux__
158#define volatile
159#endif
160
161#define MINIX_ROOT_INO 1
162#define MINIX_BAD_INO 2
163
164#define TEST_BUFFER_BLOCKS 16
165#define MAX_GOOD_BLOCKS 512
166
167#define UPPER(size,n) (((size)+((n)-1))/(n))
168#define INODE_SIZE (sizeof(struct minix_inode))
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000169#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000170#define INODE_SIZE2 (sizeof(struct minix2_inode))
171#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
172 : MINIX_INODES_PER_BLOCK))
173#else
174#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
175#endif
176#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
177
178#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
179
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000180static char *device_name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000181static int DEV = -1;
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000182static uint32_t BLOCKS;
183static int check;
184static int badblocks;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185static int namelen = 30; /* default (changed to 30, per Linus's
186
187 suggestion, Sun Nov 21 08:05:07 1993) */
Eric Andersene494fdd1999-10-19 20:23:03 +0000188static int dirsize = 32;
189static int magic = MINIX_SUPER_MAGIC2;
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000190static int version2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000191
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000192static char root_block[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +0000193
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000194static char *inode_buffer;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195
Eric Andersene494fdd1999-10-19 20:23:03 +0000196#define Inode (((struct minix_inode *) inode_buffer)-1)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000197#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000198#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
199#endif
200static char super_block_buffer[BLOCK_SIZE];
201static char boot_block_buffer[512];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202
Eric Andersene494fdd1999-10-19 20:23:03 +0000203#define Super (*(struct minix_super_block *)super_block_buffer)
Eric Andersenb225e2a2004-08-28 00:43:07 +0000204#define INODES (Super.s_ninodes)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000205#ifdef CONFIG_FEATURE_MINIX2
Eric Andersenb225e2a2004-08-28 00:43:07 +0000206#define ZONES (version2 ? Super.s_zones : Super.s_nzones)
Eric Andersene494fdd1999-10-19 20:23:03 +0000207#else
Eric Andersenb225e2a2004-08-28 00:43:07 +0000208#define ZONES (Super.s_nzones)
Eric Andersene494fdd1999-10-19 20:23:03 +0000209#endif
Eric Andersenb225e2a2004-08-28 00:43:07 +0000210#define IMAPS (Super.s_imap_blocks)
211#define ZMAPS (Super.s_zmap_blocks)
212#define FIRSTZONE (Super.s_firstdatazone)
213#define ZONESIZE (Super.s_log_zone_size)
214#define MAXSIZE (Super.s_max_size)
Eric Andersene494fdd1999-10-19 20:23:03 +0000215#define MAGIC (Super.s_magic)
216#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
217
218static char *inode_map;
219static char *zone_map;
220
221static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000222static int used_good_blocks;
223static unsigned long req_nr_inodes;
Eric Andersene494fdd1999-10-19 20:23:03 +0000224
Rob Landley88621d72006-08-29 19:41:06 +0000225static int bit(char * a,unsigned int i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000226{
227 return (a[i >> 3] & (1<<(i & 7))) != 0;
228}
229#define inode_in_use(x) (bit(inode_map,(x)))
230#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000231
232#define mark_inode(x) (setbit(inode_map,(x)))
233#define unmark_inode(x) (clrbit(inode_map,(x)))
234
235#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
236#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
237
Eric Andersene494fdd1999-10-19 20:23:03 +0000238/*
239 * Check to make certain that our new filesystem won't be created on
240 * an already mounted partition. Code adapted from mke2fs, Copyright
241 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
242 */
Rob Landley88621d72006-08-29 19:41:06 +0000243static void check_mount(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000244{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 FILE *f;
246 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000247
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000249 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 while ((mnt = getmntent(f)) != NULL)
251 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000252 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000254 if (!mnt)
255 return;
256
Manuel Novoa III cad53642003-03-19 09:13:01 +0000257 bb_error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000258}
259
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000261{
262 char ch;
263
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264 if (lseek(fd, offset, 0) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000265 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000267 return 0;
268 return 1;
269}
270
Rob Landley88621d72006-08-29 19:41:06 +0000271static int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000272{
273 int high, low;
274
275 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000277 low = high;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000279 const int mid = (low + high) / 2;
280
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000282 low = mid;
283 else
284 high = mid;
285 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000287 return (low + 1);
288}
289
Rob Landley88621d72006-08-29 19:41:06 +0000290static int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000291{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292 int fd;
293 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000294
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000295 fd = xopen(file, O_RDWR);
Eric Andersene494fdd1999-10-19 20:23:03 +0000296 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
297 close(fd);
298 return (size * 512);
299 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000300
Eric Andersene494fdd1999-10-19 20:23:03 +0000301 size = count_blocks(fd);
302 close(fd);
303 return size;
304}
305
Rob Landley88621d72006-08-29 19:41:06 +0000306static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000307{
308 /* Mark the super block valid. */
309 Super.s_state |= MINIX_VALID_FS;
310 Super.s_state &= ~MINIX_ERROR_FS;
311
312 if (lseek(DEV, 0, SEEK_SET))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000313 bb_error_msg_and_die("seek to boot block failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000314 if (512 != write(DEV, boot_block_buffer, 512))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000315 bb_error_msg_and_die("unable to clear boot sector");
Eric Andersene494fdd1999-10-19 20:23:03 +0000316 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000317 bb_error_msg_and_die("seek failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000318 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000319 bb_error_msg_and_die("unable to write super-block");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000321 bb_error_msg_and_die("unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000322 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 bb_error_msg_and_die("unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000325 bb_error_msg_and_die("unable to write inodes");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326
Eric Andersene494fdd1999-10-19 20:23:03 +0000327}
328
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000330{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000332 bb_error_msg_and_die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000333 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000334 bb_error_msg_and_die("write failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000335}
336
Eric Andersene674eb71999-10-19 20:52:57 +0000337static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000338{
339 int blk;
340
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000342 bb_error_msg_and_die("too many bad blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000343 if (used_good_blocks)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 blk = good_blocks_table[used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000345 else
346 blk = FIRSTZONE;
347 while (blk < ZONES && zone_in_use(blk))
348 blk++;
349 if (blk >= ZONES)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000350 bb_error_msg_and_die("not enough good blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000351 good_blocks_table[used_good_blocks] = blk;
352 used_good_blocks++;
353 return blk;
354}
355
Rob Landley88621d72006-08-29 19:41:06 +0000356static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000357{
358 int blk;
359
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 for (blk = 0; blk < used_good_blocks; blk++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 mark_zone(good_blocks_table[blk]);
362}
363
Eric Andersen92d23242001-03-19 23:49:41 +0000364static int next(int zone)
Eric Andersene494fdd1999-10-19 20:23:03 +0000365{
366 if (!zone)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 zone = FIRSTZONE - 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000368 while (++zone < ZONES)
369 if (zone_in_use(zone))
370 return zone;
371 return 0;
372}
373
Rob Landley88621d72006-08-29 19:41:06 +0000374static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000375{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
377 int i, j, zone;
378 int ind = 0, dind = 0;
379 unsigned short ind_block[BLOCK_SIZE >> 1];
380 unsigned short dind_block[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000381
382#define NEXT_BAD (zone = next(zone))
383
384 if (!badblocks)
385 return;
386 mark_inode(MINIX_BAD_INO);
387 inode->i_nlinks = 1;
388 inode->i_time = time(NULL);
389 inode->i_mode = S_IFREG + 0000;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 inode->i_size = badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000391 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000393 inode->i_zone[i] = zone;
394 if (!NEXT_BAD)
395 goto end_bad;
396 }
397 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 memset(ind_block, 0, BLOCK_SIZE);
399 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000400 ind_block[i] = zone;
401 if (!NEXT_BAD)
402 goto end_bad;
403 }
404 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 memset(dind_block, 0, BLOCK_SIZE);
406 for (i = 0; i < 512; i++) {
407 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000408 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 memset(ind_block, 0, BLOCK_SIZE);
410 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000411 ind_block[j] = zone;
412 if (!NEXT_BAD)
413 goto end_bad;
414 }
415 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000416 bb_error_msg_and_die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000418 if (ind)
419 write_block(ind, (char *) ind_block);
420 if (dind)
421 write_block(dind, (char *) dind_block);
422}
423
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000424#ifdef CONFIG_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000425static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000426{
427 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
428 int i, j, zone;
429 int ind = 0, dind = 0;
430 unsigned long ind_block[BLOCK_SIZE >> 2];
431 unsigned long dind_block[BLOCK_SIZE >> 2];
432
433 if (!badblocks)
434 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000436 inode->i_nlinks = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
Eric Andersene494fdd1999-10-19 20:23:03 +0000438 inode->i_mode = S_IFREG + 0000;
439 inode->i_size = badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000441 for (i = 0; i < 7; i++) {
442 inode->i_zone[i] = zone;
443 if (!NEXT_BAD)
444 goto end_bad;
445 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 inode->i_zone[7] = ind = get_free_block();
447 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000448 for (i = 0; i < 256; i++) {
449 ind_block[i] = zone;
450 if (!NEXT_BAD)
451 goto end_bad;
452 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 inode->i_zone[8] = dind = get_free_block();
454 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000455 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456 write_block(ind, (char *) ind_block);
457 dind_block[i] = ind = get_free_block();
458 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000459 for (j = 0; j < 256; j++) {
460 ind_block[j] = zone;
461 if (!NEXT_BAD)
462 goto end_bad;
463 }
464 }
465 /* Could make triple indirect block here */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000466 bb_error_msg_and_die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000468 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000470 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 write_block(dind, (char *) dind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000472}
473#endif
474
Rob Landley88621d72006-08-29 19:41:06 +0000475static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000476{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000477 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000478
479 mark_inode(MINIX_ROOT_INO);
480 inode->i_zone[0] = get_free_block();
481 inode->i_nlinks = 2;
482 inode->i_time = time(NULL);
483 if (badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000484 inode->i_size = 3 * dirsize;
485 else {
486 root_block[2 * dirsize] = '\0';
487 root_block[2 * dirsize + 1] = '\0';
488 inode->i_size = 2 * dirsize;
489 }
490 inode->i_mode = S_IFDIR + 0755;
491 inode->i_uid = getuid();
492 if (inode->i_uid)
493 inode->i_gid = getgid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 write_block(inode->i_zone[0], root_block);
495}
496
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000497#ifdef CONFIG_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000498static void make_root_inode2(void)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499{
500 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
501
502 mark_inode(MINIX_ROOT_INO);
503 inode->i_zone[0] = get_free_block();
504 inode->i_nlinks = 2;
505 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
506 if (badblocks)
507 inode->i_size = 3 * dirsize;
508 else {
509 root_block[2 * dirsize] = '\0';
510 root_block[2 * dirsize + 1] = '\0';
511 inode->i_size = 2 * dirsize;
512 }
513 inode->i_mode = S_IFDIR + 0755;
514 inode->i_uid = getuid();
515 if (inode->i_uid)
516 inode->i_gid = getgid();
517 write_block(inode->i_zone[0], root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000518}
519#endif
520
Rob Landley88621d72006-08-29 19:41:06 +0000521static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000522{
523 int i;
524 unsigned long inodes;
525
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 memset(super_block_buffer, 0, BLOCK_SIZE);
527 memset(boot_block_buffer, 0, 512);
Eric Andersene494fdd1999-10-19 20:23:03 +0000528 MAGIC = magic;
529 ZONESIZE = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
Eric Andersenb225e2a2004-08-28 00:43:07 +0000531#ifdef CONFIG_FEATURE_MINIX2
532 if (version2) {
533 Super.s_zones = BLOCKS;
534 } else
535#endif
536 Super.s_nzones = BLOCKS;
537
Eric Andersene494fdd1999-10-19 20:23:03 +0000538/* some magic nrs: 1 inode / 3 blocks */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 if (req_nr_inodes == 0)
540 inodes = BLOCKS / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000541 else
542 inodes = req_nr_inodes;
543 /* Round up inode count to fill block size */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000544#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000545 if (version2)
546 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 ~(MINIX2_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000548 else
549#endif
550 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 ~(MINIX_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000552 if (inodes > 65535)
553 inodes = 65535;
554 INODES = inodes;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000556 ZMAPS = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 i = 0;
558 while (ZMAPS !=
559 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
560 BITS_PER_BLOCK) && i < 1000) {
561 ZMAPS =
562 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
563 BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000564 i++;
565 }
566 /* Real bad hack but overwise mkfs.minix can be thrown
567 * in infinite loop...
568 * try:
569 * dd if=/dev/zero of=test.fs count=10 bs=1024
570 * /sbin/mkfs.minix -i 200 test.fs
571 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572 if (i >= 999) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000573 bb_error_msg_and_die("unable to allocate buffers for maps");
Eric Andersene494fdd1999-10-19 20:23:03 +0000574 }
575 FIRSTZONE = NORM_FIRSTZONE;
Erik Andersen0d068a22000-03-21 22:32:57 +0000576 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
577 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
579 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
580 for (i = FIRSTZONE; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000581 unmark_zone(i);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 for (i = MINIX_ROOT_INO; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000583 unmark_inode(i);
Erik Andersen0d068a22000-03-21 22:32:57 +0000584 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
Eric Andersenb225e2a2004-08-28 00:43:07 +0000586 printf("%ld inodes\n", (long)INODES);
587 printf("%ld blocks\n", (long)ZONES);
588 printf("Firstdatazone=%ld (%ld)\n", (long)FIRSTZONE, (long)NORM_FIRSTZONE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
Eric Andersenb225e2a2004-08-28 00:43:07 +0000590 printf("Maxsize=%ld\n\n", (long)MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000591}
592
593/*
594 * Perform a test of a block; return the number of
Eric Andersenaff114c2004-04-14 17:51:38 +0000595 * blocks readable/writable.
Eric Andersene494fdd1999-10-19 20:23:03 +0000596 */
Rob Landley88621d72006-08-29 19:41:06 +0000597static long do_check(char *buffer, int try, unsigned int current_block)
Eric Andersene494fdd1999-10-19 20:23:03 +0000598{
599 long got;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600
Eric Andersene494fdd1999-10-19 20:23:03 +0000601 /* Seek to the correct loc. */
602 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 current_block * BLOCK_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000604 bb_error_msg_and_die("seek failed during testing of blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000605 }
606
607
608 /* Try the read */
609 got = read(DEV, buffer, try * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000610 if (got < 0)
611 got = 0;
612 if (got & (BLOCK_SIZE - 1)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000613 printf("Weird values in do_check: probably bugs\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000614 }
615 got /= BLOCK_SIZE;
616 return got;
617}
618
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000619static unsigned int currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000620
Eric Andersene674eb71999-10-19 20:52:57 +0000621static void alarm_intr(int alnum)
Eric Andersene494fdd1999-10-19 20:23:03 +0000622{
623 if (currently_testing >= ZONES)
624 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000626 alarm(5);
627 if (!currently_testing)
628 return;
629 printf("%d ...", currently_testing);
630 fflush(stdout);
631}
632
Eric Andersene674eb71999-10-19 20:52:57 +0000633static void check_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000634{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 int try, got;
Eric Andersene494fdd1999-10-19 20:23:03 +0000636 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
637
Erik Andersene49d5ec2000-02-08 19:58:47 +0000638 currently_testing = 0;
639 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000640 alarm(5);
641 while (currently_testing < ZONES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000642 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
643 currently_testing * BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000644 bb_error_msg_and_die("seek failed in check_blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000645 try = TEST_BUFFER_BLOCKS;
646 if (currently_testing + try > ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 try = ZONES - currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000648 got = do_check(buffer, try, currently_testing);
649 currently_testing += got;
650 if (got == try)
651 continue;
652 if (currently_testing < FIRSTZONE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000653 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
Eric Andersene494fdd1999-10-19 20:23:03 +0000654 mark_zone(currently_testing);
655 badblocks++;
656 currently_testing++;
657 }
658 if (badblocks > 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000659 printf("%d bad blocks\n", badblocks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000660 else if (badblocks == 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000661 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000662}
663
Eric Anderseneaecbf32001-10-31 10:41:31 +0000664static void get_list_blocks(char *filename)
Eric Andersene494fdd1999-10-19 20:23:03 +0000665{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 FILE *listfile;
667 unsigned long blockno;
Eric Andersene494fdd1999-10-19 20:23:03 +0000668
Rob Landleyd921b2e2006-08-03 15:41:12 +0000669 listfile = xfopen(filename, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670 while (!feof(listfile)) {
671 fscanf(listfile, "%ld\n", &blockno);
672 mark_zone(blockno);
673 badblocks++;
674 }
675 if (badblocks > 1)
676 printf("%d bad blocks\n", badblocks);
677 else if (badblocks == 1)
678 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000679}
680
Rob Landleydfba7412006-03-06 20:47:33 +0000681int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000682{
Eric Andersen32322032000-06-06 22:12:35 +0000683 int i=1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000684 char *tmp;
685 struct stat statbuf;
686 char *listfile = NULL;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000687 int stopIt=FALSE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000688
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000690 bb_error_msg_and_die("bad inode size");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000691#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000693 bb_error_msg_and_die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000694#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000695
Erik Andersen330fd2b2000-05-19 05:35:19 +0000696 /* Parse options */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000697 argv++;
698 while (--argc >= 0 && *argv && **argv) {
699 if (**argv == '-') {
700 stopIt=FALSE;
701 while (i > 0 && *++(*argv) && stopIt==FALSE) {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000702 switch (**argv) {
703 case 'c':
704 check = 1;
705 break;
706 case 'i':
Eric Andersen32322032000-06-06 22:12:35 +0000707 {
708 char *cp=NULL;
709 if (*(*argv+1) != 0) {
710 cp = ++(*argv);
711 } else {
712 if (--argc == 0) {
713 goto goodbye;
714 }
715 cp = *(++argv);
716 }
717 req_nr_inodes = strtoul(cp, &tmp, 0);
718 if (*tmp)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000719 bb_show_usage();
Eric Andersen32322032000-06-06 22:12:35 +0000720 stopIt=TRUE;
721 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000722 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000723 case 'l':
724 if (--argc == 0) {
725 goto goodbye;
726 }
727 listfile = *(++argv);
728 break;
729 case 'n':
730 {
731 char *cp=NULL;
732
Erik Andersen330fd2b2000-05-19 05:35:19 +0000733 if (*(*argv+1) != 0) {
734 cp = ++(*argv);
735 } else {
Eric Andersen32322032000-06-06 22:12:35 +0000736 if (--argc == 0) {
737 goto goodbye;
738 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000739 cp = *(++argv);
740 }
741 i = strtoul(cp, &tmp, 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000742 if (*tmp)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000743 bb_show_usage();
Erik Andersen330fd2b2000-05-19 05:35:19 +0000744 if (i == 14)
745 magic = MINIX_SUPER_MAGIC;
746 else if (i == 30)
747 magic = MINIX_SUPER_MAGIC2;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000748 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000749 bb_show_usage();
Erik Andersen330fd2b2000-05-19 05:35:19 +0000750 namelen = i;
751 dirsize = i + 2;
752 stopIt=TRUE;
753 break;
754 }
755 case 'v':
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000756#ifdef CONFIG_FEATURE_MINIX2
Erik Andersen330fd2b2000-05-19 05:35:19 +0000757 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000758#else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000759 bb_error_msg("%s: not compiled with minix v2 support",
Matt Kraaid537a952000-07-14 01:51:25 +0000760 device_name);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000761 exit(-1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000762#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000763 break;
764 case '-':
765 case 'h':
766 default:
767goodbye:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000768 bb_show_usage();
Erik Andersen330fd2b2000-05-19 05:35:19 +0000769 }
770 }
771 } else {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000772 if (device_name == NULL)
773 device_name = *argv;
774 else if (BLOCKS == 0)
775 BLOCKS = strtol(*argv, &tmp, 0);
776 else {
777 goto goodbye;
778 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000780 argv++;
781 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000782
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 if (device_name && !BLOCKS)
784 BLOCKS = get_size(device_name) / 1024;
785 if (!device_name || BLOCKS < 10) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000786 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000788#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 if (version2) {
790 if (namelen == 14)
791 magic = MINIX2_SUPER_MAGIC;
792 else
793 magic = MINIX2_SUPER_MAGIC2;
794 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000795#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 if (BLOCKS > 65535)
797 BLOCKS = 65535;
798 check_mount(); /* is it already mounted? */
799 tmp = root_block;
800 *(short *) tmp = 1;
801 strcpy(tmp + 2, ".");
802 tmp += dirsize;
803 *(short *) tmp = 1;
804 strcpy(tmp + 2, "..");
805 tmp += dirsize;
806 *(short *) tmp = 2;
807 strcpy(tmp + 2, ".badblocks");
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000808 DEV = xopen(device_name, O_RDWR);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 if (fstat(DEV, &statbuf) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000810 bb_error_msg_and_die("unable to stat %s", device_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 if (!S_ISBLK(statbuf.st_mode))
812 check = 0;
813 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000814 bb_error_msg_and_die("will not try to make filesystem on '%s'", device_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 setup_tables();
816 if (check)
817 check_blocks();
818 else if (listfile)
819 get_list_blocks(listfile);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000820#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821 if (version2) {
822 make_root_inode2();
823 make_bad_inode2();
824 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000825#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 {
827 make_root_inode();
828 make_bad_inode();
829 }
830 mark_good_blocks();
831 write_tables();
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000832 return 0;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000833
Eric Andersene494fdd1999-10-19 20:23:03 +0000834}