1 /*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13 /* Usage: mke2fs [options] device
14 *
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
17 */
18
19 #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <fcntl.h>
25 #include <ctype.h>
26 #include <time.h>
27 #ifdef __linux__
28 #include <sys/utsname.h>
29 #endif
30 #ifdef HAVE_GETOPT_H
31 #include <getopt.h>
32 #else
33 extern char *optarg;
34 extern int optind;
35 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #ifdef HAVE_STDLIB_H
40 #include <stdlib.h>
41 #endif
42 #ifdef HAVE_ERRNO_H
43 #include <errno.h>
44 #endif
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <libgen.h>
49 #include <limits.h>
50 #include <blkid/blkid.h>
51
52 #include "ext2fs/ext2_fs.h"
53 #include "ext2fs/ext2fsP.h"
54 #include "et/com_err.h"
55 #include "uuid/uuid.h"
56 #include "e2p/e2p.h"
57 #include "ext2fs/ext2fs.h"
58 #include "util.h"
59 #include "profile.h"
60 #include "prof_err.h"
61 #include "../version.h"
62 #include "nls-enable.h"
63 #include "quota/mkquota.h"
64
65 #define STRIDE_LENGTH 8
66
67 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
68
69 #ifndef __sparc__
70 #define ZAP_BOOTBLOCK
71 #endif
72
73 #ifndef ROOT_SYSCONFDIR
74 #define ROOT_SYSCONFDIR "/etc"
75 #endif
76
77 #define DISCARD_STEP_MB (2048)
78
79 extern int isatty(int);
80 extern FILE *fpopen(const char *cmd, const char *mode);
81
82 static const char * program_name = "mke2fs";
83 static const char * device_name /* = NULL */;
84
85 /* Command line options */
86 static int cflag;
87 static int verbose;
88 static int quiet;
89 static int super_only;
90 static int discard = 1; /* attempt to discard device before fs creation */
91 static int direct_io;
92 static int force;
93 static int noaction;
94 static uid_t root_uid;
95 static gid_t root_gid;
96 int journal_size;
97 int journal_flags;
98 static int lazy_itable_init;
99 static char *bad_blocks_filename = NULL;
100 static __u32 fs_stride;
101 static int quotatype = -1; /* Initialize both user and group quotas by default */
102
103 static struct ext2_super_block fs_param;
104 static char *fs_uuid = NULL;
105 static char *creator_os;
106 static char *volume_label;
107 static char *mount_dir;
108 char *journal_device;
109 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
110 static char **fs_types;
111
112 static profile_t profile;
113
114 static int sys_page_size = 4096;
115 static int linux_version_code = 0;
116
usage(void)117 static void usage(void)
118 {
119 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
120 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
121 "[-J journal-options]\n"
122 "\t[-G flex-group-size] [-N number-of-inodes]\n"
123 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
124 "\t[-g blocks-per-group] [-L volume-label] "
125 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
126 "[-r fs-revision] [-E extended-option[,...]]\n"
127 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
128 "[-jnqvDFKSV] device [blocks-count]\n"),
129 program_name);
130 exit(1);
131 }
132
int_log2(unsigned long long arg)133 static int int_log2(unsigned long long arg)
134 {
135 int l = 0;
136
137 arg >>= 1;
138 while (arg) {
139 l++;
140 arg >>= 1;
141 }
142 return l;
143 }
144
int_log10(unsigned long long arg)145 static int int_log10(unsigned long long arg)
146 {
147 int l;
148
149 for (l=0; arg ; l++)
150 arg = arg / 10;
151 return l;
152 }
153
154 #ifdef __linux__
parse_version_number(const char * s)155 static int parse_version_number(const char *s)
156 {
157 int major, minor, rev;
158 char *endptr;
159 const char *cp = s;
160
161 if (!s)
162 return 0;
163 major = strtol(cp, &endptr, 10);
164 if (cp == endptr || *endptr != '.')
165 return 0;
166 cp = endptr + 1;
167 minor = strtol(cp, &endptr, 10);
168 if (cp == endptr || *endptr != '.')
169 return 0;
170 cp = endptr + 1;
171 rev = strtol(cp, &endptr, 10);
172 if (cp == endptr)
173 return 0;
174 return ((((major * 256) + minor) * 256) + rev);
175 }
176 #endif
177
178 /*
179 * Helper function for read_bb_file and test_disk
180 */
invalid_block(ext2_filsys fs EXT2FS_ATTR ((unused)),blk_t blk)181 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
182 {
183 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
184 return;
185 }
186
187 /*
188 * Reads the bad blocks list from a file
189 */
read_bb_file(ext2_filsys fs,badblocks_list * bb_list,const char * bad_blocks_file)190 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
191 const char *bad_blocks_file)
192 {
193 FILE *f;
194 errcode_t retval;
195
196 f = fopen(bad_blocks_file, "r");
197 if (!f) {
198 com_err("read_bad_blocks_file", errno,
199 _("while trying to open %s"), bad_blocks_file);
200 exit(1);
201 }
202 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
203 fclose (f);
204 if (retval) {
205 com_err("ext2fs_read_bb_FILE", retval, "%s",
206 _("while reading in list of bad blocks from file"));
207 exit(1);
208 }
209 }
210
211 /*
212 * Runs the badblocks program to test the disk
213 */
test_disk(ext2_filsys fs,badblocks_list * bb_list)214 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
215 {
216 FILE *f;
217 errcode_t retval;
218 char buf[1024];
219
220 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
221 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
222 fs->device_name, ext2fs_blocks_count(fs->super)-1);
223 if (verbose)
224 printf(_("Running command: %s\n"), buf);
225 f = popen(buf, "r");
226 if (!f) {
227 com_err("popen", errno,
228 _("while trying to run '%s'"), buf);
229 exit(1);
230 }
231 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
232 pclose(f);
233 if (retval) {
234 com_err("ext2fs_read_bb_FILE", retval, "%s",
235 _("while processing list of bad blocks from program"));
236 exit(1);
237 }
238 }
239
handle_bad_blocks(ext2_filsys fs,badblocks_list bb_list)240 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
241 {
242 dgrp_t i;
243 blk_t j;
244 unsigned must_be_good;
245 blk_t blk;
246 badblocks_iterate bb_iter;
247 errcode_t retval;
248 blk_t group_block;
249 int group;
250 int group_bad;
251
252 if (!bb_list)
253 return;
254
255 /*
256 * The primary superblock and group descriptors *must* be
257 * good; if not, abort.
258 */
259 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
260 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
261 if (ext2fs_badblocks_list_test(bb_list, i)) {
262 fprintf(stderr, _("Block %d in primary "
263 "superblock/group descriptor area bad.\n"), i);
264 fprintf(stderr, _("Blocks %u through %u must be good "
265 "in order to build a filesystem.\n"),
266 fs->super->s_first_data_block, must_be_good);
267 fputs(_("Aborting....\n"), stderr);
268 exit(1);
269 }
270 }
271
272 /*
273 * See if any of the bad blocks are showing up in the backup
274 * superblocks and/or group descriptors. If so, issue a
275 * warning and adjust the block counts appropriately.
276 */
277 group_block = fs->super->s_first_data_block +
278 fs->super->s_blocks_per_group;
279
280 for (i = 1; i < fs->group_desc_count; i++) {
281 group_bad = 0;
282 for (j=0; j < fs->desc_blocks+1; j++) {
283 if (ext2fs_badblocks_list_test(bb_list,
284 group_block + j)) {
285 if (!group_bad)
286 fprintf(stderr,
287 _("Warning: the backup superblock/group descriptors at block %u contain\n"
288 " bad blocks.\n\n"),
289 group_block);
290 group_bad++;
291 group = ext2fs_group_of_blk2(fs, group_block+j);
292 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
293 ext2fs_group_desc_csum_set(fs, group);
294 ext2fs_free_blocks_count_add(fs->super, 1);
295 }
296 }
297 group_block += fs->super->s_blocks_per_group;
298 }
299
300 /*
301 * Mark all the bad blocks as used...
302 */
303 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
304 if (retval) {
305 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
306 _("while marking bad blocks as used"));
307 exit(1);
308 }
309 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
310 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
311 ext2fs_badblocks_list_iterate_end(bb_iter);
312 }
313
write_inode_tables(ext2_filsys fs,int lazy_flag,int itable_zeroed)314 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
315 {
316 errcode_t retval;
317 blk64_t blk;
318 dgrp_t i;
319 int num;
320 struct ext2fs_numeric_progress_struct progress;
321
322 ext2fs_numeric_progress_init(fs, &progress,
323 _("Writing inode tables: "),
324 fs->group_desc_count);
325
326 for (i = 0; i < fs->group_desc_count; i++) {
327 ext2fs_numeric_progress_update(fs, &progress, i);
328
329 blk = ext2fs_inode_table_loc(fs, i);
330 num = fs->inode_blocks_per_group;
331
332 if (lazy_flag)
333 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
334 ext2fs_bg_itable_unused(fs, i)) *
335 EXT2_INODE_SIZE(fs->super),
336 EXT2_BLOCK_SIZE(fs->super));
337 if (!lazy_flag || itable_zeroed) {
338 /* The kernel doesn't need to zero the itable blocks */
339 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
340 ext2fs_group_desc_csum_set(fs, i);
341 }
342 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
343 if (retval) {
344 fprintf(stderr, _("\nCould not write %d "
345 "blocks in inode table starting at %llu: %s\n"),
346 num, blk, error_message(retval));
347 exit(1);
348 }
349 if (sync_kludge) {
350 if (sync_kludge == 1)
351 sync();
352 else if ((i % sync_kludge) == 0)
353 sync();
354 }
355 }
356 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
357 ext2fs_numeric_progress_close(fs, &progress,
358 _("done \n"));
359 }
360
create_root_dir(ext2_filsys fs)361 static void create_root_dir(ext2_filsys fs)
362 {
363 errcode_t retval;
364 struct ext2_inode inode;
365
366 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
367 if (retval) {
368 com_err("ext2fs_mkdir", retval, "%s",
369 _("while creating root dir"));
370 exit(1);
371 }
372 if (root_uid != 0 || root_gid != 0) {
373 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
374 if (retval) {
375 com_err("ext2fs_read_inode", retval, "%s",
376 _("while reading root inode"));
377 exit(1);
378 }
379
380 inode.i_uid = root_uid;
381 ext2fs_set_i_uid_high(inode, root_uid >> 16);
382 inode.i_gid = root_gid;
383 ext2fs_set_i_gid_high(inode, root_gid >> 16);
384
385 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
386 if (retval) {
387 com_err("ext2fs_write_inode", retval, "%s",
388 _("while setting root inode ownership"));
389 exit(1);
390 }
391 }
392 }
393
create_lost_and_found(ext2_filsys fs)394 static void create_lost_and_found(ext2_filsys fs)
395 {
396 unsigned int lpf_size = 0;
397 errcode_t retval;
398 ext2_ino_t ino;
399 const char *name = "lost+found";
400 int i;
401
402 fs->umask = 077;
403 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
404 if (retval) {
405 com_err("ext2fs_mkdir", retval, "%s",
406 _("while creating /lost+found"));
407 exit(1);
408 }
409
410 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
411 if (retval) {
412 com_err("ext2_lookup", retval, "%s",
413 _("while looking up /lost+found"));
414 exit(1);
415 }
416
417 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
418 /* Ensure that lost+found is at least 2 blocks, so we always
419 * test large empty blocks for big-block filesystems. */
420 if ((lpf_size += fs->blocksize) >= 16*1024 &&
421 lpf_size >= 2 * fs->blocksize)
422 break;
423 retval = ext2fs_expand_dir(fs, ino);
424 if (retval) {
425 com_err("ext2fs_expand_dir", retval, "%s",
426 _("while expanding /lost+found"));
427 exit(1);
428 }
429 }
430 }
431
create_bad_block_inode(ext2_filsys fs,badblocks_list bb_list)432 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
433 {
434 errcode_t retval;
435
436 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
437 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
438 retval = ext2fs_update_bb_inode(fs, bb_list);
439 if (retval) {
440 com_err("ext2fs_update_bb_inode", retval, "%s",
441 _("while setting bad block inode"));
442 exit(1);
443 }
444
445 }
446
reserve_inodes(ext2_filsys fs)447 static void reserve_inodes(ext2_filsys fs)
448 {
449 ext2_ino_t i;
450
451 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
452 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
453 ext2fs_mark_ib_dirty(fs);
454 }
455
456 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
457 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
458 #define BSD_LABEL_OFFSET 64
459
zap_sector(ext2_filsys fs,int sect,int nsect)460 static void zap_sector(ext2_filsys fs, int sect, int nsect)
461 {
462 char *buf;
463 int retval;
464 unsigned int *magic;
465
466 buf = malloc(512*nsect);
467 if (!buf) {
468 printf(_("Out of memory erasing sectors %d-%d\n"),
469 sect, sect + nsect - 1);
470 exit(1);
471 }
472
473 if (sect == 0) {
474 /* Check for a BSD disklabel, and don't erase it if so */
475 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
476 if (retval)
477 fprintf(stderr,
478 _("Warning: could not read block 0: %s\n"),
479 error_message(retval));
480 else {
481 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
482 if ((*magic == BSD_DISKMAGIC) ||
483 (*magic == BSD_MAGICDISK))
484 return;
485 }
486 }
487
488 memset(buf, 0, 512*nsect);
489 io_channel_set_blksize(fs->io, 512);
490 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
491 io_channel_set_blksize(fs->io, fs->blocksize);
492 free(buf);
493 if (retval)
494 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
495 sect, error_message(retval));
496 }
497
create_journal_dev(ext2_filsys fs)498 static void create_journal_dev(ext2_filsys fs)
499 {
500 struct ext2fs_numeric_progress_struct progress;
501 errcode_t retval;
502 char *buf;
503 blk64_t blk, err_blk;
504 int c, count, err_count;
505
506 retval = ext2fs_create_journal_superblock(fs,
507 ext2fs_blocks_count(fs->super), 0, &buf);
508 if (retval) {
509 com_err("create_journal_dev", retval, "%s",
510 _("while initializing journal superblock"));
511 exit(1);
512 }
513
514 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
515 goto write_superblock;
516
517 ext2fs_numeric_progress_init(fs, &progress,
518 _("Zeroing journal device: "),
519 ext2fs_blocks_count(fs->super));
520 blk = 0;
521 count = ext2fs_blocks_count(fs->super);
522 while (count > 0) {
523 if (count > 1024)
524 c = 1024;
525 else
526 c = count;
527 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
528 if (retval) {
529 com_err("create_journal_dev", retval,
530 _("while zeroing journal device "
531 "(block %llu, count %d)"),
532 err_blk, err_count);
533 exit(1);
534 }
535 blk += c;
536 count -= c;
537 ext2fs_numeric_progress_update(fs, &progress, blk);
538 }
539 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
540
541 ext2fs_numeric_progress_close(fs, &progress, NULL);
542 write_superblock:
543 retval = io_channel_write_blk64(fs->io,
544 fs->super->s_first_data_block+1,
545 1, buf);
546 if (retval) {
547 com_err("create_journal_dev", retval, "%s",
548 _("while writing journal superblock"));
549 exit(1);
550 }
551 }
552
show_stats(ext2_filsys fs)553 static void show_stats(ext2_filsys fs)
554 {
555 struct ext2_super_block *s = fs->super;
556 char buf[80];
557 char *os;
558 blk64_t group_block;
559 dgrp_t i;
560 int need, col_left;
561
562 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
563 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
564 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
565
566 memset(buf, 0, sizeof(buf));
567 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
568 printf(_("Filesystem label=%s\n"), buf);
569 os = e2p_os2string(fs->super->s_creator_os);
570 if (os)
571 printf(_("OS type: %s\n"), os);
572 free(os);
573 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
574 s->s_log_block_size);
575 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
576 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
577 printf(_("Cluster size=%u (log=%u)\n"),
578 fs->blocksize << fs->cluster_ratio_bits,
579 s->s_log_cluster_size);
580 else
581 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
582 s->s_log_cluster_size);
583 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
584 s->s_raid_stride, s->s_raid_stripe_width);
585 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
586 ext2fs_blocks_count(s));
587 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
588 ext2fs_r_blocks_count(s),
589 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
590 printf(_("First data block=%u\n"), s->s_first_data_block);
591 if (root_uid != 0 || root_gid != 0)
592 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
593 if (s->s_reserved_gdt_blocks)
594 printf(_("Maximum filesystem blocks=%lu\n"),
595 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
596 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
597 if (fs->group_desc_count > 1)
598 printf(_("%u block groups\n"), fs->group_desc_count);
599 else
600 printf(_("%u block group\n"), fs->group_desc_count);
601 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
602 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
603 printf(_("%u blocks per group, %u clusters per group\n"),
604 s->s_blocks_per_group, s->s_clusters_per_group);
605 else
606 printf(_("%u blocks per group, %u fragments per group\n"),
607 s->s_blocks_per_group, s->s_clusters_per_group);
608 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
609
610 if (fs->group_desc_count == 1) {
611 printf("\n");
612 return;
613 }
614
615 printf("%s", _("Superblock backups stored on blocks: "));
616 group_block = s->s_first_data_block;
617 col_left = 0;
618 for (i = 1; i < fs->group_desc_count; i++) {
619 group_block += s->s_blocks_per_group;
620 if (!ext2fs_bg_has_super(fs, i))
621 continue;
622 if (i != 1)
623 printf(", ");
624 need = int_log10(group_block) + 2;
625 if (need > col_left) {
626 printf("\n\t");
627 col_left = 72;
628 }
629 col_left -= need;
630 printf("%llu", group_block);
631 }
632 printf("\n\n");
633 }
634
635 /*
636 * Set the S_CREATOR_OS field. Return true if OS is known,
637 * otherwise, 0.
638 */
set_os(struct ext2_super_block * sb,char * os)639 static int set_os(struct ext2_super_block *sb, char *os)
640 {
641 if (isdigit (*os))
642 sb->s_creator_os = atoi (os);
643 else if (strcasecmp(os, "linux") == 0)
644 sb->s_creator_os = EXT2_OS_LINUX;
645 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
646 sb->s_creator_os = EXT2_OS_HURD;
647 else if (strcasecmp(os, "freebsd") == 0)
648 sb->s_creator_os = EXT2_OS_FREEBSD;
649 else if (strcasecmp(os, "lites") == 0)
650 sb->s_creator_os = EXT2_OS_LITES;
651 else
652 return 0;
653 return 1;
654 }
655
656 #define PATH_SET "PATH=/sbin"
657
parse_extended_opts(struct ext2_super_block * param,const char * opts)658 static void parse_extended_opts(struct ext2_super_block *param,
659 const char *opts)
660 {
661 char *buf, *token, *next, *p, *arg, *badopt = 0;
662 int len;
663 int r_usage = 0;
664
665 len = strlen(opts);
666 buf = malloc(len+1);
667 if (!buf) {
668 fprintf(stderr, "%s",
669 _("Couldn't allocate memory to parse options!\n"));
670 exit(1);
671 }
672 strcpy(buf, opts);
673 for (token = buf; token && *token; token = next) {
674 p = strchr(token, ',');
675 next = 0;
676 if (p) {
677 *p = 0;
678 next = p+1;
679 }
680 arg = strchr(token, '=');
681 if (arg) {
682 *arg = 0;
683 arg++;
684 }
685 if (strcmp(token, "desc-size") == 0 ||
686 strcmp(token, "desc_size") == 0) {
687 int desc_size;
688
689 if (!(fs_param.s_feature_incompat &
690 EXT4_FEATURE_INCOMPAT_64BIT)) {
691 fprintf(stderr,
692 _("%s requires '-O 64bit'\n"), token);
693 r_usage++;
694 continue;
695 }
696 if (param->s_reserved_gdt_blocks != 0) {
697 fprintf(stderr,
698 _("'%s' must be before 'resize=%u'\n"),
699 token, param->s_reserved_gdt_blocks);
700 r_usage++;
701 continue;
702 }
703 if (!arg) {
704 r_usage++;
705 badopt = token;
706 continue;
707 }
708 desc_size = strtoul(arg, &p, 0);
709 if (*p || (desc_size & (desc_size - 1))) {
710 fprintf(stderr,
711 _("Invalid desc_size: '%s'\n"), arg);
712 r_usage++;
713 continue;
714 }
715 param->s_desc_size = desc_size;
716 } else if (strcmp(token, "mmp_update_interval") == 0) {
717 if (!arg) {
718 r_usage++;
719 badopt = token;
720 continue;
721 }
722 param->s_mmp_update_interval = strtoul(arg, &p, 0);
723 if (*p) {
724 fprintf(stderr,
725 _("Invalid mmp_update_interval: %s\n"),
726 arg);
727 r_usage++;
728 continue;
729 }
730 } else if (strcmp(token, "stride") == 0) {
731 if (!arg) {
732 r_usage++;
733 badopt = token;
734 continue;
735 }
736 param->s_raid_stride = strtoul(arg, &p, 0);
737 if (*p) {
738 fprintf(stderr,
739 _("Invalid stride parameter: %s\n"),
740 arg);
741 r_usage++;
742 continue;
743 }
744 } else if (strcmp(token, "stripe-width") == 0 ||
745 strcmp(token, "stripe_width") == 0) {
746 if (!arg) {
747 r_usage++;
748 badopt = token;
749 continue;
750 }
751 param->s_raid_stripe_width = strtoul(arg, &p, 0);
752 if (*p) {
753 fprintf(stderr,
754 _("Invalid stripe-width parameter: %s\n"),
755 arg);
756 r_usage++;
757 continue;
758 }
759 } else if (!strcmp(token, "resize")) {
760 blk64_t resize;
761 unsigned long bpg, rsv_groups;
762 unsigned long group_desc_count, desc_blocks;
763 unsigned int gdpb, blocksize;
764 int rsv_gdb;
765
766 if (!arg) {
767 r_usage++;
768 badopt = token;
769 continue;
770 }
771
772 resize = parse_num_blocks2(arg,
773 param->s_log_block_size);
774
775 if (resize == 0) {
776 fprintf(stderr,
777 _("Invalid resize parameter: %s\n"),
778 arg);
779 r_usage++;
780 continue;
781 }
782 if (resize <= ext2fs_blocks_count(param)) {
783 fprintf(stderr, "%s",
784 _("The resize maximum must be greater "
785 "than the filesystem size.\n"));
786 r_usage++;
787 continue;
788 }
789
790 blocksize = EXT2_BLOCK_SIZE(param);
791 bpg = param->s_blocks_per_group;
792 if (!bpg)
793 bpg = blocksize * 8;
794 gdpb = EXT2_DESC_PER_BLOCK(param);
795 group_desc_count = (__u32) ext2fs_div64_ceil(
796 ext2fs_blocks_count(param), bpg);
797 desc_blocks = (group_desc_count +
798 gdpb - 1) / gdpb;
799 rsv_groups = ext2fs_div64_ceil(resize, bpg);
800 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
801 desc_blocks;
802 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
803 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
804
805 if (rsv_gdb > 0) {
806 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
807 fprintf(stderr, "%s",
808 _("On-line resizing not supported with revision 0 filesystems\n"));
809 free(buf);
810 exit(1);
811 }
812 param->s_feature_compat |=
813 EXT2_FEATURE_COMPAT_RESIZE_INODE;
814
815 param->s_reserved_gdt_blocks = rsv_gdb;
816 }
817 } else if (!strcmp(token, "test_fs")) {
818 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
819 } else if (!strcmp(token, "lazy_itable_init")) {
820 if (arg)
821 lazy_itable_init = strtoul(arg, &p, 0);
822 else
823 lazy_itable_init = 1;
824 } else if (!strcmp(token, "lazy_journal_init")) {
825 if (arg)
826 journal_flags |= strtoul(arg, &p, 0) ?
827 EXT2_MKJOURNAL_LAZYINIT : 0;
828 else
829 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
830 } else if (!strcmp(token, "root_owner")) {
831 if (arg) {
832 root_uid = strtoul(arg, &p, 0);
833 if (*p != ':') {
834 fprintf(stderr,
835 _("Invalid root_owner: '%s'\n"),
836 arg);
837 r_usage++;
838 continue;
839 }
840 p++;
841 root_gid = strtoul(p, &p, 0);
842 if (*p) {
843 fprintf(stderr,
844 _("Invalid root_owner: '%s'\n"),
845 arg);
846 r_usage++;
847 continue;
848 }
849 } else {
850 root_uid = getuid();
851 root_gid = getgid();
852 }
853 } else if (!strcmp(token, "discard")) {
854 discard = 1;
855 } else if (!strcmp(token, "nodiscard")) {
856 discard = 0;
857 } else if (!strcmp(token, "quotatype")) {
858 if (!arg) {
859 r_usage++;
860 badopt = token;
861 continue;
862 }
863 if (!strncmp(arg, "usr", 3)) {
864 quotatype = 0;
865 } else if (!strncmp(arg, "grp", 3)) {
866 quotatype = 1;
867 } else {
868 fprintf(stderr,
869 _("Invalid quotatype parameter: %s\n"),
870 arg);
871 r_usage++;
872 continue;
873 }
874 } else {
875 r_usage++;
876 badopt = token;
877 }
878 }
879 if (r_usage) {
880 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
881 "Extended options are separated by commas, "
882 "and may take an argument which\n"
883 "\tis set off by an equals ('=') sign.\n\n"
884 "Valid extended options are:\n"
885 "\tstride=<RAID per-disk data chunk in blocks>\n"
886 "\tstripe-width=<RAID stride * data disks in blocks>\n"
887 "\tresize=<resize maximum size in blocks>\n"
888 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
889 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
890 "\troot_uid=<uid of root directory>\n"
891 "\troot_gid=<gid of root directory>\n"
892 "\ttest_fs\n"
893 "\tdiscard\n"
894 "\tnodiscard\n"
895 "\tquotatype=<usr OR grp>\n\n"),
896 badopt ? badopt : "");
897 free(buf);
898 exit(1);
899 }
900 if (param->s_raid_stride &&
901 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
902 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
903 "multiple of stride %u.\n\n"),
904 param->s_raid_stripe_width, param->s_raid_stride);
905
906 free(buf);
907 }
908
909 static __u32 ok_features[3] = {
910 /* Compat */
911 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
912 EXT2_FEATURE_COMPAT_RESIZE_INODE |
913 EXT2_FEATURE_COMPAT_DIR_INDEX |
914 EXT2_FEATURE_COMPAT_EXT_ATTR,
915 /* Incompat */
916 EXT2_FEATURE_INCOMPAT_FILETYPE|
917 EXT3_FEATURE_INCOMPAT_EXTENTS|
918 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
919 EXT2_FEATURE_INCOMPAT_META_BG|
920 EXT4_FEATURE_INCOMPAT_FLEX_BG|
921 EXT4_FEATURE_INCOMPAT_MMP |
922 EXT4_FEATURE_INCOMPAT_64BIT,
923 /* R/O compat */
924 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
925 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
926 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
927 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
928 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
929 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
930 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
931 #ifdef CONFIG_QUOTA
932 EXT4_FEATURE_RO_COMPAT_QUOTA|
933 #endif
934 0
935 };
936
937
syntax_err_report(const char * filename,long err,int line_num)938 static void syntax_err_report(const char *filename, long err, int line_num)
939 {
940 fprintf(stderr,
941 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
942 filename, line_num, error_message(err));
943 exit(1);
944 }
945
946 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
947
edit_feature(const char * str,__u32 * compat_array)948 static void edit_feature(const char *str, __u32 *compat_array)
949 {
950 if (!str)
951 return;
952
953 if (e2p_edit_feature(str, compat_array, ok_features)) {
954 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
955 str);
956 exit(1);
957 }
958 }
959
edit_mntopts(const char * str,__u32 * mntopts)960 static void edit_mntopts(const char *str, __u32 *mntopts)
961 {
962 if (!str)
963 return;
964
965 if (e2p_edit_mntopts(str, mntopts, ~0)) {
966 fprintf(stderr, _("Invalid mount option set: %s\n"),
967 str);
968 exit(1);
969 }
970 }
971
972 struct str_list {
973 char **list;
974 int num;
975 int max;
976 };
977
init_list(struct str_list * sl)978 static errcode_t init_list(struct str_list *sl)
979 {
980 sl->num = 0;
981 sl->max = 0;
982 sl->list = malloc((sl->max+1) * sizeof(char *));
983 if (!sl->list)
984 return ENOMEM;
985 sl->list[0] = 0;
986 return 0;
987 }
988
push_string(struct str_list * sl,const char * str)989 static errcode_t push_string(struct str_list *sl, const char *str)
990 {
991 char **new_list;
992
993 if (sl->num >= sl->max) {
994 sl->max += 2;
995 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
996 if (!new_list)
997 return ENOMEM;
998 sl->list = new_list;
999 }
1000 sl->list[sl->num] = malloc(strlen(str)+1);
1001 if (sl->list[sl->num] == 0)
1002 return ENOMEM;
1003 strcpy(sl->list[sl->num], str);
1004 sl->num++;
1005 sl->list[sl->num] = 0;
1006 return 0;
1007 }
1008
print_str_list(char ** list)1009 static void print_str_list(char **list)
1010 {
1011 char **cpp;
1012
1013 for (cpp = list; *cpp; cpp++) {
1014 printf("'%s'", *cpp);
1015 if (cpp[1])
1016 fputs(", ", stdout);
1017 }
1018 fputc('\n', stdout);
1019 }
1020
1021 /*
1022 * Return TRUE if the profile has the given subsection
1023 */
profile_has_subsection(profile_t prof,const char * section,const char * subsection)1024 static int profile_has_subsection(profile_t prof, const char *section,
1025 const char *subsection)
1026 {
1027 void *state;
1028 const char *names[4];
1029 char *name;
1030 int ret = 0;
1031
1032 names[0] = section;
1033 names[1] = subsection;
1034 names[2] = 0;
1035
1036 if (profile_iterator_create(prof, names,
1037 PROFILE_ITER_LIST_SECTION |
1038 PROFILE_ITER_RELATIONS_ONLY, &state))
1039 return 0;
1040
1041 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1042 free(name);
1043 ret = 1;
1044 }
1045
1046 profile_iterator_free(&state);
1047 return ret;
1048 }
1049
parse_fs_type(const char * fs_type,const char * usage_types,struct ext2_super_block * sb,blk64_t fs_blocks_count,char * progname)1050 static char **parse_fs_type(const char *fs_type,
1051 const char *usage_types,
1052 struct ext2_super_block *sb,
1053 blk64_t fs_blocks_count,
1054 char *progname)
1055 {
1056 const char *ext_type = 0;
1057 char *parse_str;
1058 char *profile_type = 0;
1059 char *cp, *t;
1060 const char *size_type;
1061 struct str_list list;
1062 unsigned long long meg;
1063 int is_hurd = 0;
1064
1065 if (init_list(&list))
1066 return 0;
1067
1068 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1069 !strcasecmp(creator_os, "hurd")))
1070 is_hurd = 1;
1071
1072 if (fs_type)
1073 ext_type = fs_type;
1074 else if (is_hurd)
1075 ext_type = "ext2";
1076 else if (!strcmp(program_name, "mke3fs"))
1077 ext_type = "ext3";
1078 else if (!strcmp(program_name, "mke4fs"))
1079 ext_type = "ext4";
1080 else if (progname) {
1081 ext_type = strrchr(progname, '/');
1082 if (ext_type)
1083 ext_type++;
1084 else
1085 ext_type = progname;
1086
1087 if (!strncmp(ext_type, "mkfs.", 5)) {
1088 ext_type += 5;
1089 if (ext_type[0] == 0)
1090 ext_type = 0;
1091 } else
1092 ext_type = 0;
1093 }
1094
1095 if (!ext_type) {
1096 profile_get_string(profile, "defaults", "fs_type", 0,
1097 "ext2", &profile_type);
1098 ext_type = profile_type;
1099 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1100 ext_type = "ext3";
1101 }
1102
1103
1104 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1105 strcmp(ext_type, "ext2")) {
1106 printf(_("\nYour mke2fs.conf file does not define the "
1107 "%s filesystem type.\n"), ext_type);
1108 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1109 !strcmp(ext_type, "ext4dev")) {
1110 printf("%s", _("You probably need to install an "
1111 "updated mke2fs.conf file.\n\n"));
1112 }
1113 if (!force) {
1114 printf("%s", _("Aborting...\n"));
1115 exit(1);
1116 }
1117 }
1118
1119 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1120 if (fs_blocks_count < 3 * meg)
1121 size_type = "floppy";
1122 else if (fs_blocks_count < 512 * meg)
1123 size_type = "small";
1124 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1125 size_type = "default";
1126 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1127 size_type = "big";
1128 else
1129 size_type = "huge";
1130
1131 if (!usage_types)
1132 usage_types = size_type;
1133
1134 parse_str = malloc(strlen(usage_types)+1);
1135 if (!parse_str) {
1136 free(profile_type);
1137 free(list.list);
1138 return 0;
1139 }
1140 strcpy(parse_str, usage_types);
1141
1142 if (ext_type)
1143 push_string(&list, ext_type);
1144 cp = parse_str;
1145 while (1) {
1146 t = strchr(cp, ',');
1147 if (t)
1148 *t = '\0';
1149
1150 if (*cp) {
1151 if (profile_has_subsection(profile, "fs_types", cp))
1152 push_string(&list, cp);
1153 else if (strcmp(cp, "default") != 0)
1154 fprintf(stderr,
1155 _("\nWarning: the fs_type %s is not "
1156 "defined in mke2fs.conf\n\n"),
1157 cp);
1158 }
1159 if (t)
1160 cp = t+1;
1161 else
1162 break;
1163 }
1164 free(parse_str);
1165 free(profile_type);
1166 if (is_hurd)
1167 push_string(&list, "hurd");
1168 return (list.list);
1169 }
1170
get_string_from_profile(char ** types,const char * opt,const char * def_val)1171 static char *get_string_from_profile(char **types, const char *opt,
1172 const char *def_val)
1173 {
1174 char *ret = 0;
1175 int i;
1176
1177 for (i=0; types[i]; i++);
1178 for (i-=1; i >=0 ; i--) {
1179 profile_get_string(profile, "fs_types", types[i],
1180 opt, 0, &ret);
1181 if (ret)
1182 return ret;
1183 }
1184 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1185 return (ret);
1186 }
1187
get_int_from_profile(char ** types,const char * opt,int def_val)1188 static int get_int_from_profile(char **types, const char *opt, int def_val)
1189 {
1190 int ret;
1191 char **cpp;
1192
1193 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1194 for (cpp = types; *cpp; cpp++)
1195 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1196 return ret;
1197 }
1198
get_double_from_profile(char ** types,const char * opt,double def_val)1199 static double get_double_from_profile(char **types, const char *opt,
1200 double def_val)
1201 {
1202 double ret;
1203 char **cpp;
1204
1205 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1206 for (cpp = types; *cpp; cpp++)
1207 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1208 return ret;
1209 }
1210
get_bool_from_profile(char ** types,const char * opt,int def_val)1211 static int get_bool_from_profile(char **types, const char *opt, int def_val)
1212 {
1213 int ret;
1214 char **cpp;
1215
1216 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1217 for (cpp = types; *cpp; cpp++)
1218 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1219 return ret;
1220 }
1221
1222 extern const char *mke2fs_default_profile;
1223 static const char *default_files[] = { "<default>", 0 };
1224
1225 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1226 /*
1227 * Sets the geometry of a device (stripe/stride), and returns the
1228 * device's alignment offset, if any, or a negative error.
1229 */
get_device_geometry(const char * file,struct ext2_super_block * fs_param,int psector_size)1230 static int get_device_geometry(const char *file,
1231 struct ext2_super_block *fs_param,
1232 int psector_size)
1233 {
1234 int rc = -1;
1235 int blocksize;
1236 blkid_probe pr;
1237 blkid_topology tp;
1238 unsigned long min_io;
1239 unsigned long opt_io;
1240 struct stat statbuf;
1241
1242 /* Nothing to do for a regular file */
1243 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1244 return 0;
1245
1246 pr = blkid_new_probe_from_filename(file);
1247 if (!pr)
1248 goto out;
1249
1250 tp = blkid_probe_get_topology(pr);
1251 if (!tp)
1252 goto out;
1253
1254 min_io = blkid_topology_get_minimum_io_size(tp);
1255 opt_io = blkid_topology_get_optimal_io_size(tp);
1256 blocksize = EXT2_BLOCK_SIZE(fs_param);
1257 if ((min_io == 0) && (psector_size > blocksize))
1258 min_io = psector_size;
1259 if ((opt_io == 0) && min_io)
1260 opt_io = min_io;
1261 if ((opt_io == 0) && (psector_size > blocksize))
1262 opt_io = psector_size;
1263
1264 /* setting stripe/stride to blocksize is pointless */
1265 if (min_io > blocksize)
1266 fs_param->s_raid_stride = min_io / blocksize;
1267 if (opt_io > blocksize)
1268 fs_param->s_raid_stripe_width = opt_io / blocksize;
1269
1270 rc = blkid_topology_get_alignment_offset(tp);
1271 out:
1272 blkid_free_probe(pr);
1273 return rc;
1274 }
1275 #endif
1276
PRS(int argc,char * argv[])1277 static void PRS(int argc, char *argv[])
1278 {
1279 int b, c;
1280 int cluster_size = 0;
1281 char *tmp, **cpp;
1282 int blocksize = 0;
1283 int inode_ratio = 0;
1284 int inode_size = 0;
1285 unsigned long flex_bg_size = 0;
1286 double reserved_ratio = -1.0;
1287 int lsector_size = 0, psector_size = 0;
1288 int show_version_only = 0;
1289 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1290 errcode_t retval;
1291 char * oldpath = getenv("PATH");
1292 char * extended_opts = 0;
1293 char * fs_type = 0;
1294 char * usage_types = 0;
1295 blk64_t dev_size;
1296 /*
1297 * NOTE: A few words about fs_blocks_count and blocksize:
1298 *
1299 * Initially, blocksize is set to zero, which implies 1024.
1300 * If -b is specified, blocksize is updated to the user's value.
1301 *
1302 * Next, the device size or the user's "blocks" command line argument
1303 * is used to set fs_blocks_count; the units are blocksize.
1304 *
1305 * Later, if blocksize hasn't been set and the profile specifies a
1306 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1307 * appropriately. Note the change in units!
1308 *
1309 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1310 */
1311 blk64_t fs_blocks_count = 0;
1312 #ifdef __linux__
1313 struct utsname ut;
1314 #endif
1315 long sysval;
1316 int s_opt = -1, r_opt = -1;
1317 char *fs_features = 0;
1318 int use_bsize;
1319 char *newpath;
1320 int pathlen = sizeof(PATH_SET) + 1;
1321
1322 if (oldpath)
1323 pathlen += strlen(oldpath);
1324 newpath = malloc(pathlen);
1325 if (!newpath) {
1326 fprintf(stderr, "%s",
1327 _("Couldn't allocate memory for new PATH.\n"));
1328 exit(1);
1329 }
1330 strcpy(newpath, PATH_SET);
1331
1332 /* Update our PATH to include /sbin */
1333 if (oldpath) {
1334 strcat (newpath, ":");
1335 strcat (newpath, oldpath);
1336 }
1337 putenv (newpath);
1338
1339 tmp = getenv("MKE2FS_SYNC");
1340 if (tmp)
1341 sync_kludge = atoi(tmp);
1342
1343 /* Determine the system page size if possible */
1344 #ifdef HAVE_SYSCONF
1345 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1346 #define _SC_PAGESIZE _SC_PAGE_SIZE
1347 #endif
1348 #ifdef _SC_PAGESIZE
1349 sysval = sysconf(_SC_PAGESIZE);
1350 if (sysval > 0)
1351 sys_page_size = sysval;
1352 #endif /* _SC_PAGESIZE */
1353 #endif /* HAVE_SYSCONF */
1354
1355 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1356 config_fn[0] = tmp;
1357 profile_set_syntax_err_cb(syntax_err_report);
1358 retval = profile_init(config_fn, &profile);
1359 if (retval == ENOENT) {
1360 retval = profile_init(default_files, &profile);
1361 if (retval)
1362 goto profile_error;
1363 retval = profile_set_default(profile, mke2fs_default_profile);
1364 if (retval)
1365 goto profile_error;
1366 } else if (retval) {
1367 profile_error:
1368 fprintf(stderr, _("Couldn't init profile successfully"
1369 " (error: %ld).\n"), retval);
1370 exit(1);
1371 }
1372
1373 setbuf(stdout, NULL);
1374 setbuf(stderr, NULL);
1375 add_error_table(&et_ext2_error_table);
1376 add_error_table(&et_prof_error_table);
1377 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1378 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1379
1380 #ifdef __linux__
1381 if (uname(&ut)) {
1382 perror("uname");
1383 exit(1);
1384 }
1385 linux_version_code = parse_version_number(ut.release);
1386 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1387 fs_param.s_rev_level = 0;
1388 #endif
1389
1390 if (argc && *argv) {
1391 program_name = get_progname(*argv);
1392
1393 /* If called as mkfs.ext3, create a journal inode */
1394 if (!strcmp(program_name, "mkfs.ext3") ||
1395 !strcmp(program_name, "mke3fs"))
1396 journal_size = -1;
1397 }
1398
1399 while ((c = getopt (argc, argv,
1400 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1401 switch (c) {
1402 case 'b':
1403 blocksize = parse_num_blocks2(optarg, -1);
1404 b = (blocksize > 0) ? blocksize : -blocksize;
1405 if (b < EXT2_MIN_BLOCK_SIZE ||
1406 b > EXT2_MAX_BLOCK_SIZE) {
1407 com_err(program_name, 0,
1408 _("invalid block size - %s"), optarg);
1409 exit(1);
1410 }
1411 if (blocksize > 4096)
1412 fprintf(stderr, _("Warning: blocksize %d not "
1413 "usable on most systems.\n"),
1414 blocksize);
1415 if (blocksize > 0)
1416 fs_param.s_log_block_size =
1417 int_log2(blocksize >>
1418 EXT2_MIN_BLOCK_LOG_SIZE);
1419 break;
1420 case 'c': /* Check for bad blocks */
1421 cflag++;
1422 break;
1423 case 'C':
1424 cluster_size = parse_num_blocks2(optarg, -1);
1425 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1426 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1427 com_err(program_name, 0,
1428 _("invalid cluster size - %s"),
1429 optarg);
1430 exit(1);
1431 }
1432 break;
1433 case 'D':
1434 direct_io = 1;
1435 break;
1436 case 'R':
1437 com_err(program_name, 0, "%s",
1438 _("'-R' is deprecated, use '-E' instead"));
1439 /* fallthrough */
1440 case 'E':
1441 extended_opts = optarg;
1442 break;
1443 case 'F':
1444 force++;
1445 break;
1446 case 'g':
1447 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1448 if (*tmp) {
1449 com_err(program_name, 0, "%s",
1450 _("Illegal number for blocks per group"));
1451 exit(1);
1452 }
1453 if ((fs_param.s_blocks_per_group % 8) != 0) {
1454 com_err(program_name, 0, "%s",
1455 _("blocks per group must be multiple of 8"));
1456 exit(1);
1457 }
1458 break;
1459 case 'G':
1460 flex_bg_size = strtoul(optarg, &tmp, 0);
1461 if (*tmp) {
1462 com_err(program_name, 0, "%s",
1463 _("Illegal number for flex_bg size"));
1464 exit(1);
1465 }
1466 if (flex_bg_size < 1 ||
1467 (flex_bg_size & (flex_bg_size-1)) != 0) {
1468 com_err(program_name, 0, "%s",
1469 _("flex_bg size must be a power of 2"));
1470 exit(1);
1471 }
1472 break;
1473 case 'i':
1474 inode_ratio = strtoul(optarg, &tmp, 0);
1475 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1476 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1477 *tmp) {
1478 com_err(program_name, 0,
1479 _("invalid inode ratio %s (min %d/max %d)"),
1480 optarg, EXT2_MIN_BLOCK_SIZE,
1481 EXT2_MAX_BLOCK_SIZE * 1024);
1482 exit(1);
1483 }
1484 break;
1485 case 'I':
1486 inode_size = strtoul(optarg, &tmp, 0);
1487 if (*tmp) {
1488 com_err(program_name, 0,
1489 _("invalid inode size - %s"), optarg);
1490 exit(1);
1491 }
1492 break;
1493 case 'j':
1494 if (!journal_size)
1495 journal_size = -1;
1496 break;
1497 case 'J':
1498 parse_journal_opts(optarg);
1499 break;
1500 case 'K':
1501 fprintf(stderr, "%s",
1502 _("Warning: -K option is deprecated and "
1503 "should not be used anymore. Use "
1504 "\'-E nodiscard\' extended option "
1505 "instead!\n"));
1506 discard = 0;
1507 break;
1508 case 'l':
1509 bad_blocks_filename = realloc(bad_blocks_filename,
1510 strlen(optarg) + 1);
1511 if (!bad_blocks_filename) {
1512 com_err(program_name, ENOMEM, "%s",
1513 _("in malloc for bad_blocks_filename"));
1514 exit(1);
1515 }
1516 strcpy(bad_blocks_filename, optarg);
1517 break;
1518 case 'L':
1519 volume_label = optarg;
1520 break;
1521 case 'm':
1522 reserved_ratio = strtod(optarg, &tmp);
1523 if ( *tmp || reserved_ratio > 50 ||
1524 reserved_ratio < 0) {
1525 com_err(program_name, 0,
1526 _("invalid reserved blocks percent - %s"),
1527 optarg);
1528 exit(1);
1529 }
1530 break;
1531 case 'M':
1532 mount_dir = optarg;
1533 break;
1534 case 'n':
1535 noaction++;
1536 break;
1537 case 'N':
1538 num_inodes = strtoul(optarg, &tmp, 0);
1539 if (*tmp) {
1540 com_err(program_name, 0,
1541 _("bad num inodes - %s"), optarg);
1542 exit(1);
1543 }
1544 break;
1545 case 'o':
1546 creator_os = optarg;
1547 break;
1548 case 'O':
1549 fs_features = optarg;
1550 break;
1551 case 'q':
1552 quiet = 1;
1553 break;
1554 case 'r':
1555 r_opt = strtoul(optarg, &tmp, 0);
1556 if (*tmp) {
1557 com_err(program_name, 0,
1558 _("bad revision level - %s"), optarg);
1559 exit(1);
1560 }
1561 fs_param.s_rev_level = r_opt;
1562 break;
1563 case 's': /* deprecated */
1564 s_opt = atoi(optarg);
1565 break;
1566 case 'S':
1567 super_only = 1;
1568 break;
1569 case 't':
1570 if (fs_type) {
1571 com_err(program_name, 0, "%s",
1572 _("The -t option may only be used once"));
1573 exit(1);
1574 }
1575 fs_type = strdup(optarg);
1576 break;
1577 case 'T':
1578 if (usage_types) {
1579 com_err(program_name, 0, "%s",
1580 _("The -T option may only be used once"));
1581 exit(1);
1582 }
1583 usage_types = strdup(optarg);
1584 break;
1585 case 'U':
1586 fs_uuid = optarg;
1587 break;
1588 case 'v':
1589 verbose = 1;
1590 break;
1591 case 'V':
1592 /* Print version number and exit */
1593 show_version_only++;
1594 break;
1595 default:
1596 usage();
1597 }
1598 }
1599 if ((optind == argc) && !show_version_only)
1600 usage();
1601 device_name = argv[optind++];
1602
1603 if (!quiet || show_version_only)
1604 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1605 E2FSPROGS_DATE);
1606
1607 if (show_version_only) {
1608 fprintf(stderr, _("\tUsing %s\n"),
1609 error_message(EXT2_ET_BASE));
1610 exit(0);
1611 }
1612
1613 /*
1614 * If there's no blocksize specified and there is a journal
1615 * device, use it to figure out the blocksize
1616 */
1617 if (blocksize <= 0 && journal_device) {
1618 ext2_filsys jfs;
1619 io_manager io_ptr;
1620
1621 #ifdef CONFIG_TESTIO_DEBUG
1622 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1623 io_ptr = test_io_manager;
1624 test_io_backing_manager = unix_io_manager;
1625 } else
1626 #endif
1627 io_ptr = unix_io_manager;
1628 retval = ext2fs_open(journal_device,
1629 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1630 0, io_ptr, &jfs);
1631 if (retval) {
1632 com_err(program_name, retval,
1633 _("while trying to open journal device %s\n"),
1634 journal_device);
1635 exit(1);
1636 }
1637 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1638 com_err(program_name, 0,
1639 _("Journal dev blocksize (%d) smaller than "
1640 "minimum blocksize %d\n"), jfs->blocksize,
1641 -blocksize);
1642 exit(1);
1643 }
1644 blocksize = jfs->blocksize;
1645 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1646 fs_param.s_log_block_size =
1647 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1648 ext2fs_close(jfs);
1649 }
1650
1651 if (optind < argc) {
1652 fs_blocks_count = parse_num_blocks2(argv[optind++],
1653 fs_param.s_log_block_size);
1654 if (!fs_blocks_count) {
1655 com_err(program_name, 0,
1656 _("invalid blocks '%s' on device '%s'"),
1657 argv[optind - 1], device_name);
1658 exit(1);
1659 }
1660 }
1661 if (optind < argc)
1662 usage();
1663
1664 if (!force)
1665 check_plausibility(device_name);
1666 check_mount(device_name, force, _("filesystem"));
1667
1668 /* Determine the size of the device (if possible) */
1669 if (noaction && fs_blocks_count) {
1670 dev_size = fs_blocks_count;
1671 retval = 0;
1672 } else
1673 retval = ext2fs_get_device_size2(device_name,
1674 EXT2_BLOCK_SIZE(&fs_param),
1675 &dev_size);
1676
1677 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1678 com_err(program_name, retval, "%s",
1679 _("while trying to determine filesystem size"));
1680 exit(1);
1681 }
1682 if (!fs_blocks_count) {
1683 if (retval == EXT2_ET_UNIMPLEMENTED) {
1684 com_err(program_name, 0, "%s",
1685 _("Couldn't determine device size; you "
1686 "must specify\nthe size of the "
1687 "filesystem\n"));
1688 exit(1);
1689 } else {
1690 if (dev_size == 0) {
1691 com_err(program_name, 0, "%s",
1692 _("Device size reported to be zero. "
1693 "Invalid partition specified, or\n\t"
1694 "partition table wasn't reread "
1695 "after running fdisk, due to\n\t"
1696 "a modified partition being busy "
1697 "and in use. You may need to reboot\n\t"
1698 "to re-read your partition table.\n"
1699 ));
1700 exit(1);
1701 }
1702 fs_blocks_count = dev_size;
1703 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1704 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1705 EXT2_BLOCK_SIZE(&fs_param))-1));
1706 }
1707 } else if (!force && (fs_blocks_count > dev_size)) {
1708 com_err(program_name, 0, "%s",
1709 _("Filesystem larger than apparent device size."));
1710 proceed_question();
1711 }
1712
1713 if (!fs_type)
1714 profile_get_string(profile, "devices", device_name,
1715 "fs_type", 0, &fs_type);
1716 if (!usage_types)
1717 profile_get_string(profile, "devices", device_name,
1718 "usage_types", 0, &usage_types);
1719
1720 /*
1721 * We have the file system (or device) size, so we can now
1722 * determine the appropriate file system types so the fs can
1723 * be appropriately configured.
1724 */
1725 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1726 fs_blocks_count ? fs_blocks_count : dev_size,
1727 argv[0]);
1728 if (!fs_types) {
1729 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
1730 exit(1);
1731 }
1732
1733 /* Figure out what features should be enabled */
1734
1735 tmp = NULL;
1736 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1737 tmp = get_string_from_profile(fs_types, "base_features",
1738 "sparse_super,filetype,resize_inode,dir_index");
1739 edit_feature(tmp, &fs_param.s_feature_compat);
1740 free(tmp);
1741
1742 /* And which mount options as well */
1743 tmp = get_string_from_profile(fs_types, "default_mntopts",
1744 "acl,user_xattr");
1745 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1746 if (tmp)
1747 free(tmp);
1748
1749 for (cpp = fs_types; *cpp; cpp++) {
1750 tmp = NULL;
1751 profile_get_string(profile, "fs_types", *cpp,
1752 "features", "", &tmp);
1753 if (tmp && *tmp)
1754 edit_feature(tmp, &fs_param.s_feature_compat);
1755 if (tmp)
1756 free(tmp);
1757 }
1758 tmp = get_string_from_profile(fs_types, "default_features",
1759 "");
1760 }
1761 edit_feature(fs_features ? fs_features : tmp,
1762 &fs_param.s_feature_compat);
1763 if (tmp)
1764 free(tmp);
1765
1766 /* Get the hardware sector sizes, if available */
1767 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1768 if (retval) {
1769 com_err(program_name, retval, "%s",
1770 _("while trying to determine hardware sector size"));
1771 exit(1);
1772 }
1773 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1774 if (retval) {
1775 com_err(program_name, retval, "%s",
1776 _("while trying to determine physical sector size"));
1777 exit(1);
1778 }
1779
1780 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1781 if (tmp != NULL)
1782 lsector_size = atoi(tmp);
1783 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1784 if (tmp != NULL)
1785 psector_size = atoi(tmp);
1786
1787 /* Older kernels may not have physical/logical distinction */
1788 if (!psector_size)
1789 psector_size = lsector_size;
1790
1791 if (blocksize <= 0) {
1792 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1793
1794 if (use_bsize == -1) {
1795 use_bsize = sys_page_size;
1796 if ((linux_version_code < (2*65536 + 6*256)) &&
1797 (use_bsize > 4096))
1798 use_bsize = 4096;
1799 }
1800 if (lsector_size && use_bsize < lsector_size)
1801 use_bsize = lsector_size;
1802 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1803 use_bsize = -blocksize;
1804 blocksize = use_bsize;
1805 fs_blocks_count /= (blocksize / 1024);
1806 } else {
1807 if (blocksize < lsector_size) { /* Impossible */
1808 com_err(program_name, EINVAL, "%s",
1809 _("while setting blocksize; too small "
1810 "for device\n"));
1811 exit(1);
1812 } else if ((blocksize < psector_size) &&
1813 (psector_size <= sys_page_size)) { /* Suboptimal */
1814 fprintf(stderr, _("Warning: specified blocksize %d is "
1815 "less than device physical sectorsize %d\n"),
1816 blocksize, psector_size);
1817 }
1818 }
1819
1820 fs_param.s_log_block_size =
1821 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1822
1823 /*
1824 * We now need to do a sanity check of fs_blocks_count for
1825 * 32-bit vs 64-bit block number support.
1826 */
1827 if ((fs_blocks_count > MAX_32_NUM) &&
1828 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1829 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1830 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1831 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1832 }
1833 if ((fs_blocks_count > MAX_32_NUM) &&
1834 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1835 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1836 "too big to be expressed\n\t"
1837 "in 32 bits using a blocksize of %d.\n"),
1838 program_name, fs_blocks_count, device_name,
1839 EXT2_BLOCK_SIZE(&fs_param));
1840 exit(1);
1841 }
1842
1843 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1844
1845 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1846 fs_types[0] = strdup("journal");
1847 fs_types[1] = 0;
1848 }
1849
1850 if (verbose) {
1851 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1852 print_str_list(fs_types);
1853 }
1854
1855 if (r_opt == EXT2_GOOD_OLD_REV &&
1856 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1857 fs_param.s_feature_ro_compat)) {
1858 fprintf(stderr, "%s", _("Filesystem features not supported "
1859 "with revision 0 filesystems\n"));
1860 exit(1);
1861 }
1862
1863 if (s_opt > 0) {
1864 if (r_opt == EXT2_GOOD_OLD_REV) {
1865 fprintf(stderr, "%s",
1866 _("Sparse superblocks not supported "
1867 "with revision 0 filesystems\n"));
1868 exit(1);
1869 }
1870 fs_param.s_feature_ro_compat |=
1871 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1872 } else if (s_opt == 0)
1873 fs_param.s_feature_ro_compat &=
1874 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1875
1876 if (journal_size != 0) {
1877 if (r_opt == EXT2_GOOD_OLD_REV) {
1878 fprintf(stderr, "%s", _("Journals not supported with "
1879 "revision 0 filesystems\n"));
1880 exit(1);
1881 }
1882 fs_param.s_feature_compat |=
1883 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1884 }
1885
1886 /* Get reserved_ratio from profile if not specified on cmd line. */
1887 if (reserved_ratio < 0.0) {
1888 reserved_ratio = get_double_from_profile(
1889 fs_types, "reserved_ratio", 5.0);
1890 if (reserved_ratio > 50 || reserved_ratio < 0) {
1891 com_err(program_name, 0,
1892 _("invalid reserved blocks percent - %lf"),
1893 reserved_ratio);
1894 exit(1);
1895 }
1896 }
1897
1898 if (fs_param.s_feature_incompat &
1899 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1900 reserved_ratio = 0;
1901 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1902 fs_param.s_feature_compat = 0;
1903 fs_param.s_feature_ro_compat = 0;
1904 }
1905
1906 /* Check the user's mkfs options for 64bit */
1907 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1908 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
1909 printf("%s", _("Extents MUST be enabled for a 64-bit "
1910 "filesystem. Pass -O extents to rectify.\n"));
1911 exit(1);
1912 }
1913
1914 /* Set first meta blockgroup via an environment variable */
1915 /* (this is mostly for debugging purposes) */
1916 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1917 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1918 fs_param.s_first_meta_bg = atoi(tmp);
1919 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1920 if (!cluster_size)
1921 cluster_size = get_int_from_profile(fs_types,
1922 "cluster_size",
1923 blocksize*16);
1924 fs_param.s_log_cluster_size =
1925 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1926 if (fs_param.s_log_cluster_size &&
1927 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
1928 com_err(program_name, 0, "%s",
1929 _("The cluster size may not be "
1930 "smaller than the block size.\n"));
1931 exit(1);
1932 }
1933 } else if (cluster_size) {
1934 com_err(program_name, 0, "%s",
1935 _("specifying a cluster size requires the "
1936 "bigalloc feature"));
1937 exit(1);
1938 } else
1939 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1940
1941 if (inode_ratio == 0) {
1942 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1943 8192);
1944 if (inode_ratio < blocksize)
1945 inode_ratio = blocksize;
1946 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1947 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
1948 }
1949
1950 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1951 retval = get_device_geometry(device_name, &fs_param, psector_size);
1952 if (retval < 0) {
1953 fprintf(stderr,
1954 _("warning: Unable to get device geometry for %s\n"),
1955 device_name);
1956 } else if (retval) {
1957 printf(_("%s alignment is offset by %lu bytes.\n"),
1958 device_name, retval);
1959 printf(_("This may result in very poor performance, "
1960 "(re)-partitioning suggested.\n"));
1961 }
1962 #endif
1963
1964 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1965
1966 /*
1967 * Initialize s_desc_size so that the parse_extended_opts()
1968 * can correctly handle "-E resize=NNN" if the 64-bit option
1969 * is set.
1970 */
1971 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
1972 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
1973
1974 /* This check should happen beyond the last assignment to blocksize */
1975 if (blocksize > sys_page_size) {
1976 if (!force) {
1977 com_err(program_name, 0,
1978 _("%d-byte blocks too big for system (max %d)"),
1979 blocksize, sys_page_size);
1980 proceed_question();
1981 }
1982 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1983 "(max %d), forced to continue\n"),
1984 blocksize, sys_page_size);
1985 }
1986
1987 lazy_itable_init = 0;
1988 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1989 lazy_itable_init = 1;
1990
1991 lazy_itable_init = get_bool_from_profile(fs_types,
1992 "lazy_itable_init",
1993 lazy_itable_init);
1994 discard = get_bool_from_profile(fs_types, "discard" , discard);
1995 journal_flags |= get_bool_from_profile(fs_types,
1996 "lazy_journal_init", 0) ?
1997 EXT2_MKJOURNAL_LAZYINIT : 0;
1998 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
1999
2000 /* Get options from profile */
2001 for (cpp = fs_types; *cpp; cpp++) {
2002 tmp = NULL;
2003 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2004 if (tmp && *tmp)
2005 parse_extended_opts(&fs_param, tmp);
2006 free(tmp);
2007 }
2008
2009 if (extended_opts)
2010 parse_extended_opts(&fs_param, extended_opts);
2011
2012 /* Can't support bigalloc feature without extents feature */
2013 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2014 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2015 com_err(program_name, 0, "%s",
2016 _("Can't support bigalloc feature without "
2017 "extents feature"));
2018 exit(1);
2019 }
2020
2021 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2022 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
2023 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2024 "features are not compatible.\n"
2025 "They can not be both enabled "
2026 "simultaneously.\n"));
2027 exit(1);
2028 }
2029
2030 if (!quiet &&
2031 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2032 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2033 "still under development\n"
2034 "See https://ext4.wiki.kernel.org/"
2035 "index.php/Bigalloc for more information\n\n"));
2036
2037 if (!quiet &&
2038 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2039 fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2040 "still under development\n"
2041 "See https://ext4.wiki.kernel.org/"
2042 "index.php/Quota for more information\n\n"));
2043
2044 /* Since sparse_super is the default, we would only have a problem
2045 * here if it was explicitly disabled.
2046 */
2047 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2048 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2049 com_err(program_name, 0, "%s",
2050 _("reserved online resize blocks not supported "
2051 "on non-sparse filesystem"));
2052 exit(1);
2053 }
2054
2055 if (fs_param.s_blocks_per_group) {
2056 if (fs_param.s_blocks_per_group < 256 ||
2057 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2058 com_err(program_name, 0, "%s",
2059 _("blocks per group count out of range"));
2060 exit(1);
2061 }
2062 }
2063
2064 /*
2065 * If the bigalloc feature is enabled, then the -g option will
2066 * specify the number of clusters per group.
2067 */
2068 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2069 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2070 fs_param.s_blocks_per_group = 0;
2071 }
2072
2073 if (inode_size == 0)
2074 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2075 if (!flex_bg_size && (fs_param.s_feature_incompat &
2076 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2077 flex_bg_size = get_int_from_profile(fs_types,
2078 "flex_bg_size", 16);
2079 if (flex_bg_size) {
2080 if (!(fs_param.s_feature_incompat &
2081 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2082 com_err(program_name, 0, "%s",
2083 _("Flex_bg feature not enabled, so "
2084 "flex_bg size may not be specified"));
2085 exit(1);
2086 }
2087 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2088 }
2089
2090 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2091 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2092 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2093 inode_size & (inode_size - 1)) {
2094 com_err(program_name, 0,
2095 _("invalid inode size %d (min %d/max %d)"),
2096 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2097 blocksize);
2098 exit(1);
2099 }
2100 fs_param.s_inode_size = inode_size;
2101 }
2102
2103 /* Make sure number of inodes specified will fit in 32 bits */
2104 if (num_inodes == 0) {
2105 unsigned long long n;
2106 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2107 if (n > MAX_32_NUM) {
2108 if (fs_param.s_feature_incompat &
2109 EXT4_FEATURE_INCOMPAT_64BIT)
2110 num_inodes = MAX_32_NUM;
2111 else {
2112 com_err(program_name, 0,
2113 _("too many inodes (%llu), raise "
2114 "inode ratio?"), n);
2115 exit(1);
2116 }
2117 }
2118 } else if (num_inodes > MAX_32_NUM) {
2119 com_err(program_name, 0,
2120 _("too many inodes (%llu), specify < 2^32 inodes"),
2121 num_inodes);
2122 exit(1);
2123 }
2124 /*
2125 * Calculate number of inodes based on the inode ratio
2126 */
2127 fs_param.s_inodes_count = num_inodes ? num_inodes :
2128 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2129
2130 if ((((unsigned long long)fs_param.s_inodes_count) *
2131 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2132 ((ext2fs_blocks_count(&fs_param)) *
2133 EXT2_BLOCK_SIZE(&fs_param))) {
2134 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2135 "(%u) too big for a\n\t"
2136 "filesystem with %llu blocks, "
2137 "specify higher inode_ratio (-i)\n\t"
2138 "or lower inode count (-N).\n"),
2139 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2140 fs_param.s_inodes_count,
2141 (unsigned long long) ext2fs_blocks_count(&fs_param));
2142 exit(1);
2143 }
2144
2145 /*
2146 * Calculate number of blocks to reserve
2147 */
2148 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2149 ext2fs_blocks_count(&fs_param) / 100.0);
2150
2151 free(fs_type);
2152 free(usage_types);
2153 }
2154
should_do_undo(const char * name)2155 static int should_do_undo(const char *name)
2156 {
2157 errcode_t retval;
2158 io_channel channel;
2159 __u16 s_magic;
2160 struct ext2_super_block super;
2161 io_manager manager = unix_io_manager;
2162 int csum_flag, force_undo;
2163
2164 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2165 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2166 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2167 if (!force_undo && (!csum_flag || !lazy_itable_init))
2168 return 0;
2169
2170 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2171 if (retval) {
2172 /*
2173 * We don't handle error cases instead we
2174 * declare that the file system doesn't exist
2175 * and let the rest of mke2fs take care of
2176 * error
2177 */
2178 retval = 0;
2179 goto open_err_out;
2180 }
2181
2182 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2183 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2184 if (retval) {
2185 retval = 0;
2186 goto err_out;
2187 }
2188
2189 #if defined(WORDS_BIGENDIAN)
2190 s_magic = ext2fs_swab16(super.s_magic);
2191 #else
2192 s_magic = super.s_magic;
2193 #endif
2194
2195 if (s_magic == EXT2_SUPER_MAGIC)
2196 retval = 1;
2197
2198 err_out:
2199 io_channel_close(channel);
2200
2201 open_err_out:
2202
2203 return retval;
2204 }
2205
mke2fs_setup_tdb(const char * name,io_manager * io_ptr)2206 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2207 {
2208 errcode_t retval = ENOMEM;
2209 char *tdb_dir = NULL, *tdb_file = NULL;
2210 char *dev_name, *tmp_name;
2211 int free_tdb_dir = 0;
2212
2213 /*
2214 * Configuration via a conf file would be
2215 * nice
2216 */
2217 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2218 if (!tdb_dir) {
2219 profile_get_string(profile, "defaults",
2220 "undo_dir", 0, "/var/lib/e2fsprogs",
2221 &tdb_dir);
2222 free_tdb_dir = 1;
2223 }
2224
2225 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2226 access(tdb_dir, W_OK)) {
2227 if (free_tdb_dir)
2228 free(tdb_dir);
2229 return 0;
2230 }
2231
2232 tmp_name = strdup(name);
2233 if (!tmp_name)
2234 goto errout;
2235 dev_name = basename(tmp_name);
2236 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2237 if (!tdb_file) {
2238 free(tmp_name);
2239 goto errout;
2240 }
2241 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2242 free(tmp_name);
2243
2244 if (!access(tdb_file, F_OK)) {
2245 if (unlink(tdb_file) < 0) {
2246 retval = errno;
2247 goto errout;
2248 }
2249 }
2250
2251 set_undo_io_backing_manager(*io_ptr);
2252 *io_ptr = undo_io_manager;
2253 retval = set_undo_io_backup_file(tdb_file);
2254 if (retval)
2255 goto errout;
2256 printf(_("Overwriting existing filesystem; this can be undone "
2257 "using the command:\n"
2258 " e2undo %s %s\n\n"), tdb_file, name);
2259
2260 if (free_tdb_dir)
2261 free(tdb_dir);
2262 free(tdb_file);
2263 return 0;
2264
2265 errout:
2266 if (free_tdb_dir)
2267 free(tdb_dir);
2268 free(tdb_file);
2269 com_err(program_name, retval, "%s",
2270 _("while trying to setup undo file\n"));
2271 return retval;
2272 }
2273
mke2fs_discard_device(ext2_filsys fs)2274 static int mke2fs_discard_device(ext2_filsys fs)
2275 {
2276 struct ext2fs_numeric_progress_struct progress;
2277 blk64_t blocks = ext2fs_blocks_count(fs->super);
2278 blk64_t count = DISCARD_STEP_MB;
2279 blk64_t cur;
2280 int retval = 0;
2281
2282 /*
2283 * Let's try if discard really works on the device, so
2284 * we do not print numeric progress resulting in failure
2285 * afterwards.
2286 */
2287 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2288 if (retval)
2289 return retval;
2290 cur = fs->blocksize;
2291
2292 count *= (1024 * 1024);
2293 count /= fs->blocksize;
2294
2295 ext2fs_numeric_progress_init(fs, &progress,
2296 _("Discarding device blocks: "),
2297 blocks);
2298 while (cur < blocks) {
2299 ext2fs_numeric_progress_update(fs, &progress, cur);
2300
2301 if (cur + count > blocks)
2302 count = blocks - cur;
2303
2304 retval = io_channel_discard(fs->io, cur, count);
2305 if (retval)
2306 break;
2307 cur += count;
2308 }
2309
2310 if (retval) {
2311 ext2fs_numeric_progress_close(fs, &progress,
2312 _("failed - "));
2313 if (!quiet)
2314 printf("%s\n",error_message(retval));
2315 } else
2316 ext2fs_numeric_progress_close(fs, &progress,
2317 _("done \n"));
2318
2319 return retval;
2320 }
2321
fix_cluster_bg_counts(ext2_filsys fs)2322 static void fix_cluster_bg_counts(ext2_filsys fs)
2323 {
2324 blk64_t cluster, num_clusters, tot_free;
2325 unsigned num = 0;
2326 int grp_free, num_free, group;
2327
2328 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2329 tot_free = num_free = group = grp_free = 0;
2330 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2331 cluster < num_clusters; cluster++) {
2332 if (!ext2fs_test_block_bitmap2(fs->block_map,
2333 EXT2FS_C2B(fs, cluster))) {
2334 grp_free++;
2335 tot_free++;
2336 }
2337 num++;
2338 if ((num == fs->super->s_clusters_per_group) ||
2339 (cluster == num_clusters-1)) {
2340 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2341 ext2fs_group_desc_csum_set(fs, group);
2342 num = 0;
2343 grp_free = 0;
2344 group++;
2345 }
2346 }
2347 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2348 }
2349
create_quota_inodes(ext2_filsys fs)2350 static int create_quota_inodes(ext2_filsys fs)
2351 {
2352 quota_ctx_t qctx;
2353
2354 quota_init_context(&qctx, fs, -1);
2355 quota_compute_usage(qctx);
2356 quota_write_inode(qctx, quotatype);
2357 quota_release_context(&qctx);
2358
2359 return 0;
2360 }
2361
main(int argc,char * argv[])2362 int main (int argc, char *argv[])
2363 {
2364 errcode_t retval = 0;
2365 ext2_filsys fs;
2366 badblocks_list bb_list = 0;
2367 unsigned int journal_blocks;
2368 unsigned int i, checkinterval;
2369 int max_mnt_count;
2370 int val, hash_alg;
2371 int flags;
2372 int old_bitmaps;
2373 io_manager io_ptr;
2374 char tdb_string[40];
2375 char *hash_alg_str;
2376 int itable_zeroed = 0;
2377
2378 #ifdef ENABLE_NLS
2379 setlocale(LC_MESSAGES, "");
2380 setlocale(LC_CTYPE, "");
2381 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2382 textdomain(NLS_CAT_NAME);
2383 set_com_err_gettext(gettext);
2384 #endif
2385 PRS(argc, argv);
2386
2387 #ifdef CONFIG_TESTIO_DEBUG
2388 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2389 io_ptr = test_io_manager;
2390 test_io_backing_manager = unix_io_manager;
2391 } else
2392 #endif
2393 io_ptr = unix_io_manager;
2394
2395 if (should_do_undo(device_name)) {
2396 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2397 if (retval)
2398 exit(1);
2399 }
2400
2401 /*
2402 * Initialize the superblock....
2403 */
2404 flags = EXT2_FLAG_EXCLUSIVE;
2405 if (direct_io)
2406 flags |= EXT2_FLAG_DIRECT_IO;
2407 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2408 &old_bitmaps);
2409 if (!old_bitmaps)
2410 flags |= EXT2_FLAG_64BITS;
2411 /*
2412 * By default, we print how many inode tables or block groups
2413 * or whatever we've written so far. The quiet flag disables
2414 * this, along with a lot of other output.
2415 */
2416 if (!quiet)
2417 flags |= EXT2_FLAG_PRINT_PROGRESS;
2418 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2419 if (retval) {
2420 com_err(device_name, retval, "%s",
2421 _("while setting up superblock"));
2422 exit(1);
2423 }
2424
2425 /* Calculate journal blocks */
2426 if (!journal_device && ((journal_size) ||
2427 (fs_param.s_feature_compat &
2428 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2429 journal_blocks = figure_journal_size(journal_size, fs);
2430
2431 /* Can't undo discard ... */
2432 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2433 retval = mke2fs_discard_device(fs);
2434 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2435 if (verbose)
2436 printf("%s",
2437 _("Discard succeeded and will return "
2438 "0s - skipping inode table wipe\n"));
2439 lazy_itable_init = 1;
2440 itable_zeroed = 1;
2441 }
2442 }
2443
2444 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2445 32768 : fs->blocksize * 8);
2446 io_channel_set_options(fs->io, tdb_string);
2447
2448 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2449 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2450
2451 if ((fs_param.s_feature_incompat &
2452 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2453 (fs_param.s_feature_ro_compat &
2454 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2455 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2456 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2457 fs->super->s_kbytes_written = 1;
2458
2459 /*
2460 * Wipe out the old on-disk superblock
2461 */
2462 if (!noaction)
2463 zap_sector(fs, 2, 6);
2464
2465 /*
2466 * Parse or generate a UUID for the filesystem
2467 */
2468 if (fs_uuid) {
2469 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2470 com_err(device_name, 0, "could not parse UUID: %s\n",
2471 fs_uuid);
2472 exit(1);
2473 }
2474 } else
2475 uuid_generate(fs->super->s_uuid);
2476
2477 /*
2478 * Initialize the directory index variables
2479 */
2480 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2481 "half_md4");
2482 hash_alg = e2p_string2hash(hash_alg_str);
2483 free(hash_alg_str);
2484 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2485 EXT2_HASH_HALF_MD4;
2486 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2487
2488 /*
2489 * Periodic checks can be enabled/disabled via config file.
2490 * Note we override the kernel include file's idea of what the default
2491 * check interval (never) should be. It's a good idea to check at
2492 * least *occasionally*, specially since servers will never rarely get
2493 * to reboot, since Linux is so robust these days. :-)
2494 *
2495 * 180 days (six months) seems like a good value.
2496 */
2497 #ifdef EXT2_DFL_CHECKINTERVAL
2498 #undef EXT2_DFL_CHECKINTERVAL
2499 #endif
2500 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2501
2502 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2503 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2504 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2505 /*
2506 * Add "jitter" to the superblock's check interval so that we
2507 * don't check all the filesystems at the same time. We use a
2508 * kludgy hack of using the UUID to derive a random jitter value
2509 */
2510 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2511 val += fs->super->s_uuid[i];
2512 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2513 } else
2514 fs->super->s_max_mnt_count = -1;
2515
2516 /*
2517 * Override the creator OS, if applicable
2518 */
2519 if (creator_os && !set_os(fs->super, creator_os)) {
2520 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2521 exit(1);
2522 }
2523
2524 /*
2525 * For the Hurd, we will turn off filetype since it doesn't
2526 * support it.
2527 */
2528 if (fs->super->s_creator_os == EXT2_OS_HURD)
2529 fs->super->s_feature_incompat &=
2530 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2531
2532 /*
2533 * Set the volume label...
2534 */
2535 if (volume_label) {
2536 memset(fs->super->s_volume_name, 0,
2537 sizeof(fs->super->s_volume_name));
2538 strncpy(fs->super->s_volume_name, volume_label,
2539 sizeof(fs->super->s_volume_name));
2540 }
2541
2542 /*
2543 * Set the last mount directory
2544 */
2545 if (mount_dir) {
2546 memset(fs->super->s_last_mounted, 0,
2547 sizeof(fs->super->s_last_mounted));
2548 strncpy(fs->super->s_last_mounted, mount_dir,
2549 sizeof(fs->super->s_last_mounted));
2550 }
2551
2552 if (!quiet || noaction)
2553 show_stats(fs);
2554
2555 if (noaction)
2556 exit(0);
2557
2558 if (fs->super->s_feature_incompat &
2559 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2560 create_journal_dev(fs);
2561 exit(ext2fs_close(fs) ? 1 : 0);
2562 }
2563
2564 if (bad_blocks_filename)
2565 read_bb_file(fs, &bb_list, bad_blocks_filename);
2566 if (cflag)
2567 test_disk(fs, &bb_list);
2568
2569 handle_bad_blocks(fs, bb_list);
2570 fs->stride = fs_stride = fs->super->s_raid_stride;
2571 if (!quiet)
2572 printf("%s", _("Allocating group tables: "));
2573 retval = ext2fs_allocate_tables(fs);
2574 if (retval) {
2575 com_err(program_name, retval, "%s",
2576 _("while trying to allocate filesystem tables"));
2577 exit(1);
2578 }
2579 if (!quiet)
2580 printf("%s", _("done \n"));
2581
2582 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2583 if (retval) {
2584 com_err(program_name, retval, "%s",
2585 _("\n\twhile converting subcluster bitmap"));
2586 exit(1);
2587 }
2588
2589 if (super_only) {
2590 fs->super->s_state |= EXT2_ERROR_FS;
2591 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2592 /*
2593 * The command "mke2fs -S" is used to recover
2594 * corrupted file systems, so do not mark any of the
2595 * inodes as unused; we want e2fsck to consider all
2596 * inodes as potentially containing recoverable data.
2597 */
2598 if (fs->super->s_feature_ro_compat &
2599 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2600 for (i = 0; i < fs->group_desc_count; i++)
2601 ext2fs_bg_itable_unused_set(fs, i, 0);
2602 }
2603 } else {
2604 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2605 blk64_t rsv = 65536 / fs->blocksize;
2606 blk64_t blocks = ext2fs_blocks_count(fs->super);
2607 blk64_t start;
2608 blk64_t ret_blk;
2609
2610 #ifdef ZAP_BOOTBLOCK
2611 zap_sector(fs, 0, 2);
2612 #endif
2613
2614 /*
2615 * Wipe out any old MD RAID (or other) metadata at the end
2616 * of the device. This will also verify that the device is
2617 * as large as we think. Be careful with very small devices.
2618 */
2619 start = (blocks & ~(rsv - 1));
2620 if (start > rsv)
2621 start -= rsv;
2622 if (start > 0)
2623 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2624 &ret_blk, NULL);
2625
2626 if (retval) {
2627 com_err(program_name, retval,
2628 _("while zeroing block %llu at end of filesystem"),
2629 ret_blk);
2630 }
2631 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2632 create_root_dir(fs);
2633 create_lost_and_found(fs);
2634 reserve_inodes(fs);
2635 create_bad_block_inode(fs, bb_list);
2636 if (fs->super->s_feature_compat &
2637 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2638 retval = ext2fs_create_resize_inode(fs);
2639 if (retval) {
2640 com_err("ext2fs_create_resize_inode", retval,
2641 "%s",
2642 _("while reserving blocks for online resize"));
2643 exit(1);
2644 }
2645 }
2646 }
2647
2648 if (journal_device) {
2649 ext2_filsys jfs;
2650
2651 if (!force)
2652 check_plausibility(journal_device);
2653 check_mount(journal_device, force, _("journal"));
2654
2655 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2656 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2657 fs->blocksize, unix_io_manager, &jfs);
2658 if (retval) {
2659 com_err(program_name, retval,
2660 _("while trying to open journal device %s\n"),
2661 journal_device);
2662 exit(1);
2663 }
2664 if (!quiet) {
2665 printf(_("Adding journal to device %s: "),
2666 journal_device);
2667 fflush(stdout);
2668 }
2669 retval = ext2fs_add_journal_device(fs, jfs);
2670 if(retval) {
2671 com_err (program_name, retval,
2672 _("\n\twhile trying to add journal to device %s"),
2673 journal_device);
2674 exit(1);
2675 }
2676 if (!quiet)
2677 printf("%s", _("done\n"));
2678 ext2fs_close(jfs);
2679 free(journal_device);
2680 } else if ((journal_size) ||
2681 (fs_param.s_feature_compat &
2682 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2683 if (super_only) {
2684 printf("%s", _("Skipping journal creation in super-only mode\n"));
2685 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2686 goto no_journal;
2687 }
2688
2689 if (!journal_blocks) {
2690 fs->super->s_feature_compat &=
2691 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2692 goto no_journal;
2693 }
2694 if (!quiet) {
2695 printf(_("Creating journal (%u blocks): "),
2696 journal_blocks);
2697 fflush(stdout);
2698 }
2699 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2700 journal_flags);
2701 if (retval) {
2702 com_err(program_name, retval, "%s",
2703 _("\n\twhile trying to create journal"));
2704 exit(1);
2705 }
2706 if (!quiet)
2707 printf("%s", _("done\n"));
2708 }
2709 no_journal:
2710 if (!super_only &&
2711 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2712 retval = ext2fs_mmp_init(fs);
2713 if (retval) {
2714 fprintf(stderr, "%s",
2715 _("\nError while enabling multiple "
2716 "mount protection feature."));
2717 exit(1);
2718 }
2719 if (!quiet)
2720 printf(_("Multiple mount protection is enabled "
2721 "with update interval %d seconds.\n"),
2722 fs->super->s_mmp_update_interval);
2723 }
2724
2725 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2726 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2727 fix_cluster_bg_counts(fs);
2728 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2729 EXT4_FEATURE_RO_COMPAT_QUOTA))
2730 create_quota_inodes(fs);
2731
2732 if (!quiet)
2733 printf("%s", _("Writing superblocks and "
2734 "filesystem accounting information: "));
2735 checkinterval = fs->super->s_checkinterval;
2736 max_mnt_count = fs->super->s_max_mnt_count;
2737 retval = ext2fs_close(fs);
2738 if (retval) {
2739 fprintf(stderr, "%s",
2740 _("\nWarning, had trouble writing out superblocks."));
2741 } else if (!quiet) {
2742 printf("%s", _("done\n\n"));
2743 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2744 print_check_message(max_mnt_count, checkinterval);
2745 }
2746
2747 remove_error_table(&et_ext2_error_table);
2748 remove_error_table(&et_prof_error_table);
2749 profile_release(profile);
2750 for (i=0; fs_types[i]; i++)
2751 free(fs_types[i]);
2752 free(fs_types);
2753 return retval;
2754 }
2755