blob: d70d20fda5cba7b9d99ae2e5fcbae209f4150bda [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
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
Denis Vlasenkoaa959592007-01-03 00:43:19 +000068#include "minix.h"
69
Denis Vlasenko66b86c62006-11-29 22:33:47 +000070#define DEBUG 0
Eric Andersen85c55202000-07-10 22:46:55 +000071
Denis Vlasenko66b86c62006-11-29 22:33:47 +000072/* If debugging, store the very same times/uids/gids for image consistency */
73#if DEBUG
74# define CUR_TIME 0
75# define GETUID 0
76# define GETGID 0
77#else
78# define CUR_TIME time(NULL)
79# define GETUID getuid()
80# define GETGID getgid()
81#endif
Eric Andersen85c55202000-07-10 22:46:55 +000082
Denis Vlasenko66b86c62006-11-29 22:33:47 +000083enum {
Denis Vlasenko66b86c62006-11-29 22:33:47 +000084 MAX_GOOD_BLOCKS = 512,
Denis Vlasenko66b86c62006-11-29 22:33:47 +000085 TEST_BUFFER_BLOCKS = 16,
86};
Eric Andersen85c55202000-07-10 22:46:55 +000087
Denis Vlasenko4efeaee2007-03-15 19:52:42 +000088#if !ENABLE_FEATURE_MINIX2
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000089enum { version2 = 0 };
90#endif
Eric Andersene494fdd1999-10-19 20:23:03 +000091
Denis Vlasenko4efeaee2007-03-15 19:52:42 +000092struct globals {
Denis Vlasenko66b86c62006-11-29 22:33:47 +000093
Denis Vlasenko4efeaee2007-03-15 19:52:42 +000094 int dev_fd;
Eric Andersene494fdd1999-10-19 20:23:03 +000095
Denis Vlasenko4efeaee2007-03-15 19:52:42 +000096#if ENABLE_FEATURE_MINIX2
97 int version2;
98#define version2 G.version2
99#endif
100 char *device_name;
101 uint32_t total_blocks;
102 int badblocks;
103 int namelen;
104 int dirsize;
105 int magic;
106 char *inode_buffer;
107 char *inode_map;
108 char *zone_map;
109 int used_good_blocks;
110 unsigned long req_nr_inodes;
111 unsigned currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000112
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000113
114 char root_block[BLOCK_SIZE];
115 char super_block_buffer[BLOCK_SIZE];
116 char boot_block_buffer[512];
117 unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
Denis Vlasenko83bb98f2007-03-18 14:43:21 +0000118 /* check_blocks(): buffer[] was the biggest static in entire bbox */
119 char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000120};
121
122#define G (*ptr_to_globals)
Eric Andersene494fdd1999-10-19 20:23:03 +0000123
Denis Vlasenkoa597aad2006-12-16 23:48:13 +0000124static ATTRIBUTE_ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
Eric Andersen56f3e352000-09-19 21:13:55 +0000125{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000126 return (size + n-1) / n;
Eric Andersen56f3e352000-09-19 21:13:55 +0000127}
Eric Andersene494fdd1999-10-19 20:23:03 +0000128
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000129#define INODE_BUF1 (((struct minix1_inode*)G.inode_buffer) - 1)
130#define INODE_BUF2 (((struct minix2_inode*)G.inode_buffer) - 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000131
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000132#define SB (*(struct minix_super_block*)G.super_block_buffer)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000133
134#define SB_INODES (SB.s_ninodes)
135#define SB_IMAPS (SB.s_imap_blocks)
136#define SB_ZMAPS (SB.s_zmap_blocks)
137#define SB_FIRSTZONE (SB.s_firstdatazone)
138#define SB_ZONE_SIZE (SB.s_log_zone_size)
139#define SB_MAXSIZE (SB.s_max_size)
140#define SB_MAGIC (SB.s_magic)
141
142#if !ENABLE_FEATURE_MINIX2
143# define SB_ZONES (SB.s_nzones)
144# define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
145#else
146# define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones)
147# define INODE_BLOCKS div_roundup(SB_INODES, \
148 version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK)
149#endif
150
151#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
152#define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
153
Denis Vlasenkocc244192007-02-06 00:37:40 +0000154/* Before you ask "where they come from?": */
155/* setbit/clrbit are supplied by sys/param.h */
156
157static int minix_bit(const char* a, unsigned i)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000158{
159 return a[i >> 3] & (1<<(i & 7));
160}
161
Denis Vlasenkocc244192007-02-06 00:37:40 +0000162static void minix_setbit(char *a, unsigned i)
163{
164 setbit(a, i);
165}
166static void minix_clrbit(char *a, unsigned i)
167{
168 clrbit(a, i);
169}
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000170
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000171/* Note: do not assume 0/1, it is 0/nonzero */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000172#define zone_in_use(x) minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
173/*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000174
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000175#define mark_inode(x) minix_setbit(G.inode_map,(x))
176#define unmark_inode(x) minix_clrbit(G.inode_map,(x))
177#define mark_zone(x) minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
178#define unmark_zone(x) minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000179
180#ifndef BLKGETSIZE
181# define BLKGETSIZE _IO(0x12,96) /* return device size */
182#endif
183
Eric Andersene494fdd1999-10-19 20:23:03 +0000184
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000186{
187 char ch;
188
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000189 if (lseek(fd, offset, SEEK_SET) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000190 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000192 return 0;
193 return 1;
194}
195
Rob Landley88621d72006-08-29 19:41:06 +0000196static int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000197{
198 int high, low;
199
200 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000201 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000202 low = high;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000203
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000205 const int mid = (low + high) / 2;
206
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000208 low = mid;
209 else
210 high = mid;
211 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000213 return (low + 1);
214}
215
Rob Landley88621d72006-08-29 19:41:06 +0000216static int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000217{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 int fd;
219 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000220
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000221 fd = xopen(file, O_RDWR);
Eric Andersene494fdd1999-10-19 20:23:03 +0000222 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
223 close(fd);
224 return (size * 512);
225 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226
Eric Andersene494fdd1999-10-19 20:23:03 +0000227 size = count_blocks(fd);
228 close(fd);
229 return size;
230}
231
Rob Landley88621d72006-08-29 19:41:06 +0000232static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000233{
234 /* Mark the super block valid. */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000235 SB.s_state |= MINIX_VALID_FS;
236 SB.s_state &= ~MINIX_ERROR_FS;
Eric Andersene494fdd1999-10-19 20:23:03 +0000237
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000238 msg_eol = "seek to 0 failed";
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000239 xlseek(G.dev_fd, 0, SEEK_SET);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000241 msg_eol = "cannot clear boot sector";
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000242 xwrite(G.dev_fd, G.boot_block_buffer, 512);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000243
244 msg_eol = "seek to BLOCK_SIZE failed";
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000245 xlseek(G.dev_fd, BLOCK_SIZE, SEEK_SET);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000246
247 msg_eol = "cannot write superblock";
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000248 xwrite(G.dev_fd, G.super_block_buffer, BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000249
250 msg_eol = "cannot write inode map";
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000251 xwrite(G.dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000252
253 msg_eol = "cannot write zone map";
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000254 xwrite(G.dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000255
256 msg_eol = "cannot write inodes";
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000257 xwrite(G.dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000258
259 msg_eol = "\n";
Eric Andersene494fdd1999-10-19 20:23:03 +0000260}
261
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000263{
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000264 xlseek(G.dev_fd, blk * BLOCK_SIZE, SEEK_SET);
265 xwrite(G.dev_fd, buffer, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000266}
267
Eric Andersene674eb71999-10-19 20:52:57 +0000268static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000269{
270 int blk;
271
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000272 if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000273 bb_error_msg_and_die("too many bad blocks");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000274 if (G.used_good_blocks)
275 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000276 else
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000277 blk = SB_FIRSTZONE;
278 while (blk < SB_ZONES && zone_in_use(blk))
Eric Andersene494fdd1999-10-19 20:23:03 +0000279 blk++;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000280 if (blk >= SB_ZONES)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000281 bb_error_msg_and_die("not enough good blocks");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000282 G.good_blocks_table[G.used_good_blocks] = blk;
283 G.used_good_blocks++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000284 return blk;
285}
286
Rob Landley88621d72006-08-29 19:41:06 +0000287static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000288{
289 int blk;
290
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000291 for (blk = 0; blk < G.used_good_blocks; blk++)
292 mark_zone(G.good_blocks_table[blk]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000293}
294
Eric Andersen92d23242001-03-19 23:49:41 +0000295static int next(int zone)
Eric Andersene494fdd1999-10-19 20:23:03 +0000296{
297 if (!zone)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000298 zone = SB_FIRSTZONE - 1;
299 while (++zone < SB_ZONES)
Eric Andersene494fdd1999-10-19 20:23:03 +0000300 if (zone_in_use(zone))
301 return zone;
302 return 0;
303}
304
Rob Landley88621d72006-08-29 19:41:06 +0000305static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000306{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000307 struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 int i, j, zone;
309 int ind = 0, dind = 0;
310 unsigned short ind_block[BLOCK_SIZE >> 1];
311 unsigned short dind_block[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000312
313#define NEXT_BAD (zone = next(zone))
314
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000315 if (!G.badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000316 return;
317 mark_inode(MINIX_BAD_INO);
318 inode->i_nlinks = 1;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000319 /* BTW, setting this makes all images different */
320 /* it's harder to check for bugs then - diff isn't helpful :(... */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000321 inode->i_time = CUR_TIME;
Eric Andersene494fdd1999-10-19 20:23:03 +0000322 inode->i_mode = S_IFREG + 0000;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000323 inode->i_size = G.badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000324 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000326 inode->i_zone[i] = zone;
327 if (!NEXT_BAD)
328 goto end_bad;
329 }
330 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 memset(ind_block, 0, BLOCK_SIZE);
332 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000333 ind_block[i] = zone;
334 if (!NEXT_BAD)
335 goto end_bad;
336 }
337 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 memset(dind_block, 0, BLOCK_SIZE);
339 for (i = 0; i < 512; i++) {
340 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000341 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342 memset(ind_block, 0, BLOCK_SIZE);
343 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000344 ind_block[j] = zone;
345 if (!NEXT_BAD)
346 goto end_bad;
347 }
348 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000349 bb_error_msg_and_die("too many bad blocks");
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000350 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000351 if (ind)
352 write_block(ind, (char *) ind_block);
353 if (dind)
354 write_block(dind, (char *) dind_block);
355}
356
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000357#if ENABLE_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000358static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000359{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000360 struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 int i, j, zone;
362 int ind = 0, dind = 0;
363 unsigned long ind_block[BLOCK_SIZE >> 2];
364 unsigned long dind_block[BLOCK_SIZE >> 2];
365
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000366 if (!G.badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000367 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000369 inode->i_nlinks = 1;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000370 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
Eric Andersene494fdd1999-10-19 20:23:03 +0000371 inode->i_mode = S_IFREG + 0000;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000372 inode->i_size = G.badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000374 for (i = 0; i < 7; i++) {
375 inode->i_zone[i] = zone;
376 if (!NEXT_BAD)
377 goto end_bad;
378 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 inode->i_zone[7] = ind = get_free_block();
380 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000381 for (i = 0; i < 256; i++) {
382 ind_block[i] = zone;
383 if (!NEXT_BAD)
384 goto end_bad;
385 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 inode->i_zone[8] = dind = get_free_block();
387 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000388 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 write_block(ind, (char *) ind_block);
390 dind_block[i] = ind = get_free_block();
391 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000392 for (j = 0; j < 256; j++) {
393 ind_block[j] = zone;
394 if (!NEXT_BAD)
395 goto end_bad;
396 }
397 }
398 /* Could make triple indirect block here */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000399 bb_error_msg_and_die("too many bad blocks");
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000400 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000401 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000402 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000403 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 write_block(dind, (char *) dind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000405}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000406#else
407void make_bad_inode2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000408#endif
409
Rob Landley88621d72006-08-29 19:41:06 +0000410static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000411{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000412 struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000413
414 mark_inode(MINIX_ROOT_INO);
415 inode->i_zone[0] = get_free_block();
416 inode->i_nlinks = 2;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000417 inode->i_time = CUR_TIME;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000418 if (G.badblocks)
419 inode->i_size = 3 * G.dirsize;
Eric Andersene494fdd1999-10-19 20:23:03 +0000420 else {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000421 G.root_block[2 * G.dirsize] = '\0';
422 G.root_block[2 * G.dirsize + 1] = '\0';
423 inode->i_size = 2 * G.dirsize;
Eric Andersene494fdd1999-10-19 20:23:03 +0000424 }
425 inode->i_mode = S_IFDIR + 0755;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000426 inode->i_uid = GETUID;
Eric Andersene494fdd1999-10-19 20:23:03 +0000427 if (inode->i_uid)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000428 inode->i_gid = GETGID;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000429 write_block(inode->i_zone[0], G.root_block);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430}
431
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000432#if ENABLE_FEATURE_MINIX2
Rob Landley88621d72006-08-29 19:41:06 +0000433static void make_root_inode2(void)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000435 struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436
437 mark_inode(MINIX_ROOT_INO);
438 inode->i_zone[0] = get_free_block();
439 inode->i_nlinks = 2;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000440 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000441 if (G.badblocks)
442 inode->i_size = 3 * G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 else {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000444 G.root_block[2 * G.dirsize] = '\0';
445 G.root_block[2 * G.dirsize + 1] = '\0';
446 inode->i_size = 2 * G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 }
448 inode->i_mode = S_IFDIR + 0755;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000449 inode->i_uid = GETUID;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 if (inode->i_uid)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000451 inode->i_gid = GETGID;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000452 write_block(inode->i_zone[0], G.root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000453}
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000454#else
455void make_root_inode2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000456#endif
457
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000458/*
459 * Perform a test of a block; return the number of
460 * blocks readable.
461 */
462static size_t do_check(char *buffer, size_t try, unsigned current_block)
463{
464 ssize_t got;
465
466 /* Seek to the correct loc. */
467 msg_eol = "seek failed during testing of blocks";
468 xlseek(G.dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
469 msg_eol = "\n";
470
471 /* Try the read */
472 got = read(G.dev_fd, buffer, try * BLOCK_SIZE);
473 if (got < 0)
474 got = 0;
475 try = ((size_t)got) / BLOCK_SIZE;
476
477 if (got & (BLOCK_SIZE - 1))
478 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
479 return try;
480}
481
482static void alarm_intr(int alnum)
483{
484 if (G.currently_testing >= SB_ZONES)
485 return;
486 signal(SIGALRM, alarm_intr);
487 alarm(5);
488 if (!G.currently_testing)
489 return;
490 printf("%d ...", G.currently_testing);
491 fflush(stdout);
492}
493
494static void check_blocks(void)
495{
496 size_t try, got;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000497
498 G.currently_testing = 0;
499 signal(SIGALRM, alarm_intr);
500 alarm(5);
501 while (G.currently_testing < SB_ZONES) {
502 msg_eol = "seek failed in check_blocks";
503 xlseek(G.dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
504 msg_eol = "\n";
505 try = TEST_BUFFER_BLOCKS;
506 if (G.currently_testing + try > SB_ZONES)
507 try = SB_ZONES - G.currently_testing;
Denis Vlasenko83bb98f2007-03-18 14:43:21 +0000508 got = do_check(G.check_blocks_buffer, try, G.currently_testing);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000509 G.currently_testing += got;
510 if (got == try)
511 continue;
512 if (G.currently_testing < SB_FIRSTZONE)
513 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
514 mark_zone(G.currently_testing);
515 G.badblocks++;
516 G.currently_testing++;
517 }
518 alarm(0);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000519 printf("%d bad block(s)\n", G.badblocks);
520}
521
522static void get_list_blocks(char *filename)
523{
524 FILE *listfile;
525 unsigned long blockno;
526
527 listfile = xfopen(filename, "r");
528 while (!feof(listfile)) {
529 fscanf(listfile, "%ld\n", &blockno);
530 mark_zone(blockno);
531 G.badblocks++;
532 }
533 printf("%d bad block(s)\n", G.badblocks);
534}
535
Rob Landley88621d72006-08-29 19:41:06 +0000536static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000537{
Eric Andersene494fdd1999-10-19 20:23:03 +0000538 unsigned long inodes;
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000539 unsigned norm_firstzone;
Denis Vlasenko28bb84b2006-11-29 23:15:04 +0000540 unsigned sb_zmaps;
541 unsigned i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000542
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000543 /* memset(G.super_block_buffer, 0, BLOCK_SIZE); */
544 /* memset(G.boot_block_buffer, 0, 512); */
545 SB_MAGIC = G.magic;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000546 SB_ZONE_SIZE = 0;
547 SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
548 if (version2)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000549 SB.s_zones = G.total_blocks;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000550 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000551 SB.s_nzones = G.total_blocks;
Eric Andersenb225e2a2004-08-28 00:43:07 +0000552
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000553 /* some magic nrs: 1 inode / 3 blocks */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000554 if (G.req_nr_inodes == 0)
555 inodes = G.total_blocks / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000556 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000557 inodes = G.req_nr_inodes;
Eric Andersene494fdd1999-10-19 20:23:03 +0000558 /* Round up inode count to fill block size */
Eric Andersene494fdd1999-10-19 20:23:03 +0000559 if (version2)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000560 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
561 ~(MINIX2_INODES_PER_BLOCK - 1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 else
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000563 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
564 ~(MINIX1_INODES_PER_BLOCK - 1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000565 if (inodes > 65535)
566 inodes = 65535;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000567 SB_INODES = inodes;
568 SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
569
Eric Andersene494fdd1999-10-19 20:23:03 +0000570 /* Real bad hack but overwise mkfs.minix can be thrown
571 * in infinite loop...
572 * try:
573 * dd if=/dev/zero of=test.fs count=10 bs=1024
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000574 * mkfs.minix -i 200 test.fs
575 */
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000576 /* This code is not insane: NORM_FIRSTZONE is not a constant,
577 * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000578 i = 999;
579 SB_ZMAPS = 0;
580 do {
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000581 norm_firstzone = NORM_FIRSTZONE;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000582 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000583 if (SB_ZMAPS == sb_zmaps) goto got_it;
584 SB_ZMAPS = sb_zmaps;
585 /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000586 } while (--i);
587 bb_error_msg_and_die("incompatible size/inode count, try different -i N");
588 got_it:
589
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000590 SB_FIRSTZONE = norm_firstzone;
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000591 G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
592 G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
593 memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
594 memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000595 for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000596 unmark_zone(i);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000597 for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000598 unmark_inode(i);
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000599 G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000600 printf("%ld inodes\n", (long)SB_INODES);
601 printf("%ld blocks\n", (long)SB_ZONES);
Denis Vlasenko016a82e2006-11-29 22:47:42 +0000602 printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)norm_firstzone);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000603 printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE);
604 printf("Maxsize=%ld\n", (long)SB_MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000605}
606
Denis Vlasenko06af2162007-02-03 17:28:39 +0000607int mkfs_minix_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000608int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000609{
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000610 struct mntent *mp;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000611 unsigned opt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000612 char *tmp;
613 struct stat statbuf;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000614 char *str_i, *str_n;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615 char *listfile = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000616
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000617 PTR_TO_GLOBALS = xzalloc(sizeof(G));
618/* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
619 G.namelen = 30;
620 G.dirsize = 32;
621 G.magic = MINIX1_SUPER_MAGIC2;
622
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000623 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000624 bb_error_msg_and_die("bad inode size");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000625#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000626 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000627 bb_error_msg_and_die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000628#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000629
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000630 opt = getopt32(argc, argv, "ci:l:n:v", &str_i, &listfile, &str_n);
631 argv += optind;
632 //if (opt & 1) -c
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000633 if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000634 //if (opt & 4) -l
635 if (opt & 8) { // -n
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000636 G.namelen = xatoi_u(str_n);
637 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
638 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000639 else bb_show_usage();
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000640 G.dirsize = G.namelen + 2;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000641 }
642 if (opt & 0x10) { // -v
643#if ENABLE_FEATURE_MINIX2
644 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000645#else
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000646 bb_error_msg_and_die("not compiled with minix v2 support");
Eric Andersene494fdd1999-10-19 20:23:03 +0000647#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000648 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000649
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000650 G.device_name = *argv++;
651 if (!G.device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000652 bb_show_usage();
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000653 if (*argv)
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000654 G.total_blocks = xatou32(*argv);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000655 else
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000656 G.total_blocks = get_size(G.device_name) / 1024;
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000657
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000658 if (G.total_blocks < 10)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000659 bb_error_msg_and_die("must have at least 10 blocks");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000660
Erik Andersene49d5ec2000-02-08 19:58:47 +0000661 if (version2) {
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000662 G.magic = MINIX2_SUPER_MAGIC2;
663 if (G.namelen == 14)
664 G.magic = MINIX2_SUPER_MAGIC;
665 } else if (G.total_blocks > 65535)
666 G.total_blocks = 65535;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000667
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000668 /* Check if it is mounted */
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000669 mp = find_mount_point(G.device_name, NULL);
670 if (mp && strcmp(G.device_name, mp->mnt_fsname) == 0)
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000671 bb_error_msg_and_die("%s is mounted on %s; "
672 "refusing to make a filesystem",
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000673 G.device_name, mp->mnt_dir);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000674
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000675 G.dev_fd = xopen(G.device_name, O_RDWR);
676 if (fstat(G.dev_fd, &statbuf) < 0)
677 bb_error_msg_and_die("cannot stat %s", G.device_name);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000678 if (!S_ISBLK(statbuf.st_mode))
679 opt &= ~1; // clear -c (check)
680
681/* I don't know why someone has special code to prevent mkfs.minix
682 * on IDE devices. Why IDE but not SCSI, etc?... */
683#if 0
684 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
685 /* what is this? */
686 bb_error_msg_and_die("will not try "
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000687 "to make filesystem on '%s'", G.device_name);
Denis Vlasenko66b86c62006-11-29 22:33:47 +0000688#endif
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000689
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000690 tmp = G.root_block;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 *(short *) tmp = 1;
692 strcpy(tmp + 2, ".");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000693 tmp += G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000694 *(short *) tmp = 1;
695 strcpy(tmp + 2, "..");
Denis Vlasenko4efeaee2007-03-15 19:52:42 +0000696 tmp += G.dirsize;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 *(short *) tmp = 2;
698 strcpy(tmp + 2, ".badblocks");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000699
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 setup_tables();
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000701
702 if (opt & 1) // -c ?
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 check_blocks();
704 else if (listfile)
705 get_list_blocks(listfile);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000706
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 if (version2) {
708 make_root_inode2();
709 make_bad_inode2();
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000710 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 make_root_inode();
712 make_bad_inode();
713 }
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000714
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 mark_good_blocks();
716 write_tables();
"Vladimir N. Oleynik"1f2dc402005-10-15 13:56:17 +0000717 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000718}