tune2fs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * tune2fs.c - Change the file system parameters on an ext2 file system
  3. *
  4. * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
  5. * Laboratoire MASI, Institut Blaise Pascal
  6. * Universite Pierre et Marie Curie (Paris VI)
  7. *
  8. * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
  9. *
  10. * %Begin-Header%
  11. * This file may be redistributed under the terms of the GNU Public
  12. * License.
  13. * %End-Header%
  14. */
  15. /*
  16. * History:
  17. * 93/06/01 - Creation
  18. * 93/10/31 - Added the -c option to change the maximal mount counts
  19. * 93/12/14 - Added -l flag to list contents of superblock
  20. * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
  21. * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
  22. * 93/12/29 - Added the -e option to change errors behavior
  23. * 94/02/27 - Ported to use the ext2fs library
  24. * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
  25. */
  26. #include <sys/types.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <getopt.h>
  34. #include "e2fsbb.h"
  35. #include "ext2fs/ext2_fs.h"
  36. #include "ext2fs/ext2fs.h"
  37. #include "uuid/uuid.h"
  38. #include "e2p/e2p.h"
  39. #include "ext2fs/jfs_user.h"
  40. #include "util.h"
  41. #include "blkid/blkid.h"
  42. #include "busybox.h"
  43. #include "grp_.h"
  44. #include "pwd_.h"
  45. static char * device_name = NULL;
  46. static char * new_label, *new_last_mounted, *new_UUID;
  47. static char * io_options;
  48. static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
  49. static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
  50. static time_t last_check_time;
  51. static int print_label;
  52. static int max_mount_count, mount_count, mount_flags;
  53. static unsigned long interval, reserved_ratio, reserved_blocks;
  54. static unsigned long resgid, resuid;
  55. static unsigned short errors;
  56. static int open_flag;
  57. static char *features_cmd;
  58. static char *mntopts_cmd;
  59. static int journal_size, journal_flags;
  60. static char *journal_device = NULL;
  61. static const char *please_fsck = "Please run e2fsck on the filesystem\n";
  62. static __u32 ok_features[3] = {
  63. EXT3_FEATURE_COMPAT_HAS_JOURNAL | EXT2_FEATURE_COMPAT_DIR_INDEX,
  64. EXT2_FEATURE_INCOMPAT_FILETYPE,
  65. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
  66. };
  67. /*
  68. * Remove an external journal from the filesystem
  69. */
  70. static void remove_journal_device(ext2_filsys fs)
  71. {
  72. char *journal_path;
  73. ext2_filsys jfs;
  74. char buf[1024];
  75. journal_superblock_t *jsb;
  76. int i, nr_users;
  77. errcode_t retval;
  78. int commit_remove_journal = 0;
  79. io_manager io_ptr;
  80. if (f_flag)
  81. commit_remove_journal = 1; /* force removal even if error */
  82. uuid_unparse(fs->super->s_journal_uuid, buf);
  83. journal_path = blkid_get_devname(NULL, "UUID", buf);
  84. if (!journal_path) {
  85. journal_path =
  86. ext2fs_find_block_device(fs->super->s_journal_dev);
  87. if (!journal_path)
  88. return;
  89. }
  90. io_ptr = unix_io_manager;
  91. retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
  92. EXT2_FLAG_JOURNAL_DEV_OK, 0,
  93. fs->blocksize, io_ptr, &jfs);
  94. if (retval) {
  95. bb_error_msg("Failed to open external journal");
  96. goto no_valid_journal;
  97. }
  98. if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  99. bb_error_msg("%s is not a journal device", journal_path);
  100. goto no_valid_journal;
  101. }
  102. /* Get the journal superblock */
  103. if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
  104. bb_error_msg("Failed to read journal superblock");
  105. goto no_valid_journal;
  106. }
  107. jsb = (journal_superblock_t *) buf;
  108. if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
  109. (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
  110. bb_error_msg("Journal superblock not found!");
  111. goto no_valid_journal;
  112. }
  113. /* Find the filesystem UUID */
  114. nr_users = ntohl(jsb->s_nr_users);
  115. for (i=0; i < nr_users; i++) {
  116. if (memcmp(fs->super->s_uuid,
  117. &jsb->s_users[i*16], 16) == 0)
  118. break;
  119. }
  120. if (i >= nr_users) {
  121. bb_error_msg("Filesystem's UUID not found on journal device");
  122. commit_remove_journal = 1;
  123. goto no_valid_journal;
  124. }
  125. nr_users--;
  126. for (i=0; i < nr_users; i++)
  127. memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
  128. jsb->s_nr_users = htonl(nr_users);
  129. /* Write back the journal superblock */
  130. if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
  131. bb_error_msg("Failed to write journal superblock");
  132. goto no_valid_journal;
  133. }
  134. commit_remove_journal = 1;
  135. no_valid_journal:
  136. if (commit_remove_journal == 0)
  137. bb_error_msg_and_die("Journal NOT removed");
  138. fs->super->s_journal_dev = 0;
  139. uuid_clear(fs->super->s_journal_uuid);
  140. ext2fs_mark_super_dirty(fs);
  141. puts("Journal removed");
  142. free(journal_path);
  143. }
  144. /* Helper function for remove_journal_inode */
  145. static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
  146. int blockcnt EXT2FS_ATTR((unused)),
  147. void *private EXT2FS_ATTR((unused)))
  148. {
  149. blk_t block;
  150. int group;
  151. block = *blocknr;
  152. ext2fs_unmark_block_bitmap(fs->block_map,block);
  153. group = ext2fs_group_of_blk(fs, block);
  154. fs->group_desc[group].bg_free_blocks_count++;
  155. fs->super->s_free_blocks_count++;
  156. return 0;
  157. }
  158. /*
  159. * Remove the journal inode from the filesystem
  160. */
  161. static void remove_journal_inode(ext2_filsys fs)
  162. {
  163. struct ext2_inode inode;
  164. errcode_t retval;
  165. ino_t ino = fs->super->s_journal_inum;
  166. char *msg = "to read";
  167. char *s = "journal inode";
  168. retval = ext2fs_read_inode(fs, ino, &inode);
  169. if (retval)
  170. goto REMOVE_JOURNAL_INODE_ERROR;
  171. if (ino == EXT2_JOURNAL_INO) {
  172. retval = ext2fs_read_bitmaps(fs);
  173. if (retval) {
  174. msg = "to read bitmaps";
  175. s = "";
  176. goto REMOVE_JOURNAL_INODE_ERROR;
  177. }
  178. retval = ext2fs_block_iterate(fs, ino, 0, NULL,
  179. release_blocks_proc, NULL);
  180. if (retval) {
  181. msg = "clearing";
  182. goto REMOVE_JOURNAL_INODE_ERROR;
  183. }
  184. memset(&inode, 0, sizeof(inode));
  185. ext2fs_mark_bb_dirty(fs);
  186. fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
  187. } else
  188. inode.i_flags &= ~EXT2_IMMUTABLE_FL;
  189. retval = ext2fs_write_inode(fs, ino, &inode);
  190. if (retval) {
  191. msg = "writing";
  192. REMOVE_JOURNAL_INODE_ERROR:
  193. bb_error_msg_and_die("Failed %s %s", msg, s);
  194. }
  195. fs->super->s_journal_inum = 0;
  196. ext2fs_mark_super_dirty(fs);
  197. }
  198. /*
  199. * Update the default mount options
  200. */
  201. static void update_mntopts(ext2_filsys fs, char *mntopts)
  202. {
  203. struct ext2_super_block *sb= fs->super;
  204. if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
  205. bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
  206. ext2fs_mark_super_dirty(fs);
  207. }
  208. /*
  209. * Update the feature set as provided by the user.
  210. */
  211. static void update_feature_set(ext2_filsys fs, char *features)
  212. {
  213. int sparse, old_sparse, filetype, old_filetype;
  214. int journal, old_journal, dxdir, old_dxdir;
  215. struct ext2_super_block *sb= fs->super;
  216. __u32 old_compat, old_incompat, old_ro_compat;
  217. old_compat = sb->s_feature_compat;
  218. old_ro_compat = sb->s_feature_ro_compat;
  219. old_incompat = sb->s_feature_incompat;
  220. old_sparse = sb->s_feature_ro_compat &
  221. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  222. old_filetype = sb->s_feature_incompat &
  223. EXT2_FEATURE_INCOMPAT_FILETYPE;
  224. old_journal = sb->s_feature_compat &
  225. EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  226. old_dxdir = sb->s_feature_compat &
  227. EXT2_FEATURE_COMPAT_DIR_INDEX;
  228. if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
  229. bb_error_msg_and_die("Invalid filesystem option set: %s", features);
  230. sparse = sb->s_feature_ro_compat &
  231. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  232. filetype = sb->s_feature_incompat &
  233. EXT2_FEATURE_INCOMPAT_FILETYPE;
  234. journal = sb->s_feature_compat &
  235. EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  236. dxdir = sb->s_feature_compat &
  237. EXT2_FEATURE_COMPAT_DIR_INDEX;
  238. if (old_journal && !journal) {
  239. if ((mount_flags & EXT2_MF_MOUNTED) &&
  240. !(mount_flags & EXT2_MF_READONLY)) {
  241. bb_error_msg_and_die(
  242. "The has_journal flag may only be "
  243. "cleared when the filesystem is\n"
  244. "unmounted or mounted "
  245. "read-only");
  246. }
  247. if (sb->s_feature_incompat &
  248. EXT3_FEATURE_INCOMPAT_RECOVER) {
  249. bb_error_msg_and_die(
  250. "The needs_recovery flag is set. "
  251. "%s before clearing the has_journal flag.",
  252. please_fsck);
  253. }
  254. if (sb->s_journal_inum) {
  255. remove_journal_inode(fs);
  256. }
  257. if (sb->s_journal_dev) {
  258. remove_journal_device(fs);
  259. }
  260. }
  261. if (journal && !old_journal) {
  262. /*
  263. * If adding a journal flag, let the create journal
  264. * code below handle creating setting the flag and
  265. * creating the journal. We supply a default size if
  266. * necessary.
  267. */
  268. if (!journal_size)
  269. journal_size = -1;
  270. sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  271. }
  272. if (dxdir && !old_dxdir) {
  273. if (!sb->s_def_hash_version)
  274. sb->s_def_hash_version = EXT2_HASH_TEA;
  275. if (uuid_is_null((unsigned char *) sb->s_hash_seed))
  276. uuid_generate((unsigned char *) sb->s_hash_seed);
  277. }
  278. if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
  279. (sb->s_feature_compat || sb->s_feature_ro_compat ||
  280. sb->s_feature_incompat))
  281. ext2fs_update_dynamic_rev(fs);
  282. if ((sparse != old_sparse) ||
  283. (filetype != old_filetype)) {
  284. sb->s_state &= ~EXT2_VALID_FS;
  285. printf("\n%s\n", please_fsck);
  286. }
  287. if ((old_compat != sb->s_feature_compat) ||
  288. (old_ro_compat != sb->s_feature_ro_compat) ||
  289. (old_incompat != sb->s_feature_incompat))
  290. ext2fs_mark_super_dirty(fs);
  291. }
  292. /*
  293. * Add a journal to the filesystem.
  294. */
  295. static void add_journal(ext2_filsys fs)
  296. {
  297. if (fs->super->s_feature_compat &
  298. EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
  299. bb_error_msg_and_die("The filesystem already has a journal");
  300. }
  301. if (journal_device) {
  302. make_journal_device(journal_device, fs, 0, 0);
  303. } else if (journal_size) {
  304. make_journal_blocks(fs, journal_size, journal_flags, 0);
  305. /*
  306. * If the filesystem wasn't mounted, we need to force
  307. * the block group descriptors out.
  308. */
  309. if ((mount_flags & EXT2_MF_MOUNTED) == 0)
  310. fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
  311. }
  312. print_check_message(fs);
  313. return;
  314. }
  315. /*
  316. * Busybox stuff
  317. */
  318. static char * x_blkid_get_devname(const char *token)
  319. {
  320. char * dev_name;
  321. if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
  322. bb_error_msg_and_die("Unable to resolve '%s'", token);
  323. return dev_name;
  324. }
  325. #ifdef CONFIG_E2LABEL
  326. static void parse_e2label_options(int argc, char ** argv)
  327. {
  328. if ((argc < 2) || (argc > 3))
  329. bb_show_usage();
  330. io_options = strchr(argv[1], '?');
  331. if (io_options)
  332. *io_options++ = 0;
  333. device_name = x_blkid_get_devname(argv[1]);
  334. if (argc == 3) {
  335. open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
  336. L_flag = 1;
  337. new_label = argv[2];
  338. } else
  339. print_label++;
  340. }
  341. #else
  342. #define parse_e2label_options(x,y)
  343. #endif
  344. static time_t parse_time(char *str)
  345. {
  346. struct tm ts;
  347. if (strcmp(str, "now") == 0) {
  348. return (time(0));
  349. }
  350. memset(&ts, 0, sizeof(ts));
  351. #ifdef HAVE_STRPTIME
  352. strptime(str, "%Y%m%d%H%M%S", &ts);
  353. #else
  354. sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
  355. &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
  356. ts.tm_year -= 1900;
  357. ts.tm_mon -= 1;
  358. if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
  359. ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
  360. ts.tm_min > 59 || ts.tm_sec > 61)
  361. ts.tm_mday = 0;
  362. #endif
  363. if (ts.tm_mday == 0) {
  364. bb_error_msg_and_die("Couldn't parse date/time specifier: %s", str);
  365. }
  366. return (mktime(&ts));
  367. }
  368. static void parse_tune2fs_options(int argc, char **argv)
  369. {
  370. int c;
  371. char * tmp;
  372. printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
  373. while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
  374. switch (c)
  375. {
  376. case 'c':
  377. if (safe_strtoi(optarg, &max_mount_count) || max_mount_count > 16000) {
  378. goto MOUNTS_COUNT_ERROR;
  379. }
  380. if (max_mount_count == 0)
  381. max_mount_count = -1;
  382. c_flag = 1;
  383. open_flag = EXT2_FLAG_RW;
  384. break;
  385. case 'C':
  386. if (safe_strtoi(optarg, &mount_count) || mount_count > 16000) {
  387. MOUNTS_COUNT_ERROR:
  388. bb_error_msg_and_die("bad mounts count - %s", optarg);
  389. }
  390. C_flag = 1;
  391. open_flag = EXT2_FLAG_RW;
  392. break;
  393. case 'e':
  394. if (strcmp (optarg, "continue") == 0)
  395. errors = EXT2_ERRORS_CONTINUE;
  396. else if (strcmp (optarg, "remount-ro") == 0)
  397. errors = EXT2_ERRORS_RO;
  398. else if (strcmp (optarg, "panic") == 0)
  399. errors = EXT2_ERRORS_PANIC;
  400. else {
  401. bb_error_msg_and_die("bad error behavior - %s", optarg);
  402. }
  403. e_flag = 1;
  404. open_flag = EXT2_FLAG_RW;
  405. break;
  406. case 'f': /* Force */
  407. f_flag = 1;
  408. break;
  409. case 'g':
  410. if (safe_strtoul(optarg, &resgid))
  411. resgid = bb_xgetgrnam(optarg);
  412. g_flag = 1;
  413. open_flag = EXT2_FLAG_RW;
  414. break;
  415. case 'i':
  416. interval = strtoul (optarg, &tmp, 0);
  417. switch (*tmp) {
  418. case 's':
  419. tmp++;
  420. break;
  421. case '\0':
  422. case 'd':
  423. case 'D': /* days */
  424. interval *= 86400;
  425. if (*tmp != '\0')
  426. tmp++;
  427. break;
  428. case 'm':
  429. case 'M': /* months! */
  430. interval *= 86400 * 30;
  431. tmp++;
  432. break;
  433. case 'w':
  434. case 'W': /* weeks */
  435. interval *= 86400 * 7;
  436. tmp++;
  437. break;
  438. }
  439. if (*tmp || interval > (365 * 86400)) {
  440. bb_error_msg_and_die("bad interval - %s", optarg);
  441. }
  442. i_flag = 1;
  443. open_flag = EXT2_FLAG_RW;
  444. break;
  445. case 'j':
  446. if (!journal_size)
  447. journal_size = -1;
  448. open_flag = EXT2_FLAG_RW;
  449. break;
  450. case 'J':
  451. parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
  452. open_flag = EXT2_FLAG_RW;
  453. break;
  454. case 'l':
  455. l_flag = 1;
  456. break;
  457. case 'L':
  458. new_label = optarg;
  459. L_flag = 1;
  460. open_flag = EXT2_FLAG_RW |
  461. EXT2_FLAG_JOURNAL_DEV_OK;
  462. break;
  463. case 'm':
  464. if(safe_strtoul(optarg, &reserved_ratio) || reserved_ratio > 50) {
  465. bb_error_msg_and_die("bad reserved block ratio - %s", optarg);
  466. }
  467. m_flag = 1;
  468. open_flag = EXT2_FLAG_RW;
  469. break;
  470. case 'M':
  471. new_last_mounted = optarg;
  472. M_flag = 1;
  473. open_flag = EXT2_FLAG_RW;
  474. break;
  475. case 'o':
  476. if (mntopts_cmd) {
  477. bb_error_msg_and_die("-o may only be specified once");
  478. }
  479. mntopts_cmd = optarg;
  480. open_flag = EXT2_FLAG_RW;
  481. break;
  482. case 'O':
  483. if (features_cmd) {
  484. bb_error_msg_and_die("-O may only be specified once");
  485. }
  486. features_cmd = optarg;
  487. open_flag = EXT2_FLAG_RW;
  488. break;
  489. case 'r':
  490. if(safe_strtoul(optarg, &reserved_blocks)) {
  491. bb_error_msg_and_die("bad reserved blocks count - %s", optarg);
  492. }
  493. r_flag = 1;
  494. open_flag = EXT2_FLAG_RW;
  495. break;
  496. case 's':
  497. s_flag = atoi(optarg);
  498. open_flag = EXT2_FLAG_RW;
  499. break;
  500. case 'T':
  501. T_flag = 1;
  502. last_check_time = parse_time(optarg);
  503. open_flag = EXT2_FLAG_RW;
  504. break;
  505. case 'u':
  506. if (safe_strtoul(optarg, &resuid))
  507. resuid = bb_xgetpwnam(optarg);
  508. u_flag = 1;
  509. open_flag = EXT2_FLAG_RW;
  510. break;
  511. case 'U':
  512. new_UUID = optarg;
  513. U_flag = 1;
  514. open_flag = EXT2_FLAG_RW |
  515. EXT2_FLAG_JOURNAL_DEV_OK;
  516. break;
  517. default:
  518. bb_show_usage();
  519. }
  520. if (optind < argc - 1 || optind == argc)
  521. bb_show_usage();
  522. if (!open_flag && !l_flag)
  523. bb_show_usage();
  524. io_options = strchr(argv[optind], '?');
  525. if (io_options)
  526. *io_options++ = 0;
  527. device_name = x_blkid_get_devname(argv[optind]);
  528. }
  529. #ifdef CONFIG_FINDFS
  530. static ATTRIBUTE_NORETURN void do_findfs(int argc, char **argv)
  531. {
  532. if ((argc != 2) ||
  533. (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
  534. bb_show_usage();
  535. device_name = x_blkid_get_devname(argv[1]);
  536. puts(device_name);
  537. exit(0);
  538. }
  539. #else
  540. #define do_findfs(x, y)
  541. #endif
  542. static void clean_up(void)
  543. {
  544. if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
  545. if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
  546. }
  547. int tune2fs_main(int argc, char **argv)
  548. {
  549. errcode_t retval;
  550. ext2_filsys fs;
  551. struct ext2_super_block *sb;
  552. io_manager io_ptr;
  553. if (ENABLE_FEATURE_CLEAN_UP)
  554. atexit(clean_up);
  555. if (ENABLE_FINDFS && (bb_applet_name[0] == 'f')) /* findfs */
  556. do_findfs(argc, argv); /* no return */
  557. else if (ENABLE_E2LABEL && (bb_applet_name[0] == 'e')) /* e2label */
  558. parse_e2label_options(argc, argv);
  559. else
  560. parse_tune2fs_options(argc, argv); /* tune2fs */
  561. io_ptr = unix_io_manager;
  562. retval = ext2fs_open2(device_name, io_options, open_flag,
  563. 0, 0, io_ptr, &fs);
  564. if (retval)
  565. bb_error_msg_and_die("No valid superblock on %s", device_name);
  566. sb = fs->super;
  567. if (print_label) {
  568. /* For e2label emulation */
  569. printf("%.*s\n", (int) sizeof(sb->s_volume_name),
  570. sb->s_volume_name);
  571. return 0;
  572. }
  573. retval = ext2fs_check_if_mounted(device_name, &mount_flags);
  574. if (retval)
  575. bb_error_msg_and_die("Could not determine if %s is mounted", device_name);
  576. /* Normally we only need to write out the superblock */
  577. fs->flags |= EXT2_FLAG_SUPER_ONLY;
  578. if (c_flag) {
  579. sb->s_max_mnt_count = max_mount_count;
  580. ext2fs_mark_super_dirty(fs);
  581. printf("Setting maximal mount count to %d\n", max_mount_count);
  582. }
  583. if (C_flag) {
  584. sb->s_mnt_count = mount_count;
  585. ext2fs_mark_super_dirty(fs);
  586. printf("Setting current mount count to %d\n", mount_count);
  587. }
  588. if (e_flag) {
  589. sb->s_errors = errors;
  590. ext2fs_mark_super_dirty(fs);
  591. printf("Setting error behavior to %d\n", errors);
  592. }
  593. if (g_flag) {
  594. sb->s_def_resgid = resgid;
  595. ext2fs_mark_super_dirty(fs);
  596. printf("Setting reserved blocks gid to %lu\n", resgid);
  597. }
  598. if (i_flag) {
  599. sb->s_checkinterval = interval;
  600. ext2fs_mark_super_dirty(fs);
  601. printf("Setting interval between check %lu seconds\n", interval);
  602. }
  603. if (m_flag) {
  604. sb->s_r_blocks_count = (sb->s_blocks_count / 100)
  605. * reserved_ratio;
  606. ext2fs_mark_super_dirty(fs);
  607. printf("Setting reserved blocks percentage to %lu (%u blocks)\n",
  608. reserved_ratio, sb->s_r_blocks_count);
  609. }
  610. if (r_flag) {
  611. if (reserved_blocks >= sb->s_blocks_count/2)
  612. bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
  613. sb->s_r_blocks_count = reserved_blocks;
  614. ext2fs_mark_super_dirty(fs);
  615. printf("Setting reserved blocks count to %lu\n", reserved_blocks);
  616. }
  617. if (s_flag == 1) {
  618. if (sb->s_feature_ro_compat &
  619. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
  620. bb_error_msg("\nThe filesystem already has sparse superblocks\n");
  621. else {
  622. sb->s_feature_ro_compat |=
  623. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  624. sb->s_state &= ~EXT2_VALID_FS;
  625. ext2fs_mark_super_dirty(fs);
  626. printf("\nSparse superblock flag set. %s", please_fsck);
  627. }
  628. }
  629. if (s_flag == 0) {
  630. if (!(sb->s_feature_ro_compat &
  631. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
  632. bb_error_msg("\nThe filesystem already has sparse superblocks disabled\n");
  633. else {
  634. sb->s_feature_ro_compat &=
  635. ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  636. sb->s_state &= ~EXT2_VALID_FS;
  637. fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
  638. ext2fs_mark_super_dirty(fs);
  639. printf("\nSparse superblock flag cleared. %s", please_fsck);
  640. }
  641. }
  642. if (T_flag) {
  643. sb->s_lastcheck = last_check_time;
  644. ext2fs_mark_super_dirty(fs);
  645. printf("Setting time filesystem last checked to %s\n",
  646. ctime(&last_check_time));
  647. }
  648. if (u_flag) {
  649. sb->s_def_resuid = resuid;
  650. ext2fs_mark_super_dirty(fs);
  651. printf("Setting reserved blocks uid to %lu\n", resuid);
  652. }
  653. if (L_flag) {
  654. if (strlen(new_label) > sizeof(sb->s_volume_name))
  655. bb_error_msg("Warning: label too long, truncating\n");
  656. memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
  657. strncpy(sb->s_volume_name, new_label,
  658. sizeof(sb->s_volume_name));
  659. ext2fs_mark_super_dirty(fs);
  660. }
  661. if (M_flag) {
  662. memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
  663. strncpy(sb->s_last_mounted, new_last_mounted,
  664. sizeof(sb->s_last_mounted));
  665. ext2fs_mark_super_dirty(fs);
  666. }
  667. if (mntopts_cmd)
  668. update_mntopts(fs, mntopts_cmd);
  669. if (features_cmd)
  670. update_feature_set(fs, features_cmd);
  671. if (journal_size || journal_device)
  672. add_journal(fs);
  673. if (U_flag) {
  674. if ((strcasecmp(new_UUID, "null") == 0) ||
  675. (strcasecmp(new_UUID, "clear") == 0)) {
  676. uuid_clear(sb->s_uuid);
  677. } else if (strcasecmp(new_UUID, "time") == 0) {
  678. uuid_generate_time(sb->s_uuid);
  679. } else if (strcasecmp(new_UUID, "random") == 0) {
  680. uuid_generate(sb->s_uuid);
  681. } else if (uuid_parse(new_UUID, sb->s_uuid)) {
  682. bb_error_msg_and_die("Invalid UUID format");
  683. }
  684. ext2fs_mark_super_dirty(fs);
  685. }
  686. if (l_flag)
  687. list_super (sb);
  688. return (ext2fs_close (fs) ? 1 : 0);
  689. }