mke2fs.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mke2fs.c - Make a ext2fs filesystem.
  4. *
  5. * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
  6. * 2003, 2004, 2005 by Theodore Ts'o.
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  9. */
  10. /* Usage: mke2fs [options] device
  11. *
  12. * The device may be a block device or a image of one, but this isn't
  13. * enforced (but it's not much fun on a character device :-).
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <fcntl.h>
  18. #include <ctype.h>
  19. #include <time.h>
  20. #include <getopt.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <mntent.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/types.h>
  27. #include "e2fsbb.h"
  28. #include "ext2fs/ext2_fs.h"
  29. #include "uuid/uuid.h"
  30. #include "e2p/e2p.h"
  31. #include "ext2fs/ext2fs.h"
  32. #include "util.h"
  33. #define STRIDE_LENGTH 8
  34. #ifndef __sparc__
  35. #define ZAP_BOOTBLOCK
  36. #endif
  37. static const char * device_name;
  38. /* Command line options */
  39. static int cflag;
  40. static int quiet;
  41. static int super_only;
  42. static int force;
  43. static int noaction;
  44. static int journal_size;
  45. static int journal_flags;
  46. static const char *bad_blocks_filename;
  47. static __u32 fs_stride;
  48. static struct ext2_super_block param;
  49. static char *creator_os;
  50. static char *volume_label;
  51. static char *mount_dir;
  52. static char *journal_device = NULL;
  53. static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
  54. static int sys_page_size = 4096;
  55. static int linux_version_code = 0;
  56. static int int_log2(int arg)
  57. {
  58. int l = 0;
  59. arg >>= 1;
  60. while (arg) {
  61. l++;
  62. arg >>= 1;
  63. }
  64. return l;
  65. }
  66. static int int_log10(unsigned int arg)
  67. {
  68. int l;
  69. for (l = 0; arg; l++)
  70. arg = arg / 10;
  71. return l;
  72. }
  73. /*
  74. * This function sets the default parameters for a filesystem
  75. *
  76. * The type is specified by the user. The size is the maximum size
  77. * (in megabytes) for which a set of parameters applies, with a size
  78. * of zero meaning that it is the default parameter for the type.
  79. * Note that order is important in the table below.
  80. */
  81. #define DEF_MAX_BLOCKSIZE -1
  82. static const char default_str[] = "default";
  83. struct mke2fs_defaults {
  84. const char *type;
  85. int size;
  86. int blocksize;
  87. int inode_ratio;
  88. };
  89. static const struct mke2fs_defaults settings[] = {
  90. { default_str, 0, 4096, 8192 },
  91. { default_str, 512, 1024, 4096 },
  92. { default_str, 3, 1024, 8192 },
  93. { "journal", 0, 4096, 8192 },
  94. { "news", 0, 4096, 4096 },
  95. { "largefile", 0, 4096, 1024 * 1024 },
  96. { "largefile4", 0, 4096, 4096 * 1024 },
  97. { 0, 0, 0, 0},
  98. };
  99. static void set_fs_defaults(const char *fs_type,
  100. struct ext2_super_block *super,
  101. int blocksize, int sector_size,
  102. int *inode_ratio)
  103. {
  104. int megs;
  105. int ratio = 0;
  106. const struct mke2fs_defaults *p;
  107. int use_bsize = 1024;
  108. megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
  109. if (inode_ratio)
  110. ratio = *inode_ratio;
  111. if (!fs_type)
  112. fs_type = default_str;
  113. for (p = settings; p->type; p++) {
  114. if ((strcmp(p->type, fs_type) != 0) &&
  115. (strcmp(p->type, default_str) != 0))
  116. continue;
  117. if ((p->size != 0) && (megs > p->size))
  118. continue;
  119. if (ratio == 0)
  120. *inode_ratio = p->inode_ratio < blocksize ?
  121. blocksize : p->inode_ratio;
  122. use_bsize = p->blocksize;
  123. }
  124. if (blocksize <= 0) {
  125. if (use_bsize == DEF_MAX_BLOCKSIZE) {
  126. use_bsize = sys_page_size;
  127. if ((linux_version_code < (2*65536 + 6*256)) &&
  128. (use_bsize > 4096))
  129. use_bsize = 4096;
  130. }
  131. if (sector_size && use_bsize < sector_size)
  132. use_bsize = sector_size;
  133. if ((blocksize < 0) && (use_bsize < (-blocksize)))
  134. use_bsize = -blocksize;
  135. blocksize = use_bsize;
  136. super->s_blocks_count /= blocksize / 1024;
  137. }
  138. super->s_log_frag_size = super->s_log_block_size =
  139. int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
  140. }
  141. /*
  142. * Helper function for read_bb_file and test_disk
  143. */
  144. static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
  145. {
  146. bb_error_msg("Bad block %u out of range; ignored", blk);
  147. }
  148. /*
  149. * Busybox stuff
  150. */
  151. static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
  152. static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)
  153. {
  154. va_list ap;
  155. if (retval) {
  156. va_start(ap, fmt);
  157. fprintf(stderr, "\nCould not ");
  158. vfprintf(stderr, fmt, ap);
  159. fprintf(stderr, "\n");
  160. va_end(ap);
  161. exit(EXIT_FAILURE);
  162. }
  163. }
  164. static void mke2fs_verbose(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  165. static void mke2fs_verbose(const char *fmt, ...)
  166. {
  167. va_list ap;
  168. if (!quiet) {
  169. va_start(ap, fmt);
  170. vfprintf(stdout, fmt, ap);
  171. fflush(stdout);
  172. va_end(ap);
  173. }
  174. }
  175. static void mke2fs_verbose_done(void)
  176. {
  177. mke2fs_verbose("done\n");
  178. }
  179. static void mke2fs_warning_msg(int retval, char *fmt, ... ) __attribute__ ((format (printf, 2, 3)));
  180. static void mke2fs_warning_msg(int retval, char *fmt, ... )
  181. {
  182. va_list ap;
  183. if (retval) {
  184. va_start(ap, fmt);
  185. fprintf(stderr, "\nWarning: ");
  186. vfprintf(stderr, fmt, ap);
  187. fprintf(stderr, "\n");
  188. va_end(ap);
  189. }
  190. }
  191. /*
  192. * Reads the bad blocks list from a file
  193. */
  194. static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
  195. const char *bad_blocks_file)
  196. {
  197. FILE *f;
  198. errcode_t retval;
  199. f = xfopen_for_read(bad_blocks_file);
  200. retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
  201. fclose (f);
  202. mke2fs_error_msg_and_die(retval, "read bad blocks from list");
  203. }
  204. /*
  205. * Runs the badblocks program to test the disk
  206. */
  207. static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
  208. {
  209. FILE *f;
  210. errcode_t retval;
  211. char buf[1024];
  212. sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
  213. quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
  214. fs->device_name, fs->super->s_blocks_count);
  215. mke2fs_verbose("Running command: %s\n", buf);
  216. f = popen(buf, "r");
  217. if (!f) {
  218. bb_perror_msg_and_die("can't run '%s'", buf);
  219. }
  220. retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
  221. pclose(f);
  222. mke2fs_error_msg_and_die(retval, "read bad blocks from program");
  223. }
  224. static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
  225. {
  226. dgrp_t i;
  227. blk_t j;
  228. unsigned must_be_good;
  229. blk_t blk;
  230. badblocks_iterate bb_iter;
  231. errcode_t retval;
  232. blk_t group_block;
  233. int group;
  234. int group_bad;
  235. if (!bb_list)
  236. return;
  237. /*
  238. * The primary superblock and group descriptors *must* be
  239. * good; if not, abort.
  240. */
  241. must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
  242. for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
  243. if (ext2fs_badblocks_list_test(bb_list, i)) {
  244. bb_error_msg_and_die(
  245. "Block %d in primary superblock/group descriptor area bad\n"
  246. "Blocks %d through %d must be good in order to build a filesystem\n"
  247. "Aborting ...", i, fs->super->s_first_data_block, must_be_good);
  248. }
  249. }
  250. /*
  251. * See if any of the bad blocks are showing up in the backup
  252. * superblocks and/or group descriptors. If so, issue a
  253. * warning and adjust the block counts appropriately.
  254. */
  255. group_block = fs->super->s_first_data_block +
  256. fs->super->s_blocks_per_group;
  257. for (i = 1; i < fs->group_desc_count; i++) {
  258. group_bad = 0;
  259. for (j=0; j < fs->desc_blocks+1; j++) {
  260. if (ext2fs_badblocks_list_test(bb_list,
  261. group_block + j)) {
  262. mke2fs_warning_msg(!group_bad,
  263. "the backup superblock/group descriptors at block %d contain\n"
  264. "bad blocks\n", group_block);
  265. group_bad++;
  266. group = ext2fs_group_of_blk(fs, group_block+j);
  267. fs->group_desc[group].bg_free_blocks_count++;
  268. fs->super->s_free_blocks_count++;
  269. }
  270. }
  271. group_block += fs->super->s_blocks_per_group;
  272. }
  273. /*
  274. * Mark all the bad blocks as used...
  275. */
  276. retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
  277. mke2fs_error_msg_and_die(retval, "mark bad blocks as used");
  278. while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
  279. ext2fs_mark_block_bitmap(fs->block_map, blk);
  280. ext2fs_badblocks_list_iterate_end(bb_iter);
  281. }
  282. /*
  283. * These functions implement a generalized progress meter.
  284. */
  285. struct progress_struct {
  286. char format[20];
  287. char backup[80];
  288. __u32 max;
  289. int skip_progress;
  290. };
  291. static void progress_init(struct progress_struct *progress,
  292. const char *label,__u32 max)
  293. {
  294. int i;
  295. memset(progress, 0, sizeof(struct progress_struct));
  296. if (quiet)
  297. return;
  298. /*
  299. * Figure out how many digits we need
  300. */
  301. i = int_log10(max);
  302. sprintf(progress->format, "%%%dd/%%%dld", i, i);
  303. memset(progress->backup, '\b', sizeof(progress->backup)-1);
  304. progress->backup[sizeof(progress->backup)-1] = 0;
  305. if ((2*i)+1 < (int) sizeof(progress->backup))
  306. progress->backup[(2*i)+1] = 0;
  307. progress->max = max;
  308. progress->skip_progress = 0;
  309. if (getenv("MKE2FS_SKIP_PROGRESS"))
  310. progress->skip_progress++;
  311. fputs(label, stdout);
  312. fflush(stdout);
  313. }
  314. static void progress_update(struct progress_struct *progress, __u32 val)
  315. {
  316. if ((progress->format[0] == 0) || progress->skip_progress)
  317. return;
  318. printf(progress->format, val, progress->max);
  319. fputs(progress->backup, stdout);
  320. }
  321. static void progress_close(struct progress_struct *progress)
  322. {
  323. if (progress->format[0] == 0)
  324. return;
  325. printf("%-28s\n", "done");
  326. }
  327. /*
  328. * Helper function which zeros out _num_ blocks starting at _blk_. In
  329. * case of an error, the details of the error is returned via _ret_blk_
  330. * and _ret_count_ if they are non-NULL pointers. Returns 0 on
  331. * success, and an error code on an error.
  332. *
  333. * As a special case, if the first argument is NULL, then it will
  334. * attempt to free the static zeroizing buffer. (This is to keep
  335. * programs that check for memory leaks happy.)
  336. */
  337. static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
  338. struct progress_struct *progress,
  339. blk_t *ret_blk, int *ret_count)
  340. {
  341. int j, count, next_update, next_update_incr;
  342. static char *buf;
  343. errcode_t retval;
  344. /* If fs is null, clean up the static buffer and return */
  345. if (!fs) {
  346. if (buf) {
  347. free(buf);
  348. buf = 0;
  349. }
  350. return 0;
  351. }
  352. /* Allocate the zeroizing buffer if necessary */
  353. if (!buf) {
  354. buf = xzalloc(fs->blocksize * STRIDE_LENGTH);
  355. }
  356. /* OK, do the write loop */
  357. next_update = 0;
  358. next_update_incr = num / 100;
  359. if (next_update_incr < 1)
  360. next_update_incr = 1;
  361. for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
  362. count = num - j;
  363. if (count > STRIDE_LENGTH)
  364. count = STRIDE_LENGTH;
  365. retval = io_channel_write_blk(fs->io, blk, count, buf);
  366. if (retval) {
  367. if (ret_count)
  368. *ret_count = count;
  369. if (ret_blk)
  370. *ret_blk = blk;
  371. return retval;
  372. }
  373. if (progress && j > next_update) {
  374. next_update += num / 100;
  375. progress_update(progress, blk);
  376. }
  377. }
  378. return 0;
  379. }
  380. static void write_inode_tables(ext2_filsys fs)
  381. {
  382. errcode_t retval;
  383. blk_t blk;
  384. dgrp_t i;
  385. int num;
  386. struct progress_struct progress;
  387. if (quiet)
  388. memset(&progress, 0, sizeof(progress));
  389. else
  390. progress_init(&progress, "Writing inode tables: ",
  391. fs->group_desc_count);
  392. for (i = 0; i < fs->group_desc_count; i++) {
  393. progress_update(&progress, i);
  394. blk = fs->group_desc[i].bg_inode_table;
  395. num = fs->inode_blocks_per_group;
  396. retval = zero_blocks(fs, blk, num, 0, &blk, &num);
  397. mke2fs_error_msg_and_die(retval,
  398. "write %d blocks in inode table starting at %d.",
  399. num, blk);
  400. if (sync_kludge) {
  401. if (sync_kludge == 1)
  402. sync();
  403. else if ((i % sync_kludge) == 0)
  404. sync();
  405. }
  406. }
  407. zero_blocks(0, 0, 0, 0, 0, 0);
  408. progress_close(&progress);
  409. }
  410. static void create_root_dir(ext2_filsys fs)
  411. {
  412. errcode_t retval;
  413. struct ext2_inode inode;
  414. retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
  415. mke2fs_error_msg_and_die(retval, "create root dir");
  416. if (geteuid()) {
  417. retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
  418. mke2fs_error_msg_and_die(retval, "read root inode");
  419. inode.i_uid = getuid();
  420. if (inode.i_uid)
  421. inode.i_gid = getgid();
  422. retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
  423. mke2fs_error_msg_and_die(retval, "set root inode ownership");
  424. }
  425. }
  426. static void create_lost_and_found(ext2_filsys fs)
  427. {
  428. errcode_t retval;
  429. ext2_ino_t ino;
  430. const char *name = "lost+found";
  431. int i = 1;
  432. char *msg = "create";
  433. int lpf_size = 0;
  434. fs->umask = 077;
  435. retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
  436. if (retval) {
  437. goto CREATE_LOST_AND_FOUND_ERROR;
  438. }
  439. retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
  440. if (retval) {
  441. msg = "lookup";
  442. goto CREATE_LOST_AND_FOUND_ERROR;
  443. }
  444. for (; i < EXT2_NDIR_BLOCKS; i++) {
  445. if ((lpf_size += fs->blocksize) >= 16*1024)
  446. break;
  447. retval = ext2fs_expand_dir(fs, ino);
  448. msg = "expand";
  449. CREATE_LOST_AND_FOUND_ERROR:
  450. mke2fs_error_msg_and_die(retval, "%s %s", msg, name);
  451. }
  452. }
  453. static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
  454. {
  455. errcode_t retval;
  456. ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
  457. fs->group_desc[0].bg_free_inodes_count--;
  458. fs->super->s_free_inodes_count--;
  459. retval = ext2fs_update_bb_inode(fs, bb_list);
  460. mke2fs_error_msg_and_die(retval, "set bad block inode");
  461. }
  462. static void reserve_inodes(ext2_filsys fs)
  463. {
  464. ext2_ino_t i;
  465. int group;
  466. for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
  467. ext2fs_mark_inode_bitmap(fs->inode_map, i);
  468. group = ext2fs_group_of_ino(fs, i);
  469. fs->group_desc[group].bg_free_inodes_count--;
  470. fs->super->s_free_inodes_count--;
  471. }
  472. ext2fs_mark_ib_dirty(fs);
  473. }
  474. #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
  475. #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
  476. #define BSD_LABEL_OFFSET 64
  477. static void zap_sector(ext2_filsys fs, int sect, int nsect)
  478. {
  479. char *buf;
  480. char *fmt = "could not %s %d";
  481. int retval;
  482. unsigned int *magic;
  483. buf = xmalloc(512*nsect);
  484. if (sect == 0) {
  485. /* Check for a BSD disklabel, and don't erase it if so */
  486. retval = io_channel_read_blk(fs->io, 0, -512, buf);
  487. if (retval)
  488. mke2fs_warning_msg(retval, fmt, "read block", 0);
  489. else {
  490. magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
  491. if ((*magic == BSD_DISKMAGIC) ||
  492. (*magic == BSD_MAGICDISK))
  493. return;
  494. }
  495. }
  496. memset(buf, 0, 512*nsect);
  497. io_channel_set_blksize(fs->io, 512);
  498. retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
  499. io_channel_set_blksize(fs->io, fs->blocksize);
  500. free(buf);
  501. mke2fs_warning_msg(retval, fmt, "erase sector", sect);
  502. }
  503. static void create_journal_dev(ext2_filsys fs)
  504. {
  505. struct progress_struct progress;
  506. errcode_t retval;
  507. char *buf;
  508. char *fmt = "%s journal superblock";
  509. blk_t blk;
  510. int count;
  511. retval = ext2fs_create_journal_superblock(fs,
  512. fs->super->s_blocks_count, 0, &buf);
  513. mke2fs_error_msg_and_die(retval, fmt, "init");
  514. if (quiet)
  515. memset(&progress, 0, sizeof(progress));
  516. else
  517. progress_init(&progress, "Zeroing journal device: ",
  518. fs->super->s_blocks_count);
  519. retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
  520. &progress, &blk, &count);
  521. mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)",
  522. blk, count);
  523. zero_blocks(0, 0, 0, 0, 0, 0);
  524. retval = io_channel_write_blk(fs->io,
  525. fs->super->s_first_data_block+1,
  526. 1, buf);
  527. mke2fs_error_msg_and_die(retval, fmt, "write");
  528. progress_close(&progress);
  529. }
  530. static void show_stats(ext2_filsys fs)
  531. {
  532. struct ext2_super_block *s = fs->super;
  533. char *os;
  534. blk_t group_block;
  535. dgrp_t i;
  536. int need, col_left;
  537. mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count),
  538. "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count);
  539. os = e2p_os2string(fs->super->s_creator_os);
  540. printf( "Filesystem label=%.*s\n"
  541. "OS type: %s\n"
  542. "Block size=%u (log=%u)\n"
  543. "Fragment size=%u (log=%u)\n"
  544. "%u inodes, %u blocks\n"
  545. "%u blocks (%2.2f%%) reserved for the super user\n"
  546. "First data block=%u\n",
  547. (int) sizeof(s->s_volume_name),
  548. s->s_volume_name,
  549. os,
  550. fs->blocksize, s->s_log_block_size,
  551. fs->fragsize, s->s_log_frag_size,
  552. s->s_inodes_count, s->s_blocks_count,
  553. s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count,
  554. s->s_first_data_block);
  555. free(os);
  556. if (s->s_reserved_gdt_blocks) {
  557. printf("Maximum filesystem blocks=%lu\n",
  558. (s->s_reserved_gdt_blocks + fs->desc_blocks) *
  559. (fs->blocksize / sizeof(struct ext2_group_desc)) *
  560. s->s_blocks_per_group);
  561. }
  562. printf( "%u block group%s\n"
  563. "%u blocks per group, %u fragments per group\n"
  564. "%u inodes per group\n",
  565. fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "",
  566. s->s_blocks_per_group, s->s_frags_per_group,
  567. s->s_inodes_per_group);
  568. if (fs->group_desc_count == 1) {
  569. bb_putchar('\n');
  570. return;
  571. }
  572. printf("Superblock backups stored on blocks: ");
  573. group_block = s->s_first_data_block;
  574. col_left = 0;
  575. for (i = 1; i < fs->group_desc_count; i++) {
  576. group_block += s->s_blocks_per_group;
  577. if (!ext2fs_bg_has_super(fs, i))
  578. continue;
  579. if (i != 1)
  580. printf(", ");
  581. need = int_log10(group_block) + 2;
  582. if (need > col_left) {
  583. printf("\n\t");
  584. col_left = 72;
  585. }
  586. col_left -= need;
  587. printf("%u", group_block);
  588. }
  589. puts("\n");
  590. }
  591. /*
  592. * Set the S_CREATOR_OS field. Return true if OS is known,
  593. * otherwise, 0.
  594. */
  595. static int set_os(struct ext2_super_block *sb, char *os)
  596. {
  597. if (isdigit (*os)) {
  598. sb->s_creator_os = atoi(os);
  599. return 1;
  600. }
  601. if ((sb->s_creator_os = e2p_string2os(os)) >= 0) {
  602. return 1;
  603. } else if (!strcasecmp("GNU", os)) {
  604. sb->s_creator_os = EXT2_OS_HURD;
  605. return 1;
  606. }
  607. return 0;
  608. }
  609. static void parse_extended_opts(struct ext2_super_block *sb_param,
  610. const char *opts)
  611. {
  612. char *buf, *token, *next, *p, *arg;
  613. int r_usage = 0;
  614. buf = xstrdup(opts);
  615. for (token = buf; token && *token; token = next) {
  616. p = strchr(token, ',');
  617. next = 0;
  618. if (p) {
  619. *p = 0;
  620. next = p+1;
  621. }
  622. arg = strchr(token, '=');
  623. if (arg) {
  624. *arg = 0;
  625. arg++;
  626. }
  627. if (strcmp(token, "stride") == 0) {
  628. if (!arg) {
  629. r_usage++;
  630. continue;
  631. }
  632. fs_stride = strtoul(arg, &p, 0);
  633. if (*p || (fs_stride == 0)) {
  634. bb_error_msg("Invalid stride parameter: %s", arg);
  635. r_usage++;
  636. continue;
  637. }
  638. } else if (!strcmp(token, "resize")) {
  639. unsigned long resize, bpg, rsv_groups;
  640. unsigned long group_desc_count, desc_blocks;
  641. unsigned int gdpb, blocksize;
  642. int rsv_gdb;
  643. if (!arg) {
  644. r_usage++;
  645. continue;
  646. }
  647. resize = parse_num_blocks(arg,
  648. sb_param->s_log_block_size);
  649. if (resize == 0) {
  650. bb_error_msg("Invalid resize parameter: %s", arg);
  651. r_usage++;
  652. continue;
  653. }
  654. if (resize <= sb_param->s_blocks_count) {
  655. bb_error_msg("The resize maximum must be greater "
  656. "than the filesystem size");
  657. r_usage++;
  658. continue;
  659. }
  660. blocksize = EXT2_BLOCK_SIZE(sb_param);
  661. bpg = sb_param->s_blocks_per_group;
  662. if (!bpg)
  663. bpg = blocksize * 8;
  664. gdpb = blocksize / sizeof(struct ext2_group_desc);
  665. group_desc_count = (sb_param->s_blocks_count +
  666. bpg - 1) / bpg;
  667. desc_blocks = (group_desc_count +
  668. gdpb - 1) / gdpb;
  669. rsv_groups = (resize + bpg - 1) / bpg;
  670. rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
  671. desc_blocks;
  672. if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
  673. rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
  674. if (rsv_gdb > 0) {
  675. sb_param->s_feature_compat |=
  676. EXT2_FEATURE_COMPAT_RESIZE_INODE;
  677. sb_param->s_reserved_gdt_blocks = rsv_gdb;
  678. }
  679. } else
  680. r_usage++;
  681. }
  682. if (r_usage) {
  683. bb_error_msg_and_die(
  684. "\nBad options specified.\n\n"
  685. "Extended options are separated by commas, "
  686. "and may take an argument which\n"
  687. "\tis set off by an equals ('=') sign.\n\n"
  688. "Valid extended options are:\n"
  689. "\tstride=<stride length in blocks>\n"
  690. "\tresize=<resize maximum size in blocks>\n");
  691. }
  692. }
  693. static __u32 ok_features[3] = {
  694. EXT3_FEATURE_COMPAT_HAS_JOURNAL |
  695. EXT2_FEATURE_COMPAT_RESIZE_INODE |
  696. EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
  697. EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
  698. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
  699. EXT2_FEATURE_INCOMPAT_META_BG,
  700. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
  701. };
  702. static int PRS(int argc, char **argv)
  703. {
  704. int c;
  705. int size;
  706. char * tmp;
  707. int blocksize = 0;
  708. int inode_ratio = 0;
  709. int inode_size = 0;
  710. int reserved_ratio = 5;
  711. int sector_size = 0;
  712. int show_version_only = 0;
  713. ext2_ino_t num_inodes = 0;
  714. errcode_t retval;
  715. char * extended_opts = NULL;
  716. const char * fs_type = NULL;
  717. blk_t dev_size;
  718. long sysval;
  719. /* Update our PATH to include /sbin */
  720. e2fs_set_sbin_path();
  721. tmp = getenv("MKE2FS_SYNC");
  722. if (tmp)
  723. sync_kludge = atoi(tmp);
  724. /* Determine the system page size if possible */
  725. #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
  726. #define _SC_PAGESIZE _SC_PAGE_SIZE
  727. #endif
  728. #ifdef _SC_PAGESIZE
  729. sysval = sysconf(_SC_PAGESIZE);
  730. if (sysval > 0)
  731. sys_page_size = sysval;
  732. #endif /* _SC_PAGESIZE */
  733. setbuf(stdout, NULL);
  734. setbuf(stderr, NULL);
  735. memset(&param, 0, sizeof(struct ext2_super_block));
  736. param.s_rev_level = 1; /* Create revision 1 filesystems now */
  737. param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
  738. param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  739. #ifdef __linux__
  740. linux_version_code = get_linux_version_code();
  741. if (linux_version_code && linux_version_code < KERNEL_VERSION(2,2,0)) {
  742. param.s_rev_level = 0;
  743. param.s_feature_incompat = 0;
  744. param.s_feature_compat = 0;
  745. param.s_feature_ro_compat = 0;
  746. }
  747. #endif
  748. /* If called as mkfs.ext3, create a journal inode */
  749. if (last_char_is(applet_name, '3'))
  750. journal_size = -1;
  751. while ((c = getopt (argc, argv,
  752. "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
  753. switch (c) {
  754. case 'b':
  755. blocksize = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE);
  756. mke2fs_warning_msg((blocksize > 4096),
  757. "blocksize %d not usable on most systems",
  758. blocksize);
  759. param.s_log_block_size =
  760. int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
  761. break;
  762. case 'c': /* Check for bad blocks */
  763. case 't': /* deprecated */
  764. cflag++;
  765. break;
  766. case 'f':
  767. size = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE);
  768. param.s_log_frag_size =
  769. int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
  770. mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option");
  771. break;
  772. case 'g':
  773. param.s_blocks_per_group = xatou32(optarg);
  774. if ((param.s_blocks_per_group % 8) != 0) {
  775. bb_error_msg_and_die("blocks per group must be multiple of 8");
  776. }
  777. break;
  778. case 'i':
  779. /* Huh? is "* 1024" correct? */
  780. inode_ratio = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE * 1024);
  781. break;
  782. case 'J':
  783. parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
  784. break;
  785. case 'j':
  786. param.s_feature_compat |=
  787. EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  788. if (!journal_size)
  789. journal_size = -1;
  790. break;
  791. case 'l':
  792. bad_blocks_filename = optarg;
  793. break;
  794. case 'm':
  795. reserved_ratio = xatou_range(optarg, 0, 50);
  796. break;
  797. case 'n':
  798. noaction++;
  799. break;
  800. case 'o':
  801. creator_os = optarg;
  802. break;
  803. case 'r':
  804. param.s_rev_level = xatoi_u(optarg);
  805. if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
  806. param.s_feature_incompat = 0;
  807. param.s_feature_compat = 0;
  808. param.s_feature_ro_compat = 0;
  809. }
  810. break;
  811. case 's': /* deprecated */
  812. if (xatou(optarg))
  813. param.s_feature_ro_compat |=
  814. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  815. else
  816. param.s_feature_ro_compat &=
  817. ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  818. break;
  819. #ifdef EXT2_DYNAMIC_REV
  820. case 'I':
  821. inode_size = xatoi_u(optarg);
  822. break;
  823. #endif
  824. case 'N':
  825. num_inodes = xatoi_u(optarg);
  826. break;
  827. case 'v':
  828. quiet = 0;
  829. break;
  830. case 'q':
  831. quiet = 1;
  832. break;
  833. case 'F':
  834. force = 1;
  835. break;
  836. case 'L':
  837. volume_label = optarg;
  838. break;
  839. case 'M':
  840. mount_dir = optarg;
  841. break;
  842. case 'O':
  843. if (!strcmp(optarg, "none")) {
  844. param.s_feature_compat = 0;
  845. param.s_feature_incompat = 0;
  846. param.s_feature_ro_compat = 0;
  847. break;
  848. }
  849. if (e2p_edit_feature(optarg,
  850. &param.s_feature_compat,
  851. ok_features)) {
  852. bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
  853. }
  854. break;
  855. case 'E':
  856. case 'R':
  857. extended_opts = optarg;
  858. break;
  859. case 'S':
  860. super_only = 1;
  861. break;
  862. case 'T':
  863. fs_type = optarg;
  864. break;
  865. case 'V':
  866. /* Print version number and exit */
  867. show_version_only = 1;
  868. quiet = 0;
  869. break;
  870. default:
  871. bb_show_usage();
  872. }
  873. }
  874. if ((optind == argc) /*&& !show_version_only*/)
  875. bb_show_usage();
  876. device_name = argv[optind++];
  877. mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
  878. if (show_version_only) {
  879. return 0;
  880. }
  881. /*
  882. * If there's no blocksize specified and there is a journal
  883. * device, use it to figure out the blocksize
  884. */
  885. if (blocksize <= 0 && journal_device) {
  886. ext2_filsys jfs;
  887. io_manager io_ptr;
  888. #ifdef CONFIG_TESTIO_DEBUG
  889. io_ptr = test_io_manager;
  890. test_io_backing_manager = unix_io_manager;
  891. #else
  892. io_ptr = unix_io_manager;
  893. #endif
  894. retval = ext2fs_open(journal_device,
  895. EXT2_FLAG_JOURNAL_DEV_OK, 0,
  896. 0, io_ptr, &jfs);
  897. mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device);
  898. if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
  899. bb_error_msg_and_die(
  900. "Journal dev blocksize (%d) smaller than "
  901. "minimum blocksize %d\n", jfs->blocksize,
  902. -blocksize);
  903. }
  904. blocksize = jfs->blocksize;
  905. param.s_log_block_size =
  906. int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
  907. ext2fs_close(jfs);
  908. }
  909. if (blocksize > sys_page_size) {
  910. mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)",
  911. blocksize, sys_page_size);
  912. if (!force) {
  913. proceed_question();
  914. }
  915. bb_error_msg("Forced to continue");
  916. }
  917. mke2fs_warning_msg(((blocksize > 4096) &&
  918. (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)),
  919. "some 2.4 kernels do not support "
  920. "blocksizes greater than 4096 using ext3.\n"
  921. "Use -b 4096 if this is an issue for you\n");
  922. if (optind < argc) {
  923. param.s_blocks_count = parse_num_blocks(argv[optind++],
  924. param.s_log_block_size);
  925. mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]);
  926. }
  927. if (optind < argc)
  928. bb_show_usage();
  929. if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  930. if (!fs_type)
  931. fs_type = "journal";
  932. reserved_ratio = 0;
  933. param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
  934. param.s_feature_compat = 0;
  935. param.s_feature_ro_compat = 0;
  936. }
  937. if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
  938. (param.s_feature_compat || param.s_feature_ro_compat ||
  939. param.s_feature_incompat))
  940. param.s_rev_level = 1; /* Create a revision 1 filesystem */
  941. check_plausibility(device_name , force);
  942. check_mount(device_name, force, "filesystem");
  943. param.s_log_frag_size = param.s_log_block_size;
  944. if (noaction && param.s_blocks_count) {
  945. dev_size = param.s_blocks_count;
  946. retval = 0;
  947. } else {
  948. retry:
  949. retval = ext2fs_get_device_size(device_name,
  950. EXT2_BLOCK_SIZE(&param),
  951. &dev_size);
  952. if ((retval == EFBIG) &&
  953. (blocksize == 0) &&
  954. (param.s_log_block_size == 0)) {
  955. param.s_log_block_size = 2;
  956. blocksize = 4096;
  957. goto retry;
  958. }
  959. }
  960. mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size");
  961. if (!param.s_blocks_count) {
  962. if (retval == EXT2_ET_UNIMPLEMENTED) {
  963. mke2fs_error_msg_and_die(1,
  964. "determine device size; you "
  965. "must specify\nthe size of the "
  966. "filesystem");
  967. } else {
  968. if (dev_size == 0) {
  969. bb_error_msg_and_die(
  970. "Device size reported to be zero. "
  971. "Invalid partition specified, or\n\t"
  972. "partition table wasn't reread "
  973. "after running fdisk, due to\n\t"
  974. "a modified partition being busy "
  975. "and in use. You may need to reboot\n\t"
  976. "to re-read your partition table.\n"
  977. );
  978. }
  979. param.s_blocks_count = dev_size;
  980. if (sys_page_size > EXT2_BLOCK_SIZE(&param))
  981. param.s_blocks_count &= ~((sys_page_size /
  982. EXT2_BLOCK_SIZE(&param))-1);
  983. }
  984. } else if (!force && (param.s_blocks_count > dev_size)) {
  985. bb_error_msg("Filesystem larger than apparent device size");
  986. proceed_question();
  987. }
  988. /*
  989. * If the user asked for HAS_JOURNAL, then make sure a journal
  990. * gets created.
  991. */
  992. if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
  993. !journal_size)
  994. journal_size = -1;
  995. /* Set first meta blockgroup via an environment variable */
  996. /* (this is mostly for debugging purposes) */
  997. if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
  998. ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
  999. param.s_first_meta_bg = atoi(tmp);
  1000. /* Get the hardware sector size, if available */
  1001. retval = ext2fs_get_device_sectsize(device_name, &sector_size);
  1002. mke2fs_error_msg_and_die(retval, "determine hardware sector size");
  1003. if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
  1004. sector_size = atoi(tmp);
  1005. set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
  1006. blocksize = EXT2_BLOCK_SIZE(&param);
  1007. if (extended_opts)
  1008. parse_extended_opts(&param, extended_opts);
  1009. /* Since sparse_super is the default, we would only have a problem
  1010. * here if it was explicitly disabled.
  1011. */
  1012. if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
  1013. !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  1014. bb_error_msg_and_die("reserved online resize blocks not supported "
  1015. "on non-sparse filesystem");
  1016. }
  1017. if (param.s_blocks_per_group) {
  1018. if (param.s_blocks_per_group < 256 ||
  1019. param.s_blocks_per_group > 8 * (unsigned) blocksize) {
  1020. bb_error_msg_and_die("blocks per group count out of range");
  1021. }
  1022. }
  1023. if (!force && param.s_blocks_count >= (1 << 31)) {
  1024. bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n"
  1025. "\t (8TB using a blocksize of 4k) are currently supported.");
  1026. }
  1027. if (inode_size) {
  1028. if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
  1029. inode_size > EXT2_BLOCK_SIZE(&param) ||
  1030. inode_size & (inode_size - 1)) {
  1031. bb_error_msg_and_die("invalid inode size %d (min %d/max %d)",
  1032. inode_size, EXT2_GOOD_OLD_INODE_SIZE,
  1033. blocksize);
  1034. }
  1035. mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE),
  1036. "%d-byte inodes not usable on most systems",
  1037. inode_size);
  1038. param.s_inode_size = inode_size;
  1039. }
  1040. /*
  1041. * Calculate number of inodes based on the inode ratio
  1042. */
  1043. param.s_inodes_count = num_inodes ? num_inodes :
  1044. ((__u64) param.s_blocks_count * blocksize)
  1045. / inode_ratio;
  1046. /*
  1047. * Calculate number of blocks to reserve
  1048. */
  1049. param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
  1050. return 1;
  1051. }
  1052. static void mke2fs_clean_up(void)
  1053. {
  1054. if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
  1055. }
  1056. int mke2fs_main (int argc, char **argv);
  1057. int mke2fs_main (int argc, char **argv)
  1058. {
  1059. errcode_t retval;
  1060. ext2_filsys fs;
  1061. badblocks_list bb_list = 0;
  1062. unsigned int i;
  1063. int val;
  1064. io_manager io_ptr;
  1065. if (ENABLE_FEATURE_CLEAN_UP)
  1066. atexit(mke2fs_clean_up);
  1067. if (!PRS(argc, argv))
  1068. return 0;
  1069. #ifdef CONFIG_TESTIO_DEBUG
  1070. io_ptr = test_io_manager;
  1071. test_io_backing_manager = unix_io_manager;
  1072. #else
  1073. io_ptr = unix_io_manager;
  1074. #endif
  1075. /*
  1076. * Initialize the superblock....
  1077. */
  1078. retval = ext2fs_initialize(device_name, 0, &param,
  1079. io_ptr, &fs);
  1080. mke2fs_error_msg_and_die(retval, "set up superblock");
  1081. /*
  1082. * Wipe out the old on-disk superblock
  1083. */
  1084. if (!noaction)
  1085. zap_sector(fs, 2, 6);
  1086. /*
  1087. * Generate a UUID for it...
  1088. */
  1089. uuid_generate(fs->super->s_uuid);
  1090. /*
  1091. * Initialize the directory index variables
  1092. */
  1093. fs->super->s_def_hash_version = EXT2_HASH_TEA;
  1094. uuid_generate((unsigned char *) fs->super->s_hash_seed);
  1095. /*
  1096. * Add "jitter" to the superblock's check interval so that we
  1097. * don't check all the filesystems at the same time. We use a
  1098. * kludgy hack of using the UUID to derive a random jitter value.
  1099. */
  1100. for (i = 0, val = 0; i < sizeof(fs->super->s_uuid); i++)
  1101. val += fs->super->s_uuid[i];
  1102. fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
  1103. /*
  1104. * Override the creator OS, if applicable
  1105. */
  1106. if (creator_os && !set_os(fs->super, creator_os)) {
  1107. bb_error_msg_and_die("unknown os - %s", creator_os);
  1108. }
  1109. /*
  1110. * For the Hurd, we will turn off filetype since it doesn't
  1111. * support it.
  1112. */
  1113. if (fs->super->s_creator_os == EXT2_OS_HURD)
  1114. fs->super->s_feature_incompat &=
  1115. ~EXT2_FEATURE_INCOMPAT_FILETYPE;
  1116. /*
  1117. * Set the volume label...
  1118. */
  1119. if (volume_label) {
  1120. snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label);
  1121. }
  1122. /*
  1123. * Set the last mount directory
  1124. */
  1125. if (mount_dir) {
  1126. snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir);
  1127. }
  1128. if (!quiet || noaction)
  1129. show_stats(fs);
  1130. if (noaction)
  1131. return 0;
  1132. if (fs->super->s_feature_incompat &
  1133. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  1134. create_journal_dev(fs);
  1135. return (ext2fs_close(fs) ? 1 : 0);
  1136. }
  1137. if (bad_blocks_filename)
  1138. read_bb_file(fs, &bb_list, bad_blocks_filename);
  1139. if (cflag)
  1140. test_disk(fs, &bb_list);
  1141. handle_bad_blocks(fs, bb_list);
  1142. fs->stride = fs_stride;
  1143. retval = ext2fs_allocate_tables(fs);
  1144. mke2fs_error_msg_and_die(retval, "allocate filesystem tables");
  1145. if (super_only) {
  1146. fs->super->s_state |= EXT2_ERROR_FS;
  1147. fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
  1148. } else {
  1149. /* rsv must be a power of two (64kB is MD RAID sb alignment) */
  1150. unsigned int rsv = 65536 / fs->blocksize;
  1151. unsigned long blocks = fs->super->s_blocks_count;
  1152. unsigned long start;
  1153. blk_t ret_blk;
  1154. #ifdef ZAP_BOOTBLOCK
  1155. zap_sector(fs, 0, 2);
  1156. #endif
  1157. /*
  1158. * Wipe out any old MD RAID (or other) metadata at the end
  1159. * of the device. This will also verify that the device is
  1160. * as large as we think. Be careful with very small devices.
  1161. */
  1162. start = (blocks & ~(rsv - 1));
  1163. if (start > rsv)
  1164. start -= rsv;
  1165. if (start > 0)
  1166. retval = zero_blocks(fs, start, blocks - start,
  1167. NULL, &ret_blk, NULL);
  1168. mke2fs_warning_msg(retval, "can't zero block %u at end of filesystem", ret_blk);
  1169. write_inode_tables(fs);
  1170. create_root_dir(fs);
  1171. create_lost_and_found(fs);
  1172. reserve_inodes(fs);
  1173. create_bad_block_inode(fs, bb_list);
  1174. if (fs->super->s_feature_compat &
  1175. EXT2_FEATURE_COMPAT_RESIZE_INODE) {
  1176. retval = ext2fs_create_resize_inode(fs);
  1177. mke2fs_error_msg_and_die(retval, "reserve blocks for online resize");
  1178. }
  1179. }
  1180. if (journal_device) {
  1181. make_journal_device(journal_device, fs, quiet, force);
  1182. } else if (journal_size) {
  1183. make_journal_blocks(fs, journal_size, journal_flags, quiet);
  1184. }
  1185. mke2fs_verbose("Writing superblocks and filesystem accounting information: ");
  1186. retval = ext2fs_flush(fs);
  1187. mke2fs_warning_msg(retval, "had trouble writing out superblocks");
  1188. mke2fs_verbose_done();
  1189. if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG"))
  1190. print_check_message(fs);
  1191. val = ext2fs_close(fs);
  1192. return (retval || val) ? 1 : 0;
  1193. }