Advertisement
tthtlc

mke2fs.c in e2fsprogs

Jan 4th, 2013
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1.  
  2. int main (int argc, char *argv[])
  3. {
  4. errcode_t retval = 0;
  5. ext2_filsys fs;
  6. badblocks_list bb_list = 0;
  7. unsigned int journal_blocks;
  8. unsigned int i, checkinterval;
  9. int max_mnt_count;
  10. int val, hash_alg;
  11. int flags;
  12. int old_bitmaps;
  13. io_manager io_ptr;
  14. char tdb_string[40];
  15. char *hash_alg_str;
  16. int itable_zeroed = 0;
  17.  
  18. #ifdef ENABLE_NLS
  19. setlocale(LC_MESSAGES, "");
  20. setlocale(LC_CTYPE, "");
  21. bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
  22. textdomain(NLS_CAT_NAME);
  23. set_com_err_gettext(gettext);
  24. #endif
  25. PRS(argc, argv);
  26.  
  27. #ifdef CONFIG_TESTIO_DEBUG
  28. if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
  29. io_ptr = test_io_manager;
  30. test_io_backing_manager = unix_io_manager;
  31. } else
  32. #endif
  33. io_ptr = unix_io_manager;
  34.  
  35. if (should_do_undo(device_name)) {
  36. retval = mke2fs_setup_tdb(device_name, &io_ptr);
  37. if (retval)
  38. exit(1);
  39. }
  40.  
  41. /*
  42. * Initialize the superblock....
  43. */
  44. flags = EXT2_FLAG_EXCLUSIVE;
  45. profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
  46. &old_bitmaps);
  47. if (!old_bitmaps)
  48. flags |= EXT2_FLAG_64BITS;
  49. /*
  50. * By default, we print how many inode tables or block groups
  51. * or whatever we've written so far. The quiet flag disables
  52. * this, along with a lot of other output.
  53. */
  54. if (!quiet)
  55. flags |= EXT2_FLAG_PRINT_PROGRESS;
  56. retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
  57. if (retval) {
  58. com_err(device_name, retval, _("while setting up superblock"));
  59. exit(1);
  60. }
  61.  
  62. /* Can't undo discard ... */
  63. if (!noaction && discard && (io_ptr != undo_io_manager)) {
  64. retval = mke2fs_discard_device(fs);
  65. if (!retval && io_channel_discard_zeroes_data(fs->io)) {
  66. if (verbose)
  67. printf(_("Discard succeeded and will return 0s "
  68. " - skipping inode table wipe\n"));
  69. lazy_itable_init = 1;
  70. itable_zeroed = 1;
  71. }
  72. }
  73.  
  74. sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
  75. 32768 : fs->blocksize * 8);
  76. io_channel_set_options(fs->io, tdb_string);
  77.  
  78. if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
  79. fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
  80.  
  81. if ((fs_param.s_feature_incompat &
  82. (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
  83. (fs_param.s_feature_ro_compat &
  84. (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
  85. EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
  86. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
  87. fs->super->s_kbytes_written = 1;
  88.  
  89. /*
  90. * Wipe out the old on-disk superblock
  91. */
  92. if (!noaction)
  93. zap_sector(fs, 2, 6);
  94.  
  95. /*
  96. * Parse or generate a UUID for the filesystem
  97. */
  98. if (fs_uuid) {
  99. if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
  100. com_err(device_name, 0, "could not parse UUID: %s\n",
  101. fs_uuid);
  102. exit(1);
  103. }
  104. } else
  105. uuid_generate(fs->super->s_uuid);
  106.  
  107. /*
  108. * Initialize the directory index variables
  109. */
  110. hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
  111. "half_md4");
  112. hash_alg = e2p_string2hash(hash_alg_str);
  113. free(hash_alg_str);
  114. fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
  115. EXT2_HASH_HALF_MD4;
  116. uuid_generate((unsigned char *) fs->super->s_hash_seed);
  117.  
  118. /*
  119. * Periodic checks can be enabled/disabled via config file.
  120. * Note we override the kernel include file's idea of what the default
  121. * check interval (never) should be. It's a good idea to check at
  122. * least *occasionally*, specially since servers will never rarely get
  123. * to reboot, since Linux is so robust these days. :-)
  124. *
  125. * 180 days (six months) seems like a good value.
  126. */
  127. #ifdef EXT2_DFL_CHECKINTERVAL
  128. #undef EXT2_DFL_CHECKINTERVAL
  129. #endif
  130. #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
  131.  
  132. if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
  133. fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
  134. fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
  135. /*
  136. * Add "jitter" to the superblock's check interval so that we
  137. * don't check all the filesystems at the same time. We use a
  138. * kludgy hack of using the UUID to derive a random jitter value
  139. */
  140. for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
  141. val += fs->super->s_uuid[i];
  142. fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
  143. } else
  144. fs->super->s_max_mnt_count = -1;
  145.  
  146. /*
  147. * Override the creator OS, if applicable
  148. */
  149. if (creator_os && !set_os(fs->super, creator_os)) {
  150. com_err (program_name, 0, _("unknown os - %s"), creator_os);
  151. exit(1);
  152. }
  153.  
  154. /*
  155. * For the Hurd, we will turn off filetype since it doesn't
  156. * support it.
  157. */
  158. if (fs->super->s_creator_os == EXT2_OS_HURD)
  159. fs->super->s_feature_incompat &=
  160. ~EXT2_FEATURE_INCOMPAT_FILETYPE;
  161.  
  162. /*
  163. * Set the volume label...
  164. */
  165. if (volume_label) {
  166. memset(fs->super->s_volume_name, 0,
  167. sizeof(fs->super->s_volume_name));
  168. strncpy(fs->super->s_volume_name, volume_label,
  169. sizeof(fs->super->s_volume_name));
  170. }
  171.  
  172. /*
  173. * Set the last mount directory
  174. */
  175. if (mount_dir) {
  176. memset(fs->super->s_last_mounted, 0,
  177. sizeof(fs->super->s_last_mounted));
  178. strncpy(fs->super->s_last_mounted, mount_dir,
  179. sizeof(fs->super->s_last_mounted));
  180. }
  181.  
  182. if (!quiet || noaction)
  183. show_stats(fs);
  184.  
  185. if (noaction)
  186. exit(0);
  187.  
  188. if (fs->super->s_feature_incompat &
  189. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  190. create_journal_dev(fs);
  191. exit(ext2fs_close(fs) ? 1 : 0);
  192. }
  193.  
  194. if (bad_blocks_filename)
  195. read_bb_file(fs, &bb_list, bad_blocks_filename);
  196. if (cflag)
  197. test_disk(fs, &bb_list);
  198.  
  199. handle_bad_blocks(fs, bb_list);
  200. fs->stride = fs_stride = fs->super->s_raid_stride;
  201. if (!quiet)
  202. printf(_("Allocating group tables: "));
  203. retval = ext2fs_allocate_tables(fs);
  204. if (retval) {
  205. com_err(program_name, retval,
  206. _("while trying to allocate filesystem tables"));
  207. exit(1);
  208. }
  209. if (!quiet)
  210. printf(_("done \n"));
  211.  
  212. retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
  213. if (retval) {
  214. com_err(program_name, retval,
  215. _("\n\twhile converting subcluster bitmap"));
  216. exit(1);
  217. }
  218.  
  219. if (super_only) {
  220. fs->super->s_state |= EXT2_ERROR_FS;
  221. fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
  222. } else {
  223. /* rsv must be a power of two (64kB is MD RAID sb alignment) */
  224. blk64_t rsv = 65536 / fs->blocksize;
  225. blk64_t blocks = ext2fs_blocks_count(fs->super);
  226. blk64_t start;
  227. blk64_t ret_blk;
  228.  
  229. #ifdef ZAP_BOOTBLOCK
  230. zap_sector(fs, 0, 2);
  231. #endif
  232.  
  233. /*
  234. * Wipe out any old MD RAID (or other) metadata at the end
  235. * of the device. This will also verify that the device is
  236. * as large as we think. Be careful with very small devices.
  237. */
  238. start = (blocks & ~(rsv - 1));
  239. if (start > rsv)
  240. start -= rsv;
  241. if (start > 0)
  242. retval = ext2fs_zero_blocks2(fs, start, blocks - start,
  243. &ret_blk, NULL);
  244.  
  245. if (retval) {
  246. com_err(program_name, retval,
  247. _("while zeroing block %llu at end of filesystem"),
  248. ret_blk);
  249. }
  250. write_inode_tables(fs, lazy_itable_init, itable_zeroed);
  251. create_root_dir(fs);
  252. create_lost_and_found(fs);
  253. reserve_inodes(fs);
  254. create_bad_block_inode(fs, bb_list);
  255. if (fs->super->s_feature_compat &
  256. EXT2_FEATURE_COMPAT_RESIZE_INODE) {
  257. retval = ext2fs_create_resize_inode(fs);
  258. if (retval) {
  259. com_err("ext2fs_create_resize_inode", retval,
  260. _("while reserving blocks for online resize"));
  261. exit(1);
  262. }
  263. }
  264. }
  265.  
  266. if (journal_device) {
  267. ext2_filsys jfs;
  268.  
  269. if (!force)
  270. check_plausibility(journal_device);
  271. check_mount(journal_device, force, _("journal"));
  272.  
  273. retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
  274. EXT2_FLAG_JOURNAL_DEV_OK, 0,
  275. fs->blocksize, unix_io_manager, &jfs);
  276. if (retval) {
  277. com_err(program_name, retval,
  278. _("while trying to open journal device %s\n"),
  279. journal_device);
  280. exit(1);
  281. }
  282. if (!quiet) {
  283. printf(_("Adding journal to device %s: "),
  284. journal_device);
  285. fflush(stdout);
  286. }
  287. retval = ext2fs_add_journal_device(fs, jfs);
  288. if(retval) {
  289. com_err (program_name, retval,
  290. _("\n\twhile trying to add journal to device %s"),
  291. journal_device);
  292. exit(1);
  293. }
  294. if (!quiet)
  295. printf(_("done\n"));
  296. ext2fs_close(jfs);
  297. free(journal_device);
  298. } else if ((journal_size) ||
  299. (fs_param.s_feature_compat &
  300. EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
  301. journal_blocks = figure_journal_size(journal_size, fs);
  302.  
  303. if (super_only) {
  304. printf(_("Skipping journal creation in super-only mode\n"));
  305. fs->super->s_journal_inum = EXT2_JOURNAL_INO;
  306. goto no_journal;
  307. }
  308.  
  309. if (!journal_blocks) {
  310. fs->super->s_feature_compat &=
  311. ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  312. goto no_journal;
  313. }
  314. if (!quiet) {
  315. printf(_("Creating journal (%u blocks): "),
  316. journal_blocks);
  317. fflush(stdout);
  318. }
  319. retval = ext2fs_add_journal_inode(fs, journal_blocks,
  320. journal_flags);
  321. if (retval) {
  322. com_err (program_name, retval,
  323. _("\n\twhile trying to create journal"));
  324. exit(1);
  325. }
  326. if (!quiet)
  327. printf(_("done\n"));
  328. }
  329. no_journal:
  330. if (!super_only &&
  331. fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
  332. retval = ext2fs_mmp_init(fs);
  333. if (retval) {
  334. fprintf(stderr, _("\nError while enabling multiple "
  335. "mount protection feature."));
  336. exit(1);
  337. }
  338. if (!quiet)
  339. printf(_("Multiple mount protection is enabled "
  340. "with update interval %d seconds.\n"),
  341. fs->super->s_mmp_update_interval);
  342. }
  343.  
  344. if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
  345. EXT4_FEATURE_RO_COMPAT_BIGALLOC))
  346. fix_cluster_bg_counts(fs);
  347. if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
  348. EXT4_FEATURE_RO_COMPAT_QUOTA))
  349. create_quota_inodes(fs);
  350.  
  351. if (!quiet)
  352. printf(_("Writing superblocks and "
  353. "filesystem accounting information: "));
  354. checkinterval = fs->super->s_checkinterval;
  355. max_mnt_count = fs->super->s_max_mnt_count;
  356. retval = ext2fs_close(fs);
  357. if (retval) {
  358. fprintf(stderr,
  359. _("\nWarning, had trouble writing out superblocks."));
  360. } else if (!quiet) {
  361. printf(_("done\n\n"));
  362. if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
  363. print_check_message(max_mnt_count, checkinterval);
  364. }
  365.  
  366. remove_error_table(&et_ext2_error_table);
  367. remove_error_table(&et_prof_error_table);
  368. profile_release(profile);
  369. for (i=0; fs_types[i]; i++)
  370. free(fs_types[i]);
  371. free(fs_types);
  372. return retval;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement