blob: 69f5c3972c2d4f1ac9a2367a9d0b329f9994b999 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8/* Usage: mke2fs [options] device
9 *
10 * The device may be a block device or a image of one, but this isn't
11 * enforced (but it's not much fun on a character device :-).
12 */
13
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000014#include <stdio.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <string.h>
16#include <fcntl.h>
17#include <ctype.h>
18#include <termios.h>
19#include <time.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000020#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <getopt.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000022#endif
23#ifdef HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <unistd.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000025#endif
26#ifdef HAVE_STDLIB_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <stdlib.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000028#endif
29#ifdef HAVE_ERRNO_H
30#include <errno.h>
31#endif
32#ifdef HAVE_MNTENT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <mntent.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000034#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000035#include <malloc.h>
36#include <sys/ioctl.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000037#include <sys/types.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000038#include <sys/stat.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000039#include <stdio.h>
Theodore Ts'of3db3561997-04-26 13:34:30 +000040
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000041#ifdef HAVE_LINUX_FS_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000042#include <linux/fs.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000043#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000044#include <linux/ext2_fs.h>
Theodore Ts'o74becf31997-04-26 14:37:06 +000045#ifdef HAVE_LINUX_MAJOR_H
46#include <linux/major.h>
47#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000048
49#include "et/com_err.h"
50#include "ext2fs/ext2fs.h"
51#include "../version.h"
52
53#define STRIDE_LENGTH 8
54
55extern int isatty(int);
Theodore Ts'of3db3561997-04-26 13:34:30 +000056extern FILE *fpopen(const char *cmd, const char *mode);
Theodore Ts'o3839e651997-04-26 13:21:57 +000057
58const char * program_name = "mke2fs";
59const char * device_name = NULL;
60
61/* Command line options */
62int cflag = 0;
63int verbose = 0;
64int quiet = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +000065int super_only = 0;
Theodore Ts'o74becf31997-04-26 14:37:06 +000066int force = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000067char *bad_blocks_filename = 0;
68
69struct ext2_super_block param;
70
71static void usage(NOARGS)
72{
73 fprintf(stderr,
74 "Usage: %s [-c|-t|-l filename] [-b block-size] "
75 "[-f fragment-size]\n\t[-i bytes-per-inode] "
Theodore Ts'of3db3561997-04-26 13:34:30 +000076 "[-m reserved-blocks-percentage] [-qvS]\n"
77 "\t[-g blocks-per-group] device [blocks-count]\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +000078 program_name);
79 exit(1);
80}
81
82static int log2(int arg)
83{
84 int l = 0;
85
86 arg >>= 1;
87 while (arg) {
88 l++;
89 arg >>= 1;
90 }
91 return l;
92}
93
Theodore Ts'o74becf31997-04-26 14:37:06 +000094static void check_plausibility(NOARGS)
95{
96#ifdef HAVE_LINUX_MAJOR_H
97 int val;
98 struct stat s;
99
100 val = stat(device_name, &s);
101
102 if(val == -1) {
103 perror("stat");
104 exit(1);
105 }
106 if(!S_ISBLK(s.st_mode)) {
107 printf("%s is not a block special device.\n", device_name);
108 printf("Proceed anyway? (y,n) ");
109 if (getchar() != 'y')
110 exit(1);
111 return;
112 }
113 if ((MAJOR(s.st_rdev) == HD_MAJOR && MINOR(s.st_rdev)%64 == 0) ||
114 (MAJOR(s.st_rdev) == SCSI_DISK_MAJOR &&
115 MINOR(s.st_rdev)%16 == 0)) {
116 printf("%s is entire device, not just one partition!\n",
117 device_name);
118 printf("Proceed anyway? (y,n) ");
119 if (getchar() != 'y')
120 exit(1);
121 return;
122 }
123#endif
124}
125
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126static void check_mount(NOARGS)
127{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000128 errcode_t retval;
129 int mount_flags;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000131 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
132 if (retval) {
133 com_err("ext2fs_check_if_mount", retval,
134 "while determining whether %s is mounted.",
135 device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000137 }
138 if (!(mount_flags & EXT2_MF_MOUNTED))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139 return;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000140
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
142 device_name);
143 exit(1);
144}
145
146/*
147 * Helper function for read_bb_file and test_disk
148 */
149static void invalid_block(ext2_filsys fs, blk_t blk)
150{
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000151 printf("Bad block %u out of range; ignored.\n", blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 return;
153}
154
155/*
156 * Reads the bad blocks list from a file
157 */
158static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
159 const char *bad_blocks_file)
160{
161 FILE *f;
162 errcode_t retval;
163
164 f = fopen(bad_blocks_file, "r");
165 if (!f) {
166 com_err("read_bad_blocks_file", errno,
167 "while trying to open %s", bad_blocks_file);
168 exit(1);
169 }
170 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
171 fclose (f);
172 if (retval) {
173 com_err("ext2fs_read_bb_FILE", retval,
174 "while reading in list of bad blocks from file");
175 exit(1);
176 }
177}
178
179/*
180 * Runs the badblocks program to test the disk
181 */
182static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
183{
184 FILE *f;
185 errcode_t retval;
186 char buf[1024];
187
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000188 sprintf(buf, "badblocks %s%s %d", quiet ? "" : "-s ",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 fs->device_name,
190 fs->super->s_blocks_count);
191 if (verbose)
192 printf("Running command: %s\n", buf);
193 f = popen(buf, "r");
194 if (!f) {
195 com_err("popen", errno,
196 "while trying run '%s'", buf);
197 exit(1);
198 }
199 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000200 pclose(f);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 if (retval) {
202 com_err("ext2fs_read_bb_FILE", retval,
203 "while processing list of bad blocks from program");
204 exit(1);
205 }
206}
207
208static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
209{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000210 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 int must_be_good;
212 blk_t blk;
213 badblocks_iterate bb_iter;
214 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000215 blk_t group_block;
216 int group;
217 int group_bad;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000218
219 if (!bb_list)
220 return;
221
222 /*
223 * The primary superblock and group descriptors *must* be
224 * good; if not, abort.
225 */
226 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
227 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
228 if (badblocks_list_test(bb_list, i)) {
229 fprintf(stderr, "Block %d in primary superblock/group "
230 "descriptor area bad.\n", i);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000231 fprintf(stderr, "Blocks %d through %d must be good "
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 "in order to build a filesystem.\n",
233 fs->super->s_first_data_block, must_be_good);
234 fprintf(stderr, "Aborting....\n");
235 exit(1);
236 }
237 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000238
239 /*
240 * See if any of the bad blocks are showing up in the backup
241 * superblocks and/or group descriptors. If so, issue a
242 * warning and adjust the block counts appropriately.
243 */
244 group_block = fs->super->s_first_data_block +
245 fs->super->s_blocks_per_group;
246 group_bad = 0;
247
248 for (i = 1; i < fs->group_desc_count; i++) {
249 for (j=0; j < fs->desc_blocks+1; j++) {
250 if (badblocks_list_test(bb_list, group_block +
251 j)) {
252 if (!group_bad)
253 fprintf(stderr,
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000254"Warning: the backup superblock/group descriptors at block %d contain\n"
Theodore Ts'of3db3561997-04-26 13:34:30 +0000255" bad blocks.\n\n",
256 group_block);
257 group_bad++;
258 group = ext2fs_group_of_blk(fs, group_block+j);
259 fs->group_desc[group].bg_free_blocks_count++;
260 fs->super->s_free_blocks_count++;
261 }
262 }
263 group_block += fs->super->s_blocks_per_group;
264 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265
266 /*
267 * Mark all the bad blocks as used...
268 */
269 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
270 if (retval) {
271 com_err("badblocks_list_iterate_begin", retval,
272 "while marking bad blocks as used");
273 exit(1);
274 }
275 while (badblocks_list_iterate(bb_iter, &blk))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000276 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000277 badblocks_list_iterate_end(bb_iter);
278}
279
280static void new_table_block(ext2_filsys fs, blk_t first_block,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000281 const char *name, int num, int initialize,
282 const char *buf, blk_t *new_block)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283{
284 errcode_t retval;
285 blk_t blk;
286 int i;
287 int count;
288
289 retval = ext2fs_get_free_blocks(fs, first_block,
290 first_block + fs->super->s_blocks_per_group,
291 num, fs->block_map, new_block);
292 if (retval) {
293 printf("Could not allocate %d block(s) for %s: %s\n",
294 num, name, error_message(retval));
295 ext2fs_unmark_valid(fs);
296 return;
297 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000298 if (initialize) {
299 blk = *new_block;
300 for (i=0; i < num; i += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
301 if (num-i > STRIDE_LENGTH)
302 count = STRIDE_LENGTH;
303 else
304 count = num - i;
305 retval = io_channel_write_blk(fs->io, blk, count, buf);
306 if (retval)
307 printf("Warning: could not write %d blocks "
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000308 "starting at %d for %s: %s\n",
Theodore Ts'of3db3561997-04-26 13:34:30 +0000309 count, blk, name,
310 error_message(retval));
311 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000312 }
313 blk = *new_block;
314 for (i = 0; i < num; i++, blk++)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000315 ext2fs_mark_block_bitmap(fs->block_map, blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000316}
317
318static void alloc_tables(ext2_filsys fs)
319{
320 blk_t group_blk;
321 int i;
322 char *buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000323
324 buf = malloc(fs->blocksize * STRIDE_LENGTH);
325 if (!buf) {
326 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
327 exit(1);
328 }
329 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
330
331 group_blk = fs->super->s_first_data_block;
332 if (!quiet)
333 printf("Writing inode tables: ");
334 for (i = 0; i < fs->group_desc_count; i++) {
335 if (!quiet)
336 printf("%4d/%4ld", i, fs->group_desc_count);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000337 new_table_block(fs, group_blk, "block bitmap", 1, 0, buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000338 &fs->group_desc[i].bg_block_bitmap);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000339 new_table_block(fs, group_blk, "inode bitmap", 1, 0, buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 &fs->group_desc[i].bg_inode_bitmap);
341 new_table_block(fs, group_blk, "inode table",
Theodore Ts'of3db3561997-04-26 13:34:30 +0000342 fs->inode_blocks_per_group,
343 !super_only, buf,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000344 &fs->group_desc[i].bg_inode_table);
345
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346 group_blk += fs->super->s_blocks_per_group;
347 if (!quiet)
348 printf("\b\b\b\b\b\b\b\b\b");
349 }
350 if (!quiet)
351 printf("done \n");
352}
353
354static void create_root_dir(ext2_filsys fs)
355{
356 errcode_t retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000357 struct ext2_inode inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000358
359 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
360 if (retval) {
361 com_err("ext2fs_mkdir", retval, "while creating root dir");
362 exit(1);
363 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000364 if (geteuid()) {
365 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
366 if (retval) {
367 com_err("ext2fs_read_inode", retval,
368 "while reading root inode");
369 exit(1);
370 }
371 inode.i_uid = geteuid();
372 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
373 if (retval) {
374 com_err("ext2fs_write_inode", retval,
375 "while setting root inode ownership");
376 exit(1);
377 }
378 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000379}
380
381static void create_lost_and_found(ext2_filsys fs)
382{
383 errcode_t retval;
384 ino_t ino;
385 const char *name = "lost+found";
386 int i;
387
388 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
389 if (retval) {
390 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
391 exit(1);
392 }
393
394 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
395 if (retval) {
396 com_err("ext2_lookup", retval, "while looking up /lost+found");
397 exit(1);
398 }
399
400 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
401 retval = ext2fs_expand_dir(fs, ino);
402 if (retval) {
403 com_err("ext2fs_expand_dir", retval,
404 "while expanding /lost+found");
405 exit(1);
406 }
407 }
408}
409
410static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
411{
412 errcode_t retval;
413
Theodore Ts'of3db3561997-04-26 13:34:30 +0000414 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000415 fs->group_desc[0].bg_free_inodes_count--;
416 fs->super->s_free_inodes_count--;
417 retval = ext2fs_update_bb_inode(fs, bb_list);
418 if (retval) {
419 com_err("ext2fs_update_bb_inode", retval,
420 "while setting bad block inode");
421 exit(1);
422 }
423
424}
425
426static void reserve_inodes(ext2_filsys fs)
427{
428 ino_t i;
429 int group;
430
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000431 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000432 ext2fs_mark_inode_bitmap(fs->inode_map, i);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000433 group = ext2fs_group_of_ino(fs, i);
434 fs->group_desc[group].bg_free_inodes_count--;
435 fs->super->s_free_inodes_count--;
436 }
437 ext2fs_mark_ib_dirty(fs);
438}
439
Theodore Ts'of3db3561997-04-26 13:34:30 +0000440static void zap_bootblock(ext2_filsys fs)
441{
442 char buf[512];
443 int retval;
444
445 memset(buf, 0, 512);
446
447 retval = io_channel_write_blk(fs->io, 0, -512, buf);
448 if (retval)
449 printf("Warning: could not erase block 0: %s\n",
450 error_message(retval));
451}
452
453
Theodore Ts'o3839e651997-04-26 13:21:57 +0000454static void show_stats(ext2_filsys fs)
455{
456 struct ext2_super_block *s = fs->super;
457 blk_t group_block;
458 int i, col_left;
459
460 if (param.s_blocks_count != s->s_blocks_count)
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000461 printf("warning: %d blocks unused.\n\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000462 param.s_blocks_count - s->s_blocks_count);
463
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000464 printf("%u inodes, %u blocks\n", s->s_inodes_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000466 printf("%u blocks (%2.2f%%) reserved for the super user\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 s->s_r_blocks_count,
468 100.0 * s->s_r_blocks_count / s->s_blocks_count);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000469 printf("First data block=%u\n", s->s_first_data_block);
470 printf("Block size=%u (log=%u)\n", fs->blocksize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471 s->s_log_block_size);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000472 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 s->s_log_frag_size);
474 printf("%lu block group%s\n", fs->group_desc_count,
475 (fs->group_desc_count > 1) ? "s" : "");
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000476 printf("%u blocks per group, %u fragments per group\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 s->s_blocks_per_group, s->s_frags_per_group);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000478 printf("%u inodes per group\n", s->s_inodes_per_group);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479
480 if (fs->group_desc_count == 1) {
481 printf("\n");
482 return;
483 }
484
485 printf("Superblock backups stored on blocks: ");
486 group_block = s->s_first_data_block;
487 col_left = 0;
488 for (i = 1; i < fs->group_desc_count; i++) {
489 group_block += s->s_blocks_per_group;
490 if (!col_left--) {
491 printf("\n\t");
492 col_left = 8;
493 }
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000494 printf("%u", group_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495 if (i != fs->group_desc_count - 1)
496 printf(", ");
497 }
498 printf("\n\n");
499}
500
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000501#define PATH_SET "PATH=/sbin"
502
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503static void PRS(int argc, char *argv[])
504{
505 char c;
506 int size;
507 char * tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 int inode_ratio = 4096;
509 int reserved_ratio = 5;
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000510 errcode_t retval;
511 char *oldpath = getenv("PATH");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512
513 /* Update our PATH to include /sbin */
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000514 if (oldpath) {
515 char *newpath;
516
517 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
518 strcpy (newpath, PATH_SET);
519 strcat (newpath, ":");
520 strcat (newpath, oldpath);
521 putenv (newpath);
522 } else
523 putenv (PATH_SET);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524
525 setbuf(stdout, NULL);
526 setbuf(stderr, NULL);
527 initialize_ext2_error_table();
528 memset(&param, 0, sizeof(struct ext2_super_block));
529
530 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
531 E2FSPROGS_VERSION, E2FSPROGS_DATE,
532 EXT2FS_VERSION, EXT2FS_DATE);
533 if (argc && *argv)
534 program_name = *argv;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000535 while ((c = getopt (argc, argv, "b:cf:g:i:l:m:qr:tvI:SF")) != EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 switch (c) {
537 case 'b':
538 size = strtoul(optarg, &tmp, 0);
539 if (size < 1024 || size > 4096 || *tmp) {
540 com_err(program_name, 0, "bad block size - %s",
541 optarg);
542 exit(1);
543 }
544 param.s_log_block_size =
545 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
546 break;
547 case 'c':
548 case 't': /* Check for bad blocks */
549 cflag = 1;
550 break;
551 case 'f':
552 size = strtoul(optarg, &tmp, 0);
553 if (size < 1024 || size > 4096 || *tmp) {
554 com_err(program_name, 0, "bad fragment size - %s",
555 optarg);
556 exit(1);
557 }
558 param.s_log_frag_size =
559 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
560 printf("Warning: fragments not supported. "
561 "Ignoring -f option\n");
562 break;
563 case 'g':
564 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565 if (*tmp) {
566 com_err(program_name, 0,
567 "Illegal number for blocks per group");
568 exit(1);
569 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570 if (param.s_blocks_per_group < 256 ||
571 param.s_blocks_per_group > 8192 || *tmp) {
572 com_err(program_name, 0,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000573 "blocks per group count out of range");
574 exit(1);
575 }
576 if ((param.s_blocks_per_group % 8) != 0) {
577 com_err(program_name, 0,
578 "blocks per group must be multiple of 8");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 exit(1);
580 }
581 break;
582 case 'i':
583 inode_ratio = strtoul(optarg, &tmp, 0);
584 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
585 *tmp) {
586 com_err(program_name, 0, "bad inode ratio - %s",
587 optarg);
588 exit(1);
589 }
590 break;
591 case 'l':
Theodore Ts'of3db3561997-04-26 13:34:30 +0000592 bad_blocks_filename = malloc(strlen(optarg)+1);
593 if (!bad_blocks_filename) {
594 com_err(program_name, ENOMEM,
595 "in malloc for bad_blocks_filename");
596 exit(1);
597 }
598 strcpy(bad_blocks_filename, optarg);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000599 break;
600 case 'm':
601 reserved_ratio = strtoul(optarg, &tmp, 0);
602 if (reserved_ratio > 50 || *tmp) {
603 com_err(program_name, 0,
604 "bad reserved blocks percent - %s",
605 optarg);
606 exit(1);
607 }
608 break;
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000609 case 'r':
610 param.s_rev_level = atoi(optarg);
611 break;
612#ifdef EXT2_DYNAMIC_REV
613 case 'I':
614 param.s_inode_size = atoi(optarg);
615 break;
616#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 case 'v':
618 verbose = 1;
619 break;
620 case 'q':
621 quiet = 1;
622 break;
Theodore Ts'o74becf31997-04-26 14:37:06 +0000623 case 'F':
624 force = 1;
625 break;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000626 case 'S':
627 super_only = 1;
628 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 default:
630 usage();
631 }
632 if (optind == argc)
633 usage();
634 device_name = argv[optind];
635 optind++;
636 if (optind < argc) {
637 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
638 if (*tmp) {
639 com_err(program_name, 0, "bad blocks count - %s",
640 argv[optind - 1]);
641 exit(1);
642 }
643 }
644 if (optind < argc)
645 usage();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000646
Theodore Ts'o74becf31997-04-26 14:37:06 +0000647 if (!force)
648 check_plausibility();
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000649 check_mount();
650
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 param.s_log_frag_size = param.s_log_block_size;
652
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000653 if (!param.s_blocks_count) {
654 retval = ext2fs_get_device_size(device_name,
655 EXT2_BLOCK_SIZE(&param),
656 &param.s_blocks_count);
657 if (retval) {
658 com_err(program_name, 0,
659 "while trying to determine filesystem size");
660 exit(1);
661 }
662 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663
664 /*
665 * Calculate number of inodes based on the inode ratio
666 */
667 param.s_inodes_count =
Theodore Ts'of3db3561997-04-26 13:34:30 +0000668 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
669 / inode_ratio;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670
671 /*
672 * Calculate number of blocks to reserve
673 */
674 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
675}
676
677int main (int argc, char *argv[])
678{
679 errcode_t retval = 0;
680 ext2_filsys fs;
681 badblocks_list bb_list = 0;
682
683 PRS(argc, argv);
684
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685 /*
686 * Initialize the superblock....
687 */
688 retval = ext2fs_initialize(device_name, 0, &param,
689 unix_io_manager, &fs);
690 if (retval) {
691 com_err(device_name, retval, "while setting up superblock");
692 exit(1);
693 }
694
695 if (!quiet)
696 show_stats(fs);
697
698 if (bad_blocks_filename)
699 read_bb_file(fs, &bb_list, bad_blocks_filename);
700 if (cflag)
701 test_disk(fs, &bb_list);
702
703 handle_bad_blocks(fs, bb_list);
704 alloc_tables(fs);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000705 if (super_only) {
706 fs->super->s_state |= EXT2_ERROR_FS;
707 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
708 } else {
709 create_root_dir(fs);
710 create_lost_and_found(fs);
711 reserve_inodes(fs);
712 create_bad_block_inode(fs, bb_list);
713 zap_bootblock(fs);
714 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715
716 if (!quiet)
717 printf("Writing superblocks and "
718 "filesystem accounting information: ");
719 ext2fs_close(fs);
720 if (!quiet)
721 printf("done\n");
722 return 0;
723}