blob: d9388b1d7ef05cd37661a9783d2111079d2400d8 [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
65#include <stdio.h>
66#include <time.h>
67#include <unistd.h>
68#include <string.h>
69#include <signal.h>
70#include <fcntl.h>
71#include <ctype.h>
72#include <stdlib.h>
Eric Andersen39cdf4e2004-01-30 22:40:05 +000073#include <stdint.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000074#include <termios.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000075#include <sys/ioctl.h>
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000076#include <sys/param.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000077#include <mntent.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000078#include "busybox.h"
Eric Andersene494fdd1999-10-19 20:23:03 +000079
Eric Andersen85c55202000-07-10 22:46:55 +000080#define MINIX_ROOT_INO 1
81#define MINIX_LINK_MAX 250
82#define MINIX2_LINK_MAX 65530
83
84#define MINIX_I_MAP_SLOTS 8
85#define MINIX_Z_MAP_SLOTS 64
86#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
87#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
88#define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
89#define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
90#define MINIX_VALID_FS 0x0001 /* Clean fs. */
91#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
92
93#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
94#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
95
96#define MINIX_V1 0x0001 /* original minix fs */
97#define MINIX_V2 0x0002 /* minix V2 fs */
98
99#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
100
101/*
102 * This is the original minix inode layout on disk.
103 * Note the 8-bit gid and atime and ctime.
104 */
105struct minix_inode {
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000106 uint16_t i_mode;
107 uint16_t i_uid;
108 uint32_t i_size;
109 uint32_t i_time;
110 uint8_t i_gid;
111 uint8_t i_nlinks;
112 uint16_t i_zone[9];
Eric Andersen85c55202000-07-10 22:46:55 +0000113};
114
115/*
116 * The new minix inode has all the time entries, as well as
117 * long block numbers and a third indirect block (7+1+1+1
118 * instead of 7+1+1). Also, some previously 8-bit values are
119 * now 16-bit. The inode is now 64 bytes instead of 32.
120 */
121struct minix2_inode {
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000122 uint16_t i_mode;
123 uint16_t i_nlinks;
124 uint16_t i_uid;
125 uint16_t i_gid;
126 uint32_t i_size;
127 uint32_t i_atime;
128 uint32_t i_mtime;
129 uint32_t i_ctime;
130 uint32_t i_zone[10];
Eric Andersen85c55202000-07-10 22:46:55 +0000131};
132
133/*
134 * minix super-block data on disk
135 */
136struct minix_super_block {
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000137 uint16_t s_ninodes;
138 uint16_t s_nzones;
139 uint16_t s_imap_blocks;
140 uint16_t s_zmap_blocks;
141 uint16_t s_firstdatazone;
142 uint16_t s_log_zone_size;
143 uint32_t s_max_size;
144 uint16_t s_magic;
145 uint16_t s_state;
146 uint32_t s_zones;
Eric Andersen85c55202000-07-10 22:46:55 +0000147};
148
149struct minix_dir_entry {
Eric Andersen39cdf4e2004-01-30 22:40:05 +0000150 uint16_t inode;
Eric Andersen85c55202000-07-10 22:46:55 +0000151 char name[0];
152};
153
154#define BLOCK_SIZE_BITS 10
155#define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
156
157#define NAME_MAX 255 /* # chars in a file name */
158
159#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
160
161#define MINIX_VALID_FS 0x0001 /* Clean fs. */
162#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
163
164#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
165#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
166
167#ifndef BLKGETSIZE
168#define BLKGETSIZE _IO(0x12,96) /* return device size */
169#endif
170
Eric Andersene494fdd1999-10-19 20:23:03 +0000171
Eric Andersene494fdd1999-10-19 20:23:03 +0000172#ifndef __linux__
173#define volatile
174#endif
175
176#define MINIX_ROOT_INO 1
177#define MINIX_BAD_INO 2
178
179#define TEST_BUFFER_BLOCKS 16
180#define MAX_GOOD_BLOCKS 512
181
182#define UPPER(size,n) (((size)+((n)-1))/(n))
183#define INODE_SIZE (sizeof(struct minix_inode))
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000184#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000185#define INODE_SIZE2 (sizeof(struct minix2_inode))
186#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
187 : MINIX_INODES_PER_BLOCK))
188#else
189#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
190#endif
191#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
192
193#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
194
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000195static char *device_name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000196static int DEV = -1;
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000197static uint32_t BLOCKS;
198static int check;
199static int badblocks;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200static int namelen = 30; /* default (changed to 30, per Linus's
201
202 suggestion, Sun Nov 21 08:05:07 1993) */
Eric Andersene494fdd1999-10-19 20:23:03 +0000203static int dirsize = 32;
204static int magic = MINIX_SUPER_MAGIC2;
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000205static int version2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000206
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000207static char root_block[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +0000208
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000209static char *inode_buffer;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210
Eric Andersene494fdd1999-10-19 20:23:03 +0000211#define Inode (((struct minix_inode *) inode_buffer)-1)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000212#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000213#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
214#endif
215static char super_block_buffer[BLOCK_SIZE];
216static char boot_block_buffer[512];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217
Eric Andersene494fdd1999-10-19 20:23:03 +0000218#define Super (*(struct minix_super_block *)super_block_buffer)
Eric Andersenb225e2a2004-08-28 00:43:07 +0000219#define INODES (Super.s_ninodes)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000220#ifdef CONFIG_FEATURE_MINIX2
Eric Andersenb225e2a2004-08-28 00:43:07 +0000221#define ZONES (version2 ? Super.s_zones : Super.s_nzones)
Eric Andersene494fdd1999-10-19 20:23:03 +0000222#else
Eric Andersenb225e2a2004-08-28 00:43:07 +0000223#define ZONES (Super.s_nzones)
Eric Andersene494fdd1999-10-19 20:23:03 +0000224#endif
Eric Andersenb225e2a2004-08-28 00:43:07 +0000225#define IMAPS (Super.s_imap_blocks)
226#define ZMAPS (Super.s_zmap_blocks)
227#define FIRSTZONE (Super.s_firstdatazone)
228#define ZONESIZE (Super.s_log_zone_size)
229#define MAXSIZE (Super.s_max_size)
Eric Andersene494fdd1999-10-19 20:23:03 +0000230#define MAGIC (Super.s_magic)
231#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
232
233static char *inode_map;
234static char *zone_map;
235
236static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000237static int used_good_blocks;
238static unsigned long req_nr_inodes;
Eric Andersene494fdd1999-10-19 20:23:03 +0000239
Eric Andersen56f3e352000-09-19 21:13:55 +0000240static inline int bit(char * a,unsigned int i)
241{
242 return (a[i >> 3] & (1<<(i & 7))) != 0;
243}
244#define inode_in_use(x) (bit(inode_map,(x)))
245#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000246
247#define mark_inode(x) (setbit(inode_map,(x)))
248#define unmark_inode(x) (clrbit(inode_map,(x)))
249
250#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
251#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
252
Eric Andersene494fdd1999-10-19 20:23:03 +0000253/*
254 * Check to make certain that our new filesystem won't be created on
255 * an already mounted partition. Code adapted from mke2fs, Copyright
256 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
257 */
Eric Andersenc774efe2003-08-06 07:34:19 +0000258static inline void check_mount(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000259{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260 FILE *f;
261 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000262
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000264 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 while ((mnt = getmntent(f)) != NULL)
266 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000267 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000269 if (!mnt)
270 return;
271
Manuel Novoa III cad53642003-03-19 09:13:01 +0000272 bb_error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000273}
274
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000276{
277 char ch;
278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 if (lseek(fd, offset, 0) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000280 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000282 return 0;
283 return 1;
284}
285
Eric Andersenc774efe2003-08-06 07:34:19 +0000286static inline int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000287{
288 int high, low;
289
290 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000292 low = high;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000294 const int mid = (low + high) / 2;
295
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000297 low = mid;
298 else
299 high = mid;
300 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000302 return (low + 1);
303}
304
Eric Andersenc774efe2003-08-06 07:34:19 +0000305static inline int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000306{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 int fd;
308 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000309
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000310 fd = bb_xopen3(file, O_RDWR, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000311 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
312 close(fd);
313 return (size * 512);
314 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315
Eric Andersene494fdd1999-10-19 20:23:03 +0000316 size = count_blocks(fd);
317 close(fd);
318 return size;
319}
320
Eric Andersenc774efe2003-08-06 07:34:19 +0000321static inline void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000322{
323 /* Mark the super block valid. */
324 Super.s_state |= MINIX_VALID_FS;
325 Super.s_state &= ~MINIX_ERROR_FS;
326
327 if (lseek(DEV, 0, SEEK_SET))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000328 bb_error_msg_and_die("seek to boot block failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000329 if (512 != write(DEV, boot_block_buffer, 512))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000330 bb_error_msg_and_die("unable to clear boot sector");
Eric Andersene494fdd1999-10-19 20:23:03 +0000331 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000332 bb_error_msg_and_die("seek failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000333 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000334 bb_error_msg_and_die("unable to write super-block");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000336 bb_error_msg_and_die("unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000338 bb_error_msg_and_die("unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000340 bb_error_msg_and_die("unable to write inodes");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341
Eric Andersene494fdd1999-10-19 20:23:03 +0000342}
343
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000345{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000347 bb_error_msg_and_die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000348 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000349 bb_error_msg_and_die("write failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000350}
351
Eric Andersene674eb71999-10-19 20:52:57 +0000352static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000353{
354 int blk;
355
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000357 bb_error_msg_and_die("too many bad blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000358 if (used_good_blocks)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 blk = good_blocks_table[used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000360 else
361 blk = FIRSTZONE;
362 while (blk < ZONES && zone_in_use(blk))
363 blk++;
364 if (blk >= ZONES)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000365 bb_error_msg_and_die("not enough good blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000366 good_blocks_table[used_good_blocks] = blk;
367 used_good_blocks++;
368 return blk;
369}
370
Eric Andersenc774efe2003-08-06 07:34:19 +0000371static inline void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000372{
373 int blk;
374
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 for (blk = 0; blk < used_good_blocks; blk++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000376 mark_zone(good_blocks_table[blk]);
377}
378
Eric Andersen92d23242001-03-19 23:49:41 +0000379static int next(int zone)
Eric Andersene494fdd1999-10-19 20:23:03 +0000380{
381 if (!zone)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 zone = FIRSTZONE - 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000383 while (++zone < ZONES)
384 if (zone_in_use(zone))
385 return zone;
386 return 0;
387}
388
Eric Andersenc774efe2003-08-06 07:34:19 +0000389static inline void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000390{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000391 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
392 int i, j, zone;
393 int ind = 0, dind = 0;
394 unsigned short ind_block[BLOCK_SIZE >> 1];
395 unsigned short dind_block[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000396
397#define NEXT_BAD (zone = next(zone))
398
399 if (!badblocks)
400 return;
401 mark_inode(MINIX_BAD_INO);
402 inode->i_nlinks = 1;
403 inode->i_time = time(NULL);
404 inode->i_mode = S_IFREG + 0000;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 inode->i_size = badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000406 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000408 inode->i_zone[i] = zone;
409 if (!NEXT_BAD)
410 goto end_bad;
411 }
412 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 memset(ind_block, 0, BLOCK_SIZE);
414 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000415 ind_block[i] = zone;
416 if (!NEXT_BAD)
417 goto end_bad;
418 }
419 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 memset(dind_block, 0, BLOCK_SIZE);
421 for (i = 0; i < 512; i++) {
422 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000423 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 memset(ind_block, 0, BLOCK_SIZE);
425 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000426 ind_block[j] = zone;
427 if (!NEXT_BAD)
428 goto end_bad;
429 }
430 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000431 bb_error_msg_and_die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000433 if (ind)
434 write_block(ind, (char *) ind_block);
435 if (dind)
436 write_block(dind, (char *) dind_block);
437}
438
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000439#ifdef CONFIG_FEATURE_MINIX2
Eric Andersenc774efe2003-08-06 07:34:19 +0000440static inline void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000441{
442 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
443 int i, j, zone;
444 int ind = 0, dind = 0;
445 unsigned long ind_block[BLOCK_SIZE >> 2];
446 unsigned long dind_block[BLOCK_SIZE >> 2];
447
448 if (!badblocks)
449 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000451 inode->i_nlinks = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
Eric Andersene494fdd1999-10-19 20:23:03 +0000453 inode->i_mode = S_IFREG + 0000;
454 inode->i_size = badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000456 for (i = 0; i < 7; i++) {
457 inode->i_zone[i] = zone;
458 if (!NEXT_BAD)
459 goto end_bad;
460 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 inode->i_zone[7] = ind = get_free_block();
462 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000463 for (i = 0; i < 256; i++) {
464 ind_block[i] = zone;
465 if (!NEXT_BAD)
466 goto end_bad;
467 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 inode->i_zone[8] = dind = get_free_block();
469 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000470 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 write_block(ind, (char *) ind_block);
472 dind_block[i] = ind = get_free_block();
473 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000474 for (j = 0; j < 256; j++) {
475 ind_block[j] = zone;
476 if (!NEXT_BAD)
477 goto end_bad;
478 }
479 }
480 /* Could make triple indirect block here */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000481 bb_error_msg_and_die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000483 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000485 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 write_block(dind, (char *) dind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000487}
488#endif
489
Eric Andersenc774efe2003-08-06 07:34:19 +0000490static inline void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000491{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000493
494 mark_inode(MINIX_ROOT_INO);
495 inode->i_zone[0] = get_free_block();
496 inode->i_nlinks = 2;
497 inode->i_time = time(NULL);
498 if (badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000499 inode->i_size = 3 * dirsize;
500 else {
501 root_block[2 * dirsize] = '\0';
502 root_block[2 * dirsize + 1] = '\0';
503 inode->i_size = 2 * dirsize;
504 }
505 inode->i_mode = S_IFDIR + 0755;
506 inode->i_uid = getuid();
507 if (inode->i_uid)
508 inode->i_gid = getgid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 write_block(inode->i_zone[0], root_block);
510}
511
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000512#ifdef CONFIG_FEATURE_MINIX2
Eric Andersenc774efe2003-08-06 07:34:19 +0000513static inline void make_root_inode2(void)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514{
515 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
516
517 mark_inode(MINIX_ROOT_INO);
518 inode->i_zone[0] = get_free_block();
519 inode->i_nlinks = 2;
520 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
521 if (badblocks)
522 inode->i_size = 3 * dirsize;
523 else {
524 root_block[2 * dirsize] = '\0';
525 root_block[2 * dirsize + 1] = '\0';
526 inode->i_size = 2 * dirsize;
527 }
528 inode->i_mode = S_IFDIR + 0755;
529 inode->i_uid = getuid();
530 if (inode->i_uid)
531 inode->i_gid = getgid();
532 write_block(inode->i_zone[0], root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000533}
534#endif
535
Eric Andersenc774efe2003-08-06 07:34:19 +0000536static inline void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000537{
538 int i;
539 unsigned long inodes;
540
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 memset(super_block_buffer, 0, BLOCK_SIZE);
542 memset(boot_block_buffer, 0, 512);
Eric Andersene494fdd1999-10-19 20:23:03 +0000543 MAGIC = magic;
544 ZONESIZE = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
Eric Andersenb225e2a2004-08-28 00:43:07 +0000546#ifdef CONFIG_FEATURE_MINIX2
547 if (version2) {
548 Super.s_zones = BLOCKS;
549 } else
550#endif
551 Super.s_nzones = BLOCKS;
552
Eric Andersene494fdd1999-10-19 20:23:03 +0000553/* some magic nrs: 1 inode / 3 blocks */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 if (req_nr_inodes == 0)
555 inodes = BLOCKS / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000556 else
557 inodes = req_nr_inodes;
558 /* Round up inode count to fill block size */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000559#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 if (version2)
561 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 ~(MINIX2_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000563 else
564#endif
565 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000566 ~(MINIX_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000567 if (inodes > 65535)
568 inodes = 65535;
569 INODES = inodes;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000571 ZMAPS = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572 i = 0;
573 while (ZMAPS !=
574 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
575 BITS_PER_BLOCK) && i < 1000) {
576 ZMAPS =
577 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
578 BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000579 i++;
580 }
581 /* Real bad hack but overwise mkfs.minix can be thrown
582 * in infinite loop...
583 * try:
584 * dd if=/dev/zero of=test.fs count=10 bs=1024
585 * /sbin/mkfs.minix -i 200 test.fs
586 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000587 if (i >= 999) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000588 bb_error_msg_and_die("unable to allocate buffers for maps");
Eric Andersene494fdd1999-10-19 20:23:03 +0000589 }
590 FIRSTZONE = NORM_FIRSTZONE;
Erik Andersen0d068a22000-03-21 22:32:57 +0000591 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
592 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
594 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
595 for (i = FIRSTZONE; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000596 unmark_zone(i);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 for (i = MINIX_ROOT_INO; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000598 unmark_inode(i);
Erik Andersen0d068a22000-03-21 22:32:57 +0000599 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
Eric Andersenb225e2a2004-08-28 00:43:07 +0000601 printf("%ld inodes\n", (long)INODES);
602 printf("%ld blocks\n", (long)ZONES);
603 printf("Firstdatazone=%ld (%ld)\n", (long)FIRSTZONE, (long)NORM_FIRSTZONE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
Eric Andersenb225e2a2004-08-28 00:43:07 +0000605 printf("Maxsize=%ld\n\n", (long)MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000606}
607
608/*
609 * Perform a test of a block; return the number of
Eric Andersenaff114c2004-04-14 17:51:38 +0000610 * blocks readable/writable.
Eric Andersene494fdd1999-10-19 20:23:03 +0000611 */
Eric Andersenc774efe2003-08-06 07:34:19 +0000612static inline long do_check(char *buffer, int try, unsigned int current_block)
Eric Andersene494fdd1999-10-19 20:23:03 +0000613{
614 long got;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615
Eric Andersene494fdd1999-10-19 20:23:03 +0000616 /* Seek to the correct loc. */
617 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 current_block * BLOCK_SIZE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000619 bb_error_msg_and_die("seek failed during testing of blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000620 }
621
622
623 /* Try the read */
624 got = read(DEV, buffer, try * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 if (got < 0)
626 got = 0;
627 if (got & (BLOCK_SIZE - 1)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000628 printf("Weird values in do_check: probably bugs\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000629 }
630 got /= BLOCK_SIZE;
631 return got;
632}
633
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000634static unsigned int currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000635
Eric Andersene674eb71999-10-19 20:52:57 +0000636static void alarm_intr(int alnum)
Eric Andersene494fdd1999-10-19 20:23:03 +0000637{
638 if (currently_testing >= ZONES)
639 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000641 alarm(5);
642 if (!currently_testing)
643 return;
644 printf("%d ...", currently_testing);
645 fflush(stdout);
646}
647
Eric Andersene674eb71999-10-19 20:52:57 +0000648static void check_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000649{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000650 int try, got;
Eric Andersene494fdd1999-10-19 20:23:03 +0000651 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
652
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 currently_testing = 0;
654 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000655 alarm(5);
656 while (currently_testing < ZONES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
658 currently_testing * BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000659 bb_error_msg_and_die("seek failed in check_blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000660 try = TEST_BUFFER_BLOCKS;
661 if (currently_testing + try > ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 try = ZONES - currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000663 got = do_check(buffer, try, currently_testing);
664 currently_testing += got;
665 if (got == try)
666 continue;
667 if (currently_testing < FIRSTZONE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000668 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
Eric Andersene494fdd1999-10-19 20:23:03 +0000669 mark_zone(currently_testing);
670 badblocks++;
671 currently_testing++;
672 }
673 if (badblocks > 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000674 printf("%d bad blocks\n", badblocks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000675 else if (badblocks == 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000676 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000677}
678
Eric Anderseneaecbf32001-10-31 10:41:31 +0000679static void get_list_blocks(char *filename)
Eric Andersene494fdd1999-10-19 20:23:03 +0000680{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000681 FILE *listfile;
682 unsigned long blockno;
Eric Andersene494fdd1999-10-19 20:23:03 +0000683
Manuel Novoa III cad53642003-03-19 09:13:01 +0000684 listfile = bb_xfopen(filename, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 while (!feof(listfile)) {
686 fscanf(listfile, "%ld\n", &blockno);
687 mark_zone(blockno);
688 badblocks++;
689 }
690 if (badblocks > 1)
691 printf("%d bad blocks\n", badblocks);
692 else if (badblocks == 1)
693 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000694}
695
Rob Landleydfba7412006-03-06 20:47:33 +0000696int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000697{
Eric Andersen32322032000-06-06 22:12:35 +0000698 int i=1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 char *tmp;
700 struct stat statbuf;
701 char *listfile = NULL;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000702 int stopIt=FALSE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000703
Erik Andersene49d5ec2000-02-08 19:58:47 +0000704 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000705 bb_error_msg_and_die("bad inode size");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000706#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000708 bb_error_msg_and_die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000709#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000710
Erik Andersen330fd2b2000-05-19 05:35:19 +0000711 /* Parse options */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000712 argv++;
713 while (--argc >= 0 && *argv && **argv) {
714 if (**argv == '-') {
715 stopIt=FALSE;
716 while (i > 0 && *++(*argv) && stopIt==FALSE) {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000717 switch (**argv) {
718 case 'c':
719 check = 1;
720 break;
721 case 'i':
Eric Andersen32322032000-06-06 22:12:35 +0000722 {
723 char *cp=NULL;
724 if (*(*argv+1) != 0) {
725 cp = ++(*argv);
726 } else {
727 if (--argc == 0) {
728 goto goodbye;
729 }
730 cp = *(++argv);
731 }
732 req_nr_inodes = strtoul(cp, &tmp, 0);
733 if (*tmp)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000734 bb_show_usage();
Eric Andersen32322032000-06-06 22:12:35 +0000735 stopIt=TRUE;
736 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000737 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000738 case 'l':
739 if (--argc == 0) {
740 goto goodbye;
741 }
742 listfile = *(++argv);
743 break;
744 case 'n':
745 {
746 char *cp=NULL;
747
Erik Andersen330fd2b2000-05-19 05:35:19 +0000748 if (*(*argv+1) != 0) {
749 cp = ++(*argv);
750 } else {
Eric Andersen32322032000-06-06 22:12:35 +0000751 if (--argc == 0) {
752 goto goodbye;
753 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000754 cp = *(++argv);
755 }
756 i = strtoul(cp, &tmp, 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000757 if (*tmp)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000758 bb_show_usage();
Erik Andersen330fd2b2000-05-19 05:35:19 +0000759 if (i == 14)
760 magic = MINIX_SUPER_MAGIC;
761 else if (i == 30)
762 magic = MINIX_SUPER_MAGIC2;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000763 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000764 bb_show_usage();
Erik Andersen330fd2b2000-05-19 05:35:19 +0000765 namelen = i;
766 dirsize = i + 2;
767 stopIt=TRUE;
768 break;
769 }
770 case 'v':
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000771#ifdef CONFIG_FEATURE_MINIX2
Erik Andersen330fd2b2000-05-19 05:35:19 +0000772 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000773#else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000774 bb_error_msg("%s: not compiled with minix v2 support",
Matt Kraaid537a952000-07-14 01:51:25 +0000775 device_name);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000776 exit(-1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000777#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000778 break;
779 case '-':
780 case 'h':
781 default:
782goodbye:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000783 bb_show_usage();
Erik Andersen330fd2b2000-05-19 05:35:19 +0000784 }
785 }
786 } else {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000787 if (device_name == NULL)
788 device_name = *argv;
789 else if (BLOCKS == 0)
790 BLOCKS = strtol(*argv, &tmp, 0);
791 else {
792 goto goodbye;
793 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000795 argv++;
796 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000797
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 if (device_name && !BLOCKS)
799 BLOCKS = get_size(device_name) / 1024;
800 if (!device_name || BLOCKS < 10) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000801 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000802 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000803#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 if (version2) {
805 if (namelen == 14)
806 magic = MINIX2_SUPER_MAGIC;
807 else
808 magic = MINIX2_SUPER_MAGIC2;
809 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000810#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 if (BLOCKS > 65535)
812 BLOCKS = 65535;
813 check_mount(); /* is it already mounted? */
814 tmp = root_block;
815 *(short *) tmp = 1;
816 strcpy(tmp + 2, ".");
817 tmp += dirsize;
818 *(short *) tmp = 1;
819 strcpy(tmp + 2, "..");
820 tmp += dirsize;
821 *(short *) tmp = 2;
822 strcpy(tmp + 2, ".badblocks");
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000823 DEV = bb_xopen3(device_name, O_RDWR, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 if (fstat(DEV, &statbuf) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000825 bb_error_msg_and_die("unable to stat %s", device_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 if (!S_ISBLK(statbuf.st_mode))
827 check = 0;
828 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000829 bb_error_msg_and_die("will not try to make filesystem on '%s'", device_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 setup_tables();
831 if (check)
832 check_blocks();
833 else if (listfile)
834 get_list_blocks(listfile);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000835#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836 if (version2) {
837 make_root_inode2();
838 make_bad_inode2();
839 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000840#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 {
842 make_root_inode();
843 make_bad_inode();
844 }
845 mark_good_blocks();
846 write_tables();
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000847 return 0;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000848
Eric Andersene494fdd1999-10-19 20:23:03 +0000849}