mke2fs.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 source tree.
  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 %u %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;
  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. for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
  359. count = num - j;
  360. if (count > STRIDE_LENGTH)
  361. count = STRIDE_LENGTH;
  362. retval = io_channel_write_blk(fs->io, blk, count, buf);
  363. if (retval) {
  364. if (ret_count)
  365. *ret_count = count;
  366. if (ret_blk)
  367. *ret_blk = blk;
  368. return retval;
  369. }
  370. if (progress && j > next_update) {
  371. next_update += num / 100;
  372. progress_update(progress, blk);
  373. }
  374. }
  375. return 0;
  376. }
  377. static void write_inode_tables(ext2_filsys fs)
  378. {
  379. errcode_t retval;
  380. blk_t blk;
  381. dgrp_t i;
  382. int num;
  383. struct progress_struct progress;
  384. if (quiet)
  385. memset(&progress, 0, sizeof(progress));
  386. else
  387. progress_init(&progress, "Writing inode tables: ",
  388. fs->group_desc_count);
  389. for (i = 0; i < fs->group_desc_count; i++) {
  390. progress_update(&progress, i);
  391. blk = fs->group_desc[i].bg_inode_table;
  392. num = fs->inode_blocks_per_group;
  393. retval = zero_blocks(fs, blk, num, 0, &blk, &num);
  394. mke2fs_error_msg_and_die(retval,
  395. "write %d blocks in inode table starting at %d.",
  396. num, blk);
  397. if (sync_kludge) {
  398. if (sync_kludge == 1)
  399. sync();
  400. else if ((i % sync_kludge) == 0)
  401. sync();
  402. }
  403. }
  404. zero_blocks(0, 0, 0, 0, 0, 0);
  405. progress_close(&progress);
  406. }
  407. static void create_root_dir(ext2_filsys fs)
  408. {
  409. errcode_t retval;
  410. struct ext2_inode inode;
  411. retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
  412. mke2fs_error_msg_and_die(retval, "create root dir");
  413. if (geteuid()) {
  414. retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
  415. mke2fs_error_msg_and_die(retval, "read root inode");
  416. inode.i_uid = getuid();
  417. if (inode.i_uid)
  418. inode.i_gid = getgid();
  419. retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
  420. mke2fs_error_msg_and_die(retval, "set root inode ownership");
  421. }
  422. }
  423. static void create_lost_and_found(ext2_filsys fs)
  424. {
  425. errcode_t retval;
  426. ext2_ino_t ino;
  427. const char *name = "lost+found";
  428. int i = 1;
  429. char *msg = "create";
  430. int lpf_size = 0;
  431. fs->umask = 077;
  432. retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
  433. if (retval) {
  434. goto CREATE_LOST_AND_FOUND_ERROR;
  435. }
  436. retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
  437. if (retval) {
  438. msg = "lookup";
  439. goto CREATE_LOST_AND_FOUND_ERROR;
  440. }
  441. for (; i < EXT2_NDIR_BLOCKS; i++) {
  442. if ((lpf_size += fs->blocksize) >= 16*1024)
  443. break;
  444. retval = ext2fs_expand_dir(fs, ino);
  445. msg = "expand";
  446. CREATE_LOST_AND_FOUND_ERROR:
  447. mke2fs_error_msg_and_die(retval, "%s %s", msg, name);
  448. }
  449. }
  450. static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
  451. {
  452. errcode_t retval;
  453. ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
  454. fs->group_desc[0].bg_free_inodes_count--;
  455. fs->super->s_free_inodes_count--;
  456. retval = ext2fs_update_bb_inode(fs, bb_list);
  457. mke2fs_error_msg_and_die(retval, "set bad block inode");
  458. }
  459. static void reserve_inodes(ext2_filsys fs)
  460. {
  461. ext2_ino_t i;
  462. int group;
  463. for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
  464. ext2fs_mark_inode_bitmap(fs->inode_map, i);
  465. group = ext2fs_group_of_ino(fs, i);
  466. fs->group_desc[group].bg_free_inodes_count--;
  467. fs->super->s_free_inodes_count--;
  468. }
  469. ext2fs_mark_ib_dirty(fs);
  470. }
  471. #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
  472. #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
  473. #define BSD_LABEL_OFFSET 64
  474. static void zap_sector(ext2_filsys fs, int sect, int nsect)
  475. {
  476. char *buf;
  477. char *fmt = "could not %s %d";
  478. int retval;
  479. unsigned int *magic;
  480. buf = xmalloc(512*nsect);
  481. if (sect == 0) {
  482. /* Check for a BSD disklabel, and don't erase it if so */
  483. retval = io_channel_read_blk(fs->io, 0, -512, buf);
  484. if (retval)
  485. mke2fs_warning_msg(retval, fmt, "read block", 0);
  486. else {
  487. magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
  488. if ((*magic == BSD_DISKMAGIC) ||
  489. (*magic == BSD_MAGICDISK))
  490. return;
  491. }
  492. }
  493. memset(buf, 0, 512*nsect);
  494. io_channel_set_blksize(fs->io, 512);
  495. retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
  496. io_channel_set_blksize(fs->io, fs->blocksize);
  497. free(buf);
  498. mke2fs_warning_msg(retval, fmt, "erase sector", sect);
  499. }
  500. static void create_journal_dev(ext2_filsys fs)
  501. {
  502. struct progress_struct progress;
  503. errcode_t retval;
  504. char *buf;
  505. char *fmt = "%s journal superblock";
  506. blk_t blk;
  507. int count;
  508. retval = ext2fs_create_journal_superblock(fs,
  509. fs->super->s_blocks_count, 0, &buf);
  510. mke2fs_error_msg_and_die(retval, fmt, "init");
  511. if (quiet)
  512. memset(&progress, 0, sizeof(progress));
  513. else
  514. progress_init(&progress, "Zeroing journal device: ",
  515. fs->super->s_blocks_count);
  516. retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
  517. &progress, &blk, &count);
  518. mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)",
  519. blk, count);
  520. zero_blocks(0, 0, 0, 0, 0, 0);
  521. retval = io_channel_write_blk(fs->io,
  522. fs->super->s_first_data_block+1,
  523. 1, buf);
  524. mke2fs_error_msg_and_die(retval, fmt, "write");
  525. progress_close(&progress);
  526. }
  527. static void show_stats(ext2_filsys fs)
  528. {
  529. struct ext2_super_block *s = fs->super;
  530. char *os;
  531. blk_t group_block;
  532. dgrp_t i;
  533. int need, col_left;
  534. mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count),
  535. "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count);
  536. os = e2p_os2string(fs->super->s_creator_os);
  537. printf( "Filesystem label=%.*s\n"
  538. "OS type: %s\n"
  539. "Block size=%u (log=%u)\n"
  540. "Fragment size=%u (log=%u)\n"
  541. "%u inodes, %u blocks\n"
  542. "%u blocks (%2.2f%%) reserved for the super user\n"
  543. "First data block=%u\n",
  544. (int) sizeof(s->s_volume_name),
  545. s->s_volume_name,
  546. os,
  547. fs->blocksize, s->s_log_block_size,
  548. fs->fragsize, s->s_log_frag_size,
  549. s->s_inodes_count, s->s_blocks_count,
  550. s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count,
  551. s->s_first_data_block);
  552. free(os);
  553. if (s->s_reserved_gdt_blocks) {
  554. printf("Maximum filesystem blocks=%lu\n",
  555. (s->s_reserved_gdt_blocks + fs->desc_blocks) *
  556. (fs->blocksize / sizeof(struct ext2_group_desc)) *
  557. s->s_blocks_per_group);
  558. }
  559. printf( "%u block group%s\n"
  560. "%u blocks per group, %u fragments per group\n"
  561. "%u inodes per group\n",
  562. fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "",
  563. s->s_blocks_per_group, s->s_frags_per_group,
  564. s->s_inodes_per_group);
  565. if (fs->group_desc_count == 1) {
  566. bb_putchar('\n');
  567. return;
  568. }
  569. printf("Superblock backups stored on blocks: ");
  570. group_block = s->s_first_data_block;
  571. col_left = 0;
  572. for (i = 1; i < fs->group_desc_count; i++) {
  573. group_block += s->s_blocks_per_group;
  574. if (!ext2fs_bg_has_super(fs, i))
  575. continue;
  576. if (i != 1)
  577. printf(", ");
  578. need = int_log10(group_block) + 2;
  579. if (need > col_left) {
  580. printf("\n\t");
  581. col_left = 72;
  582. }
  583. col_left -= need;
  584. printf("%u", group_block);
  585. }
  586. puts("\n");
  587. }
  588. /*
  589. * Set the S_CREATOR_OS field. Return true if OS is known,
  590. * otherwise, 0.
  591. */
  592. static int set_os(struct ext2_super_block *sb, char *os)
  593. {
  594. if (isdigit (*os)) {
  595. sb->s_creator_os = atoi(os);
  596. return 1;
  597. }
  598. if ((sb->s_creator_os = e2p_string2os(os)) >= 0) {
  599. return 1;
  600. } else if (!strcasecmp("GNU", os)) {
  601. sb->s_creator_os = EXT2_OS_HURD;
  602. return 1;
  603. }
  604. return 0;
  605. }
  606. static void parse_extended_opts(struct ext2_super_block *sb_param,
  607. const char *opts)
  608. {
  609. char *buf, *token, *next, *p, *arg;
  610. int r_usage = 0;
  611. buf = xstrdup(opts);
  612. for (token = buf; token && *token; token = next) {
  613. p = strchr(token, ',');
  614. next = 0;
  615. if (p) {
  616. *p = 0;
  617. next = p+1;
  618. }
  619. arg = strchr(token, '=');
  620. if (arg) {
  621. *arg = 0;
  622. arg++;
  623. }
  624. if (strcmp(token, "stride") == 0) {
  625. if (!arg) {
  626. r_usage++;
  627. continue;
  628. }
  629. fs_stride = strtoul(arg, &p, 0);
  630. if (*p || (fs_stride == 0)) {
  631. bb_error_msg("Invalid stride parameter: %s", arg);
  632. r_usage++;
  633. continue;
  634. }
  635. } else if (!strcmp(token, "resize")) {
  636. unsigned long resize, bpg, rsv_groups;
  637. unsigned long group_desc_count, desc_blocks;
  638. unsigned int gdpb, blocksize;
  639. int rsv_gdb;
  640. if (!arg) {
  641. r_usage++;
  642. continue;
  643. }
  644. resize = parse_num_blocks(arg,
  645. sb_param->s_log_block_size);
  646. if (resize == 0) {
  647. bb_error_msg("Invalid resize parameter: %s", arg);
  648. r_usage++;
  649. continue;
  650. }
  651. if (resize <= sb_param->s_blocks_count) {
  652. bb_error_msg("The resize maximum must be greater "
  653. "than the filesystem size");
  654. r_usage++;
  655. continue;
  656. }
  657. blocksize = EXT2_BLOCK_SIZE(sb_param);
  658. bpg = sb_param->s_blocks_per_group;
  659. if (!bpg)
  660. bpg = blocksize * 8;
  661. gdpb = blocksize / sizeof(struct ext2_group_desc);
  662. group_desc_count = (sb_param->s_blocks_count +
  663. bpg - 1) / bpg;
  664. desc_blocks = (group_desc_count +
  665. gdpb - 1) / gdpb;
  666. rsv_groups = (resize + bpg - 1) / bpg;
  667. rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
  668. desc_blocks;
  669. if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
  670. rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
  671. if (rsv_gdb > 0) {
  672. sb_param->s_feature_compat |=
  673. EXT2_FEATURE_COMPAT_RESIZE_INO;
  674. sb_param->s_reserved_gdt_blocks = rsv_gdb;
  675. }
  676. } else
  677. r_usage++;
  678. }
  679. if (r_usage) {
  680. bb_error_msg_and_die(
  681. "\nBad options specified.\n\n"
  682. "Extended options are separated by commas, "
  683. "and may take an argument which\n"
  684. "\tis set off by an equals ('=') sign.\n\n"
  685. "Valid extended options are:\n"
  686. "\tstride=<stride length in blocks>\n"
  687. "\tresize=<resize maximum size in blocks>\n");
  688. }
  689. }
  690. static __u32 ok_features[3] = {
  691. EXT3_FEATURE_COMPAT_HAS_JOURNAL |
  692. EXT2_FEATURE_COMPAT_RESIZE_INO |
  693. EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
  694. EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
  695. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
  696. EXT2_FEATURE_INCOMPAT_META_BG,
  697. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
  698. };
  699. static int PRS(int argc, char **argv)
  700. {
  701. int c;
  702. int size;
  703. char * tmp;
  704. int blocksize = 0;
  705. int inode_ratio = 0;
  706. int inode_size = 0;
  707. int reserved_ratio = 5;
  708. int sector_size = 0;
  709. int show_version_only = 0;
  710. ext2_ino_t num_inodes = 0;
  711. errcode_t retval;
  712. char * extended_opts = NULL;
  713. const char * fs_type = NULL;
  714. blk_t dev_size;
  715. long sysval;
  716. /* Update our PATH to include /sbin */
  717. e2fs_set_sbin_path();
  718. tmp = getenv("MKE2FS_SYNC");
  719. if (tmp)
  720. sync_kludge = atoi(tmp);
  721. /* Determine the system page size if possible */
  722. #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
  723. #define _SC_PAGESIZE _SC_PAGE_SIZE
  724. #endif
  725. #ifdef _SC_PAGESIZE
  726. sysval = sysconf(_SC_PAGESIZE);
  727. if (sysval > 0)
  728. sys_page_size = sysval;
  729. #endif /* _SC_PAGESIZE */
  730. setbuf(stdout, NULL);
  731. setbuf(stderr, NULL);
  732. memset(&param, 0, sizeof(struct ext2_super_block));
  733. param.s_rev_level = 1; /* Create revision 1 filesystems now */
  734. param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
  735. param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  736. #ifdef __linux__
  737. linux_version_code = get_linux_version_code();
  738. if (linux_version_code && linux_version_code < KERNEL_VERSION(2,2,0)) {
  739. param.s_rev_level = 0;
  740. param.s_feature_incompat = 0;
  741. param.s_feature_compat = 0;
  742. param.s_feature_ro_compat = 0;
  743. }
  744. #endif
  745. /* If called as mkfs.ext3, create a journal inode */
  746. if (last_char_is(applet_name, '3'))
  747. journal_size = -1;
  748. while ((c = getopt (argc, argv,
  749. "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
  750. switch (c) {
  751. case 'b':
  752. blocksize = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE);
  753. mke2fs_warning_msg((blocksize > 4096),
  754. "blocksize %d not usable on most systems",
  755. blocksize);
  756. param.s_log_block_size =
  757. int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
  758. break;
  759. case 'c': /* Check for bad blocks */
  760. case 't': /* deprecated */
  761. cflag++;
  762. break;
  763. case 'f':
  764. size = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE);
  765. param.s_log_frag_size =
  766. int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
  767. mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option");
  768. break;
  769. case 'g':
  770. param.s_blocks_per_group = xatou32(optarg);
  771. if ((param.s_blocks_per_group % 8) != 0) {
  772. bb_error_msg_and_die("blocks per group must be multiple of 8");
  773. }
  774. break;
  775. case 'i':
  776. /* Huh? is "* 1024" correct? */
  777. inode_ratio = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE * 1024);
  778. break;
  779. case 'J':
  780. parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
  781. break;
  782. case 'j':
  783. param.s_feature_compat |=
  784. EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  785. if (!journal_size)
  786. journal_size = -1;
  787. break;
  788. case 'l':
  789. bad_blocks_filename = optarg;
  790. break;
  791. case 'm':
  792. reserved_ratio = xatou_range(optarg, 0, 50);
  793. break;
  794. case 'n':
  795. noaction++;
  796. break;
  797. case 'o':
  798. creator_os = optarg;
  799. break;
  800. case 'r':
  801. param.s_rev_level = xatoi_positive(optarg);
  802. if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
  803. param.s_feature_incompat = 0;
  804. param.s_feature_compat = 0;
  805. param.s_feature_ro_compat = 0;
  806. }
  807. break;
  808. case 's': /* deprecated */
  809. if (xatou(optarg))
  810. param.s_feature_ro_compat |=
  811. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  812. else
  813. param.s_feature_ro_compat &=
  814. ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  815. break;
  816. #ifdef EXT2_DYNAMIC_REV
  817. case 'I':
  818. inode_size = xatoi_positive(optarg);
  819. break;
  820. #endif
  821. case 'N':
  822. num_inodes = xatoi_positive(optarg);
  823. break;
  824. case 'v':
  825. quiet = 0;
  826. break;
  827. case 'q':
  828. quiet = 1;
  829. break;
  830. case 'F':
  831. force = 1;
  832. break;
  833. case 'L':
  834. volume_label = optarg;
  835. break;
  836. case 'M':
  837. mount_dir = optarg;
  838. break;
  839. case 'O':
  840. if (!strcmp(optarg, "none")) {
  841. param.s_feature_compat = 0;
  842. param.s_feature_incompat = 0;
  843. param.s_feature_ro_compat = 0;
  844. break;
  845. }
  846. if (e2p_edit_feature(optarg,
  847. &param.s_feature_compat,
  848. ok_features)) {
  849. bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
  850. }
  851. break;
  852. case 'E':
  853. case 'R':
  854. extended_opts = optarg;
  855. break;
  856. case 'S':
  857. super_only = 1;
  858. break;
  859. case 'T':
  860. fs_type = optarg;
  861. break;
  862. case 'V':
  863. /* Print version number and exit */
  864. show_version_only = 1;
  865. quiet = 0;
  866. break;
  867. default:
  868. bb_show_usage();
  869. }
  870. }
  871. if ((optind == argc) /*&& !show_version_only*/)
  872. bb_show_usage();
  873. device_name = argv[optind++];
  874. mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
  875. if (show_version_only) {
  876. return 0;
  877. }
  878. /*
  879. * If there's no blocksize specified and there is a journal
  880. * device, use it to figure out the blocksize
  881. */
  882. if (blocksize <= 0 && journal_device) {
  883. ext2_filsys jfs;
  884. io_manager io_ptr;
  885. #ifdef CONFIG_TESTIO_DEBUG
  886. io_ptr = test_io_manager;
  887. test_io_backing_manager = unix_io_manager;
  888. #else
  889. io_ptr = unix_io_manager;
  890. #endif
  891. retval = ext2fs_open(journal_device,
  892. EXT2_FLAG_JOURNAL_DEV_OK, 0,
  893. 0, io_ptr, &jfs);
  894. mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device);
  895. if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
  896. bb_error_msg_and_die(
  897. "Journal dev blocksize (%d) smaller than "
  898. "minimum blocksize %d\n", jfs->blocksize,
  899. -blocksize);
  900. }
  901. blocksize = jfs->blocksize;
  902. param.s_log_block_size =
  903. int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
  904. ext2fs_close(jfs);
  905. }
  906. if (blocksize > sys_page_size) {
  907. mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)",
  908. blocksize, sys_page_size);
  909. if (!force) {
  910. proceed_question();
  911. }
  912. bb_error_msg("Forced to continue");
  913. }
  914. mke2fs_warning_msg(((blocksize > 4096) &&
  915. (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)),
  916. "some 2.4 kernels do not support "
  917. "blocksizes greater than 4096 using ext3.\n"
  918. "Use -b 4096 if this is an issue for you\n");
  919. if (optind < argc) {
  920. param.s_blocks_count = parse_num_blocks(argv[optind++],
  921. param.s_log_block_size);
  922. mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]);
  923. }
  924. if (optind < argc)
  925. bb_show_usage();
  926. if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  927. if (!fs_type)
  928. fs_type = "journal";
  929. reserved_ratio = 0;
  930. param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
  931. param.s_feature_compat = 0;
  932. param.s_feature_ro_compat = 0;
  933. }
  934. if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
  935. (param.s_feature_compat || param.s_feature_ro_compat ||
  936. param.s_feature_incompat))
  937. param.s_rev_level = 1; /* Create a revision 1 filesystem */
  938. check_plausibility(device_name , force);
  939. check_mount(device_name, force, "filesystem");
  940. param.s_log_frag_size = param.s_log_block_size;
  941. if (noaction && param.s_blocks_count) {
  942. dev_size = param.s_blocks_count;
  943. retval = 0;
  944. } else {
  945. retry:
  946. retval = ext2fs_get_device_size(device_name,
  947. EXT2_BLOCK_SIZE(&param),
  948. &dev_size);
  949. if ((retval == EFBIG) &&
  950. (blocksize == 0) &&
  951. (param.s_log_block_size == 0)) {
  952. param.s_log_block_size = 2;
  953. blocksize = 4096;
  954. goto retry;
  955. }
  956. }
  957. mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size");
  958. if (!param.s_blocks_count) {
  959. if (retval == EXT2_ET_UNIMPLEMENTED) {
  960. mke2fs_error_msg_and_die(1,
  961. "determine device size; you "
  962. "must specify\nthe size of the "
  963. "filesystem");
  964. } else {
  965. if (dev_size == 0) {
  966. bb_error_msg_and_die(
  967. "Device size reported to be zero. "
  968. "Invalid partition specified, or\n\t"
  969. "partition table wasn't reread "
  970. "after running fdisk, due to\n\t"
  971. "a modified partition being busy "
  972. "and in use. You may need to reboot\n\t"
  973. "to re-read your partition table.\n"
  974. );
  975. }
  976. param.s_blocks_count = dev_size;
  977. if (sys_page_size > EXT2_BLOCK_SIZE(&param))
  978. param.s_blocks_count &= ~((sys_page_size /
  979. EXT2_BLOCK_SIZE(&param))-1);
  980. }
  981. } else if (!force && (param.s_blocks_count > dev_size)) {
  982. bb_error_msg("Filesystem larger than apparent device size");
  983. proceed_question();
  984. }
  985. /*
  986. * If the user asked for HAS_JOURNAL, then make sure a journal
  987. * gets created.
  988. */
  989. if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
  990. !journal_size)
  991. journal_size = -1;
  992. /* Set first meta blockgroup via an environment variable */
  993. /* (this is mostly for debugging purposes) */
  994. if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
  995. ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
  996. param.s_first_meta_bg = atoi(tmp);
  997. /* Get the hardware sector size, if available */
  998. retval = ext2fs_get_device_sectsize(device_name, &sector_size);
  999. mke2fs_error_msg_and_die(retval, "determine hardware sector size");
  1000. if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
  1001. sector_size = atoi(tmp);
  1002. set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
  1003. blocksize = EXT2_BLOCK_SIZE(&param);
  1004. if (extended_opts)
  1005. parse_extended_opts(&param, extended_opts);
  1006. /* Since sparse_super is the default, we would only have a problem
  1007. * here if it was explicitly disabled.
  1008. */
  1009. if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INO) &&
  1010. !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  1011. bb_error_msg_and_die("reserved online resize blocks not supported "
  1012. "on non-sparse filesystem");
  1013. }
  1014. if (param.s_blocks_per_group) {
  1015. if (param.s_blocks_per_group < 256 ||
  1016. param.s_blocks_per_group > 8 * (unsigned) blocksize) {
  1017. bb_error_msg_and_die("blocks per group count out of range");
  1018. }
  1019. }
  1020. if (!force && param.s_blocks_count >= (1 << 31)) {
  1021. bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n"
  1022. "\t (8TB using a blocksize of 4k) are currently supported.");
  1023. }
  1024. if (inode_size) {
  1025. if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
  1026. inode_size > EXT2_BLOCK_SIZE(&param) ||
  1027. inode_size & (inode_size - 1)) {
  1028. bb_error_msg_and_die("invalid inode size %d (min %d/max %d)",
  1029. inode_size, EXT2_GOOD_OLD_INODE_SIZE,
  1030. blocksize);
  1031. }
  1032. mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE),
  1033. "%d-byte inodes not usable on most systems",
  1034. inode_size);
  1035. param.s_inode_size = inode_size;
  1036. }
  1037. /*
  1038. * Calculate number of inodes based on the inode ratio
  1039. */
  1040. param.s_inodes_count = num_inodes ? num_inodes :
  1041. ((__u64) param.s_blocks_count * blocksize)
  1042. / inode_ratio;
  1043. /*
  1044. * Calculate number of blocks to reserve
  1045. */
  1046. param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
  1047. return 1;
  1048. }
  1049. static void mke2fs_clean_up(void)
  1050. {
  1051. if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
  1052. }
  1053. int mke2fs_main (int argc, char **argv);
  1054. int mke2fs_main (int argc, char **argv)
  1055. {
  1056. errcode_t retval;
  1057. ext2_filsys fs;
  1058. badblocks_list bb_list = 0;
  1059. unsigned int i;
  1060. int val;
  1061. io_manager io_ptr;
  1062. if (ENABLE_FEATURE_CLEAN_UP)
  1063. atexit(mke2fs_clean_up);
  1064. if (!PRS(argc, argv))
  1065. return 0;
  1066. #ifdef CONFIG_TESTIO_DEBUG
  1067. io_ptr = test_io_manager;
  1068. test_io_backing_manager = unix_io_manager;
  1069. #else
  1070. io_ptr = unix_io_manager;
  1071. #endif
  1072. /*
  1073. * Initialize the superblock....
  1074. */
  1075. retval = ext2fs_initialize(device_name, 0, &param,
  1076. io_ptr, &fs);
  1077. mke2fs_error_msg_and_die(retval, "set up superblock");
  1078. /*
  1079. * Wipe out the old on-disk superblock
  1080. */
  1081. if (!noaction)
  1082. zap_sector(fs, 2, 6);
  1083. /*
  1084. * Generate a UUID for it...
  1085. */
  1086. uuid_generate(fs->super->s_uuid);
  1087. /*
  1088. * Initialize the directory index variables
  1089. */
  1090. fs->super->s_def_hash_version = EXT2_HASH_TEA;
  1091. uuid_generate((unsigned char *) fs->super->s_hash_seed);
  1092. /*
  1093. * Add "jitter" to the superblock's check interval so that we
  1094. * don't check all the filesystems at the same time. We use a
  1095. * kludgy hack of using the UUID to derive a random jitter value.
  1096. */
  1097. for (i = 0, val = 0; i < sizeof(fs->super->s_uuid); i++)
  1098. val += fs->super->s_uuid[i];
  1099. fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
  1100. /*
  1101. * Override the creator OS, if applicable
  1102. */
  1103. if (creator_os && !set_os(fs->super, creator_os)) {
  1104. bb_error_msg_and_die("unknown os - %s", creator_os);
  1105. }
  1106. /*
  1107. * For the Hurd, we will turn off filetype since it doesn't
  1108. * support it.
  1109. */
  1110. if (fs->super->s_creator_os == EXT2_OS_HURD)
  1111. fs->super->s_feature_incompat &=
  1112. ~EXT2_FEATURE_INCOMPAT_FILETYPE;
  1113. /*
  1114. * Set the volume label...
  1115. */
  1116. if (volume_label) {
  1117. snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label);
  1118. }
  1119. /*
  1120. * Set the last mount directory
  1121. */
  1122. if (mount_dir) {
  1123. snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir);
  1124. }
  1125. if (!quiet || noaction)
  1126. show_stats(fs);
  1127. if (noaction)
  1128. return 0;
  1129. if (fs->super->s_feature_incompat &
  1130. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  1131. create_journal_dev(fs);
  1132. return (ext2fs_close(fs) ? 1 : 0);
  1133. }
  1134. if (bad_blocks_filename)
  1135. read_bb_file(fs, &bb_list, bad_blocks_filename);
  1136. if (cflag)
  1137. test_disk(fs, &bb_list);
  1138. handle_bad_blocks(fs, bb_list);
  1139. fs->stride = fs_stride;
  1140. retval = ext2fs_allocate_tables(fs);
  1141. mke2fs_error_msg_and_die(retval, "allocate filesystem tables");
  1142. if (super_only) {
  1143. fs->super->s_state |= EXT2_ERROR_FS;
  1144. fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
  1145. } else {
  1146. /* rsv must be a power of two (64kB is MD RAID sb alignment) */
  1147. unsigned int rsv = 65536 / fs->blocksize;
  1148. unsigned long blocks = fs->super->s_blocks_count;
  1149. unsigned long start;
  1150. blk_t ret_blk;
  1151. #ifdef ZAP_BOOTBLOCK
  1152. zap_sector(fs, 0, 2);
  1153. #endif
  1154. /*
  1155. * Wipe out any old MD RAID (or other) metadata at the end
  1156. * of the device. This will also verify that the device is
  1157. * as large as we think. Be careful with very small devices.
  1158. */
  1159. start = (blocks & ~(rsv - 1));
  1160. if (start > rsv)
  1161. start -= rsv;
  1162. if (start > 0)
  1163. retval = zero_blocks(fs, start, blocks - start,
  1164. NULL, &ret_blk, NULL);
  1165. mke2fs_warning_msg(retval, "can't zero block %u at end of filesystem", ret_blk);
  1166. write_inode_tables(fs);
  1167. create_root_dir(fs);
  1168. create_lost_and_found(fs);
  1169. reserve_inodes(fs);
  1170. create_bad_block_inode(fs, bb_list);
  1171. if (fs->super->s_feature_compat &
  1172. EXT2_FEATURE_COMPAT_RESIZE_INO) {
  1173. retval = ext2fs_create_resize_inode(fs);
  1174. mke2fs_error_msg_and_die(retval, "reserve blocks for online resize");
  1175. }
  1176. }
  1177. if (journal_device) {
  1178. make_journal_device(journal_device, fs, quiet, force);
  1179. } else if (journal_size) {
  1180. make_journal_blocks(fs, journal_size, journal_flags, quiet);
  1181. }
  1182. mke2fs_verbose("Writing superblocks and filesystem accounting information: ");
  1183. retval = ext2fs_flush(fs);
  1184. mke2fs_warning_msg(retval, "had trouble writing out superblocks");
  1185. mke2fs_verbose_done();
  1186. if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG"))
  1187. print_check_message(fs);
  1188. val = ext2fs_close(fs);
  1189. return (retval || val) ? 1 : 0;
  1190. }