Whamcloud - gitweb
527991ae4eb1bb02d149a7b042ca22a62a101046
[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 #include <libgen.h>
46
47 #include "ext2fs/ext2_fs.h"
48 #include "ext2fs/ext2fs.h"
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
51 #include "e2p/e2p.h"
52 #include "jfs_user.h"
53 #include "util.h"
54 #include "blkid/blkid.h"
55
56 #include "../version.h"
57 #include "nls-enable.h"
58
59 /* 
60  * Tune2fs supports these features in addition to the standard features.
61  */
62 #define EXT2_TUNE2FS_INCOMPAT   (EXT3_FEATURE_INCOMPAT_EXTENTS)
63 #define EXT2_TUNE2FS_RO_COMPAT  (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|\
64                                  EXT4_FEATURE_RO_COMPAT_GDT_CSUM|       \
65                                  EXT4_FEATURE_RO_COMPAT_DIR_NLINK|      \
66                                  EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)
67
68
69 const char * program_name = "tune2fs";
70 char * device_name;
71 char * new_label, *new_last_mounted, *new_UUID;
72 char * io_options;
73 static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
74 static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
75 static int I_flag;
76 static time_t last_check_time;
77 static int print_label;
78 static int max_mount_count, mount_count, mount_flags;
79 static unsigned long interval, reserved_blocks;
80 static double reserved_ratio;
81 static unsigned long resgid, resuid;
82 static unsigned short errors;
83 static int open_flag;
84 static char *features_cmd;
85 static char *mntopts_cmd;
86 static int stride, stripe_width;
87 static int stride_set, stripe_width_set;
88 static char *extended_cmd;
89 static unsigned long int new_inode_size;
90
91 int journal_size, journal_flags;
92 char *journal_device;
93
94 static struct list_head blk_move_list;
95
96 struct blk_move {
97         struct list_head list;
98         blk_t old_loc;
99         blk_t new_loc;
100 };
101
102
103 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
104
105 void do_findfs(int argc, char **argv);
106
107 static void usage(void)
108 {
109         fprintf(stderr,
110                 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
111                   "[-g group]\n"
112                   "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
113                   "\t[-m reserved_blocks_percent] "
114                   "[-o [^]mount_options[,...]] \n"
115                   "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
116                   "[-L volume_label]\n"
117                   "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
118                   "\t[-E extended-option[,...]] [-T last_check_time] "
119                   "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
120         exit (1);
121 }
122
123 static __u32 ok_features[3] = {
124         /* Compat */
125         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
126                 EXT2_FEATURE_COMPAT_DIR_INDEX,
127         /* Incompat */
128         EXT2_FEATURE_INCOMPAT_FILETYPE |
129                 EXT3_FEATURE_INCOMPAT_EXTENTS |
130                 EXT4_FEATURE_INCOMPAT_FLEX_BG,
131         /* R/O compat */
132         EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
133                 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
134                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
135 };
136
137 static __u32 clear_ok_features[3] = {
138         /* Compat */
139         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
140                 EXT2_FEATURE_COMPAT_RESIZE_INODE |
141                 EXT2_FEATURE_COMPAT_DIR_INDEX,
142         /* Incompat */
143         EXT2_FEATURE_INCOMPAT_FILETYPE |
144                 EXT4_FEATURE_INCOMPAT_FLEX_BG,
145         /* R/O compat */
146         EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
147                 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
148 };
149
150 /*
151  * Remove an external journal from the filesystem
152  */
153 static void remove_journal_device(ext2_filsys fs)
154 {
155         char            *journal_path;
156         ext2_filsys     jfs;
157         char            buf[1024];
158         journal_superblock_t    *jsb;
159         int             i, nr_users;
160         errcode_t       retval;
161         int             commit_remove_journal = 0;
162         io_manager      io_ptr;
163
164         if (f_flag)
165                 commit_remove_journal = 1; /* force removal even if error */
166
167         uuid_unparse(fs->super->s_journal_uuid, buf);
168         journal_path = blkid_get_devname(NULL, "UUID", buf);
169
170         if (!journal_path) {
171                 journal_path =
172                         ext2fs_find_block_device(fs->super->s_journal_dev);
173                 if (!journal_path)
174                         return;
175         }
176
177 #ifdef CONFIG_TESTIO_DEBUG
178         io_ptr = test_io_manager;
179         test_io_backing_manager = unix_io_manager;
180 #else
181         io_ptr = unix_io_manager;
182 #endif
183         retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
184                              EXT2_FLAG_JOURNAL_DEV_OK, 0,
185                              fs->blocksize, io_ptr, &jfs);
186         if (retval) {
187                 com_err(program_name, retval,
188                         _("while trying to open external journal"));
189                 goto no_valid_journal;
190         }
191         if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
192                 fprintf(stderr, _("%s is not a journal device.\n"),
193                         journal_path);
194                 goto no_valid_journal;
195         }
196
197         /* Get the journal superblock */
198         if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
199                 com_err(program_name, retval,
200                         _("while reading journal superblock"));
201                 goto no_valid_journal;
202         }
203
204         jsb = (journal_superblock_t *) buf;
205         if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
206             (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
207                 fputs(_("Journal superblock not found!\n"), stderr);
208                 goto no_valid_journal;
209         }
210
211         /* Find the filesystem UUID */
212         nr_users = ntohl(jsb->s_nr_users);
213         for (i=0; i < nr_users; i++) {
214                 if (memcmp(fs->super->s_uuid,
215                            &jsb->s_users[i*16], 16) == 0)
216                         break;
217         }
218         if (i >= nr_users) {
219                 fputs(_("Filesystem's UUID not found on journal device.\n"), 
220                       stderr);
221                 commit_remove_journal = 1;
222                 goto no_valid_journal;
223         }
224         nr_users--;
225         for (i=0; i < nr_users; i++)
226                 memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
227         jsb->s_nr_users = htonl(nr_users);
228
229         /* Write back the journal superblock */
230         if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
231                 com_err(program_name, retval,
232                         "while writing journal superblock.");
233                 goto no_valid_journal;
234         }
235
236         commit_remove_journal = 1;
237
238 no_valid_journal:
239         if (commit_remove_journal == 0) {
240                 fputs(_("Journal NOT removed\n"), stderr);
241                 exit(1);
242         }
243         fs->super->s_journal_dev = 0;
244         uuid_clear(fs->super->s_journal_uuid);
245         ext2fs_mark_super_dirty(fs);
246         fputs(_("Journal removed\n"), stdout);
247         free(journal_path);
248 }
249
250 /* Helper function for remove_journal_inode */
251 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
252                                int blockcnt EXT2FS_ATTR((unused)), 
253                                void *private EXT2FS_ATTR((unused)))
254 {
255         blk_t   block;
256         int     group;
257
258         block = *blocknr;
259         ext2fs_unmark_block_bitmap(fs->block_map,block);
260         group = ext2fs_group_of_blk(fs, block);
261         fs->group_desc[group].bg_free_blocks_count++;
262         ext2fs_group_desc_csum_set(fs, group);
263         fs->super->s_free_blocks_count++;
264         return 0;
265 }
266
267 /*
268  * Remove the journal inode from the filesystem
269  */
270 static void remove_journal_inode(ext2_filsys fs)
271 {
272         struct ext2_inode       inode;
273         errcode_t               retval;
274         ino_t                   ino = fs->super->s_journal_inum;
275         
276         retval = ext2fs_read_inode(fs, ino,  &inode);
277         if (retval) {
278                 com_err(program_name, retval,
279                         _("while reading journal inode"));
280                 exit(1);
281         }
282         if (ino == EXT2_JOURNAL_INO) {
283                 retval = ext2fs_read_bitmaps(fs);
284                 if (retval) {
285                         com_err(program_name, retval,
286                                 _("while reading bitmaps"));
287                         exit(1);
288                 }
289                 retval = ext2fs_block_iterate(fs, ino,
290                                               BLOCK_FLAG_READ_ONLY, NULL,
291                                               release_blocks_proc, NULL);
292                 if (retval) {
293                         com_err(program_name, retval,
294                                 _("while clearing journal inode"));
295                         exit(1);
296                 }
297                 memset(&inode, 0, sizeof(inode));
298                 ext2fs_mark_bb_dirty(fs);
299                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
300         } else
301                 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
302         retval = ext2fs_write_inode(fs, ino, &inode);
303         if (retval) {
304                 com_err(program_name, retval,
305                         _("while writing journal inode"));
306                 exit(1);
307         }
308         fs->super->s_journal_inum = 0;
309         ext2fs_mark_super_dirty(fs);
310 }
311
312 /*
313  * Update the default mount options
314  */
315 static void update_mntopts(ext2_filsys fs, char *mntopts)
316 {
317         struct ext2_super_block *sb= fs->super;
318
319         if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
320                 fprintf(stderr, _("Invalid mount option set: %s\n"),
321                         mntopts);
322                 exit(1);
323         }
324         ext2fs_mark_super_dirty(fs);
325 }
326
327 /*
328  * Update the feature set as provided by the user.
329  */
330 static void update_feature_set(ext2_filsys fs, char *features)
331 {
332         struct ext2_super_block *sb= fs->super;
333         __u32           old_features[3];
334         int             type_err;
335         unsigned int    mask_err;
336
337 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
338                                 ((&sb->s_feature_compat)[(type)] & (mask)))
339 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
340                                  !((&sb->s_feature_compat)[(type)] & (mask)))
341 #define FEATURE_CHANGED(type, mask) ((mask) & \
342                      (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
343
344         old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
345         old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
346         old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
347
348         if (e2p_edit_feature2(features, &sb->s_feature_compat,
349                               ok_features, clear_ok_features,
350                               &type_err, &mask_err)) {
351                 if (!mask_err)
352                         fprintf(stderr,
353                                 _("Invalid filesystem option set: %s\n"),
354                                 features);
355                 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
356                         fprintf(stderr, _("Clearing filesystem feature '%s' "
357                                           "not supported.\n"),
358                                 e2p_feature2string(type_err &
359                                                    E2P_FEATURE_TYPE_MASK,
360                                                    mask_err));
361                 else
362                         fprintf(stderr, _("Setting filesystem feature '%s' "
363                                           "not supported.\n"),
364                                 e2p_feature2string(type_err, mask_err));
365                 exit(1);
366         }
367
368         if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
369                 if ((mount_flags & EXT2_MF_MOUNTED) &&
370                     !(mount_flags & EXT2_MF_READONLY)) {
371                         fputs(_("The has_journal flag may only be "
372                                 "cleared when the filesystem is\n"
373                                 "unmounted or mounted "
374                                 "read-only.\n"), stderr);
375                         exit(1);
376                 }
377                 if (sb->s_feature_incompat &
378                     EXT3_FEATURE_INCOMPAT_RECOVER) {
379                         fputs(_("The needs_recovery flag is set.  "
380                                 "Please run e2fsck before clearing\n"
381                                 "the has_journal flag.\n"), stderr);
382                         exit(1);
383                 }
384                 if (sb->s_journal_inum) {
385                         remove_journal_inode(fs);
386                 }
387                 if (sb->s_journal_dev) {
388                         remove_journal_device(fs);
389                 }
390         }
391
392         if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
393                 /*
394                  * If adding a journal flag, let the create journal
395                  * code below handle creating setting the flag and
396                  * creating the journal.  We supply a default size if
397                  * necessary.
398                  */
399                 if (!journal_size)
400                         journal_size = -1;
401                 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
402         }
403
404         if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
405                 if (!sb->s_def_hash_version)
406                         sb->s_def_hash_version = EXT2_HASH_TEA;
407                 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
408                         uuid_generate((unsigned char *) sb->s_hash_seed);
409         }
410
411         if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
412                 if (ext2fs_check_desc(fs)) {
413                         fputs(_("Clearing the flex_bg flag would "
414                                 "cause the the filesystem to be\n"
415                                 "inconsistent.\n"), stderr);
416                         exit(1);
417                 }
418         }
419
420         if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
421             (sb->s_feature_compat || sb->s_feature_ro_compat ||
422              sb->s_feature_incompat))
423                 ext2fs_update_dynamic_rev(fs);
424
425         if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
426                             EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
427             FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
428                             EXT4_FEATURE_RO_COMPAT_GDT_CSUM) ||
429             FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
430                             EXT2_FEATURE_INCOMPAT_FILETYPE) ||
431             FEATURE_CHANGED(E2P_FEATURE_COMPAT,
432                             EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
433             FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
434                         EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
435                 sb->s_state &= ~EXT2_VALID_FS;
436                 printf("\n%s\n", _(please_fsck));
437         }
438
439         if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
440             (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
441             (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
442                 ext2fs_mark_super_dirty(fs);
443 }
444
445 /*
446  * Add a journal to the filesystem.
447  */
448 static void add_journal(ext2_filsys fs)
449 {
450         unsigned long journal_blocks;
451         errcode_t       retval;
452         ext2_filsys     jfs;
453         io_manager      io_ptr;
454
455         if (fs->super->s_feature_compat &
456             EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
457                 fputs(_("The filesystem already has a journal.\n"), stderr);
458                 goto err;
459         }
460         if (journal_device) {
461                 check_plausibility(journal_device);
462                 check_mount(journal_device, 0, _("journal"));
463 #ifdef CONFIG_TESTIO_DEBUG
464                 io_ptr = test_io_manager;
465                 test_io_backing_manager = unix_io_manager;
466 #else
467                 io_ptr = unix_io_manager;
468 #endif
469                 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
470                                      EXT2_FLAG_JOURNAL_DEV_OK, 0,
471                                      fs->blocksize, io_ptr, &jfs);
472                 if (retval) {
473                         com_err(program_name, retval,
474                                 _("\n\twhile trying to open journal on %s\n"),
475                                 journal_device);
476                         goto err;
477                 }
478                 printf(_("Creating journal on device %s: "),
479                        journal_device);
480                 fflush(stdout);
481
482                 retval = ext2fs_add_journal_device(fs, jfs);
483                 ext2fs_close(jfs);
484                 if (retval) {
485                         com_err (program_name, retval,
486                                  _("while adding filesystem to journal on %s"),
487                                  journal_device);
488                         goto err;
489                 }
490                 fputs(_("done\n"), stdout);
491         } else if (journal_size) {
492                 fputs(_("Creating journal inode: "), stdout);
493                 fflush(stdout);
494                 journal_blocks = figure_journal_size(journal_size, fs);
495
496                 retval = ext2fs_add_journal_inode(fs, journal_blocks,
497                                                   journal_flags);
498                 if (retval) {
499                         fprintf(stderr, "\n");
500                         com_err(program_name, retval,
501                                 _("\n\twhile trying to create journal file"));
502                         exit(1);
503                 } else
504                         fputs(_("done\n"), stdout);
505                 /*
506                  * If the filesystem wasn't mounted, we need to force
507                  * the block group descriptors out.
508                  */
509                 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
510                         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
511         }
512         print_check_message(fs);
513         return;
514
515 err:
516         if (journal_device)
517                 free(journal_device);
518         exit(1);
519 }
520
521
522 static void parse_e2label_options(int argc, char ** argv)
523 {
524         if ((argc < 2) || (argc > 3)) {
525                 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
526                 exit(1);
527         }
528         io_options = strchr(argv[1], '?');
529         if (io_options)
530                 *io_options++ = 0;
531         device_name = blkid_get_devname(NULL, argv[1], NULL);
532         if (!device_name) {
533                 com_err("e2label", 0, _("Unable to resolve '%s'"), 
534                         argv[1]);
535                 exit(1);
536         }
537         open_flag = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_JOURNAL_DEV_OK;
538         if (argc == 3) {
539                 open_flag |= EXT2_FLAG_RW;
540                 L_flag = 1;
541                 new_label = argv[2];
542         } else 
543                 print_label++;
544 }
545
546 static time_t parse_time(char *str)
547 {
548         struct  tm      ts;
549
550         if (strcmp(str, "now") == 0) {
551                 return (time(0));
552         }
553         memset(&ts, 0, sizeof(ts));
554 #ifdef HAVE_STRPTIME
555         strptime(str, "%Y%m%d%H%M%S", &ts);
556 #else
557         sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
558                &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
559         ts.tm_year -= 1900;
560         ts.tm_mon -= 1;
561         if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
562             ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
563             ts.tm_min > 59 || ts.tm_sec > 61)
564                 ts.tm_mday = 0;
565 #endif
566         if (ts.tm_mday == 0) {
567                 com_err(program_name, 0,
568                         _("Couldn't parse date/time specifier: %s"),
569                         str);
570                 usage();
571         }
572         ts.tm_isdst = -1;
573         return (mktime(&ts));
574 }
575
576 static void parse_tune2fs_options(int argc, char **argv)
577 {
578         int c;
579         char * tmp;
580         struct group * gr;
581         struct passwd * pw;
582
583         open_flag = EXT2_FLAG_SOFTSUPP_FEATURES;
584
585         printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
586         while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:T:U:")) != EOF)
587                 switch (c)
588                 {
589                         case 'c':
590                                 max_mount_count = strtol (optarg, &tmp, 0);
591                                 if (*tmp || max_mount_count > 16000) {
592                                         com_err (program_name, 0,
593                                                  _("bad mounts count - %s"),
594                                                  optarg);
595                                         usage();
596                                 }
597                                 if (max_mount_count == 0)
598                                         max_mount_count = -1;
599                                 c_flag = 1;
600                                 open_flag = EXT2_FLAG_RW;
601                                 break;
602                         case 'C':
603                                 mount_count = strtoul (optarg, &tmp, 0);
604                                 if (*tmp || mount_count > 16000) {
605                                         com_err (program_name, 0,
606                                                  _("bad mounts count - %s"),
607                                                  optarg);
608                                         usage();
609                                 }
610                                 C_flag = 1;
611                                 open_flag = EXT2_FLAG_RW;
612                                 break;
613                         case 'e':
614                                 if (strcmp (optarg, "continue") == 0)
615                                         errors = EXT2_ERRORS_CONTINUE;
616                                 else if (strcmp (optarg, "remount-ro") == 0)
617                                         errors = EXT2_ERRORS_RO;
618                                 else if (strcmp (optarg, "panic") == 0)
619                                         errors = EXT2_ERRORS_PANIC;
620                                 else {
621                                         com_err (program_name, 0,
622                                                  _("bad error behavior - %s"),
623                                                  optarg);
624                                         usage();
625                                 }
626                                 e_flag = 1;
627                                 open_flag = EXT2_FLAG_RW;
628                                 break;
629                         case 'E':
630                                 extended_cmd = optarg;
631                                 open_flag |= EXT2_FLAG_RW;
632                                 break;
633                         case 'f': /* Force */
634                                 f_flag = 1;
635                                 break;
636                         case 'g':
637                                 resgid = strtoul (optarg, &tmp, 0);
638                                 if (*tmp) {
639                                         gr = getgrnam (optarg);
640                                         if (gr == NULL)
641                                                 tmp = optarg;
642                                         else {
643                                                 resgid = gr->gr_gid;
644                                                 *tmp =0;
645                                         }
646                                 }
647                                 if (*tmp) {
648                                         com_err (program_name, 0,
649                                                  _("bad gid/group name - %s"),
650                                                  optarg);
651                                         usage();
652                                 }
653                                 g_flag = 1;
654                                 open_flag = EXT2_FLAG_RW;
655                                 break;
656                         case 'i':
657                                 interval = strtoul (optarg, &tmp, 0);
658                                 switch (*tmp) {
659                                 case 's':
660                                         tmp++;
661                                         break;
662                                 case '\0':
663                                 case 'd':
664                                 case 'D': /* days */
665                                         interval *= 86400;
666                                         if (*tmp != '\0')
667                                                 tmp++;
668                                         break;
669                                 case 'm':
670                                 case 'M': /* months! */
671                                         interval *= 86400 * 30;
672                                         tmp++;
673                                         break;
674                                 case 'w':
675                                 case 'W': /* weeks */
676                                         interval *= 86400 * 7;
677                                         tmp++;
678                                         break;
679                                 }
680                                 if (*tmp) {
681                                         com_err (program_name, 0,
682                                                 _("bad interval - %s"), optarg);
683                                         usage();
684                                 }
685                                 i_flag = 1;
686                                 open_flag = EXT2_FLAG_RW;
687                                 break;
688                         case 'j':
689                                 if (!journal_size)
690                                         journal_size = -1;
691                                 open_flag = EXT2_FLAG_RW;
692                                 break;
693                         case 'J':
694                                 parse_journal_opts(optarg);
695                                 open_flag = EXT2_FLAG_RW;
696                                 break;
697                         case 'l':
698                                 l_flag = 1;
699                                 break;
700                         case 'L':
701                                 new_label = optarg;
702                                 L_flag = 1;
703                                 open_flag |= EXT2_FLAG_RW |
704                                         EXT2_FLAG_JOURNAL_DEV_OK;
705                                 break;
706                         case 'm':
707                                 reserved_ratio = strtod(optarg, &tmp);
708                                 if (*tmp || reserved_ratio > 50) {
709                                         com_err (program_name, 0,
710                                                  _("bad reserved block ratio - %s"),
711                                                  optarg);
712                                         usage();
713                                 }
714                                 m_flag = 1;
715                                 open_flag = EXT2_FLAG_RW;
716                                 break;
717                         case 'M':
718                                 new_last_mounted = optarg;
719                                 M_flag = 1;
720                                 open_flag = EXT2_FLAG_RW;
721                                 break;
722                         case 'o':
723                                 if (mntopts_cmd) {
724                                         com_err (program_name, 0,
725                                          _("-o may only be specified once"));
726                                         usage();
727                                 }
728                                 mntopts_cmd = optarg;
729                                 open_flag = EXT2_FLAG_RW;
730                                 break;
731                                 
732                         case 'O':
733                                 if (features_cmd) {
734                                         com_err (program_name, 0,
735                                          _("-O may only be specified once"));
736                                         usage();
737                                 }
738                                 features_cmd = optarg;
739                                 open_flag = EXT2_FLAG_RW;
740                                 break;
741                         case 'r':
742                                 reserved_blocks = strtoul (optarg, &tmp, 0);
743                                 if (*tmp) {
744                                         com_err (program_name, 0,
745                                                  _("bad reserved blocks count - %s"),
746                                                  optarg);
747                                         usage();
748                                 }
749                                 r_flag = 1;
750                                 open_flag = EXT2_FLAG_RW;
751                                 break;
752                         case 's': /* Deprecated */
753                                 s_flag = atoi(optarg);
754                                 open_flag = EXT2_FLAG_RW;
755                                 break;
756                         case 'T':
757                                 T_flag = 1;
758                                 last_check_time = parse_time(optarg);
759                                 open_flag = EXT2_FLAG_RW;
760                                 break;
761                         case 'u':
762                                 resuid = strtoul (optarg, &tmp, 0);
763                                 if (*tmp) {
764                                         pw = getpwnam (optarg);
765                                         if (pw == NULL)
766                                                 tmp = optarg;
767                                         else {
768                                                 resuid = pw->pw_uid;
769                                                 *tmp = 0;
770                                         }
771                                 }
772                                 if (*tmp) {
773                                         com_err (program_name, 0,
774                                                  _("bad uid/user name - %s"),
775                                                  optarg);
776                                         usage();
777                                 }
778                                 u_flag = 1;
779                                 open_flag = EXT2_FLAG_RW;
780                                 break;
781                         case 'U':
782                                 new_UUID = optarg;
783                                 U_flag = 1;
784                                 open_flag = EXT2_FLAG_RW |
785                                         EXT2_FLAG_JOURNAL_DEV_OK;
786                                 break;
787                         case 'I':
788                                 new_inode_size = strtoul (optarg, &tmp, 0);
789                                 if (*tmp) {
790                                         com_err (program_name, 0,
791                                                 _("bad inode size - %s"),
792                                                         optarg);
793                                         usage();
794                                 }
795                                 if (!((new_inode_size &
796                                                 (new_inode_size - 1)) == 0)) {
797                                         com_err (program_name, 0,
798                                                 _("Inode size must be a "
799                                                 "power of two- %s"),
800                                                         optarg);
801                                         usage();
802                                 }
803                                 open_flag = EXT2_FLAG_RW;
804                                 I_flag = 1;
805                                 break;
806                         default:
807                                 usage();
808                 }
809         if (optind < argc - 1 || optind == argc)
810                 usage();
811         if (!open_flag && !l_flag)
812                 usage();
813         io_options = strchr(argv[optind], '?');
814         if (io_options)
815                 *io_options++ = 0;
816         device_name = blkid_get_devname(NULL, argv[optind], NULL);
817         if (!device_name) {
818                 com_err("tune2fs", 0, _("Unable to resolve '%s'"), 
819                         argv[optind]);
820                 exit(1);
821         }
822 }
823
824 void do_findfs(int argc, char **argv)
825 {
826         char    *dev;
827
828         if ((argc != 2) ||
829             (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
830                 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
831                 exit(2);
832         }
833         dev = blkid_get_devname(NULL, argv[1], NULL);
834         if (!dev) {
835                 com_err("findfs", 0, _("Unable to resolve '%s'"), 
836                         argv[1]);
837                 exit(1);
838         }
839         puts(dev);
840         exit(0);
841 }
842
843 /*
844  * Note!  If any extended options are incompatible with the
845  * intersection of the SOFTSUPP features and those features explicitly
846  * enabled for tune2fs, there needs to be an explicit test for them
847  * here.
848  */
849 static void parse_extended_opts(ext2_filsys fs, const char *opts)
850 {
851         char    *buf, *token, *next, *p, *arg;
852         int     len;
853         int     r_usage = 0;
854
855         len = strlen(opts);
856         buf = malloc(len+1);
857         if (!buf) {
858                 fprintf(stderr,
859                         _("Couldn't allocate memory to parse options!\n"));
860                 exit(1);
861         }
862         strcpy(buf, opts);
863         for (token = buf; token && *token; token = next) {
864                 p = strchr(token, ',');
865                 next = 0;
866                 if (p) {
867                         *p = 0;
868                         next = p+1;
869                 }
870                 arg = strchr(token, '=');
871                 if (arg) {
872                         *arg = 0;
873                         arg++;
874                 }
875                 if (!strcmp(token, "test_fs")) {
876                         fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
877                         printf("Setting test filesystem flag\n");
878                         ext2fs_mark_super_dirty(fs);
879                 } else if (!strcmp(token, "^test_fs")) {
880                         fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
881                         printf("Clearing test filesystem flag\n");
882                         ext2fs_mark_super_dirty(fs);
883                 } else if (strcmp(token, "stride") == 0) {
884                         if (!arg) {
885                                 r_usage++;
886                                 continue;
887                         }
888                         stride = strtoul(arg, &p, 0);
889                         if (*p || (stride == 0)) {
890                                 fprintf(stderr,
891                                        _("Invalid RAID stride: %s\n"),
892                                         arg);
893                                 r_usage++;
894                                 continue;
895                         }
896                         stride_set = 1;
897                 } else if (strcmp(token, "stripe-width") == 0 ||
898                            strcmp(token, "stripe_width") == 0) {
899                         if (!arg) {
900                                 r_usage++;
901                                 continue;
902                         }
903                         stripe_width = strtoul(arg, &p, 0);
904                         if (*p || (stripe_width == 0)) {
905                                 fprintf(stderr,
906                                         _("Invalid RAID stripe-width: %s\n"),
907                                         arg);
908                                 r_usage++;
909                                 continue;
910                         }
911                         stripe_width_set = 1;
912                 } else 
913                         r_usage++;
914         }
915         if (r_usage) {
916                 fprintf(stderr, _("\nBad options specified.\n\n"
917                         "Extended options are separated by commas, "
918                         "and may take an argument which\n"
919                         "\tis set off by an equals ('=') sign.\n\n"
920                         "Valid extended options are:\n"
921                         "\tstride=<RAID per-disk chunk size in blocks>\n"
922                         "\tstripe-width=<RAID stride*data disks in blocks>\n"
923                         "\ttest_fs\n"
924                         "\t^test_fs\n"));
925                 free(buf);
926                 exit(1);
927         }
928         free(buf);
929 }       
930
931 static int get_move_bitmap(ext2_filsys fs, int new_ino_blks_per_grp,
932                                         ext2fs_block_bitmap bmap)
933 {
934         dgrp_t i;
935         blk_t j, needed_blocks = 0;
936         blk_t start_blk, end_blk;
937
938         for (i = 0; i < fs->group_desc_count; i++) {
939
940                 start_blk = fs->group_desc[i].bg_inode_table +
941                                         fs->inode_blocks_per_group;
942
943                 end_blk = fs->group_desc[i].bg_inode_table +
944                                         new_ino_blks_per_grp;
945
946                 for (j = start_blk; j < end_blk; j++) {
947
948                         if (ext2fs_test_block_bitmap(fs->block_map, j)) {
949                                 /* FIXME!!
950                                  * What happens if the block is marked
951                                  * as a bad block
952                                  */
953                                 ext2fs_mark_block_bitmap(bmap, j);
954                                 needed_blocks++;
955                         } else {
956                                 /*
957                                  * We are going to use this block for
958                                  * inode table. So mark them used.
959                                  */
960                                 ext2fs_mark_block_bitmap(fs->block_map, j);
961                         }
962                 }
963         }
964
965         if (needed_blocks > fs->super->s_free_blocks_count ) {
966                 return ENOSPC;
967         }
968
969         return 0;
970 }
971
972 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
973 {
974         char *buf;
975         errcode_t retval;
976         blk_t blk, new_blk;
977         struct blk_move *bmv;
978
979
980         retval = ext2fs_get_mem(fs->blocksize, &buf);
981         if (retval)
982                 return retval;
983
984         for (blk = fs->super->s_first_data_block;
985                         blk < fs->super->s_blocks_count; blk++) {
986
987                 if (!ext2fs_test_block_bitmap(bmap, blk))
988                         continue;
989
990                 retval = ext2fs_new_block(fs, blk, NULL, &new_blk);
991                 if (retval)
992                         goto err_out;
993
994                 /* Mark this block as allocated */
995                 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
996
997                 /* Add it to block move list */
998                 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
999                 if (retval)
1000                         goto err_out;
1001
1002                 bmv->old_loc = blk;
1003                 bmv->new_loc = new_blk;
1004
1005                 list_add(&(bmv->list), &blk_move_list);
1006
1007                 retval = io_channel_read_blk(fs->io, blk, 1, buf);
1008                 if (retval)
1009                         goto err_out;
1010
1011                 retval = io_channel_write_blk(fs->io, new_blk, 1, buf);
1012                 if (retval)
1013                         goto err_out;
1014         }
1015
1016 err_out:
1017         ext2fs_free_mem(&buf);
1018         return retval;
1019 }
1020
1021 static blk_t transalate_block(blk_t blk)
1022 {
1023         struct list_head *entry;
1024         struct blk_move *bmv;
1025
1026         list_for_each(entry, &blk_move_list) {
1027
1028                 bmv = list_entry(entry, struct blk_move, list);
1029                 if (bmv->old_loc == blk)
1030                         return bmv->new_loc;
1031         }
1032
1033         return 0;
1034 }
1035
1036 static int process_block(ext2_filsys fs, blk_t  *block_nr,
1037                          e2_blkcnt_t blockcnt,
1038                          blk_t ref_block EXT2FS_ATTR((unused)),
1039                          int ref_offset EXT2FS_ATTR((unused)),
1040                          void *priv_data EXT2FS_ATTR((unused)))
1041 {
1042         int ret = 0;
1043         blk_t new_blk;
1044
1045
1046         new_blk = transalate_block(*block_nr);
1047         if (new_blk) {
1048                 *block_nr = new_blk;
1049                 /*
1050                  * This will force the ext2fs_write_inode in the iterator
1051                  */
1052                 ret |= BLOCK_CHANGED;
1053         }
1054
1055         return ret;
1056 }
1057
1058 static int inode_scan_and_fix(ext2_filsys fs)
1059 {
1060         errcode_t retval = 0;
1061         ext2_ino_t ino;
1062         blk_t blk;
1063         char *block_buf = 0;
1064         struct ext2_inode inode;
1065         ext2_inode_scan scan = NULL;
1066
1067         retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1068         if (retval)
1069                 return retval;
1070
1071         retval = ext2fs_open_inode_scan(fs, 0, &scan);
1072         if (retval)
1073                 goto err_out;
1074
1075         while (1) {
1076
1077                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1078                 if (retval)
1079                         goto err_out;
1080
1081                 if (!ino)
1082                         break;
1083
1084                 if (inode.i_links_count == 0)
1085                         continue; /* inode not in use */
1086
1087                 /* FIXME!!
1088                  * If we end up modifying the journal inode
1089                  * the sb->s_jnl_blocks will differ. But a
1090                  * subsequent e2fsck fixes that.
1091                  * Do we need to fix this ??
1092                  */
1093
1094                 if (inode.i_file_acl) {
1095
1096                         blk = transalate_block(inode.i_file_acl);
1097                         if (!blk)
1098                                 continue;
1099
1100                         inode.i_file_acl = blk;
1101
1102                         /*
1103                          * Write the inode to disk so that inode table
1104                          * resizing can work
1105                          */
1106                         retval = ext2fs_write_inode(fs, ino, &inode);
1107                         if (retval)
1108                                 goto err_out;
1109                 }
1110
1111                 if (!ext2fs_inode_has_valid_blocks(&inode))
1112                         continue;
1113
1114                 retval = ext2fs_block_iterate2(fs, ino, 0,
1115                                                 block_buf, process_block,
1116                                                 0);
1117                 if (retval)
1118                         goto err_out;
1119
1120         }
1121
1122 err_out:
1123         ext2fs_free_mem(&block_buf);
1124
1125         return retval;
1126
1127 }
1128
1129 static int expand_inode_table(ext2_filsys fs, unsigned long int new_inode_size)
1130 {
1131         dgrp_t i;
1132         blk_t blk;
1133         errcode_t retval;
1134         int new_ino_blks_per_grp, j;
1135         char *old_itable = NULL, *new_itable = NULL;
1136         char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1137         unsigned long int old_inode_size;
1138         int old_itable_size, new_itable_size;
1139
1140         old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1141         old_inode_size = EXT2_INODE_SIZE(fs->super);
1142
1143         new_ino_blks_per_grp = ext2fs_div_ceil(
1144                                         EXT2_INODES_PER_GROUP(fs->super) *
1145                                         new_inode_size,
1146                                         fs->blocksize);
1147
1148         new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1149
1150         retval = ext2fs_get_mem(old_itable_size, &old_itable);
1151         if (retval)
1152                 return retval;
1153
1154         retval = ext2fs_get_mem(new_itable_size, &new_itable);
1155         if (retval)
1156                 goto err_out;
1157
1158         tmp_old_itable = old_itable;
1159         tmp_new_itable = new_itable;
1160
1161         for (i = 0; i < fs->group_desc_count; i++) {
1162
1163                 blk = fs->group_desc[i].bg_inode_table;
1164                 retval = io_channel_read_blk(fs->io, blk,
1165                                 fs->inode_blocks_per_group, old_itable);
1166                 if (retval)
1167                         goto err_out;
1168
1169                 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1170
1171                         memcpy(new_itable, old_itable, old_inode_size);
1172
1173                         memset(new_itable+old_inode_size, 0,
1174                                         new_inode_size - old_inode_size);
1175
1176                         new_itable +=  new_inode_size;
1177                         old_itable += old_inode_size;
1178                 }
1179
1180                 /* reset the pointer */
1181                 old_itable = tmp_old_itable;
1182                 new_itable = tmp_new_itable;
1183
1184                 retval = io_channel_write_blk(fs->io, blk,
1185                                         new_ino_blks_per_grp, new_itable);
1186                 if (retval)
1187                         goto err_out;
1188         }
1189
1190         /* Update the meta data */
1191         fs->inode_blocks_per_group = new_ino_blks_per_grp;
1192         fs->super->s_inode_size = new_inode_size;
1193
1194 err_out:
1195         if (old_itable)
1196                 ext2fs_free_mem(&old_itable);
1197
1198         if (new_itable)
1199                 ext2fs_free_mem(&new_itable);
1200
1201         return retval;
1202
1203 }
1204
1205 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1206 {
1207         blk_t           blk;
1208         ext2_ino_t      ino;
1209         unsigned int    group = 0;
1210         unsigned int    count = 0;
1211         int             total_free = 0;
1212         int             group_free = 0;
1213
1214         /*
1215          * First calculate the block statistics
1216          */
1217         for (blk = fs->super->s_first_data_block;
1218              blk < fs->super->s_blocks_count; blk++) {
1219                 if (!ext2fs_fast_test_block_bitmap(fs->block_map, blk)) {
1220                         group_free++;
1221                         total_free++;
1222                 }
1223                 count++;
1224                 if ((count == fs->super->s_blocks_per_group) ||
1225                     (blk == fs->super->s_blocks_count-1)) {
1226                         fs->group_desc[group++].bg_free_blocks_count =
1227                                 group_free;
1228                         count = 0;
1229                         group_free = 0;
1230                 }
1231         }
1232         fs->super->s_free_blocks_count = total_free;
1233
1234         /*
1235          * Next, calculate the inode statistics
1236          */
1237         group_free = 0;
1238         total_free = 0;
1239         count = 0;
1240         group = 0;
1241
1242         /* Protect loop from wrap-around if s_inodes_count maxed */
1243         for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1244                 if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
1245                         group_free++;
1246                         total_free++;
1247                 }
1248                 count++;
1249                 if ((count == fs->super->s_inodes_per_group) ||
1250                     (ino == fs->super->s_inodes_count)) {
1251                         fs->group_desc[group++].bg_free_inodes_count =
1252                                 group_free;
1253                         count = 0;
1254                         group_free = 0;
1255                 }
1256         }
1257         fs->super->s_free_inodes_count = total_free;
1258         ext2fs_mark_super_dirty(fs);
1259         return 0;
1260 }
1261
1262 #define list_for_each_safe(pos, pnext, head) \
1263         for (pos = (head)->next, pnext = pos->next; pos != (head); \
1264              pos = pnext, pnext = pos->next)
1265
1266 static void free_blk_move_list()
1267 {
1268         struct list_head *entry, *tmp;
1269         struct blk_move *bmv;
1270
1271         list_for_each_safe(entry, tmp, &blk_move_list) {
1272
1273                 bmv = list_entry(entry, struct blk_move, list);
1274                 list_del(entry);
1275                 ext2fs_free_mem(&bmv);
1276         }
1277
1278         return ;
1279 }
1280 static int resize_inode(ext2_filsys fs, unsigned long int new_inode_size)
1281 {
1282         errcode_t retval;
1283         int new_ino_blks_per_grp;
1284         ext2fs_block_bitmap bmap;
1285
1286         if (new_inode_size <= EXT2_INODE_SIZE(fs->super)) {
1287                 fprintf(stderr, _("New inode size too small\n"));
1288                 return EXT2_ET_INVALID_ARGUMENT;
1289         }
1290
1291         ext2fs_read_inode_bitmap(fs);
1292         ext2fs_read_block_bitmap(fs);
1293         INIT_LIST_HEAD(&blk_move_list);
1294
1295
1296         new_ino_blks_per_grp = ext2fs_div_ceil(
1297                                         EXT2_INODES_PER_GROUP(fs->super)*
1298                                         new_inode_size,
1299                                         fs->blocksize);
1300
1301         /* We may change the file system.
1302          * Mark the file system as invalid so that
1303          * the user is prompted to run fsck.
1304          */
1305         fs->super->s_state &= ~EXT2_VALID_FS;
1306
1307         retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1308                                                 &bmap);
1309         if (retval)
1310                 return retval;
1311
1312         retval = get_move_bitmap(fs, new_ino_blks_per_grp, bmap);
1313         if (retval)
1314                 goto err_out;
1315
1316         retval = move_block(fs, bmap);
1317         if (retval)
1318                 goto err_out;
1319
1320         retval = inode_scan_and_fix(fs);
1321         if (retval)
1322                 goto err_out;
1323
1324         retval = expand_inode_table(fs, new_inode_size);
1325         if (retval)
1326                 goto err_out;
1327
1328         ext2fs_calculate_summary_stats(fs);
1329
1330         fs->super->s_state |= EXT2_VALID_FS;
1331         /* mark super block and block bitmap as dirty */
1332         ext2fs_mark_super_dirty(fs);
1333         ext2fs_mark_bb_dirty(fs);
1334
1335 err_out:
1336         free_blk_move_list();
1337         ext2fs_free_block_bitmap(bmap);
1338
1339         return retval;
1340 }
1341
1342 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1343 {
1344         errcode_t retval = 0;
1345         char *tdb_dir, tdb_file[PATH_MAX];
1346         char *device_name, *tmp_name;
1347
1348 #if 0 /* FIXME!! */
1349         /*
1350          * Configuration via a conf file would be
1351          * nice
1352          */
1353         profile_get_string(profile, "scratch_files",
1354                                         "directory", 0, 0,
1355                                         &tdb_dir);
1356 #endif
1357         tmp_name = strdup(name);
1358         device_name = basename(tmp_name);
1359
1360         tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1361         if (!tdb_dir)
1362                 tdb_dir="/var/lib/e2fsprogs";
1363
1364         if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1365             access(tdb_dir, W_OK))
1366                 return 0;
1367
1368         sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, device_name);
1369
1370         if (!access(tdb_file, F_OK)) {
1371                 if (unlink(tdb_file) < 0) {
1372                         retval = errno;
1373                         com_err(program_name, retval,
1374                                 _("while trying to delete %s"),
1375                                 tdb_file);
1376                         return retval;
1377                 }
1378         }
1379
1380         set_undo_io_backing_manager(*io_ptr);
1381         *io_ptr = undo_io_manager;
1382         set_undo_io_backup_file(tdb_file);
1383         printf(_("To undo the tune2fs operations please run "
1384                  "the command\n    undoe2fs %s %s\n\n"),
1385                  tdb_file, name);
1386 err_out:
1387         free(tmp_name);
1388         return retval;
1389 }
1390
1391 int main (int argc, char ** argv)
1392 {
1393         errcode_t retval;
1394         ext2_filsys fs;
1395         struct ext2_super_block *sb;
1396         io_manager io_ptr;
1397
1398 #ifdef ENABLE_NLS
1399         setlocale(LC_MESSAGES, "");
1400         setlocale(LC_CTYPE, "");
1401         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1402         textdomain(NLS_CAT_NAME);
1403 #endif
1404         if (argc && *argv)
1405                 program_name = *argv;
1406         add_error_table(&et_ext2_error_table);
1407
1408         if (strcmp(get_progname(argv[0]), "findfs") == 0)
1409                 do_findfs(argc, argv);
1410         if (strcmp(get_progname(argv[0]), "e2label") == 0)
1411                 parse_e2label_options(argc, argv);
1412         else
1413                 parse_tune2fs_options(argc, argv);
1414         
1415 #ifdef CONFIG_TESTIO_DEBUG
1416         io_ptr = test_io_manager;
1417         test_io_backing_manager = unix_io_manager;
1418 #else
1419         io_ptr = unix_io_manager;
1420 #endif
1421
1422         if (I_flag) {
1423                 /*
1424                  * If inode resize is requested use the
1425                  * Undo I/O manager
1426                  */
1427                 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1428                 if (retval)
1429                         exit(1);
1430         }
1431
1432         retval = ext2fs_open2(device_name, io_options, open_flag, 
1433                               0, 0, io_ptr, &fs);
1434         if (retval) {
1435                 com_err (program_name, retval, _("while trying to open %s"),
1436                          device_name);
1437                 fprintf(stderr,
1438                         _("Couldn't find valid filesystem superblock.\n"));
1439                 exit(1);
1440         }
1441         sb = fs->super;
1442         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1443         if ((sb->s_feature_incompat & !EXT2_TUNE2FS_INCOMPAT) ||
1444             (sb->s_feature_ro_compat & !EXT2_TUNE2FS_RO_COMPAT)) {
1445                 fprintf(stderr, 
1446                         _("Filesystem %s has unsupported features enabled.\n"),
1447                         device_name);
1448                 exit(1);
1449         }
1450         if (print_label) {
1451                 /* For e2label emulation */
1452                 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1453                        sb->s_volume_name);
1454                 remove_error_table(&et_ext2_error_table);
1455                 exit(0);
1456         }
1457         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1458         if (retval) {
1459                 com_err("ext2fs_check_if_mount", retval,
1460                         _("while determining whether %s is mounted."),
1461                         device_name);
1462                 exit(1);
1463         }
1464         /* Normally we only need to write out the superblock */
1465         fs->flags |= EXT2_FLAG_SUPER_ONLY;
1466
1467         if (c_flag) {
1468                 sb->s_max_mnt_count = max_mount_count;
1469                 ext2fs_mark_super_dirty(fs);
1470                 printf (_("Setting maximal mount count to %d\n"),
1471                         max_mount_count);
1472         }
1473         if (C_flag) {
1474                 sb->s_mnt_count = mount_count;
1475                 ext2fs_mark_super_dirty(fs);
1476                 printf (_("Setting current mount count to %d\n"), mount_count);
1477         }
1478         if (e_flag) {
1479                 sb->s_errors = errors;
1480                 ext2fs_mark_super_dirty(fs);
1481                 printf (_("Setting error behavior to %d\n"), errors);
1482         }
1483         if (g_flag) {
1484                 sb->s_def_resgid = resgid;
1485                 ext2fs_mark_super_dirty(fs);
1486                 printf (_("Setting reserved blocks gid to %lu\n"), resgid);
1487         }
1488         if (i_flag) {
1489                 sb->s_checkinterval = interval;
1490                 ext2fs_mark_super_dirty(fs);
1491                 printf (_("Setting interval between checks to %lu seconds\n"), interval);
1492         }
1493         if (m_flag) {
1494                 sb->s_r_blocks_count = e2p_percent(reserved_ratio,
1495                                                    sb->s_blocks_count);
1496                 ext2fs_mark_super_dirty(fs);
1497                 printf (_("Setting reserved blocks percentage to %g%% (%u blocks)\n"),
1498                         reserved_ratio, sb->s_r_blocks_count);
1499         }
1500         if (r_flag) {
1501                 if (reserved_blocks >= sb->s_blocks_count/2) {
1502                         com_err (program_name, 0,
1503                                  _("reserved blocks count is too big (%lu)"),
1504                                  reserved_blocks);
1505                         exit (1);
1506                 }
1507                 sb->s_r_blocks_count = reserved_blocks;
1508                 ext2fs_mark_super_dirty(fs);
1509                 printf (_("Setting reserved blocks count to %lu\n"),
1510                         reserved_blocks);
1511         }
1512         if (s_flag == 1) {
1513                 if (sb->s_feature_ro_compat &
1514                     EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1515                         fputs(_("\nThe filesystem already has sparse "
1516                                 "superblocks.\n"), stderr);
1517                 else {
1518                         sb->s_feature_ro_compat |=
1519                                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1520                         sb->s_state &= ~EXT2_VALID_FS;
1521                         ext2fs_mark_super_dirty(fs);
1522                         printf(_("\nSparse superblock flag set.  %s"),
1523                                _(please_fsck));
1524                 }
1525         }
1526         if (s_flag == 0) {
1527                 fputs(_("\nClearing the sparse superflag not supported.\n"),
1528                       stderr);
1529                 exit(1);
1530         }
1531         if (T_flag) {
1532                 sb->s_lastcheck = last_check_time;
1533                 ext2fs_mark_super_dirty(fs);
1534                 printf(_("Setting time filesystem last checked to %s\n"),
1535                        ctime(&last_check_time));
1536         }
1537         if (u_flag) {
1538                 sb->s_def_resuid = resuid;
1539                 ext2fs_mark_super_dirty(fs);
1540                 printf (_("Setting reserved blocks uid to %lu\n"), resuid);
1541         }
1542         if (L_flag) {
1543                 if (strlen(new_label) > sizeof(sb->s_volume_name))
1544                         fputs(_("Warning: label too long, truncating.\n"), 
1545                               stderr);
1546                 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1547                 strncpy(sb->s_volume_name, new_label,
1548                         sizeof(sb->s_volume_name));
1549                 ext2fs_mark_super_dirty(fs);
1550         }
1551         if (M_flag) {
1552                 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1553                 strncpy(sb->s_last_mounted, new_last_mounted,
1554                         sizeof(sb->s_last_mounted));
1555                 ext2fs_mark_super_dirty(fs);
1556         }
1557         if (mntopts_cmd)
1558                 update_mntopts(fs, mntopts_cmd);
1559         if (features_cmd)
1560                 update_feature_set(fs, features_cmd);
1561         if (extended_cmd)
1562                 parse_extended_opts(fs, extended_cmd);
1563         if (journal_size || journal_device)
1564                 add_journal(fs);
1565         
1566         if (U_flag) {
1567                 if ((strcasecmp(new_UUID, "null") == 0) ||
1568                     (strcasecmp(new_UUID, "clear") == 0)) {
1569                         uuid_clear(sb->s_uuid);
1570                 } else if (strcasecmp(new_UUID, "time") == 0) {
1571                         uuid_generate_time(sb->s_uuid);
1572                 } else if (strcasecmp(new_UUID, "random") == 0) {
1573                         uuid_generate(sb->s_uuid);
1574                 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1575                         com_err(program_name, 0, _("Invalid UUID format\n"));
1576                         exit(1);
1577                 }
1578                 ext2fs_mark_super_dirty(fs);
1579         }
1580         if (I_flag) {
1581                 if (mount_flags & EXT2_MF_MOUNTED) {
1582                         fputs(_("The inode size may only be "
1583                                 "changed when the filesystem is "
1584                                 "unmounted.\n"), stderr);
1585                         exit(1);
1586                 }
1587                 /*
1588                  * We want to update group descriptor also
1589                  * with the new free inode count
1590                  */
1591                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1592                 if (resize_inode(fs, new_inode_size)) {
1593                         fputs(_("Error in resizing the inode size.\n"
1594                                 "Run undoe2fs to undo the "
1595                                 "file system changes. \n"), stderr);
1596                 } else {
1597                         printf (_("Setting inode size  %d\n"),
1598                                                         new_inode_size);
1599                 }
1600         }
1601
1602         if (l_flag)
1603                 list_super (sb);
1604         if (stride_set) {
1605                 sb->s_raid_stride = stride;
1606                 ext2fs_mark_super_dirty(fs);
1607                 printf(_("Setting stride size to %d\n"), stride);
1608         }
1609         if (stripe_width_set) {
1610                 sb->s_raid_stripe_width = stripe_width;
1611                 ext2fs_mark_super_dirty(fs);
1612                 printf(_("Setting stripe width to %d\n"), stripe_width);
1613         }
1614         remove_error_table(&et_ext2_error_table);
1615         return (ext2fs_close (fs) ? 1 : 0);
1616 }