blob: 4435cb64a82f9a960947ef44bb628923b322f6da [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)
38 *
39 * 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 *
52 * -c for readablility checking (SLOW!)
53 * -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
59 * enforced (but it's not much fun on a character device :-).
60 */
61
Eric Andersene674eb71999-10-19 20:52:57 +000062#include "internal.h"
Eric Andersene494fdd1999-10-19 20:23:03 +000063#include <stdio.h>
64#include <time.h>
65#include <unistd.h>
66#include <string.h>
67#include <signal.h>
68#include <fcntl.h>
69#include <ctype.h>
70#include <stdlib.h>
71#include <termios.h>
72#include <sys/stat.h>
73#include <sys/ioctl.h>
74#include <mntent.h>
75#include <getopt.h>
76
77#include <linux/fs.h>
78#include <linux/minix_fs.h>
79
Eric Andersene494fdd1999-10-19 20:23:03 +000080#ifdef MINIX2_SUPER_MAGIC2
81#define HAVE_MINIX2 1
82#endif
83
84#ifndef __GNUC__
85#error "needs gcc for the bitop-__asm__'s"
86#endif
87
88#ifndef __linux__
89#define volatile
90#endif
91
92#define MINIX_ROOT_INO 1
93#define MINIX_BAD_INO 2
94
95#define TEST_BUFFER_BLOCKS 16
96#define MAX_GOOD_BLOCKS 512
97
98#define UPPER(size,n) (((size)+((n)-1))/(n))
99#define INODE_SIZE (sizeof(struct minix_inode))
100#ifdef HAVE_MINIX2
101#define INODE_SIZE2 (sizeof(struct minix2_inode))
102#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
103 : MINIX_INODES_PER_BLOCK))
104#else
105#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
106#endif
107#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
108
109#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
110
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111static char *program_name = "mkfs";
112static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000113static int DEV = -1;
114static long BLOCKS = 0;
115static int check = 0;
116static int badblocks = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117static int namelen = 30; /* default (changed to 30, per Linus's
118
119 suggestion, Sun Nov 21 08:05:07 1993) */
Eric Andersene494fdd1999-10-19 20:23:03 +0000120static int dirsize = 32;
121static int magic = MINIX_SUPER_MAGIC2;
122static int version2 = 0;
123
124static char root_block[BLOCK_SIZE] = "\0";
125
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126static char *inode_buffer = NULL;
127
Eric Andersene494fdd1999-10-19 20:23:03 +0000128#define Inode (((struct minix_inode *) inode_buffer)-1)
129#ifdef HAVE_MINIX2
130#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
131#endif
132static char super_block_buffer[BLOCK_SIZE];
133static char boot_block_buffer[512];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134
Eric Andersene494fdd1999-10-19 20:23:03 +0000135#define Super (*(struct minix_super_block *)super_block_buffer)
136#define INODES ((unsigned long)Super.s_ninodes)
137#ifdef HAVE_MINIX2
138#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
139#else
140#define ZONES ((unsigned long)(Super.s_nzones))
141#endif
142#define IMAPS ((unsigned long)Super.s_imap_blocks)
143#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
144#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
145#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
146#define MAXSIZE ((unsigned long)Super.s_max_size)
147#define MAGIC (Super.s_magic)
148#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
149
150static char *inode_map;
151static char *zone_map;
152
153static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
154static int used_good_blocks = 0;
155static unsigned long req_nr_inodes = 0;
156
Eric Andersene494fdd1999-10-19 20:23:03 +0000157#define inode_in_use(x) (bit(inode_map,(x)))
158#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
159
160#define mark_inode(x) (setbit(inode_map,(x)))
161#define unmark_inode(x) (clrbit(inode_map,(x)))
162
163#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
164#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
165
166/*
167 * Volatile to let gcc know that this doesn't return. When trying
168 * to compile this under minix, volatile gives a warning, as
169 * exit() isn't defined as volatile under minix.
170 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171static volatile void die(char *str)
172{
Eric Andersene494fdd1999-10-19 20:23:03 +0000173 fprintf(stderr, "%s: %s\n", program_name, str);
174 exit(8);
175}
176
Eric Andersene674eb71999-10-19 20:52:57 +0000177static volatile void show_usage()
Eric Andersene494fdd1999-10-19 20:23:03 +0000178{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
180 BB_VER, BB_BT);
181 fprintf(stderr,
182 "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n\n",
183 program_name);
Eric Andersend73dc5b1999-11-10 23:13:02 +0000184 fprintf(stderr, "Make a MINIX filesystem.\n\n");
185 fprintf(stderr, "OPTIONS:\n");
186 fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 fprintf(stderr,
188 "\t-n [14|30]\tSpecify the maximum length of filenames\n");
189 fprintf(stderr,
190 "\t-i\t\tSpecify the number of inodes for the filesystem\n");
191 fprintf(stderr,
192 "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
Eric Andersend73dc5b1999-11-10 23:13:02 +0000193 fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000194 exit(16);
195}
196
197/*
198 * Check to make certain that our new filesystem won't be created on
199 * an already mounted partition. Code adapted from mke2fs, Copyright
200 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
201 */
202static void check_mount(void)
203{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 FILE *f;
205 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000206
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000208 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 while ((mnt = getmntent(f)) != NULL)
210 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000211 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000213 if (!mnt)
214 return;
215
Eric Andersene674eb71999-10-19 20:52:57 +0000216 die("%s is mounted; will not make a filesystem here!");
Eric Andersene494fdd1999-10-19 20:23:03 +0000217}
218
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000220{
221 char ch;
222
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 if (lseek(fd, offset, 0) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000224 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000226 return 0;
227 return 1;
228}
229
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230static int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000231{
232 int high, low;
233
234 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000236 low = high;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000238 const int mid = (low + high) / 2;
239
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000241 low = mid;
242 else
243 high = mid;
244 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000246 return (low + 1);
247}
248
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249static int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000250{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 int fd;
252 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000253
254 fd = open(file, O_RDWR);
255 if (fd < 0) {
256 perror(file);
257 exit(1);
258 }
259 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
260 close(fd);
261 return (size * 512);
262 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263
Eric Andersene494fdd1999-10-19 20:23:03 +0000264 size = count_blocks(fd);
265 close(fd);
266 return size;
267}
268
Eric Andersene674eb71999-10-19 20:52:57 +0000269static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000270{
271 /* Mark the super block valid. */
272 Super.s_state |= MINIX_VALID_FS;
273 Super.s_state &= ~MINIX_ERROR_FS;
274
275 if (lseek(DEV, 0, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000276 die("seek to boot block failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000277 if (512 != write(DEV, boot_block_buffer, 512))
Eric Andersene674eb71999-10-19 20:52:57 +0000278 die("unable to clear boot sector");
Eric Andersene494fdd1999-10-19 20:23:03 +0000279 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000280 die("seek failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000281 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000282 die("unable to write super-block");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000284 die("unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000286 die("unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000288 die("unable to write inodes");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289
Eric Andersene494fdd1999-10-19 20:23:03 +0000290}
291
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000293{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000295 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000296 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000297 die("write failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000298}
299
Eric Andersene674eb71999-10-19 20:52:57 +0000300static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000301{
302 int blk;
303
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Eric Andersene674eb71999-10-19 20:52:57 +0000305 die("too many bad blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000306 if (used_good_blocks)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 blk = good_blocks_table[used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000308 else
309 blk = FIRSTZONE;
310 while (blk < ZONES && zone_in_use(blk))
311 blk++;
312 if (blk >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000313 die("not enough good blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000314 good_blocks_table[used_good_blocks] = blk;
315 used_good_blocks++;
316 return blk;
317}
318
Eric Andersene674eb71999-10-19 20:52:57 +0000319static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000320{
321 int blk;
322
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 for (blk = 0; blk < used_good_blocks; blk++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000324 mark_zone(good_blocks_table[blk]);
325}
326
327inline int next(int zone)
328{
329 if (!zone)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 zone = FIRSTZONE - 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000331 while (++zone < ZONES)
332 if (zone_in_use(zone))
333 return zone;
334 return 0;
335}
336
Eric Andersene674eb71999-10-19 20:52:57 +0000337static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000338{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
340 int i, j, zone;
341 int ind = 0, dind = 0;
342 unsigned short ind_block[BLOCK_SIZE >> 1];
343 unsigned short dind_block[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000344
345#define NEXT_BAD (zone = next(zone))
346
347 if (!badblocks)
348 return;
349 mark_inode(MINIX_BAD_INO);
350 inode->i_nlinks = 1;
351 inode->i_time = time(NULL);
352 inode->i_mode = S_IFREG + 0000;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353 inode->i_size = badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000354 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000356 inode->i_zone[i] = zone;
357 if (!NEXT_BAD)
358 goto end_bad;
359 }
360 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 memset(ind_block, 0, BLOCK_SIZE);
362 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000363 ind_block[i] = zone;
364 if (!NEXT_BAD)
365 goto end_bad;
366 }
367 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 memset(dind_block, 0, BLOCK_SIZE);
369 for (i = 0; i < 512; i++) {
370 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000371 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 memset(ind_block, 0, BLOCK_SIZE);
373 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000374 ind_block[j] = zone;
375 if (!NEXT_BAD)
376 goto end_bad;
377 }
378 }
Eric Andersene674eb71999-10-19 20:52:57 +0000379 die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000381 if (ind)
382 write_block(ind, (char *) ind_block);
383 if (dind)
384 write_block(dind, (char *) dind_block);
385}
386
387#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000389{
390 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
391 int i, j, zone;
392 int ind = 0, dind = 0;
393 unsigned long ind_block[BLOCK_SIZE >> 2];
394 unsigned long dind_block[BLOCK_SIZE >> 2];
395
396 if (!badblocks)
397 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000399 inode->i_nlinks = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
Eric Andersene494fdd1999-10-19 20:23:03 +0000401 inode->i_mode = S_IFREG + 0000;
402 inode->i_size = badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000404 for (i = 0; i < 7; i++) {
405 inode->i_zone[i] = zone;
406 if (!NEXT_BAD)
407 goto end_bad;
408 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 inode->i_zone[7] = ind = get_free_block();
410 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000411 for (i = 0; i < 256; i++) {
412 ind_block[i] = zone;
413 if (!NEXT_BAD)
414 goto end_bad;
415 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000416 inode->i_zone[8] = dind = get_free_block();
417 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000418 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 write_block(ind, (char *) ind_block);
420 dind_block[i] = ind = get_free_block();
421 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000422 for (j = 0; j < 256; j++) {
423 ind_block[j] = zone;
424 if (!NEXT_BAD)
425 goto end_bad;
426 }
427 }
428 /* Could make triple indirect block here */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 die("too many bad blocks");
430 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000431 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000433 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 write_block(dind, (char *) dind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000435}
436#endif
437
Eric Andersene674eb71999-10-19 20:52:57 +0000438static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000439{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000441
442 mark_inode(MINIX_ROOT_INO);
443 inode->i_zone[0] = get_free_block();
444 inode->i_nlinks = 2;
445 inode->i_time = time(NULL);
446 if (badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000447 inode->i_size = 3 * dirsize;
448 else {
449 root_block[2 * dirsize] = '\0';
450 root_block[2 * dirsize + 1] = '\0';
451 inode->i_size = 2 * dirsize;
452 }
453 inode->i_mode = S_IFDIR + 0755;
454 inode->i_uid = getuid();
455 if (inode->i_uid)
456 inode->i_gid = getgid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 write_block(inode->i_zone[0], root_block);
458}
459
460#ifdef HAVE_MINIX2
461static void make_root_inode2(void)
462{
463 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
464
465 mark_inode(MINIX_ROOT_INO);
466 inode->i_zone[0] = get_free_block();
467 inode->i_nlinks = 2;
468 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
469 if (badblocks)
470 inode->i_size = 3 * dirsize;
471 else {
472 root_block[2 * dirsize] = '\0';
473 root_block[2 * dirsize + 1] = '\0';
474 inode->i_size = 2 * dirsize;
475 }
476 inode->i_mode = S_IFDIR + 0755;
477 inode->i_uid = getuid();
478 if (inode->i_uid)
479 inode->i_gid = getgid();
480 write_block(inode->i_zone[0], root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000481}
482#endif
483
Eric Andersene674eb71999-10-19 20:52:57 +0000484static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000485{
486 int i;
487 unsigned long inodes;
488
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 memset(super_block_buffer, 0, BLOCK_SIZE);
490 memset(boot_block_buffer, 0, 512);
Eric Andersene494fdd1999-10-19 20:23:03 +0000491 MAGIC = magic;
492 ZONESIZE = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
Eric Andersene494fdd1999-10-19 20:23:03 +0000494 ZONES = BLOCKS;
495/* some magic nrs: 1 inode / 3 blocks */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 if (req_nr_inodes == 0)
497 inodes = BLOCKS / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000498 else
499 inodes = req_nr_inodes;
500 /* Round up inode count to fill block size */
501#ifdef HAVE_MINIX2
502 if (version2)
503 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 ~(MINIX2_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000505 else
506#endif
507 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 ~(MINIX_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000509 if (inodes > 65535)
510 inodes = 65535;
511 INODES = inodes;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000513 ZMAPS = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 i = 0;
515 while (ZMAPS !=
516 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
517 BITS_PER_BLOCK) && i < 1000) {
518 ZMAPS =
519 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
520 BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000521 i++;
522 }
523 /* Real bad hack but overwise mkfs.minix can be thrown
524 * in infinite loop...
525 * try:
526 * dd if=/dev/zero of=test.fs count=10 bs=1024
527 * /sbin/mkfs.minix -i 200 test.fs
528 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529 if (i >= 999) {
530 die("unable to allocate buffers for maps");
Eric Andersene494fdd1999-10-19 20:23:03 +0000531 }
532 FIRSTZONE = NORM_FIRSTZONE;
533 inode_map = malloc(IMAPS * BLOCK_SIZE);
534 zone_map = malloc(ZMAPS * BLOCK_SIZE);
535 if (!inode_map || !zone_map)
Eric Andersene674eb71999-10-19 20:52:57 +0000536 die("unable to allocate buffers for maps");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000537 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
538 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
539 for (i = FIRSTZONE; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000540 unmark_zone(i);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 for (i = MINIX_ROOT_INO; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000542 unmark_inode(i);
543 inode_buffer = malloc(INODE_BUFFER_SIZE);
544 if (!inode_buffer)
Eric Andersene674eb71999-10-19 20:52:57 +0000545 die("unable to allocate buffer for inodes");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
547 printf("%ld inodes\n", INODES);
548 printf("%ld blocks\n", ZONES);
549 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
550 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
551 printf("Maxsize=%ld\n\n", MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000552}
553
554/*
555 * Perform a test of a block; return the number of
556 * blocks readable/writeable.
557 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558long do_check(char *buffer, int try, unsigned int current_block)
Eric Andersene494fdd1999-10-19 20:23:03 +0000559{
560 long got;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 /* Seek to the correct loc. */
563 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564 current_block * BLOCK_SIZE) {
565 die("seek failed during testing of blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000566 }
567
568
569 /* Try the read */
570 got = read(DEV, buffer, try * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 if (got < 0)
572 got = 0;
573 if (got & (BLOCK_SIZE - 1)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000574 printf("Weird values in do_check: probably bugs\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 }
576 got /= BLOCK_SIZE;
577 return got;
578}
579
580static unsigned int currently_testing = 0;
581
Eric Andersene674eb71999-10-19 20:52:57 +0000582static void alarm_intr(int alnum)
Eric Andersene494fdd1999-10-19 20:23:03 +0000583{
584 if (currently_testing >= ZONES)
585 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000587 alarm(5);
588 if (!currently_testing)
589 return;
590 printf("%d ...", currently_testing);
591 fflush(stdout);
592}
593
Eric Andersene674eb71999-10-19 20:52:57 +0000594static void check_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000595{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 int try, got;
Eric Andersene494fdd1999-10-19 20:23:03 +0000597 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
598
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 currently_testing = 0;
600 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000601 alarm(5);
602 while (currently_testing < ZONES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
604 currently_testing * BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +0000605 die("seek failed in check_blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000606 try = TEST_BUFFER_BLOCKS;
607 if (currently_testing + try > ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608 try = ZONES - currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000609 got = do_check(buffer, try, currently_testing);
610 currently_testing += got;
611 if (got == try)
612 continue;
613 if (currently_testing < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000614 die("bad blocks before data-area: cannot make fs");
Eric Andersene494fdd1999-10-19 20:23:03 +0000615 mark_zone(currently_testing);
616 badblocks++;
617 currently_testing++;
618 }
619 if (badblocks > 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000620 printf("%d bad blocks\n", badblocks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000621 else if (badblocks == 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000622 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000623}
624
Eric Andersene674eb71999-10-19 20:52:57 +0000625static void get_list_blocks(filename)
Eric Andersene494fdd1999-10-19 20:23:03 +0000626char *filename;
627
628{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629 FILE *listfile;
630 unsigned long blockno;
Eric Andersene494fdd1999-10-19 20:23:03 +0000631
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632 listfile = fopen(filename, "r");
633 if (listfile == (FILE *) NULL) {
634 die("can't open file of bad blocks");
635 }
636 while (!feof(listfile)) {
637 fscanf(listfile, "%ld\n", &blockno);
638 mark_zone(blockno);
639 badblocks++;
640 }
641 if (badblocks > 1)
642 printf("%d bad blocks\n", badblocks);
643 else if (badblocks == 1)
644 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000645}
646
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647extern int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000648{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000649 int i;
650 char *tmp;
651 struct stat statbuf;
652 char *listfile = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000653
Erik Andersene49d5ec2000-02-08 19:58:47 +0000654 if (argc && *argv)
655 program_name = *argv;
656 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
657 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000658#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
660 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000661#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 opterr = 0;
663 while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
664 switch (i) {
665 case 'c':
666 check = 1;
667 break;
668 case 'i':
669 req_nr_inodes = (unsigned long) atol(optarg);
670 break;
671 case 'l':
672 listfile = optarg;
673 break;
674 case 'n':
675 i = strtoul(optarg, &tmp, 0);
676 if (*tmp)
677 show_usage();
678 if (i == 14)
679 magic = MINIX_SUPER_MAGIC;
680 else if (i == 30)
681 magic = MINIX_SUPER_MAGIC2;
682 else
683 show_usage();
684 namelen = i;
685 dirsize = i + 2;
686 break;
687 case 'v':
Eric Andersene494fdd1999-10-19 20:23:03 +0000688#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000690#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 fprintf(stderr, "%s: not compiled with minix v2 support\n",
692 program_name, device_name);
693 exit(-1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000694#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 break;
696 default:
697 show_usage();
698 }
699 argc -= optind;
700 argv += optind;
701 if (argc > 0 && !device_name) {
702 device_name = argv[0];
703 argc--;
704 argv++;
705 }
706 if (argc > 0) {
707 BLOCKS = strtol(argv[0], &tmp, 0);
708 if (*tmp) {
709 printf("strtol error: number of blocks not specified");
710 show_usage();
711 }
712 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000713
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 if (device_name && !BLOCKS)
715 BLOCKS = get_size(device_name) / 1024;
716 if (!device_name || BLOCKS < 10) {
717 show_usage();
718 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000719#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 if (version2) {
721 if (namelen == 14)
722 magic = MINIX2_SUPER_MAGIC;
723 else
724 magic = MINIX2_SUPER_MAGIC2;
725 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000726#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 if (BLOCKS > 65535)
728 BLOCKS = 65535;
729 check_mount(); /* is it already mounted? */
730 tmp = root_block;
731 *(short *) tmp = 1;
732 strcpy(tmp + 2, ".");
733 tmp += dirsize;
734 *(short *) tmp = 1;
735 strcpy(tmp + 2, "..");
736 tmp += dirsize;
737 *(short *) tmp = 2;
738 strcpy(tmp + 2, ".badblocks");
739 DEV = open(device_name, O_RDWR);
740 if (DEV < 0)
741 die("unable to open %s");
742 if (fstat(DEV, &statbuf) < 0)
743 die("unable to stat %s");
744 if (!S_ISBLK(statbuf.st_mode))
745 check = 0;
746 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
747 die("will not try to make filesystem on '%s'");
748 setup_tables();
749 if (check)
750 check_blocks();
751 else if (listfile)
752 get_list_blocks(listfile);
Eric Andersene494fdd1999-10-19 20:23:03 +0000753#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754 if (version2) {
755 make_root_inode2();
756 make_bad_inode2();
757 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000758#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 {
760 make_root_inode();
761 make_bad_inode();
762 }
763 mark_good_blocks();
764 write_tables();
765 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000766}