3
0

tune2fs.c 19 KB

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