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