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