Whamcloud - gitweb
Enable FLEX_BG feature support
[tools/e2fsprogs.git] / misc / tune2fs.c
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 /*
17  * History:
18  * 93/06/01     - Creation
19  * 93/10/31     - Added the -c option to change the maximal mount counts
20  * 93/12/14     - Added -l flag to list contents of superblock
21  *                M.J.E. Mol (marcel@duteca.et.tudelft.nl)
22  *                F.W. ten Wolde (franky@duteca.et.tudelft.nl)
23  * 93/12/29     - Added the -e option to change errors behavior
24  * 94/02/27     - Ported to use the ext2fs library
25  * 94/03/06     - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
26  */
27
28 #define _XOPEN_SOURCE 500 /* for inclusion of strptime() */
29 #define _BSD_SOURCE /* for inclusion of strcasecmp() */
30 #include <fcntl.h>
31 #include <grp.h>
32 #ifdef HAVE_GETOPT_H
33 #include <getopt.h>
34 #else
35 extern char *optarg;
36 extern int optind;
37 #endif
38 #include <pwd.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45
46 #include "ext2fs/ext2_fs.h"
47 #include "ext2fs/ext2fs.h"
48 #include "et/com_err.h"
49 #include "uuid/uuid.h"
50 #include "e2p/e2p.h"
51 #include "jfs_user.h"
52 #include "util.h"
53 #include "blkid/blkid.h"
54
55 #include "../version.h"
56 #include "nls-enable.h"
57
58 const char * program_name = "tune2fs";
59 char * device_name;
60 char * new_label, *new_last_mounted, *new_UUID;
61 char * io_options;
62 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
63 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
64 static time_t last_check_time;
65 static int print_label;
66 static int max_mount_count, mount_count, mount_flags;
67 static unsigned long interval, reserved_blocks;
68 static double reserved_ratio;
69 static unsigned long resgid, resuid;
70 static unsigned short errors;
71 static int open_flag;
72 static char *features_cmd;
73 static char *mntopts_cmd;
74
75 int journal_size, journal_flags;
76 char *journal_device;
77
78 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
79
80 void do_findfs(int argc, char **argv);
81
82 static void usage(void)
83 {
84         fprintf(stderr,
85                 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
86                   "[-g group]\n"
87                   "\t[-i interval[d|m|w]] [-j] [-J journal_options]\n"
88                   "\t[-l] [-s sparse_flag] [-m reserved_blocks_percent]\n"
89                   "\t[-o [^]mount_options[,...]] [-r reserved_blocks_count]\n"
90                   "\t[-u user] [-C mount_count] [-L volume_label] "
91                   "[-M last_mounted_dir]\n"
92                   "\t[-O [^]feature[,...]] [-T last_check_time] [-U UUID]"
93                   " device\n"), program_name);
94         exit (1);
95 }
96
97 static __u32 ok_features[3] = {
98         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
99                 EXT2_FEATURE_COMPAT_DIR_INDEX,  /* Compat */
100         EXT2_FEATURE_INCOMPAT_FILETYPE|         /* Incompat */
101                 EXT4_FEATURE_INCOMPAT_FLEX_BG,
102         EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER     /* R/O compat */
103 };
104
105 /*
106  * Remove an external journal from the filesystem
107  */
108 static void remove_journal_device(ext2_filsys fs)
109 {
110         char            *journal_path;
111         ext2_filsys     jfs;
112         char            buf[1024];
113         journal_superblock_t    *jsb;
114         int             i, nr_users;
115         errcode_t       retval;
116         int             commit_remove_journal = 0;
117         io_manager      io_ptr;
118
119         if (f_flag)
120                 commit_remove_journal = 1; /* force removal even if error */
121
122         uuid_unparse(fs->super->s_journal_uuid, buf);
123         journal_path = blkid_get_devname(NULL, "UUID", buf);
124
125         if (!journal_path) {
126                 journal_path =
127                         ext2fs_find_block_device(fs->super->s_journal_dev);
128                 if (!journal_path)
129                         return;
130         }
131
132 #ifdef CONFIG_TESTIO_DEBUG
133         io_ptr = test_io_manager;
134         test_io_backing_manager = unix_io_manager;
135 #else
136         io_ptr = unix_io_manager;
137 #endif
138         retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
139                              EXT2_FLAG_JOURNAL_DEV_OK, 0,
140                              fs->blocksize, io_ptr, &jfs);
141         if (retval) {
142                 com_err(program_name, retval,
143                         _("while trying to open external journal"));
144                 goto no_valid_journal;
145         }
146         if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
147                 fprintf(stderr, _("%s is not a journal device.\n"),
148                         journal_path);
149                 goto no_valid_journal;
150         }
151
152         /* Get the journal superblock */
153         if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
154                 com_err(program_name, retval,
155                         _("while reading journal superblock"));
156                 goto no_valid_journal;
157         }
158
159         jsb = (journal_superblock_t *) buf;
160         if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
161             (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
162                 fputs(_("Journal superblock not found!\n"), stderr);
163                 goto no_valid_journal;
164         }
165
166         /* Find the filesystem UUID */
167         nr_users = ntohl(jsb->s_nr_users);
168         for (i=0; i < nr_users; i++) {
169                 if (memcmp(fs->super->s_uuid,
170                            &jsb->s_users[i*16], 16) == 0)
171                         break;
172         }
173         if (i >= nr_users) {
174                 fputs(_("Filesystem's UUID not found on journal device.\n"), 
175                       stderr);
176                 commit_remove_journal = 1;
177                 goto no_valid_journal;
178         }
179         nr_users--;
180         for (i=0; i < nr_users; i++)
181                 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
182         jsb->s_nr_users = htonl(nr_users);
183
184         /* Write back the journal superblock */
185         if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
186                 com_err(program_name, retval,
187                         "while writing journal superblock.");
188                 goto no_valid_journal;
189         }
190
191         commit_remove_journal = 1;
192
193 no_valid_journal:
194         if (commit_remove_journal == 0) {
195                 fputs(_("Journal NOT removed\n"), stderr);
196                 exit(1);
197         }
198         fs->super->s_journal_dev = 0;
199         uuid_clear(fs->super->s_journal_uuid);
200         ext2fs_mark_super_dirty(fs);
201         fputs(_("Journal removed\n"), stdout);
202         free(journal_path);
203 }
204
205 /* Helper function for remove_journal_inode */
206 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
207                                int blockcnt EXT2FS_ATTR((unused)), 
208                                void *private EXT2FS_ATTR((unused)))
209 {
210         blk_t   block;
211         int     group;
212
213         block = *blocknr;
214         ext2fs_unmark_block_bitmap(fs->block_map,block);
215         group = ext2fs_group_of_blk(fs, block);
216         fs->group_desc[group].bg_free_blocks_count++;
217         fs->super->s_free_blocks_count++;
218         return 0;
219 }
220
221 /*
222  * Remove the journal inode from the filesystem
223  */
224 static void remove_journal_inode(ext2_filsys fs)
225 {
226         struct ext2_inode       inode;
227         errcode_t               retval;
228         ino_t                   ino = fs->super->s_journal_inum;
229         
230         retval = ext2fs_read_inode(fs, ino,  &inode);
231         if (retval) {
232                 com_err(program_name, retval,
233                         _("while reading journal inode"));
234                 exit(1);
235         }
236         if (ino == EXT2_JOURNAL_INO) {
237                 retval = ext2fs_read_bitmaps(fs);
238                 if (retval) {
239                         com_err(program_name, retval,
240                                 _("while reading bitmaps"));
241                         exit(1);
242                 }
243                 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
244                                               release_blocks_proc, NULL);
245                 if (retval) {
246                         com_err(program_name, retval,
247                                 _("while clearing journal inode"));
248                         exit(1);
249                 }
250                 memset(&inode, 0, sizeof(inode));
251                 ext2fs_mark_bb_dirty(fs);
252                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
253         } else
254                 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
255         retval = ext2fs_write_inode(fs, ino, &inode);
256         if (retval) {
257                 com_err(program_name, retval,
258                         _("while writing journal inode"));
259                 exit(1);
260         }
261         fs->super->s_journal_inum = 0;
262         ext2fs_mark_super_dirty(fs);
263 }
264
265 /*
266  * Update the default mount options
267  */
268 static void update_mntopts(ext2_filsys fs, char *mntopts)
269 {
270         struct ext2_super_block *sb= fs->super;
271
272         if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
273                 fprintf(stderr, _("Invalid mount option set: %s\n"),
274                         mntopts);
275                 exit(1);
276         }
277         ext2fs_mark_super_dirty(fs);
278 }
279
280 /*
281  * Update the feature set as provided by the user.
282  */
283 static void update_feature_set(ext2_filsys fs, char *features)
284 {
285         int sparse, old_sparse, filetype, old_filetype;
286         int journal, old_journal, dxdir, old_dxdir;
287         int flex_bg, old_flex_bg;
288         struct ext2_super_block *sb= fs->super;
289         __u32   old_compat, old_incompat, old_ro_compat;
290
291         old_compat = sb->s_feature_compat;
292         old_ro_compat = sb->s_feature_ro_compat;
293         old_incompat = sb->s_feature_incompat;
294
295         old_sparse = sb->s_feature_ro_compat &
296                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
297         old_filetype = sb->s_feature_incompat &
298                 EXT2_FEATURE_INCOMPAT_FILETYPE;
299         old_flex_bg = sb->s_feature_incompat &
300                 EXT4_FEATURE_INCOMPAT_FLEX_BG;
301         old_journal = sb->s_feature_compat &
302                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
303         old_dxdir = sb->s_feature_compat &
304                 EXT2_FEATURE_COMPAT_DIR_INDEX;
305         if (e2p_edit_feature(features, &sb->s_feature_compat,
306                              ok_features)) {
307                 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
308                         features);
309                 exit(1);
310         }
311         sparse = sb->s_feature_ro_compat &
312                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
313         filetype = sb->s_feature_incompat &
314                 EXT2_FEATURE_INCOMPAT_FILETYPE;
315         flex_bg = sb->s_feature_incompat &
316                 EXT4_FEATURE_INCOMPAT_FLEX_BG;
317         journal = sb->s_feature_compat &
318                 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
319         dxdir = sb->s_feature_compat &
320                 EXT2_FEATURE_COMPAT_DIR_INDEX;
321         if (old_journal && !journal) {
322                 if ((mount_flags & EXT2_MF_MOUNTED) &&
323                     !(mount_flags & EXT2_MF_READONLY)) {
324                         fputs(_("The has_journal flag may only be "
325                                 "cleared when the filesystem is\n"
326                                 "unmounted or mounted "
327                                 "read-only.\n"), stderr);
328                         exit(1);
329                 }
330                 if (sb->s_feature_incompat &
331                     EXT3_FEATURE_INCOMPAT_RECOVER) {
332                         fputs(_("The needs_recovery flag is set.  "
333                                 "Please run e2fsck before clearing\n"
334                                 "the has_journal flag.\n"), stderr);
335                         exit(1);
336                 }
337                 if (sb->s_journal_inum) {
338                         remove_journal_inode(fs);
339                 }
340                 if (sb->s_journal_dev) {
341                         remove_journal_device(fs);
342                 }
343         }
344         if (journal && !old_journal) {
345                 /*
346                  * If adding a journal flag, let the create journal
347                  * code below handle creating setting the flag and
348                  * creating the journal.  We supply a default size if
349                  * necessary.
350                  */
351                 if (!journal_size)
352                         journal_size = -1;
353                 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
354         }
355         if (dxdir && !old_dxdir) {
356                 if (!sb->s_def_hash_version)
357                         sb->s_def_hash_version = EXT2_HASH_TEA;
358                 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
359                         uuid_generate((unsigned char *) sb->s_hash_seed);
360         }
361         if (!flex_bg && old_flex_bg) {
362                 if (ext2fs_check_desc(fs)) {
363                         fputs(_("Clearing the flex_bg flag would "
364                                 "cause the the filesystem to be\n"
365                                 "inconsistent.\n"), stderr);
366                         exit(1);
367                 }
368         }
369
370         if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
371             (sb->s_feature_compat || sb->s_feature_ro_compat ||
372              sb->s_feature_incompat))
373                 ext2fs_update_dynamic_rev(fs);
374         if ((sparse != old_sparse) ||
375             (filetype != old_filetype)) {
376                 sb->s_state &= ~EXT2_VALID_FS;
377                 printf("\n%s\n", _(please_fsck));
378         }
379         if ((old_compat != sb->s_feature_compat) ||
380             (old_ro_compat != sb->s_feature_ro_compat) ||
381             (old_incompat != sb->s_feature_incompat))
382                 ext2fs_mark_super_dirty(fs);
383 }
384
385 /*
386  * Add a journal to the filesystem.
387  */
388 static void add_journal(ext2_filsys fs)
389 {
390         unsigned long journal_blocks;
391         errcode_t       retval;
392         ext2_filsys     jfs;
393         io_manager      io_ptr;
394
395         if (fs->super->s_feature_compat &
396             EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
397                 fputs(_("The filesystem already has a journal.\n"), stderr);
398                 goto err;
399         }
400         if (journal_device) {
401                 check_plausibility(journal_device);
402                 check_mount(journal_device, 0, _("journal"));
403 #ifdef CONFIG_TESTIO_DEBUG
404                 io_ptr = test_io_manager;
405                 test_io_backing_manager = unix_io_manager;
406 #else
407                 io_ptr = unix_io_manager;
408 #endif
409                 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
410                                      EXT2_FLAG_JOURNAL_DEV_OK, 0,
411                                      fs->blocksize, io_ptr, &jfs);
412                 if (retval) {
413                         com_err(program_name, retval,
414                                 _("\n\twhile trying to open journal on %s\n"),
415                                 journal_device);
416                         goto err;
417                 }
418                 printf(_("Creating journal on device %s: "),
419                        journal_device);
420                 fflush(stdout);
421
422                 retval = ext2fs_add_journal_device(fs, jfs);
423                 ext2fs_close(jfs);
424                 if (retval) {
425                         com_err (program_name, retval,
426                                  _("while adding filesystem to journal on %s"),
427                                  journal_device);
428                         goto err;
429                 }
430                 fputs(_("done\n"), stdout);
431         } else if (journal_size) {
432                 fputs(_("Creating journal inode: "), stdout);
433                 fflush(stdout);
434                 journal_blocks = figure_journal_size(journal_size, fs);
435
436                 retval = ext2fs_add_journal_inode(fs, journal_blocks,
437                                                   journal_flags);
438                 if (retval) {
439                         fprintf(stderr, "\n");
440                         com_err(program_name, retval,
441                                 _("\n\twhile trying to create journal file"));
442                         exit(1);
443                 } else
444                         fputs(_("done\n"), stdout);
445                 /*
446                  * If the filesystem wasn't mounted, we need to force
447                  * the block group descriptors out.
448                  */
449                 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
450                         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
451         }
452         print_check_message(fs);
453         return;
454
455 err:
456         if (journal_device)
457                 free(journal_device);
458         exit(1);
459 }
460
461
462 static void parse_e2label_options(int argc, char ** argv)
463 {
464         if ((argc < 2) || (argc > 3)) {
465                 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
466                 exit(1);
467         }
468         io_options = strchr(argv[1], '?');
469         if (io_options)
470                 *io_options++ = 0;
471         device_name = blkid_get_devname(NULL, argv[1], NULL);
472         if (!device_name) {
473                 com_err("e2label", 0, _("Unable to resolve '%s'"), 
474                         argv[1]);
475                 exit(1);
476         }
477         if (argc == 3) {
478                 open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
479                 L_flag = 1;
480                 new_label = argv[2];
481         } else 
482                 print_label++;
483 }
484
485 static time_t parse_time(char *str)
486 {
487         struct  tm      ts;
488
489         if (strcmp(str, "now") == 0) {
490                 return (time(0));
491         }
492         memset(&ts, 0, sizeof(ts));
493 #ifdef HAVE_STRPTIME
494         strptime(str, "%Y%m%d%H%M%S", &ts);
495 #else
496         sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
497                &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
498         ts.tm_year -= 1900;
499         ts.tm_mon -= 1;
500         if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
501             ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
502             ts.tm_min > 59 || ts.tm_sec > 61)
503                 ts.tm_mday = 0;
504 #endif
505         if (ts.tm_mday == 0) {
506                 com_err(program_name, 0,
507                         _("Couldn't parse date/time specifier: %s"),
508                         str);
509                 usage();
510         }
511         return (mktime(&ts));
512 }
513
514 static void parse_tune2fs_options(int argc, char **argv)
515 {
516         int c;
517         char * tmp;
518         struct group * gr;
519         struct passwd * pw;
520
521         printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
522         while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
523                 switch (c)
524                 {
525                         case 'c':
526                                 max_mount_count = strtol (optarg, &tmp, 0);
527                                 if (*tmp || max_mount_count > 16000) {
528                                         com_err (program_name, 0,
529                                                  _("bad mounts count - %s"),
530                                                  optarg);
531                                         usage();
532                                 }
533                                 if (max_mount_count == 0)
534                                         max_mount_count = -1;
535                                 c_flag = 1;
536                                 open_flag = EXT2_FLAG_RW;
537                                 break;
538                         case 'C':
539                                 mount_count = strtoul (optarg, &tmp, 0);
540                                 if (*tmp || mount_count > 16000) {
541                                         com_err (program_name, 0,
542                                                  _("bad mounts count - %s"),
543                                                  optarg);
544                                         usage();
545                                 }
546                                 C_flag = 1;
547                                 open_flag = EXT2_FLAG_RW;
548                                 break;
549                         case 'e':
550                                 if (strcmp (optarg, "continue") == 0)
551                                         errors = EXT2_ERRORS_CONTINUE;
552                                 else if (strcmp (optarg, "remount-ro") == 0)
553                                         errors = EXT2_ERRORS_RO;
554                                 else if (strcmp (optarg, "panic") == 0)
555                                         errors = EXT2_ERRORS_PANIC;
556                                 else {
557                                         com_err (program_name, 0,
558                                                  _("bad error behavior - %s"),
559                                                  optarg);
560                                         usage();
561                                 }
562                                 e_flag = 1;
563                                 open_flag = EXT2_FLAG_RW;
564                                 break;
565                         case 'f': /* Force */
566                                 f_flag = 1;
567                                 break;
568                         case 'g':
569                                 resgid = strtoul (optarg, &tmp, 0);
570                                 if (*tmp) {
571                                         gr = getgrnam (optarg);
572                                         if (gr == NULL)
573                                                 tmp = optarg;
574                                         else {
575                                                 resgid = gr->gr_gid;
576                                                 *tmp =0;
577                                         }
578                                 }
579                                 if (*tmp) {
580                                         com_err (program_name, 0,
581                                                  _("bad gid/group name - %s"),
582                                                  optarg);
583                                         usage();
584                                 }
585                                 g_flag = 1;
586                                 open_flag = EXT2_FLAG_RW;
587                                 break;
588                         case 'i':
589                                 interval = strtoul (optarg, &tmp, 0);
590                                 switch (*tmp) {
591                                 case 's':
592                                         tmp++;
593                                         break;
594                                 case '\0':
595                                 case 'd':
596                                 case 'D': /* days */
597                                         interval *= 86400;
598                                         if (*tmp != '\0')
599                                                 tmp++;
600                                         break;
601                                 case 'm':
602                                 case 'M': /* months! */
603                                         interval *= 86400 * 30;
604                                         tmp++;
605                                         break;
606                                 case 'w':
607                                 case 'W': /* weeks */
608                                         interval *= 86400 * 7;
609                                         tmp++;
610                                         break;
611                                 }
612                                 if (*tmp) {
613                                         com_err (program_name, 0,
614                                                 _("bad interval - %s"), optarg);
615                                         usage();
616                                 }
617                                 i_flag = 1;
618                                 open_flag = EXT2_FLAG_RW;
619                                 break;
620                         case 'j':
621                                 if (!journal_size)
622                                         journal_size = -1;
623                                 open_flag = EXT2_FLAG_RW;
624                                 break;
625                         case 'J':
626                                 parse_journal_opts(optarg);
627                                 open_flag = EXT2_FLAG_RW;
628                                 break;
629                         case 'l':
630                                 l_flag = 1;
631                                 break;
632                         case 'L':
633                                 new_label = optarg;
634                                 L_flag = 1;
635                                 open_flag = EXT2_FLAG_RW |
636                                         EXT2_FLAG_JOURNAL_DEV_OK;
637                                 break;
638                         case 'm':
639                                 reserved_ratio = strtod(optarg, &tmp);
640                                 if (*tmp || reserved_ratio > 50) {
641                                         com_err (program_name, 0,
642                                                  _("bad reserved block ratio - %s"),
643                                                  optarg);
644                                         usage();
645                                 }
646                                 m_flag = 1;
647                                 open_flag = EXT2_FLAG_RW;
648                                 break;
649                         case 'M':
650                                 new_last_mounted = optarg;
651                                 M_flag = 1;
652                                 open_flag = EXT2_FLAG_RW;
653                                 break;
654                         case 'o':
655                                 if (mntopts_cmd) {
656                                         com_err (program_name, 0,
657                                          _("-o may only be specified once"));
658                                         usage();
659                                 }
660                                 mntopts_cmd = optarg;
661                                 open_flag = EXT2_FLAG_RW;
662                                 break;
663                                 
664                         case 'O':
665                                 if (features_cmd) {
666                                         com_err (program_name, 0,
667                                          _("-O may only be specified once"));
668                                         usage();
669                                 }
670                                 features_cmd = optarg;
671                                 open_flag = EXT2_FLAG_RW;
672                                 break;
673                         case 'r':
674                                 reserved_blocks = strtoul (optarg, &tmp, 0);
675                                 if (*tmp) {
676                                         com_err (program_name, 0,
677                                                  _("bad reserved blocks count - %s"),
678                                                  optarg);
679                                         usage();
680                                 }
681                                 r_flag = 1;
682                                 open_flag = EXT2_FLAG_RW;
683                                 break;
684                         case 's':
685                                 s_flag = atoi(optarg);
686                                 open_flag = EXT2_FLAG_RW;
687                                 break;
688                         case 'T':
689                                 T_flag = 1;
690                                 last_check_time = parse_time(optarg);
691                                 open_flag = EXT2_FLAG_RW;
692                                 break;
693                         case 'u':
694                                 resuid = strtoul (optarg, &tmp, 0);
695                                 if (*tmp) {
696                                         pw = getpwnam (optarg);
697                                         if (pw == NULL)
698                                                 tmp = optarg;
699                                         else {
700                                                 resuid = pw->pw_uid;
701                                                 *tmp = 0;
702                                         }
703                                 }
704                                 if (*tmp) {
705                                         com_err (program_name, 0,
706                                                  _("bad uid/user name - %s"),
707                                                  optarg);
708                                         usage();
709                                 }
710                                 u_flag = 1;
711                                 open_flag = EXT2_FLAG_RW;
712                                 break;
713                         case 'U':
714                                 new_UUID = optarg;
715                                 U_flag = 1;
716                                 open_flag = EXT2_FLAG_RW |
717                                         EXT2_FLAG_JOURNAL_DEV_OK;
718                                 break;
719                         default:
720                                 usage();
721                 }
722         if (optind < argc - 1 || optind == argc)
723                 usage();
724         if (!open_flag && !l_flag)
725                 usage();
726         io_options = strchr(argv[optind], '?');
727         if (io_options)
728                 *io_options++ = 0;
729         device_name = blkid_get_devname(NULL, argv[optind], NULL);
730         if (!device_name) {
731                 com_err("tune2fs", 0, _("Unable to resolve '%s'"), 
732                         argv[optind]);
733                 exit(1);
734         }
735 }
736
737 void do_findfs(int argc, char **argv)
738 {
739         char    *dev;
740
741         if ((argc != 2) ||
742             (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
743                 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
744                 exit(2);
745         }
746         dev = blkid_get_devname(NULL, argv[1], NULL);
747         if (!dev) {
748                 com_err("findfs", 0, _("Unable to resolve '%s'"), 
749                         argv[1]);
750                 exit(1);
751         }
752         puts(dev);
753         exit(0);
754 }
755
756
757 int main (int argc, char ** argv)
758 {
759         errcode_t retval;
760         ext2_filsys fs;
761         struct ext2_super_block *sb;
762         io_manager io_ptr;
763
764 #ifdef ENABLE_NLS
765         setlocale(LC_MESSAGES, "");
766         setlocale(LC_CTYPE, "");
767         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
768         textdomain(NLS_CAT_NAME);
769 #endif
770         if (argc && *argv)
771                 program_name = *argv;
772         add_error_table(&et_ext2_error_table);
773
774         if (strcmp(get_progname(argv[0]), "findfs") == 0)
775                 do_findfs(argc, argv);
776         if (strcmp(get_progname(argv[0]), "e2label") == 0)
777                 parse_e2label_options(argc, argv);
778         else
779                 parse_tune2fs_options(argc, argv);
780         
781 #ifdef CONFIG_TESTIO_DEBUG
782         io_ptr = test_io_manager;
783         test_io_backing_manager = unix_io_manager;
784 #else
785         io_ptr = unix_io_manager;
786 #endif
787         retval = ext2fs_open2(device_name, io_options, open_flag, 
788                               0, 0, io_ptr, &fs);
789         if (retval) {
790                 com_err (program_name, retval, _("while trying to open %s"),
791                          device_name);
792                 fprintf(stderr,
793                         _("Couldn't find valid filesystem superblock.\n"));
794                 exit(1);
795         }
796         sb = fs->super;
797         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
798         if (print_label) {
799                 /* For e2label emulation */
800                 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
801                        sb->s_volume_name);
802                 remove_error_table(&et_ext2_error_table);
803                 exit(0);
804         }
805         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
806         if (retval) {
807                 com_err("ext2fs_check_if_mount", retval,
808                         _("while determining whether %s is mounted."),
809                         device_name);
810                 exit(1);
811         }
812         /* Normally we only need to write out the superblock */
813         fs->flags |= EXT2_FLAG_SUPER_ONLY;
814
815         if (c_flag) {
816                 sb->s_max_mnt_count = max_mount_count;
817                 ext2fs_mark_super_dirty(fs);
818                 printf (_("Setting maximal mount count to %d\n"),
819                         max_mount_count);
820         }
821         if (C_flag) {
822                 sb->s_mnt_count = mount_count;
823                 ext2fs_mark_super_dirty(fs);
824                 printf (_("Setting current mount count to %d\n"), mount_count);
825         }
826         if (e_flag) {
827                 sb->s_errors = errors;
828                 ext2fs_mark_super_dirty(fs);
829                 printf (_("Setting error behavior to %d\n"), errors);
830         }
831         if (g_flag) {
832                 sb->s_def_resgid = resgid;
833                 ext2fs_mark_super_dirty(fs);
834                 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
835         }
836         if (i_flag) {
837                 sb->s_checkinterval = interval;
838                 ext2fs_mark_super_dirty(fs);
839                 printf (_("Setting interval between checks to %lu seconds\n"), interval);
840         }
841         if (m_flag) {
842                 sb->s_r_blocks_count = e2p_percent(reserved_ratio,
843                                                    sb->s_blocks_count);
844                 ext2fs_mark_super_dirty(fs);
845                 printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
846                         reserved_ratio, sb->s_r_blocks_count);
847         }
848         if (r_flag) {
849                 if (reserved_blocks >= sb->s_blocks_count/2) {
850                         com_err (program_name, 0,
851                                  _("reserved blocks count is too big (%lu)"),
852                                  reserved_blocks);
853                         exit (1);
854                 }
855                 sb->s_r_blocks_count = reserved_blocks;
856                 ext2fs_mark_super_dirty(fs);
857                 printf (_("Setting reserved blocks count to %lu\n"),
858                         reserved_blocks);
859         }
860         if (s_flag == 1) {
861                 if (sb->s_feature_ro_compat &
862                     EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
863                         fputs(_("\nThe filesystem already has sparse "
864                                 "superblocks.\n"), stderr);
865                 else {
866                         sb->s_feature_ro_compat |=
867                                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
868                         sb->s_state &= ~EXT2_VALID_FS;
869                         ext2fs_mark_super_dirty(fs);
870                         printf(_("\nSparse superblock flag set.  %s"),
871                                _(please_fsck));
872                 }
873         }
874         if (s_flag == 0) {
875                 if (!(sb->s_feature_ro_compat &
876                       EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
877                         fputs(_("\nThe filesystem already has sparse "
878                                 "superblocks disabled.\n"), stderr);
879                 else {
880                         sb->s_feature_ro_compat &=
881                                 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
882                         sb->s_state &= ~EXT2_VALID_FS;
883                         fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
884                         ext2fs_mark_super_dirty(fs);
885                         printf(_("\nSparse superblock flag cleared.  %s"),
886                                _(please_fsck));
887                 }
888         }
889         if (T_flag) {
890                 sb->s_lastcheck = last_check_time;
891                 ext2fs_mark_super_dirty(fs);
892                 printf(_("Setting time filesystem last checked to %s\n"),
893                        ctime(&last_check_time));
894         }
895         if (u_flag) {
896                 sb->s_def_resuid = resuid;
897                 ext2fs_mark_super_dirty(fs);
898                 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
899         }
900         if (L_flag) {
901                 if (strlen(new_label) > sizeof(sb->s_volume_name))
902                         fputs(_("Warning: label too long, truncating.\n"), 
903                               stderr);
904                 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
905                 strncpy(sb->s_volume_name, new_label,
906                         sizeof(sb->s_volume_name));
907                 ext2fs_mark_super_dirty(fs);
908         }
909         if (M_flag) {
910                 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
911                 strncpy(sb->s_last_mounted, new_last_mounted,
912                         sizeof(sb->s_last_mounted));
913                 ext2fs_mark_super_dirty(fs);
914         }
915         if (mntopts_cmd)
916                 update_mntopts(fs, mntopts_cmd);
917         if (features_cmd)
918                 update_feature_set(fs, features_cmd);
919         if (journal_size || journal_device)
920                 add_journal(fs);
921         
922         if (U_flag) {
923                 if ((strcasecmp(new_UUID, "null") == 0) ||
924                     (strcasecmp(new_UUID, "clear") == 0)) {
925                         uuid_clear(sb->s_uuid);
926                 } else if (strcasecmp(new_UUID, "time") == 0) {
927                         uuid_generate_time(sb->s_uuid);
928                 } else if (strcasecmp(new_UUID, "random") == 0) {
929                         uuid_generate(sb->s_uuid);
930                 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
931                         com_err(program_name, 0, _("Invalid UUID format\n"));
932                         exit(1);
933                 }
934                 ext2fs_mark_super_dirty(fs);
935         }
936
937         if (l_flag)
938                 list_super (sb);
939         remove_error_table(&et_ext2_error_table);
940         return (ext2fs_close (fs) ? 1 : 0);
941 }