blob: 670d03a3a92072004636d8d60bc7b72595eea543 [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 :-).
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 Andersene674eb71999-10-19 20:52:57 +000065#include "internal.h"
Eric Andersene494fdd1999-10-19 20:23:03 +000066#include <stdio.h>
67#include <time.h>
68#include <unistd.h>
69#include <string.h>
70#include <signal.h>
71#include <fcntl.h>
72#include <ctype.h>
73#include <stdlib.h>
74#include <termios.h>
75#include <sys/stat.h>
76#include <sys/ioctl.h>
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000077#include <sys/param.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000078#include <mntent.h>
Eric Andersene494fdd1999-10-19 20:23:03 +000079
80#include <linux/fs.h>
81#include <linux/minix_fs.h>
82
Eric Andersene494fdd1999-10-19 20:23:03 +000083#ifdef MINIX2_SUPER_MAGIC2
84#define HAVE_MINIX2 1
85#endif
86
Eric Andersene494fdd1999-10-19 20:23:03 +000087#ifndef __linux__
88#define volatile
89#endif
90
91#define MINIX_ROOT_INO 1
92#define MINIX_BAD_INO 2
93
94#define TEST_BUFFER_BLOCKS 16
95#define MAX_GOOD_BLOCKS 512
96
97#define UPPER(size,n) (((size)+((n)-1))/(n))
98#define INODE_SIZE (sizeof(struct minix_inode))
99#ifdef HAVE_MINIX2
100#define INODE_SIZE2 (sizeof(struct minix2_inode))
101#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
102 : MINIX_INODES_PER_BLOCK))
103#else
104#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
105#endif
106#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
107
108#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
109
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110static char *program_name = "mkfs";
111static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000112static int DEV = -1;
113static long BLOCKS = 0;
114static int check = 0;
115static int badblocks = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116static int namelen = 30; /* default (changed to 30, per Linus's
117
118 suggestion, Sun Nov 21 08:05:07 1993) */
Eric Andersene494fdd1999-10-19 20:23:03 +0000119static int dirsize = 32;
120static int magic = MINIX_SUPER_MAGIC2;
121static int version2 = 0;
122
123static char root_block[BLOCK_SIZE] = "\0";
124
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125static char *inode_buffer = NULL;
126
Eric Andersene494fdd1999-10-19 20:23:03 +0000127#define Inode (((struct minix_inode *) inode_buffer)-1)
128#ifdef HAVE_MINIX2
129#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
130#endif
131static char super_block_buffer[BLOCK_SIZE];
132static char boot_block_buffer[512];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133
Eric Andersene494fdd1999-10-19 20:23:03 +0000134#define Super (*(struct minix_super_block *)super_block_buffer)
135#define INODES ((unsigned long)Super.s_ninodes)
136#ifdef HAVE_MINIX2
137#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
138#else
139#define ZONES ((unsigned long)(Super.s_nzones))
140#endif
141#define IMAPS ((unsigned long)Super.s_imap_blocks)
142#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
143#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
144#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
145#define MAXSIZE ((unsigned long)Super.s_max_size)
146#define MAGIC (Super.s_magic)
147#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
148
149static char *inode_map;
150static char *zone_map;
151
152static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
153static int used_good_blocks = 0;
154static unsigned long req_nr_inodes = 0;
155
Erik Andersen4f3f7572000-04-28 00:18:56 +0000156#define inode_in_use(x) (isset(inode_map,(x)))
157#define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000158
159#define mark_inode(x) (setbit(inode_map,(x)))
160#define unmark_inode(x) (clrbit(inode_map,(x)))
161
162#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
163#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
164
165/*
166 * Volatile to let gcc know that this doesn't return. When trying
167 * to compile this under minix, volatile gives a warning, as
168 * exit() isn't defined as volatile under minix.
169 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170static volatile void die(char *str)
171{
Eric Andersene494fdd1999-10-19 20:23:03 +0000172 fprintf(stderr, "%s: %s\n", program_name, str);
173 exit(8);
174}
175
Erik Andersen330fd2b2000-05-19 05:35:19 +0000176static volatile void show_usage() __attribute__ ((noreturn));
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,
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000182 "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 program_name);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000184#ifndef BB_FEATURE_TRIVIAL_HELP
185 fprintf(stderr, "\nMake a MINIX filesystem.\n\n");
Eric Andersend73dc5b1999-11-10 23:13:02 +0000186 fprintf(stderr, "OPTIONS:\n");
187 fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 fprintf(stderr,
189 "\t-n [14|30]\tSpecify the maximum length of filenames\n");
190 fprintf(stderr,
Eric Andersen815e9042000-06-06 16:15:23 +0000191 "\t-i INODES\tSpecify the number of inodes for the filesystem\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 fprintf(stderr,
193 "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
Eric Andersend73dc5b1999-11-10 23:13:02 +0000194 fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000195#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000196 exit(16);
197}
198
199/*
200 * Check to make certain that our new filesystem won't be created on
201 * an already mounted partition. Code adapted from mke2fs, Copyright
202 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
203 */
204static void check_mount(void)
205{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 FILE *f;
207 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000208
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000210 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 while ((mnt = getmntent(f)) != NULL)
212 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000213 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000215 if (!mnt)
216 return;
217
Eric Andersene674eb71999-10-19 20:52:57 +0000218 die("%s is mounted; will not make a filesystem here!");
Eric Andersene494fdd1999-10-19 20:23:03 +0000219}
220
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221static long valid_offset(int fd, int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000222{
223 char ch;
224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 if (lseek(fd, offset, 0) < 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000226 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227 if (read(fd, &ch, 1) < 1)
Eric Andersene494fdd1999-10-19 20:23:03 +0000228 return 0;
229 return 1;
230}
231
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232static int count_blocks(int fd)
Eric Andersene494fdd1999-10-19 20:23:03 +0000233{
234 int high, low;
235
236 low = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 for (high = 1; valid_offset(fd, high); high *= 2)
Eric Andersene494fdd1999-10-19 20:23:03 +0000238 low = high;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 while (low < high - 1) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000240 const int mid = (low + high) / 2;
241
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242 if (valid_offset(fd, mid))
Eric Andersene494fdd1999-10-19 20:23:03 +0000243 low = mid;
244 else
245 high = mid;
246 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 valid_offset(fd, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000248 return (low + 1);
249}
250
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251static int get_size(const char *file)
Eric Andersene494fdd1999-10-19 20:23:03 +0000252{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 int fd;
254 long size;
Eric Andersene494fdd1999-10-19 20:23:03 +0000255
256 fd = open(file, O_RDWR);
257 if (fd < 0) {
258 perror(file);
259 exit(1);
260 }
261 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
262 close(fd);
263 return (size * 512);
264 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265
Eric Andersene494fdd1999-10-19 20:23:03 +0000266 size = count_blocks(fd);
267 close(fd);
268 return size;
269}
270
Eric Andersene674eb71999-10-19 20:52:57 +0000271static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000272{
273 /* Mark the super block valid. */
274 Super.s_state |= MINIX_VALID_FS;
275 Super.s_state &= ~MINIX_ERROR_FS;
276
277 if (lseek(DEV, 0, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000278 die("seek to boot block failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000279 if (512 != write(DEV, boot_block_buffer, 512))
Eric Andersene674eb71999-10-19 20:52:57 +0000280 die("unable to clear boot sector");
Eric Andersene494fdd1999-10-19 20:23:03 +0000281 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000282 die("seek failed in write_tables");
Eric Andersene494fdd1999-10-19 20:23:03 +0000283 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000284 die("unable to write super-block");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000286 die("unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000288 die("unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000290 die("unable to write inodes");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291
Eric Andersene494fdd1999-10-19 20:23:03 +0000292}
293
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294static void write_block(int blk, char *buffer)
Eric Andersene494fdd1999-10-19 20:23:03 +0000295{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000297 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000298 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000299 die("write failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000300}
301
Eric Andersene674eb71999-10-19 20:52:57 +0000302static int get_free_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000303{
304 int blk;
305
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
Eric Andersene674eb71999-10-19 20:52:57 +0000307 die("too many bad blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000308 if (used_good_blocks)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 blk = good_blocks_table[used_good_blocks - 1] + 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000310 else
311 blk = FIRSTZONE;
312 while (blk < ZONES && zone_in_use(blk))
313 blk++;
314 if (blk >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000315 die("not enough good blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000316 good_blocks_table[used_good_blocks] = blk;
317 used_good_blocks++;
318 return blk;
319}
320
Eric Andersene674eb71999-10-19 20:52:57 +0000321static void mark_good_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000322{
323 int blk;
324
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 for (blk = 0; blk < used_good_blocks; blk++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000326 mark_zone(good_blocks_table[blk]);
327}
328
329inline int next(int zone)
330{
331 if (!zone)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 zone = FIRSTZONE - 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000333 while (++zone < ZONES)
334 if (zone_in_use(zone))
335 return zone;
336 return 0;
337}
338
Eric Andersene674eb71999-10-19 20:52:57 +0000339static void make_bad_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000340{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
342 int i, j, zone;
343 int ind = 0, dind = 0;
344 unsigned short ind_block[BLOCK_SIZE >> 1];
345 unsigned short dind_block[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000346
347#define NEXT_BAD (zone = next(zone))
348
349 if (!badblocks)
350 return;
351 mark_inode(MINIX_BAD_INO);
352 inode->i_nlinks = 1;
353 inode->i_time = time(NULL);
354 inode->i_mode = S_IFREG + 0000;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 inode->i_size = badblocks * BLOCK_SIZE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000356 zone = next(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 for (i = 0; i < 7; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000358 inode->i_zone[i] = zone;
359 if (!NEXT_BAD)
360 goto end_bad;
361 }
362 inode->i_zone[7] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 memset(ind_block, 0, BLOCK_SIZE);
364 for (i = 0; i < 512; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000365 ind_block[i] = zone;
366 if (!NEXT_BAD)
367 goto end_bad;
368 }
369 inode->i_zone[8] = dind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 memset(dind_block, 0, BLOCK_SIZE);
371 for (i = 0; i < 512; i++) {
372 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000373 dind_block[i] = ind = get_free_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 memset(ind_block, 0, BLOCK_SIZE);
375 for (j = 0; j < 512; j++) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000376 ind_block[j] = zone;
377 if (!NEXT_BAD)
378 goto end_bad;
379 }
380 }
Eric Andersene674eb71999-10-19 20:52:57 +0000381 die("too many bad blocks");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000383 if (ind)
384 write_block(ind, (char *) ind_block);
385 if (dind)
386 write_block(dind, (char *) dind_block);
387}
388
389#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390static void make_bad_inode2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000391{
392 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
393 int i, j, zone;
394 int ind = 0, dind = 0;
395 unsigned long ind_block[BLOCK_SIZE >> 2];
396 unsigned long dind_block[BLOCK_SIZE >> 2];
397
398 if (!badblocks)
399 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 mark_inode(MINIX_BAD_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +0000401 inode->i_nlinks = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000402 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
Eric Andersene494fdd1999-10-19 20:23:03 +0000403 inode->i_mode = S_IFREG + 0000;
404 inode->i_size = badblocks * BLOCK_SIZE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 zone = next(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000406 for (i = 0; i < 7; i++) {
407 inode->i_zone[i] = zone;
408 if (!NEXT_BAD)
409 goto end_bad;
410 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 inode->i_zone[7] = ind = get_free_block();
412 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000413 for (i = 0; i < 256; i++) {
414 ind_block[i] = zone;
415 if (!NEXT_BAD)
416 goto end_bad;
417 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000418 inode->i_zone[8] = dind = get_free_block();
419 memset(dind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000420 for (i = 0; i < 256; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421 write_block(ind, (char *) ind_block);
422 dind_block[i] = ind = get_free_block();
423 memset(ind_block, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000424 for (j = 0; j < 256; j++) {
425 ind_block[j] = zone;
426 if (!NEXT_BAD)
427 goto end_bad;
428 }
429 }
430 /* Could make triple indirect block here */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 die("too many bad blocks");
432 end_bad:
Eric Andersene494fdd1999-10-19 20:23:03 +0000433 if (ind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 write_block(ind, (char *) ind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000435 if (dind)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436 write_block(dind, (char *) dind_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000437}
438#endif
439
Eric Andersene674eb71999-10-19 20:52:57 +0000440static void make_root_inode(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000441{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
Eric Andersene494fdd1999-10-19 20:23:03 +0000443
444 mark_inode(MINIX_ROOT_INO);
445 inode->i_zone[0] = get_free_block();
446 inode->i_nlinks = 2;
447 inode->i_time = time(NULL);
448 if (badblocks)
Eric Andersene494fdd1999-10-19 20:23:03 +0000449 inode->i_size = 3 * dirsize;
450 else {
451 root_block[2 * dirsize] = '\0';
452 root_block[2 * dirsize + 1] = '\0';
453 inode->i_size = 2 * dirsize;
454 }
455 inode->i_mode = S_IFDIR + 0755;
456 inode->i_uid = getuid();
457 if (inode->i_uid)
458 inode->i_gid = getgid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 write_block(inode->i_zone[0], root_block);
460}
461
462#ifdef HAVE_MINIX2
463static void make_root_inode2(void)
464{
465 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
466
467 mark_inode(MINIX_ROOT_INO);
468 inode->i_zone[0] = get_free_block();
469 inode->i_nlinks = 2;
470 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
471 if (badblocks)
472 inode->i_size = 3 * dirsize;
473 else {
474 root_block[2 * dirsize] = '\0';
475 root_block[2 * dirsize + 1] = '\0';
476 inode->i_size = 2 * dirsize;
477 }
478 inode->i_mode = S_IFDIR + 0755;
479 inode->i_uid = getuid();
480 if (inode->i_uid)
481 inode->i_gid = getgid();
482 write_block(inode->i_zone[0], root_block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000483}
484#endif
485
Eric Andersene674eb71999-10-19 20:52:57 +0000486static void setup_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000487{
488 int i;
489 unsigned long inodes;
490
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491 memset(super_block_buffer, 0, BLOCK_SIZE);
492 memset(boot_block_buffer, 0, 512);
Eric Andersene494fdd1999-10-19 20:23:03 +0000493 MAGIC = magic;
494 ZONESIZE = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
Eric Andersene494fdd1999-10-19 20:23:03 +0000496 ZONES = BLOCKS;
497/* some magic nrs: 1 inode / 3 blocks */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498 if (req_nr_inodes == 0)
499 inodes = BLOCKS / 3;
Eric Andersene494fdd1999-10-19 20:23:03 +0000500 else
501 inodes = req_nr_inodes;
502 /* Round up inode count to fill block size */
503#ifdef HAVE_MINIX2
504 if (version2)
505 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 ~(MINIX2_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000507 else
508#endif
509 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 ~(MINIX_INODES_PER_BLOCK - 1));
Eric Andersene494fdd1999-10-19 20:23:03 +0000511 if (inodes > 65535)
512 inodes = 65535;
513 INODES = inodes;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000515 ZMAPS = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 i = 0;
517 while (ZMAPS !=
518 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
519 BITS_PER_BLOCK) && i < 1000) {
520 ZMAPS =
521 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
522 BITS_PER_BLOCK);
Eric Andersene494fdd1999-10-19 20:23:03 +0000523 i++;
524 }
525 /* Real bad hack but overwise mkfs.minix can be thrown
526 * in infinite loop...
527 * try:
528 * dd if=/dev/zero of=test.fs count=10 bs=1024
529 * /sbin/mkfs.minix -i 200 test.fs
530 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531 if (i >= 999) {
532 die("unable to allocate buffers for maps");
Eric Andersene494fdd1999-10-19 20:23:03 +0000533 }
534 FIRSTZONE = NORM_FIRSTZONE;
Erik Andersen0d068a22000-03-21 22:32:57 +0000535 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
536 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
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);
Erik Andersen0d068a22000-03-21 22:32:57 +0000543 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
545 printf("%ld inodes\n", INODES);
546 printf("%ld blocks\n", ZONES);
547 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
548 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
549 printf("Maxsize=%ld\n\n", MAXSIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000550}
551
552/*
553 * Perform a test of a block; return the number of
554 * blocks readable/writeable.
555 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556long do_check(char *buffer, int try, unsigned int current_block)
Eric Andersene494fdd1999-10-19 20:23:03 +0000557{
558 long got;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 /* Seek to the correct loc. */
561 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 current_block * BLOCK_SIZE) {
563 die("seek failed during testing of blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000564 }
565
566
567 /* Try the read */
568 got = read(DEV, buffer, try * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 if (got < 0)
570 got = 0;
571 if (got & (BLOCK_SIZE - 1)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000572 printf("Weird values in do_check: probably bugs\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000573 }
574 got /= BLOCK_SIZE;
575 return got;
576}
577
578static unsigned int currently_testing = 0;
579
Eric Andersene674eb71999-10-19 20:52:57 +0000580static void alarm_intr(int alnum)
Eric Andersene494fdd1999-10-19 20:23:03 +0000581{
582 if (currently_testing >= ZONES)
583 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000585 alarm(5);
586 if (!currently_testing)
587 return;
588 printf("%d ...", currently_testing);
589 fflush(stdout);
590}
591
Eric Andersene674eb71999-10-19 20:52:57 +0000592static void check_blocks(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000593{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 int try, got;
Eric Andersene494fdd1999-10-19 20:23:03 +0000595 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
596
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 currently_testing = 0;
598 signal(SIGALRM, alarm_intr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000599 alarm(5);
600 while (currently_testing < ZONES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
602 currently_testing * BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +0000603 die("seek failed in check_blocks");
Eric Andersene494fdd1999-10-19 20:23:03 +0000604 try = TEST_BUFFER_BLOCKS;
605 if (currently_testing + try > ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606 try = ZONES - currently_testing;
Eric Andersene494fdd1999-10-19 20:23:03 +0000607 got = do_check(buffer, try, currently_testing);
608 currently_testing += got;
609 if (got == try)
610 continue;
611 if (currently_testing < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000612 die("bad blocks before data-area: cannot make fs");
Eric Andersene494fdd1999-10-19 20:23:03 +0000613 mark_zone(currently_testing);
614 badblocks++;
615 currently_testing++;
616 }
617 if (badblocks > 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000618 printf("%d bad blocks\n", badblocks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000619 else if (badblocks == 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000620 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000621}
622
Eric Andersene674eb71999-10-19 20:52:57 +0000623static void get_list_blocks(filename)
Eric Andersene494fdd1999-10-19 20:23:03 +0000624char *filename;
625
626{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 FILE *listfile;
628 unsigned long blockno;
Eric Andersene494fdd1999-10-19 20:23:03 +0000629
Erik Andersene49d5ec2000-02-08 19:58:47 +0000630 listfile = fopen(filename, "r");
631 if (listfile == (FILE *) NULL) {
632 die("can't open file of bad blocks");
633 }
634 while (!feof(listfile)) {
635 fscanf(listfile, "%ld\n", &blockno);
636 mark_zone(blockno);
637 badblocks++;
638 }
639 if (badblocks > 1)
640 printf("%d bad blocks\n", badblocks);
641 else if (badblocks == 1)
642 printf("one bad block\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000643}
644
Erik Andersene49d5ec2000-02-08 19:58:47 +0000645extern int mkfs_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +0000646{
Eric Andersen32322032000-06-06 22:12:35 +0000647 int i=1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000648 char *tmp;
649 struct stat statbuf;
650 char *listfile = NULL;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000651 int stopIt=FALSE;
Eric Andersene494fdd1999-10-19 20:23:03 +0000652
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 if (argc && *argv)
654 program_name = *argv;
655 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
656 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000657#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
659 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +0000660#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000661
662 /* Parse options */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000663 argv++;
664 while (--argc >= 0 && *argv && **argv) {
665 if (**argv == '-') {
666 stopIt=FALSE;
667 while (i > 0 && *++(*argv) && stopIt==FALSE) {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000668 switch (**argv) {
669 case 'c':
670 check = 1;
671 break;
672 case 'i':
Eric Andersen32322032000-06-06 22:12:35 +0000673 {
674 char *cp=NULL;
675 if (*(*argv+1) != 0) {
676 cp = ++(*argv);
677 } else {
678 if (--argc == 0) {
679 goto goodbye;
680 }
681 cp = *(++argv);
682 }
683 req_nr_inodes = strtoul(cp, &tmp, 0);
684 if (*tmp)
685 show_usage();
Eric Andersen32322032000-06-06 22:12:35 +0000686 stopIt=TRUE;
687 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000688 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000689 case 'l':
690 if (--argc == 0) {
691 goto goodbye;
692 }
693 listfile = *(++argv);
694 break;
695 case 'n':
696 {
697 char *cp=NULL;
698
Erik Andersen330fd2b2000-05-19 05:35:19 +0000699 if (*(*argv+1) != 0) {
700 cp = ++(*argv);
701 } else {
Eric Andersen32322032000-06-06 22:12:35 +0000702 if (--argc == 0) {
703 goto goodbye;
704 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000705 cp = *(++argv);
706 }
707 i = strtoul(cp, &tmp, 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000708 if (*tmp)
709 show_usage();
710 if (i == 14)
711 magic = MINIX_SUPER_MAGIC;
712 else if (i == 30)
713 magic = MINIX_SUPER_MAGIC2;
714 else
715 show_usage();
716 namelen = i;
717 dirsize = i + 2;
718 stopIt=TRUE;
719 break;
720 }
721 case 'v':
Eric Andersene494fdd1999-10-19 20:23:03 +0000722#ifdef HAVE_MINIX2
Erik Andersen330fd2b2000-05-19 05:35:19 +0000723 version2 = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000724#else
Erik Andersen330fd2b2000-05-19 05:35:19 +0000725 fprintf(stderr, "%s: not compiled with minix v2 support\n",
726 program_name, device_name);
727 exit(-1);
Eric Andersene494fdd1999-10-19 20:23:03 +0000728#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +0000729 break;
730 case '-':
731 case 'h':
732 default:
733goodbye:
734 show_usage();
735 }
736 }
737 } else {
Erik Andersen330fd2b2000-05-19 05:35:19 +0000738 if (device_name == NULL)
739 device_name = *argv;
740 else if (BLOCKS == 0)
741 BLOCKS = strtol(*argv, &tmp, 0);
742 else {
743 goto goodbye;
744 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 argv++;
747 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000748
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749 if (device_name && !BLOCKS)
750 BLOCKS = get_size(device_name) / 1024;
751 if (!device_name || BLOCKS < 10) {
752 show_usage();
753 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000754#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000755 if (version2) {
756 if (namelen == 14)
757 magic = MINIX2_SUPER_MAGIC;
758 else
759 magic = MINIX2_SUPER_MAGIC2;
760 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000761#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 if (BLOCKS > 65535)
763 BLOCKS = 65535;
764 check_mount(); /* is it already mounted? */
765 tmp = root_block;
766 *(short *) tmp = 1;
767 strcpy(tmp + 2, ".");
768 tmp += dirsize;
769 *(short *) tmp = 1;
770 strcpy(tmp + 2, "..");
771 tmp += dirsize;
772 *(short *) tmp = 2;
773 strcpy(tmp + 2, ".badblocks");
774 DEV = open(device_name, O_RDWR);
775 if (DEV < 0)
776 die("unable to open %s");
777 if (fstat(DEV, &statbuf) < 0)
778 die("unable to stat %s");
779 if (!S_ISBLK(statbuf.st_mode))
780 check = 0;
781 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
782 die("will not try to make filesystem on '%s'");
783 setup_tables();
784 if (check)
785 check_blocks();
786 else if (listfile)
787 get_list_blocks(listfile);
Eric Andersene494fdd1999-10-19 20:23:03 +0000788#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 if (version2) {
790 make_root_inode2();
791 make_bad_inode2();
792 } else
Eric Andersene494fdd1999-10-19 20:23:03 +0000793#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 {
795 make_root_inode();
796 make_bad_inode();
797 }
798 mark_good_blocks();
799 write_tables();
Eric Andersenbd4a75f2000-06-19 20:03:48 +0000800 return( 0);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000801
Eric Andersene494fdd1999-10-19 20:23:03 +0000802}