Whamcloud - gitweb
4e731f5cc221d99f554f91283d8749324230f9c3
[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 /* 
59  * Tune2fs supports these features in addition to the standard features.
60  */
61 #define EXT2_TUNE2FS_INCOMPAT   (EXT3_FEATURE_INCOMPAT_EXTENTS)
62 #define EXT2_TUNE2FS_RO_COMPAT  (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
63                                  EXT4_FEATURE_RO_COMPAT_GDT_CSUM|       \
64                                  EXT4_FEATURE_RO_COMPAT_DIR_NLINK|      \
65                                  EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
66
67
68 const char * program_name = "tune2fs";
69 char * device_name;
70 char * new_label, *new_last_mounted, *new_UUID;
71 char * io_options;
72 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
73 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
74 static time_t last_check_time;
75 static int print_label;
76 static int max_mount_count, mount_count, mount_flags;
77 static unsigned long interval, reserved_blocks;
78 static double reserved_ratio;
79 static unsigned long resgid, resuid;
80 static unsigned short errors;
81 static int open_flag;
82 static char *features_cmd;
83 static char *mntopts_cmd;
84 static int stride, stripe_width;
85 static int stride_set, stripe_width_set;
86 static char *extended_cmd;
87
88 int journal_size, journal_flags;
89 char *journal_device;
90
91 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
92
93 void do_findfs(int argc, char **argv);
94
95 static void usage(void)
96 {
97         fprintf(stderr,
98                 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
99                   "[-g group]\n"
100                   "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
101                   "\t[-m reserved_blocks_percent] "
102                   "[-o [^]mount_options[,...]] \n"
103                   "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
104                   "[-L volume_label]\n"
105                   "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
106                   "\t[-E extended-option[,...]] [-T last_check_time] "
107                   "[-U UUID] device\n"), program_name);
108         exit (1);
109 }
110
111 static __u32 ok_features[3] = {
112         /* Compat */
113         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
114                 EXT2_FEATURE_COMPAT_DIR_INDEX,
115         /* Incompat */
116         EXT2_FEATURE_INCOMPAT_FILETYPE,
117         /* R/O compat */
118         EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
119                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
120 };
121
122 static __u32 clear_ok_features[3] = {
123         /* Compat */
124         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
125                 EXT2_FEATURE_COMPAT_RESIZE_INODE |
126                 EXT2_FEATURE_COMPAT_DIR_INDEX,
127         /* Incompat */
128         EXT2_FEATURE_INCOMPAT_FILETYPE,
129         /* R/O compat */
130         EXT2_FEATURE_RO_COMPAT_LARGE_FILE
131 };
132
133 /*
134  * Remove an external journal from the filesystem
135  */
136 static void remove_journal_device(ext2_filsys fs)
137 {
138         char            *journal_path;
139         ext2_filsys     jfs;
140         char            buf[1024];
141         journal_superblock_t    *jsb;
142         int             i, nr_users;
143         errcode_t       retval;
144         int             commit_remove_journal = 0;
145         io_manager      io_ptr;
146
147         if (f_flag)
148                 commit_remove_journal = 1; /* force removal even if error */
149
150         uuid_unparse(fs->super->s_journal_uuid, buf);
151         journal_path = blkid_get_devname(NULL, "UUID", buf);
152
153         if (!journal_path) {
154                 journal_path =
155                         ext2fs_find_block_device(fs->super->s_journal_dev);
156                 if (!journal_path)
157                         return;
158         }
159
160 #ifdef CONFIG_TESTIO_DEBUG
161         io_ptr = test_io_manager;
162         test_io_backing_manager = unix_io_manager;
163 #else
164         io_ptr = unix_io_manager;
165 #endif
166         retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
167                              EXT2_FLAG_JOURNAL_DEV_OK, 0,
168                              fs->blocksize, io_ptr, &jfs);
169         if (retval) {
170                 com_err(program_name, retval,
171                         _("while trying to open external journal"));
172                 goto no_valid_journal;
173         }
174         if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
175                 fprintf(stderr, _("%s is not a journal device.\n"),
176                         journal_path);
177                 goto no_valid_journal;
178         }
179
180         /* Get the journal superblock */
181         if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
182                 com_err(program_name, retval,
183                         _("while reading journal superblock"));
184                 goto no_valid_journal;
185         }
186
187         jsb = (journal_superblock_t *) buf;
188         if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
189             (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
190                 fputs(_("Journal superblock not found!\n"), stderr);
191                 goto no_valid_journal;
192         }
193
194         /* Find the filesystem UUID */
195         nr_users = ntohl(jsb->s_nr_users);
196         for (i=0; i < nr_users; i++) {
197                 if (memcmp(fs->super->s_uuid,
198                            &jsb->s_users[i*16], 16) == 0)
199                         break;
200         }
201         if (i >= nr_users) {
202                 fputs(_("Filesystem's UUID not found on journal device.\n"), 
203                       stderr);
204                 commit_remove_journal = 1;
205                 goto no_valid_journal;
206         }
207         nr_users--;
208         for (i=0; i < nr_users; i++)
209                 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
210         jsb->s_nr_users = htonl(nr_users);
211
212         /* Write back the journal superblock */
213         if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
214                 com_err(program_name, retval,
215                         "while writing journal superblock.");
216                 goto no_valid_journal;
217         }
218
219         commit_remove_journal = 1;
220
221 no_valid_journal:
222         if (commit_remove_journal == 0) {
223                 fputs(_("Journal NOT removed\n"), stderr);
224                 exit(1);
225         }
226         fs->super->s_journal_dev = 0;
227         uuid_clear(fs->super->s_journal_uuid);
228         ext2fs_mark_super_dirty(fs);
229         fputs(_("Journal removed\n"), stdout);
230         free(journal_path);
231 }
232
233 /* Helper function for remove_journal_inode */
234 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
235                                int blockcnt EXT2FS_ATTR((unused)), 
236                                void *private EXT2FS_ATTR((unused)))
237 {
238         blk_t   block;
239         int     group;
240
241         block = *blocknr;
242         ext2fs_unmark_block_bitmap(fs->block_map,block);
243         group = ext2fs_group_of_blk(fs, block);
244         fs->group_desc[group].bg_free_blocks_count++;
245         fs->super->s_free_blocks_count++;
246         return 0;
247 }
248
249 /*
250  * Remove the journal inode from the filesystem
251  */
252 static void remove_journal_inode(ext2_filsys fs)
253 {
254         struct ext2_inode       inode;
255         errcode_t               retval;
256         ino_t                   ino = fs->super->s_journal_inum;
257         
258         retval = ext2fs_read_inode(fs, ino,  &inode);
259         if (retval) {
260                 com_err(program_name, retval,
261                         _("while reading journal inode"));
262                 exit(1);
263         }
264         if (ino == EXT2_JOURNAL_INO) {
265                 retval = ext2fs_read_bitmaps(fs);
266                 if (retval) {
267                         com_err(program_name, retval,
268                                 _("while reading bitmaps"));
269                         exit(1);
270                 }
271                 retval = ext2fs_block_iterate(fs, ino, 0, NULL,
272                                               release_blocks_proc, NULL);
273                 if (retval) {
274                         com_err(program_name, retval,
275                                 _("while clearing journal inode"));
276                         exit(1);
277                 }
278                 memset(&inode, 0, sizeof(inode));
279                 ext2fs_mark_bb_dirty(fs);
280                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
281         } else
282                 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
283         retval = ext2fs_write_inode(fs, ino, &inode);
284         if (retval) {
285                 com_err(program_name, retval,
286                         _("while writing journal inode"));
287                 exit(1);
288         }
289         fs->super->s_journal_inum = 0;
290         ext2fs_mark_super_dirty(fs);
291 }
292
293 /*
294  * Update the default mount options
295  */
296 static void update_mntopts(ext2_filsys fs, char *mntopts)
297 {
298         struct ext2_super_block *sb= fs->super;
299
300         if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
301                 fprintf(stderr, _("Invalid mount option set: %s\n"),
302                         mntopts);
303                 exit(1);
304         }
305         ext2fs_mark_super_dirty(fs);
306 }
307
308 /*
309  * Update the feature set as provided by the user.
310  */
311 static void update_feature_set(ext2_filsys fs, char *features)
312 {
313         struct ext2_super_block *sb= fs->super;
314         __u32   old_compat, old_incompat, old_ro_compat;
315         __u32           old_features[3];
316         int             type_err;
317         unsigned int    mask_err;
318
319 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
320                                 ((&sb->s_feature_compat)[(type)] & (mask)))
321 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
322                                  !((&sb->s_feature_compat)[(type)] & (mask)))
323 #define FEATURE_CHANGED(type, mask) ((mask) & \
324                      (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
325
326         old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
327         old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
328         old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
329
330         if (e2p_edit_feature2(features, &sb->s_feature_compat,
331                               ok_features, clear_ok_features,
332                               &type_err, &mask_err)) {
333                 if (!mask_err)
334                         fprintf(stderr,
335                                 _("Invalid filesystem option set: %s\n"),
336                                 features);
337                 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
338                         fprintf(stderr, _("Clearing filesystem feature '%s' "
339                                           "not supported.\n"),
340                                 e2p_feature2string(type_err &
341                                                    E2P_FEATURE_TYPE_MASK,
342                                                    mask_err));
343                 else
344                         fprintf(stderr, _("Setting filesystem feature '%s' "
345                                           "not supported.\n"),
346                                 e2p_feature2string(type_err, mask_err));
347                 exit(1);
348         }
349
350         if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
351                 if ((mount_flags & EXT2_MF_MOUNTED) &&
352                     !(mount_flags & EXT2_MF_READONLY)) {
353                         fputs(_("The has_journal flag may only be "
354                                 "cleared when the filesystem is\n"
355                                 "unmounted or mounted "
356                                 "read-only.\n"), stderr);
357                         exit(1);
358                 }
359                 if (sb->s_feature_incompat &
360                     EXT3_FEATURE_INCOMPAT_RECOVER) {
361                         fputs(_("The needs_recovery flag is set.  "
362                                 "Please run e2fsck before clearing\n"
363                                 "the has_journal flag.\n"), stderr);
364                         exit(1);
365                 }
366                 if (sb->s_journal_inum) {
367                         remove_journal_inode(fs);
368                 }
369                 if (sb->s_journal_dev) {
370                         remove_journal_device(fs);
371                 }
372         }
373
374         if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
375                 /*
376                  * If adding a journal flag, let the create journal
377                  * code below handle creating setting the flag and
378                  * creating the journal.  We supply a default size if
379                  * necessary.
380                  */
381                 if (!journal_size)
382                         journal_size = -1;
383                 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
384         }
385
386         if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
387                 if (!sb->s_def_hash_version)
388                         sb->s_def_hash_version = EXT2_HASH_TEA;
389                 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
390                         uuid_generate((unsigned char *) sb->s_hash_seed);
391         }
392
393         if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
394             (sb->s_feature_compat || sb->s_feature_ro_compat ||
395              sb->s_feature_incompat))
396                 ext2fs_update_dynamic_rev(fs);
397
398         if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
399                             EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
400             FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
401                             EXT2_FEATURE_INCOMPAT_FILETYPE) ||
402             FEATURE_CHANGED(E2P_FEATURE_COMPAT,
403                             EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
404             FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
405                         EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
406                 sb->s_state &= ~EXT2_VALID_FS;
407                 printf("\n%s\n", _(please_fsck));
408         }
409
410         if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
411             (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
412             (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
413                 ext2fs_mark_super_dirty(fs);
414 }
415
416 /*
417  * Add a journal to the filesystem.
418  */
419 static void add_journal(ext2_filsys fs)
420 {
421         unsigned long journal_blocks;
422         errcode_t       retval;
423         ext2_filsys     jfs;
424         io_manager      io_ptr;
425
426         if (fs->super->s_feature_compat &
427             EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
428                 fputs(_("The filesystem already has a journal.\n"), stderr);
429                 goto err;
430         }
431         if (journal_device) {
432                 check_plausibility(journal_device);
433                 check_mount(journal_device, 0, _("journal"));
434 #ifdef CONFIG_TESTIO_DEBUG
435                 io_ptr = test_io_manager;
436                 test_io_backing_manager = unix_io_manager;
437 #else
438                 io_ptr = unix_io_manager;
439 #endif
440                 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
441                                      EXT2_FLAG_JOURNAL_DEV_OK, 0,
442                                      fs->blocksize, io_ptr, &jfs);
443                 if (retval) {
444                         com_err(program_name, retval,
445                                 _("\n\twhile trying to open journal on %s\n"),
446                                 journal_device);
447                         goto err;
448                 }
449                 printf(_("Creating journal on device %s: "),
450                        journal_device);
451                 fflush(stdout);
452
453                 retval = ext2fs_add_journal_device(fs, jfs);
454                 ext2fs_close(jfs);
455                 if (retval) {
456                         com_err (program_name, retval,
457                                  _("while adding filesystem to journal on %s"),
458                                  journal_device);
459                         goto err;
460                 }
461                 fputs(_("done\n"), stdout);
462         } else if (journal_size) {
463                 fputs(_("Creating journal inode: "), stdout);
464                 fflush(stdout);
465                 journal_blocks = figure_journal_size(journal_size, fs);
466
467                 retval = ext2fs_add_journal_inode(fs, journal_blocks,
468                                                   journal_flags);
469                 if (retval) {
470                         fprintf(stderr, "\n");
471                         com_err(program_name, retval,
472                                 _("\n\twhile trying to create journal file"));
473                         exit(1);
474                 } else
475                         fputs(_("done\n"), stdout);
476                 /*
477                  * If the filesystem wasn't mounted, we need to force
478                  * the block group descriptors out.
479                  */
480                 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
481                         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
482         }
483         print_check_message(fs);
484         return;
485
486 err:
487         if (journal_device)
488                 free(journal_device);
489         exit(1);
490 }
491
492
493 static void parse_e2label_options(int argc, char ** argv)
494 {
495         if ((argc < 2) || (argc > 3)) {
496                 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
497                 exit(1);
498         }
499         io_options = strchr(argv[1], '?');
500         if (io_options)
501                 *io_options++ = 0;
502         device_name = blkid_get_devname(NULL, argv[1], NULL);
503         if (!device_name) {
504                 com_err("e2label", 0, _("Unable to resolve '%s'"), 
505                         argv[1]);
506                 exit(1);
507         }
508         open_flag = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_JOURNAL_DEV_OK;
509         if (argc == 3) {
510                 open_flag |= EXT2_FLAG_RW;
511                 L_flag = 1;
512                 new_label = argv[2];
513         } else 
514                 print_label++;
515 }
516
517 static time_t parse_time(char *str)
518 {
519         struct  tm      ts;
520
521         if (strcmp(str, "now") == 0) {
522                 return (time(0));
523         }
524         memset(&ts, 0, sizeof(ts));
525 #ifdef HAVE_STRPTIME
526         strptime(str, "%Y%m%d%H%M%S", &ts);
527 #else
528         sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
529                &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
530         ts.tm_year -= 1900;
531         ts.tm_mon -= 1;
532         if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
533             ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
534             ts.tm_min > 59 || ts.tm_sec > 61)
535                 ts.tm_mday = 0;
536 #endif
537         if (ts.tm_mday == 0) {
538                 com_err(program_name, 0,
539                         _("Couldn't parse date/time specifier: %s"),
540                         str);
541                 usage();
542         }
543         ts.tm_isdst = -1;
544         return (mktime(&ts));
545 }
546
547 static void parse_tune2fs_options(int argc, char **argv)
548 {
549         int c;
550         char * tmp;
551         struct group * gr;
552         struct passwd * pw;
553
554         open_flag = EXT2_FLAG_SOFTSUPP_FEATURES;
555
556         printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
557         while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:J:L:M:O:T:U:")) != EOF)
558                 switch (c)
559                 {
560                         case 'c':
561                                 max_mount_count = strtol (optarg, &tmp, 0);
562                                 if (*tmp || max_mount_count > 16000) {
563                                         com_err (program_name, 0,
564                                                  _("bad mounts count - %s"),
565                                                  optarg);
566                                         usage();
567                                 }
568                                 if (max_mount_count == 0)
569                                         max_mount_count = -1;
570                                 c_flag = 1;
571                                 open_flag = EXT2_FLAG_RW;
572                                 break;
573                         case 'C':
574                                 mount_count = strtoul (optarg, &tmp, 0);
575                                 if (*tmp || mount_count > 16000) {
576                                         com_err (program_name, 0,
577                                                  _("bad mounts count - %s"),
578                                                  optarg);
579                                         usage();
580                                 }
581                                 C_flag = 1;
582                                 open_flag = EXT2_FLAG_RW;
583                                 break;
584                         case 'e':
585                                 if (strcmp (optarg, "continue") == 0)
586                                         errors = EXT2_ERRORS_CONTINUE;
587                                 else if (strcmp (optarg, "remount-ro") == 0)
588                                         errors = EXT2_ERRORS_RO;
589                                 else if (strcmp (optarg, "panic") == 0)
590                                         errors = EXT2_ERRORS_PANIC;
591                                 else {
592                                         com_err (program_name, 0,
593                                                  _("bad error behavior - %s"),
594                                                  optarg);
595                                         usage();
596                                 }
597                                 e_flag = 1;
598                                 open_flag = EXT2_FLAG_RW;
599                                 break;
600                         case 'E':
601                                 extended_cmd = optarg;
602                                 open_flag |= EXT2_FLAG_RW;
603                                 break;
604                         case 'f': /* Force */
605                                 f_flag = 1;
606                                 break;
607                         case 'g':
608                                 resgid = strtoul (optarg, &tmp, 0);
609                                 if (*tmp) {
610                                         gr = getgrnam (optarg);
611                                         if (gr == NULL)
612                                                 tmp = optarg;
613                                         else {
614                                                 resgid = gr->gr_gid;
615                                                 *tmp =0;
616                                         }
617                                 }
618                                 if (*tmp) {
619                                         com_err (program_name, 0,
620                                                  _("bad gid/group name - %s"),
621                                                  optarg);
622                                         usage();
623                                 }
624                                 g_flag = 1;
625                                 open_flag = EXT2_FLAG_RW;
626                                 break;
627                         case 'i':
628                                 interval = strtoul (optarg, &tmp, 0);
629                                 switch (*tmp) {
630                                 case 's':
631                                         tmp++;
632                                         break;
633                                 case '\0':
634                                 case 'd':
635                                 case 'D': /* days */
636                                         interval *= 86400;
637                                         if (*tmp != '\0')
638                                                 tmp++;
639                                         break;
640                                 case 'm':
641                                 case 'M': /* months! */
642                                         interval *= 86400 * 30;
643                                         tmp++;
644                                         break;
645                                 case 'w':
646                                 case 'W': /* weeks */
647                                         interval *= 86400 * 7;
648                                         tmp++;
649                                         break;
650                                 }
651                                 if (*tmp) {
652                                         com_err (program_name, 0,
653                                                 _("bad interval - %s"), optarg);
654                                         usage();
655                                 }
656                                 i_flag = 1;
657                                 open_flag = EXT2_FLAG_RW;
658                                 break;
659                         case 'j':
660                                 if (!journal_size)
661                                         journal_size = -1;
662                                 open_flag = EXT2_FLAG_RW;
663                                 break;
664                         case 'J':
665                                 parse_journal_opts(optarg);
666                                 open_flag = EXT2_FLAG_RW;
667                                 break;
668                         case 'l':
669                                 l_flag = 1;
670                                 break;
671                         case 'L':
672                                 new_label = optarg;
673                                 L_flag = 1;
674                                 open_flag |= EXT2_FLAG_RW |
675                                         EXT2_FLAG_JOURNAL_DEV_OK;
676                                 break;
677                         case 'm':
678                                 reserved_ratio = strtod(optarg, &tmp);
679                                 if (*tmp || reserved_ratio > 50) {
680                                         com_err (program_name, 0,
681                                                  _("bad reserved block ratio - %s"),
682                                                  optarg);
683                                         usage();
684                                 }
685                                 m_flag = 1;
686                                 open_flag = EXT2_FLAG_RW;
687                                 break;
688                         case 'M':
689                                 new_last_mounted = optarg;
690                                 M_flag = 1;
691                                 open_flag = EXT2_FLAG_RW;
692                                 break;
693                         case 'o':
694                                 if (mntopts_cmd) {
695                                         com_err (program_name, 0,
696                                          _("-o may only be specified once"));
697                                         usage();
698                                 }
699                                 mntopts_cmd = optarg;
700                                 open_flag = EXT2_FLAG_RW;
701                                 break;
702                                 
703                         case 'O':
704                                 if (features_cmd) {
705                                         com_err (program_name, 0,
706                                          _("-O may only be specified once"));
707                                         usage();
708                                 }
709                                 features_cmd = optarg;
710                                 open_flag = EXT2_FLAG_RW;
711                                 break;
712                         case 'r':
713                                 reserved_blocks = strtoul (optarg, &tmp, 0);
714                                 if (*tmp) {
715                                         com_err (program_name, 0,
716                                                  _("bad reserved blocks count - %s"),
717                                                  optarg);
718                                         usage();
719                                 }
720                                 r_flag = 1;
721                                 open_flag = EXT2_FLAG_RW;
722                                 break;
723                         case 's': /* Deprecated */
724                                 s_flag = atoi(optarg);
725                                 open_flag = EXT2_FLAG_RW;
726                                 break;
727                         case 'T':
728                                 T_flag = 1;
729                                 last_check_time = parse_time(optarg);
730                                 open_flag = EXT2_FLAG_RW;
731                                 break;
732                         case 'u':
733                                 resuid = strtoul (optarg, &tmp, 0);
734                                 if (*tmp) {
735                                         pw = getpwnam (optarg);
736                                         if (pw == NULL)
737                                                 tmp = optarg;
738                                         else {
739                                                 resuid = pw->pw_uid;
740                                                 *tmp = 0;
741                                         }
742                                 }
743                                 if (*tmp) {
744                                         com_err (program_name, 0,
745                                                  _("bad uid/user name - %s"),
746                                                  optarg);
747                                         usage();
748                                 }
749                                 u_flag = 1;
750                                 open_flag = EXT2_FLAG_RW;
751                                 break;
752                         case 'U':
753                                 new_UUID = optarg;
754                                 U_flag = 1;
755                                 open_flag = EXT2_FLAG_RW |
756                                         EXT2_FLAG_JOURNAL_DEV_OK;
757                                 break;
758                         default:
759                                 usage();
760                 }
761         if (optind < argc - 1 || optind == argc)
762                 usage();
763         if (!open_flag && !l_flag)
764                 usage();
765         io_options = strchr(argv[optind], '?');
766         if (io_options)
767                 *io_options++ = 0;
768         device_name = blkid_get_devname(NULL, argv[optind], NULL);
769         if (!device_name) {
770                 com_err("tune2fs", 0, _("Unable to resolve '%s'"), 
771                         argv[optind]);
772                 exit(1);
773         }
774 }
775
776 void do_findfs(int argc, char **argv)
777 {
778         char    *dev;
779
780         if ((argc != 2) ||
781             (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
782                 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
783                 exit(2);
784         }
785         dev = blkid_get_devname(NULL, argv[1], NULL);
786         if (!dev) {
787                 com_err("findfs", 0, _("Unable to resolve '%s'"), 
788                         argv[1]);
789                 exit(1);
790         }
791         puts(dev);
792         exit(0);
793 }
794
795 /*
796  * Note!  If any extended options are incompatible with the
797  * intersection of the SOFTSUPP features and those features explicitly
798  * enabled for tune2fs, there needs to be an explicit test for them
799  * here.
800  */
801 static void parse_extended_opts(ext2_filsys fs, const char *opts)
802 {
803         char    *buf, *token, *next, *p, *arg;
804         int     len;
805         int     r_usage = 0;
806
807         len = strlen(opts);
808         buf = malloc(len+1);
809         if (!buf) {
810                 fprintf(stderr,
811                         _("Couldn't allocate memory to parse options!\n"));
812                 exit(1);
813         }
814         strcpy(buf, opts);
815         for (token = buf; token && *token; token = next) {
816                 p = strchr(token, ',');
817                 next = 0;
818                 if (p) {
819                         *p = 0;
820                         next = p+1;
821                 }
822                 arg = strchr(token, '=');
823                 if (arg) {
824                         *arg = 0;
825                         arg++;
826                 }
827                 if (!strcmp(token, "test_fs")) {
828                         fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
829                         printf("Setting test filesystem flag\n");
830                         ext2fs_mark_super_dirty(fs);
831                 } else if (!strcmp(token, "^test_fs")) {
832                         fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
833                         printf("Clearing test filesystem flag\n");
834                         ext2fs_mark_super_dirty(fs);
835                 } else if (strcmp(token, "stride") == 0) {
836                         if (!arg) {
837                                 r_usage++;
838                                 continue;
839                         }
840                         stride = strtoul(arg, &p, 0);
841                         if (*p || (stride == 0)) {
842                                 fprintf(stderr,
843                                        _("Invalid RAID stride: %s\n"),
844                                         arg);
845                                 r_usage++;
846                                 continue;
847                         }
848                         stride_set = 1;
849                 } else if (strcmp(token, "stripe-width") == 0 ||
850                            strcmp(token, "stripe_width") == 0) {
851                         if (!arg) {
852                                 r_usage++;
853                                 continue;
854                         }
855                         stripe_width = strtoul(arg, &p, 0);
856                         if (*p || (stripe_width == 0)) {
857                                 fprintf(stderr,
858                                         _("Invalid RAID stripe-width: %s\n"),
859                                         arg);
860                                 r_usage++;
861                                 continue;
862                         }
863                         stripe_width_set = 1;
864                 } else 
865                         r_usage++;
866         }
867         if (r_usage) {
868                 fprintf(stderr, _("\nBad options specified.\n\n"
869                         "Extended options are separated by commas, "
870                         "and may take an argument which\n"
871                         "\tis set off by an equals ('=') sign.\n\n"
872                         "Valid extended options are:\n"
873                         "\tstride=<RAID per-disk chunk size in blocks>\n"
874                         "\tstripe-width=<RAID stride*data disks in blocks>\n"
875                         "\ttest_fs\n"
876                         "\t^test_fs\n"));
877                 free(buf);
878                 exit(1);
879         }
880         free(buf);
881 }       
882
883
884 int main (int argc, char ** argv)
885 {
886         errcode_t retval;
887         ext2_filsys fs;
888         struct ext2_super_block *sb;
889         io_manager io_ptr;
890
891 #ifdef ENABLE_NLS
892         setlocale(LC_MESSAGES, "");
893         setlocale(LC_CTYPE, "");
894         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
895         textdomain(NLS_CAT_NAME);
896 #endif
897         if (argc && *argv)
898                 program_name = *argv;
899         add_error_table(&et_ext2_error_table);
900
901         if (strcmp(get_progname(argv[0]), "findfs") == 0)
902                 do_findfs(argc, argv);
903         if (strcmp(get_progname(argv[0]), "e2label") == 0)
904                 parse_e2label_options(argc, argv);
905         else
906                 parse_tune2fs_options(argc, argv);
907         
908 #ifdef CONFIG_TESTIO_DEBUG
909         io_ptr = test_io_manager;
910         test_io_backing_manager = unix_io_manager;
911 #else
912         io_ptr = unix_io_manager;
913 #endif
914         retval = ext2fs_open2(device_name, io_options, open_flag, 
915                               0, 0, io_ptr, &fs);
916         if (retval) {
917                 com_err (program_name, retval, _("while trying to open %s"),
918                          device_name);
919                 fprintf(stderr,
920                         _("Couldn't find valid filesystem superblock.\n"));
921                 exit(1);
922         }
923         sb = fs->super;
924         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
925         if ((sb->s_feature_incompat & !EXT2_TUNE2FS_INCOMPAT) ||
926             (sb->s_feature_ro_compat & !EXT2_TUNE2FS_RO_COMPAT)) {
927                 fprintf(stderr, 
928                         _("Filesystem %s has unsupported features enabled.\n"),
929                         device_name);
930                 exit(1);
931         }
932         if (print_label) {
933                 /* For e2label emulation */
934                 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
935                        sb->s_volume_name);
936                 remove_error_table(&et_ext2_error_table);
937                 exit(0);
938         }
939         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
940         if (retval) {
941                 com_err("ext2fs_check_if_mount", retval,
942                         _("while determining whether %s is mounted."),
943                         device_name);
944                 exit(1);
945         }
946         /* Normally we only need to write out the superblock */
947         fs->flags |= EXT2_FLAG_SUPER_ONLY;
948
949         if (c_flag) {
950                 sb->s_max_mnt_count = max_mount_count;
951                 ext2fs_mark_super_dirty(fs);
952                 printf (_("Setting maximal mount count to %d\n"),
953                         max_mount_count);
954         }
955         if (C_flag) {
956                 sb->s_mnt_count = mount_count;
957                 ext2fs_mark_super_dirty(fs);
958                 printf (_("Setting current mount count to %d\n"), mount_count);
959         }
960         if (e_flag) {
961                 sb->s_errors = errors;
962                 ext2fs_mark_super_dirty(fs);
963                 printf (_("Setting error behavior to %d\n"), errors);
964         }
965         if (g_flag) {
966                 sb->s_def_resgid = resgid;
967                 ext2fs_mark_super_dirty(fs);
968                 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
969         }
970         if (i_flag) {
971                 sb->s_checkinterval = interval;
972                 ext2fs_mark_super_dirty(fs);
973                 printf (_("Setting interval between checks to %lu seconds\n"), interval);
974         }
975         if (m_flag) {
976                 sb->s_r_blocks_count = e2p_percent(reserved_ratio,
977                                                    sb->s_blocks_count);
978                 ext2fs_mark_super_dirty(fs);
979                 printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
980                         reserved_ratio, sb->s_r_blocks_count);
981         }
982         if (r_flag) {
983                 if (reserved_blocks >= sb->s_blocks_count/2) {
984                         com_err (program_name, 0,
985                                  _("reserved blocks count is too big (%lu)"),
986                                  reserved_blocks);
987                         exit (1);
988                 }
989                 sb->s_r_blocks_count = reserved_blocks;
990                 ext2fs_mark_super_dirty(fs);
991                 printf (_("Setting reserved blocks count to %lu\n"),
992                         reserved_blocks);
993         }
994         if (s_flag == 1) {
995                 if (sb->s_feature_ro_compat &
996                     EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
997                         fputs(_("\nThe filesystem already has sparse "
998                                 "superblocks.\n"), stderr);
999                 else {
1000                         sb->s_feature_ro_compat |=
1001                                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1002                         sb->s_state &= ~EXT2_VALID_FS;
1003                         ext2fs_mark_super_dirty(fs);
1004                         printf(_("\nSparse superblock flag set.  %s"),
1005                                _(please_fsck));
1006                 }
1007         }
1008         if (s_flag == 0) {
1009                 fputs(_("\nClearing the sparse superflag not supported.\n"),
1010                       stderr);
1011                 exit(1);
1012         }
1013         if (T_flag) {
1014                 sb->s_lastcheck = last_check_time;
1015                 ext2fs_mark_super_dirty(fs);
1016                 printf(_("Setting time filesystem last checked to %s\n"),
1017                        ctime(&last_check_time));
1018         }
1019         if (u_flag) {
1020                 sb->s_def_resuid = resuid;
1021                 ext2fs_mark_super_dirty(fs);
1022                 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
1023         }
1024         if (L_flag) {
1025                 if (strlen(new_label) > sizeof(sb->s_volume_name))
1026                         fputs(_("Warning: label too long, truncating.\n"), 
1027                               stderr);
1028                 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1029                 strncpy(sb->s_volume_name, new_label,
1030                         sizeof(sb->s_volume_name));
1031                 ext2fs_mark_super_dirty(fs);
1032         }
1033         if (M_flag) {
1034                 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1035                 strncpy(sb->s_last_mounted, new_last_mounted,
1036                         sizeof(sb->s_last_mounted));
1037                 ext2fs_mark_super_dirty(fs);
1038         }
1039         if (mntopts_cmd)
1040                 update_mntopts(fs, mntopts_cmd);
1041         if (features_cmd)
1042                 update_feature_set(fs, features_cmd);
1043         if (extended_cmd)
1044                 parse_extended_opts(fs, extended_cmd);
1045         if (journal_size || journal_device)
1046                 add_journal(fs);
1047         
1048         if (U_flag) {
1049                 if ((strcasecmp(new_UUID, "null") == 0) ||
1050                     (strcasecmp(new_UUID, "clear") == 0)) {
1051                         uuid_clear(sb->s_uuid);
1052                 } else if (strcasecmp(new_UUID, "time") == 0) {
1053                         uuid_generate_time(sb->s_uuid);
1054                 } else if (strcasecmp(new_UUID, "random") == 0) {
1055                         uuid_generate(sb->s_uuid);
1056                 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1057                         com_err(program_name, 0, _("Invalid UUID format\n"));
1058                         exit(1);
1059                 }
1060                 ext2fs_mark_super_dirty(fs);
1061         }
1062
1063         if (l_flag)
1064                 list_super (sb);
1065         if (stride_set) {
1066                 sb->s_raid_stride = stride;
1067                 ext2fs_mark_super_dirty(fs);
1068                 printf(_("Setting stride size to %d\n"), stride);
1069         }
1070         if (stripe_width_set) {
1071                 sb->s_raid_stripe_width = stripe_width;
1072                 ext2fs_mark_super_dirty(fs);
1073                 printf(_("Setting stripe width to %d\n"), stripe_width);
1074         }
1075         remove_error_table(&et_ext2_error_table);
1076         return (ext2fs_close (fs) ? 1 : 0);
1077 }