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