blob: 1ee3d4cfa5c6253380326a5b9652246fe82fae4a [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;
Erik Andersen0d068a22000-03-21 22:32:57 +0000533 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
534 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
536 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
537 for (i = FIRSTZONE; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000538 unmark_zone(i);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 for (i = MINIX_ROOT_INO; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000540 unmark_inode(i);
Erik Andersen0d068a22000-03-21 22:32:57 +0000541 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
543 printf("%ld inodes\n", INODES);
544 printf("%ld blocks\n", ZONES);
545 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
546 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
547 printf("Maxsize=%ld\n\n", MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000548}
549
550/*
551 * Perform a test of a block; return the number of
552 * blocks readable/writeable.
553 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554long do_check(char *buffer, int try, unsigned int current_block)
Eric Andersene494fdd1999-10-19 20:23:03 +0000555{
556 long got;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557
Eric Andersene494fdd1999-10-19 20:23:03 +0000558 /* Seek to the correct loc. */
559 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 current_block * BLOCK_SIZE) {
561 die("seek failed during testing of blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 }
563
564
565 /* Try the read */
566 got = read(DEV, buffer, try * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 if (got < 0)
568 got = 0;
569 if (got & (BLOCK_SIZE - 1)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000570 printf("Weird values in do_check: probably bugs\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000571 }
572 got /= BLOCK_SIZE;
573 return got;
574}
575
576static unsigned int currently_testing = 0;
577
Eric Andersene674eb71999-10-19 20:52:57 +0000578static void alarm_intr(int alnum)
Eric Andersene494fdd1999-10-19 20:23:03 +0000579{
580 if (currently_testing >= ZONES)
581 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000583 alarm(5);
584 if (!currently_testing)
585 return;
586 printf("%d ...", currently_testing);
587 fflush(stdout);
588}
589
Eric Andersene674eb71999-10-19 20:52:57 +0000590static void check_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000591{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000592 int try, got;
Eric Andersene494fdd1999-10-19 20:23:03 +0000593 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
594
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595 currently_testing = 0;
596 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000597 alarm(5);
598 while (currently_testing < ZONES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
600 currently_testing * BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +0000601 die("seek failed in check_blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000602 try = TEST_BUFFER_BLOCKS;
603 if (currently_testing + try > ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604 try = ZONES - currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000605 got = do_check(buffer, try, currently_testing);
606 currently_testing += got;
607 if (got == try)
608 continue;
609 if (currently_testing < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000610 die("bad blocks before data-area: cannot make fs");
Eric Andersene494fdd1999-10-19 20:23:03 +0000611 mark_zone(currently_testing);
612 badblocks++;
613 currently_testing++;
614 }
615 if (badblocks > 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000616 printf("%d bad blocks\n", badblocks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000617 else if (badblocks == 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000618 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000619}
620
Eric Andersene674eb71999-10-19 20:52:57 +0000621static void get_list_blocks(filename)
Eric Andersene494fdd1999-10-19 20:23:03 +0000622char *filename;
623
624{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 FILE *listfile;
626 unsigned long blockno;
Eric Andersene494fdd1999-10-19 20:23:03 +0000627
Erik Andersene49d5ec2000-02-08 19:58:47 +0000628 listfile = fopen(filename, "r");
629 if (listfile == (FILE *) NULL) {
630 die("can't open file of bad blocks");
631 }
632 while (!feof(listfile)) {
633 fscanf(listfile, "%ld\n", &blockno);
634 mark_zone(blockno);
635 badblocks++;
636 }
637 if (badblocks > 1)
638 printf("%d bad blocks\n", badblocks);
639 else if (badblocks == 1)
640 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000641}
642
Erik Andersene49d5ec2000-02-08 19:58:47 +0000643extern int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000644{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000645 int i;
646 char *tmp;
647 struct stat statbuf;
648 char *listfile = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000649
Erik Andersene49d5ec2000-02-08 19:58:47 +0000650 if (argc && *argv)
651 program_name = *argv;
652 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
653 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000654#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
656 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000657#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 opterr = 0;
659 while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
660 switch (i) {
661 case 'c':
662 check = 1;
663 break;
664 case 'i':
665 req_nr_inodes = (unsigned long) atol(optarg);
666 break;
667 case 'l':
668 listfile = optarg;
669 break;
670 case 'n':
671 i = strtoul(optarg, &tmp, 0);
672 if (*tmp)
673 show_usage();
674 if (i == 14)
675 magic = MINIX_SUPER_MAGIC;
676 else if (i == 30)
677 magic = MINIX_SUPER_MAGIC2;
678 else
679 show_usage();
680 namelen = i;
681 dirsize = i + 2;
682 break;
683 case 'v':
Eric Andersene494fdd1999-10-19 20:23:03 +0000684#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000686#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 fprintf(stderr, "%s: not compiled with minix v2 support\n",
688 program_name, device_name);
689 exit(-1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000690#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 break;
692 default:
693 show_usage();
694 }
695 argc -= optind;
696 argv += optind;
697 if (argc > 0 && !device_name) {
698 device_name = argv[0];
699 argc--;
700 argv++;
701 }
702 if (argc > 0) {
703 BLOCKS = strtol(argv[0], &tmp, 0);
704 if (*tmp) {
705 printf("strtol error: number of blocks not specified");
706 show_usage();
707 }
708 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000709
Erik Andersene49d5ec2000-02-08 19:58:47 +0000710 if (device_name && !BLOCKS)
711 BLOCKS = get_size(device_name) / 1024;
712 if (!device_name || BLOCKS < 10) {
713 show_usage();
714 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000715#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 if (version2) {
717 if (namelen == 14)
718 magic = MINIX2_SUPER_MAGIC;
719 else
720 magic = MINIX2_SUPER_MAGIC2;
721 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000722#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 if (BLOCKS > 65535)
724 BLOCKS = 65535;
725 check_mount(); /* is it already mounted? */
726 tmp = root_block;
727 *(short *) tmp = 1;
728 strcpy(tmp + 2, ".");
729 tmp += dirsize;
730 *(short *) tmp = 1;
731 strcpy(tmp + 2, "..");
732 tmp += dirsize;
733 *(short *) tmp = 2;
734 strcpy(tmp + 2, ".badblocks");
735 DEV = open(device_name, O_RDWR);
736 if (DEV < 0)
737 die("unable to open %s");
738 if (fstat(DEV, &statbuf) < 0)
739 die("unable to stat %s");
740 if (!S_ISBLK(statbuf.st_mode))
741 check = 0;
742 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
743 die("will not try to make filesystem on '%s'");
744 setup_tables();
745 if (check)
746 check_blocks();
747 else if (listfile)
748 get_list_blocks(listfile);
Eric Andersene494fdd1999-10-19 20:23:03 +0000749#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000750 if (version2) {
751 make_root_inode2();
752 make_bad_inode2();
753 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000754#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000755 {
756 make_root_inode();
757 make_bad_inode();
758 }
759 mark_good_blocks();
760 write_tables();
761 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000762}