mke2fs.c 34 KB

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