Whamcloud - gitweb
tune2fs: miscellaneous whitespace fixups
[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 "config.h"
31 #include <fcntl.h>
32 #include <grp.h>
33 #ifdef HAVE_GETOPT_H
34 #include <getopt.h>
35 #else
36 extern char *optarg;
37 extern int optind;
38 #endif
39 #include <pwd.h>
40 #include <stdio.h>
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include <libgen.h>
49 #include <limits.h>
50
51 #include "ext2fs/ext2_fs.h"
52 #include "ext2fs/ext2fs.h"
53 #include "et/com_err.h"
54 #include "uuid/uuid.h"
55 #include "e2p/e2p.h"
56 #include "jfs_user.h"
57 #include "util.h"
58 #include "blkid/blkid.h"
59 #include "quota/mkquota.h"
60
61 #include "../version.h"
62 #include "nls-enable.h"
63
64 #define QOPT_ENABLE     (1)
65 #define QOPT_DISABLE    (-1)
66
67 extern int ask_yn(const char *string, int def);
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, Q_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;
80 static blk64_t reserved_blocks;
81 static double reserved_ratio;
82 static unsigned long resgid, resuid;
83 static unsigned short errors;
84 static int open_flag;
85 static char *features_cmd;
86 static char *mntopts_cmd;
87 static int stride, stripe_width;
88 static int stride_set, stripe_width_set;
89 static char *extended_cmd;
90 static unsigned long new_inode_size;
91 static char *ext_mount_opts;
92 static int usrquota, grpquota;
93
94 int journal_size, journal_flags;
95 char *journal_device;
96
97 static struct list_head blk_move_list;
98
99 struct blk_move {
100         struct list_head list;
101         blk64_t old_loc;
102         blk64_t new_loc;
103 };
104
105
106 static const char *please_fsck = N_("Please run e2fsck on the filesystem.\n");
107
108 #ifdef CONFIG_BUILD_FINDFS
109 void do_findfs(int argc, char **argv);
110 #endif
111
112 static void usage(void)
113 {
114         fprintf(stderr,
115                 _("Usage: %s [-c max_mounts_count] [-e errors_behavior] "
116                   "[-g group]\n"
117                   "\t[-i interval[d|m|w]] [-j] [-J journal_options] [-l]\n"
118                   "\t[-m reserved_blocks_percent] "
119                   "[-o [^]mount_options[,...]] \n"
120                   "\t[-r reserved_blocks_count] [-u user] [-C mount_count] "
121                   "[-L volume_label]\n"
122                   "\t[-M last_mounted_dir] [-O [^]feature[,...]]\n"
123                   "\t[-E extended-option[,...]] [-T last_check_time] "
124                   "[-U UUID]\n\t[ -I new_inode_size ] device\n"), program_name);
125         exit(1);
126 }
127
128 static __u32 ok_features[3] = {
129         /* Compat */
130         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
131                 EXT2_FEATURE_COMPAT_DIR_INDEX,
132         /* Incompat */
133         EXT2_FEATURE_INCOMPAT_FILETYPE |
134                 EXT3_FEATURE_INCOMPAT_EXTENTS |
135                 EXT4_FEATURE_INCOMPAT_FLEX_BG,
136         /* R/O compat */
137         EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
138                 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
139                 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
140                 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
141                 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
142                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
143                 EXT4_FEATURE_RO_COMPAT_QUOTA
144 };
145
146 static __u32 clear_ok_features[3] = {
147         /* Compat */
148         EXT3_FEATURE_COMPAT_HAS_JOURNAL |
149                 EXT2_FEATURE_COMPAT_RESIZE_INODE |
150                 EXT2_FEATURE_COMPAT_DIR_INDEX,
151         /* Incompat */
152         EXT2_FEATURE_INCOMPAT_FILETYPE |
153                 EXT4_FEATURE_INCOMPAT_FLEX_BG,
154         /* R/O compat */
155         EXT2_FEATURE_RO_COMPAT_LARGE_FILE |
156                 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
157                 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
158                 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
159                 EXT4_FEATURE_RO_COMPAT_GDT_CSUM |
160                 EXT4_FEATURE_RO_COMPAT_QUOTA
161 };
162
163 /*
164  * Remove an external journal from the filesystem
165  */
166 static void remove_journal_device(ext2_filsys fs)
167 {
168         char            *journal_path;
169         ext2_filsys     jfs;
170         char            buf[1024];
171         journal_superblock_t    *jsb;
172         int             i, nr_users;
173         errcode_t       retval;
174         int             commit_remove_journal = 0;
175         io_manager      io_ptr;
176
177         if (f_flag)
178                 commit_remove_journal = 1; /* force removal even if error */
179
180         uuid_unparse(fs->super->s_journal_uuid, buf);
181         journal_path = blkid_get_devname(NULL, "UUID", buf);
182
183         if (!journal_path) {
184                 journal_path =
185                         ext2fs_find_block_device(fs->super->s_journal_dev);
186                 if (!journal_path)
187                         goto no_valid_journal;
188         }
189
190 #ifdef CONFIG_TESTIO_DEBUG
191         if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
192                 io_ptr = test_io_manager;
193                 test_io_backing_manager = unix_io_manager;
194         } else
195 #endif
196                 io_ptr = unix_io_manager;
197         retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
198                              EXT2_FLAG_JOURNAL_DEV_OK, 0,
199                              fs->blocksize, io_ptr, &jfs);
200         if (retval) {
201                 com_err(program_name, retval,
202                         _("while trying to open external journal"));
203                 goto no_valid_journal;
204         }
205         if (!(jfs->super->s_feature_incompat &
206               EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
207                 fprintf(stderr, _("%s is not a journal device.\n"),
208                         journal_path);
209                 goto no_valid_journal;
210         }
211
212         /* Get the journal superblock */
213         if ((retval = io_channel_read_blk64(jfs->io, 1, -1024, buf))) {
214                 com_err(program_name, retval,
215                         _("while reading journal superblock"));
216                 goto no_valid_journal;
217         }
218
219         jsb = (journal_superblock_t *) buf;
220         if ((jsb->s_header.h_magic != (unsigned)ntohl(JFS_MAGIC_NUMBER)) ||
221             (jsb->s_header.h_blocktype != (unsigned)ntohl(JFS_SUPERBLOCK_V2))) {
222                 fputs(_("Journal superblock not found!\n"), stderr);
223                 goto no_valid_journal;
224         }
225
226         /* Find the filesystem UUID */
227         nr_users = ntohl(jsb->s_nr_users);
228         for (i = 0; i < nr_users; i++) {
229                 if (memcmp(fs->super->s_uuid, &jsb->s_users[i * 16], 16) == 0)
230                         break;
231         }
232         if (i >= nr_users) {
233                 fputs(_("Filesystem's UUID not found on journal device.\n"),
234                       stderr);
235                 commit_remove_journal = 1;
236                 goto no_valid_journal;
237         }
238         nr_users--;
239         for (i = 0; i < nr_users; i++)
240                 memcpy(&jsb->s_users[i * 16], &jsb->s_users[(i + 1) * 16], 16);
241         jsb->s_nr_users = htonl(nr_users);
242
243         /* Write back the journal superblock */
244         if ((retval = io_channel_write_blk64(jfs->io, 1, -1024, buf))) {
245                 com_err(program_name, retval,
246                         "while writing journal superblock.");
247                 goto no_valid_journal;
248         }
249
250         commit_remove_journal = 1;
251
252 no_valid_journal:
253         if (commit_remove_journal == 0) {
254                 fputs(_("Cannot locate journal device. It was NOT removed\n"
255                         "Use -f option to remove missing journal device.\n"),
256                       stderr);
257                 exit(1);
258         }
259         fs->super->s_journal_dev = 0;
260         uuid_clear(fs->super->s_journal_uuid);
261         ext2fs_mark_super_dirty(fs);
262         fputs(_("Journal removed\n"), stdout);
263         free(journal_path);
264 }
265
266 /* Helper function for remove_journal_inode */
267 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
268                                e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
269                                blk64_t ref_block EXT2FS_ATTR((unused)),
270                                int ref_offset EXT2FS_ATTR((unused)),
271                                void *private EXT2FS_ATTR((unused)))
272 {
273         blk64_t block;
274         int     group;
275
276         block = *blocknr;
277         ext2fs_unmark_block_bitmap2(fs->block_map, block);
278         group = ext2fs_group_of_blk2(fs, block);
279         ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
280         ext2fs_group_desc_csum_set(fs, group);
281         ext2fs_free_blocks_count_add(fs->super, EXT2FS_CLUSTER_RATIO(fs));
282         return 0;
283 }
284
285 /*
286  * Remove the journal inode from the filesystem
287  */
288 static void remove_journal_inode(ext2_filsys fs)
289 {
290         struct ext2_inode       inode;
291         errcode_t               retval;
292         ino_t                   ino = fs->super->s_journal_inum;
293
294         retval = ext2fs_read_inode(fs, ino,  &inode);
295         if (retval) {
296                 com_err(program_name, retval,
297                         _("while reading journal inode"));
298                 exit(1);
299         }
300         if (ino == EXT2_JOURNAL_INO) {
301                 retval = ext2fs_read_bitmaps(fs);
302                 if (retval) {
303                         com_err(program_name, retval,
304                                 _("while reading bitmaps"));
305                         exit(1);
306                 }
307                 retval = ext2fs_block_iterate3(fs, ino,
308                                                BLOCK_FLAG_READ_ONLY, NULL,
309                                                release_blocks_proc, NULL);
310                 if (retval) {
311                         com_err(program_name, retval,
312                                 _("while clearing journal inode"));
313                         exit(1);
314                 }
315                 memset(&inode, 0, sizeof(inode));
316                 ext2fs_mark_bb_dirty(fs);
317                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
318         } else
319                 inode.i_flags &= ~EXT2_IMMUTABLE_FL;
320         retval = ext2fs_write_inode(fs, ino, &inode);
321         if (retval) {
322                 com_err(program_name, retval,
323                         _("while writing journal inode"));
324                 exit(1);
325         }
326         fs->super->s_journal_inum = 0;
327         ext2fs_mark_super_dirty(fs);
328 }
329
330 /*
331  * Update the default mount options
332  */
333 static void update_mntopts(ext2_filsys fs, char *mntopts)
334 {
335         struct ext2_super_block *sb = fs->super;
336
337         if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0)) {
338                 fprintf(stderr, _("Invalid mount option set: %s\n"),
339                         mntopts);
340                 exit(1);
341         }
342         ext2fs_mark_super_dirty(fs);
343 }
344
345 static void request_fsck_afterwards(ext2_filsys fs)
346 {
347         static int requested = 0;
348
349         if (requested++)
350                 return;
351         fs->super->s_state &= ~EXT2_VALID_FS;
352         printf("\n%s\n", _(please_fsck));
353         if (mount_flags & EXT2_MF_READONLY)
354                 printf(_("(and reboot afterwards!)\n"));
355 }
356
357 /*
358  * Update the feature set as provided by the user.
359  */
360 static void update_feature_set(ext2_filsys fs, char *features)
361 {
362         struct ext2_super_block *sb = fs->super;
363         struct ext2_group_desc *gd;
364         __u32           old_features[3];
365         int             i, type_err;
366         unsigned int    mask_err;
367
368 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
369                                 ((&sb->s_feature_compat)[(type)] & (mask)))
370 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
371                                  !((&sb->s_feature_compat)[(type)] & (mask)))
372 #define FEATURE_CHANGED(type, mask) ((mask) & \
373                      (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
374
375         old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
376         old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
377         old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
378
379         if (e2p_edit_feature2(features, &sb->s_feature_compat,
380                               ok_features, clear_ok_features,
381                               &type_err, &mask_err)) {
382                 if (!mask_err)
383                         fprintf(stderr,
384                                 _("Invalid filesystem option set: %s\n"),
385                                 features);
386                 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
387                         fprintf(stderr, _("Clearing filesystem feature '%s' "
388                                           "not supported.\n"),
389                                 e2p_feature2string(type_err &
390                                                    E2P_FEATURE_TYPE_MASK,
391                                                    mask_err));
392                 else
393                         fprintf(stderr, _("Setting filesystem feature '%s' "
394                                           "not supported.\n"),
395                                 e2p_feature2string(type_err, mask_err));
396                 exit(1);
397         }
398
399         if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
400                 if ((mount_flags & EXT2_MF_MOUNTED) &&
401                     !(mount_flags & EXT2_MF_READONLY)) {
402                         fputs(_("The has_journal feature may only be "
403                                 "cleared when the filesystem is\n"
404                                 "unmounted or mounted "
405                                 "read-only.\n"), stderr);
406                         exit(1);
407                 }
408                 if (sb->s_feature_incompat &
409                     EXT3_FEATURE_INCOMPAT_RECOVER) {
410                         fputs(_("The needs_recovery flag is set.  "
411                                 "Please run e2fsck before clearing\n"
412                                 "the has_journal flag.\n"), stderr);
413                         exit(1);
414                 }
415                 if (sb->s_journal_inum) {
416                         remove_journal_inode(fs);
417                 }
418                 if (sb->s_journal_dev) {
419                         remove_journal_device(fs);
420                 }
421         }
422
423         if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
424                 /*
425                  * If adding a journal flag, let the create journal
426                  * code below handle setting the flag and creating the
427                  * journal.  We supply a default size if necessary.
428                  */
429                 if (!journal_size)
430                         journal_size = -1;
431                 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
432         }
433
434         if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
435                 if (!sb->s_def_hash_version)
436                         sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
437                 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
438                         uuid_generate((unsigned char *) sb->s_hash_seed);
439         }
440
441         if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
442                 if (ext2fs_check_desc(fs)) {
443                         fputs(_("Clearing the flex_bg flag would "
444                                 "cause the the filesystem to be\n"
445                                 "inconsistent.\n"), stderr);
446                         exit(1);
447                 }
448         }
449
450         if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
451                             EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
452                 if ((mount_flags & EXT2_MF_MOUNTED) &&
453                     !(mount_flags & EXT2_MF_READONLY)) {
454                         fputs(_("The huge_file feature may only be "
455                                 "cleared when the filesystem is\n"
456                                 "unmounted or mounted "
457                                 "read-only.\n"), stderr);
458                         exit(1);
459                 }
460         }
461
462         if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
463                        EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
464                 for (i = 0; i < fs->group_desc_count; i++) {
465                         gd = ext2fs_group_desc(fs, fs->group_desc, i);
466                         gd->bg_itable_unused = 0;
467                         gd->bg_flags = EXT2_BG_INODE_ZEROED;
468                         ext2fs_group_desc_csum_set(fs, i);
469                 }
470                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
471         }
472
473         if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
474                         EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
475                 for (i = 0; i < fs->group_desc_count; i++) {
476                         gd = ext2fs_group_desc(fs, fs->group_desc, i);
477                         if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
478                                 /* 
479                                  * XXX what we really should do is zap
480                                  * uninitialized inode tables instead.
481                                  */
482                                 request_fsck_afterwards(fs);
483                                 break;
484                         }
485                         gd->bg_itable_unused = 0;
486                         gd->bg_flags = 0;
487                         gd->bg_checksum = 0;
488                 }
489                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
490         }
491
492         if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
493                                 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
494                 /*
495                  * Set the Q_flag here and handle the quota options in the code
496                  * below.
497                  */
498                 if (!Q_flag) {
499                         Q_flag = 1;
500                         /* Enable both user quota and group quota by default */
501                         usrquota = QOPT_ENABLE;
502                         grpquota = QOPT_ENABLE;
503                 }
504                 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
505         }
506
507         if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
508                                 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
509                 /*
510                  * Set the Q_flag here and handle the quota options in the code
511                  * below.
512                  */
513                 if (Q_flag)
514                         fputs(_("\nWarning: '^quota' option overrides '-Q'"
515                                 "arguments.\n"), stderr);
516                 Q_flag = 1;
517                 /* Disable both user quota and group quota by default */
518                 usrquota = QOPT_DISABLE;
519                 grpquota = QOPT_DISABLE;
520         }
521
522         if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
523             (sb->s_feature_compat || sb->s_feature_ro_compat ||
524              sb->s_feature_incompat))
525                 ext2fs_update_dynamic_rev(fs);
526
527         if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
528                             EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
529             FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
530                         EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
531             FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
532                             EXT2_FEATURE_INCOMPAT_FILETYPE) ||
533             FEATURE_CHANGED(E2P_FEATURE_COMPAT,
534                             EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
535             FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
536                         EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
537                 request_fsck_afterwards(fs);
538
539         if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
540             (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
541             (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
542                 ext2fs_mark_super_dirty(fs);
543 }
544
545 /*
546  * Add a journal to the filesystem.
547  */
548 static void add_journal(ext2_filsys fs)
549 {
550         unsigned long journal_blocks;
551         errcode_t       retval;
552         ext2_filsys     jfs;
553         io_manager      io_ptr;
554
555         if (fs->super->s_feature_compat &
556             EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
557                 fputs(_("The filesystem already has a journal.\n"), stderr);
558                 goto err;
559         }
560         if (journal_device) {
561                 check_plausibility(journal_device);
562                 check_mount(journal_device, 0, _("journal"));
563 #ifdef CONFIG_TESTIO_DEBUG
564                 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
565                         io_ptr = test_io_manager;
566                         test_io_backing_manager = unix_io_manager;
567                 } else
568 #endif
569                         io_ptr = unix_io_manager;
570                 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
571                                      EXT2_FLAG_JOURNAL_DEV_OK, 0,
572                                      fs->blocksize, io_ptr, &jfs);
573                 if (retval) {
574                         com_err(program_name, retval,
575                                 _("\n\twhile trying to open journal on %s\n"),
576                                 journal_device);
577                         goto err;
578                 }
579                 printf(_("Creating journal on device %s: "),
580                        journal_device);
581                 fflush(stdout);
582
583                 retval = ext2fs_add_journal_device(fs, jfs);
584                 ext2fs_close(jfs);
585                 if (retval) {
586                         com_err(program_name, retval,
587                                 _("while adding filesystem to journal on %s"),
588                                 journal_device);
589                         goto err;
590                 }
591                 fputs(_("done\n"), stdout);
592         } else if (journal_size) {
593                 fputs(_("Creating journal inode: "), stdout);
594                 fflush(stdout);
595                 journal_blocks = figure_journal_size(journal_size, fs);
596
597                 retval = ext2fs_add_journal_inode(fs, journal_blocks,
598                                                   journal_flags);
599                 if (retval) {
600                         fprintf(stderr, "\n");
601                         com_err(program_name, retval,
602                                 _("\n\twhile trying to create journal file"));
603                         exit(1);
604                 } else
605                         fputs(_("done\n"), stdout);
606                 /*
607                  * If the filesystem wasn't mounted, we need to force
608                  * the block group descriptors out.
609                  */
610                 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
611                         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
612         }
613         print_check_message(fs->super->s_max_mnt_count,
614                             fs->super->s_checkinterval);
615         return;
616
617 err:
618         free(journal_device);
619         exit(1);
620 }
621
622 void handle_quota_options(ext2_filsys fs)
623 {
624         quota_ctx_t qctx;
625         errcode_t retval;
626         ext2_ino_t qf_ino;
627
628         if (!usrquota && !grpquota)
629                 /* Nothing to do. */
630                 return;
631
632         init_quota_context(&qctx, fs, -1);
633
634         if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
635                 if ((qf_ino = quota_file_exists(fs, USRQUOTA, QFMT_VFS_V1)) > 0)
636                         set_sb_quota_inum(fs, qf_ino, USRQUOTA);
637                 else
638                         write_quota_inode(qctx, USRQUOTA);
639         } else if (usrquota == QOPT_DISABLE) {
640                 remove_quota_inode(fs, USRQUOTA);
641         }
642
643         if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
644                 if ((qf_ino = quota_file_exists(fs, GRPQUOTA, QFMT_VFS_V1)) > 0)
645                         set_sb_quota_inum(fs, qf_ino, GRPQUOTA);
646                 else
647                         write_quota_inode(qctx, GRPQUOTA);
648         } else if (grpquota == QOPT_DISABLE) {
649                 remove_quota_inode(fs, GRPQUOTA);
650         }
651
652         release_quota_context(&qctx);
653
654         if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
655                 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
656                 ext2fs_mark_super_dirty(fs);
657         } else if ((usrquota == QOPT_DISABLE) && (grpquota == QOPT_DISABLE)) {
658                 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
659                 ext2fs_mark_super_dirty(fs);
660         }
661
662         return;
663 }
664
665 void parse_quota_opts(const char *opts)
666 {
667         char    *buf, *token, *next, *p, *arg;
668         int     len;
669
670         len = strlen(opts);
671         buf = malloc(len+1);
672         if (!buf) {
673                 fputs(_("Couldn't allocate memory to parse quota "
674                         "options!\n"), stderr);
675                 exit(1);
676         }
677         strcpy(buf, opts);
678         for (token = buf; token && *token; token = next) {
679                 p = strchr(token, ',');
680                 next = 0;
681                 if (p) {
682                         *p = 0;
683                         next = p+1;
684                 }
685
686                 if (strcmp(token, "usrquota") == 0) {
687                         usrquota = QOPT_ENABLE;
688                 } else if (strcmp(token, "^usrquota") == 0) {
689                         usrquota = QOPT_DISABLE;
690                 } else if (strcmp(token, "grpquota") == 0) {
691                         grpquota = QOPT_ENABLE;
692                 } else if (strcmp(token, "^grpquota") == 0) {
693                         grpquota = QOPT_DISABLE;
694                 } else {
695                         fputs(_("\nBad quota options specified.\n\n"
696                                 "Following valid quota options are available "
697                                 "(pass by separating with comma):\n"
698                                 "\t[^]usrquota\n"
699                                 "\t[^]grpquota\n"
700                                 "\n\n"), stderr);
701                         free(buf);
702                         exit(1);
703                 }
704         }
705         free(buf);
706 }
707
708
709
710 static void parse_e2label_options(int argc, char ** argv)
711 {
712         if ((argc < 2) || (argc > 3)) {
713                 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
714                 exit(1);
715         }
716         io_options = strchr(argv[1], '?');
717         if (io_options)
718                 *io_options++ = 0;
719         device_name = blkid_get_devname(NULL, argv[1], NULL);
720         if (!device_name) {
721                 com_err("e2label", 0, _("Unable to resolve '%s'"),
722                         argv[1]);
723                 exit(1);
724         }
725         open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
726         if (argc == 3) {
727                 open_flag |= EXT2_FLAG_RW;
728                 L_flag = 1;
729                 new_label = argv[2];
730         } else
731                 print_label++;
732 }
733
734 static time_t parse_time(char *str)
735 {
736         struct  tm      ts;
737
738         if (strcmp(str, "now") == 0) {
739                 return (time(0));
740         }
741         memset(&ts, 0, sizeof(ts));
742 #ifdef HAVE_STRPTIME
743         strptime(str, "%Y%m%d%H%M%S", &ts);
744 #else
745         sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
746                &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
747         ts.tm_year -= 1900;
748         ts.tm_mon -= 1;
749         if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
750             ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
751             ts.tm_min > 59 || ts.tm_sec > 61)
752                 ts.tm_mday = 0;
753 #endif
754         if (ts.tm_mday == 0) {
755                 com_err(program_name, 0,
756                         _("Couldn't parse date/time specifier: %s"),
757                         str);
758                 usage();
759         }
760         ts.tm_isdst = -1;
761         return (mktime(&ts));
762 }
763
764 static void parse_tune2fs_options(int argc, char **argv)
765 {
766         int c;
767         char *tmp;
768         struct group *gr;
769         struct passwd *pw;
770
771         open_flag = 0;
772
773         printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
774         while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:E:I:J:L:M:O:Q:T:U:")) != EOF)
775                 switch (c) {
776                 case 'c':
777                         max_mount_count = strtol(optarg, &tmp, 0);
778                         if (*tmp || max_mount_count > 16000) {
779                                 com_err(program_name, 0,
780                                         _("bad mounts count - %s"),
781                                         optarg);
782                                 usage();
783                         }
784                         if (max_mount_count == 0)
785                                 max_mount_count = -1;
786                         c_flag = 1;
787                         open_flag = EXT2_FLAG_RW;
788                         break;
789                 case 'C':
790                         mount_count = strtoul(optarg, &tmp, 0);
791                         if (*tmp || mount_count > 16000) {
792                                 com_err(program_name, 0,
793                                         _("bad mounts count - %s"),
794                                         optarg);
795                                 usage();
796                         }
797                         C_flag = 1;
798                         open_flag = EXT2_FLAG_RW;
799                         break;
800                 case 'e':
801                         if (strcmp(optarg, "continue") == 0)
802                                 errors = EXT2_ERRORS_CONTINUE;
803                         else if (strcmp(optarg, "remount-ro") == 0)
804                                 errors = EXT2_ERRORS_RO;
805                         else if (strcmp(optarg, "panic") == 0)
806                                 errors = EXT2_ERRORS_PANIC;
807                         else {
808                                 com_err(program_name, 0,
809                                         _("bad error behavior - %s"),
810                                         optarg);
811                                 usage();
812                         }
813                         e_flag = 1;
814                         open_flag = EXT2_FLAG_RW;
815                         break;
816                 case 'E':
817                         extended_cmd = optarg;
818                         open_flag |= EXT2_FLAG_RW;
819                         break;
820                 case 'f': /* Force */
821                         f_flag = 1;
822                         break;
823                 case 'g':
824                         resgid = strtoul(optarg, &tmp, 0);
825                         if (*tmp) {
826                                 gr = getgrnam(optarg);
827                                 if (gr == NULL)
828                                         tmp = optarg;
829                                 else {
830                                         resgid = gr->gr_gid;
831                                         *tmp = 0;
832                                 }
833                         }
834                         if (*tmp) {
835                                 com_err(program_name, 0,
836                                         _("bad gid/group name - %s"),
837                                         optarg);
838                                 usage();
839                         }
840                         g_flag = 1;
841                         open_flag = EXT2_FLAG_RW;
842                         break;
843                 case 'i':
844                         interval = strtoul(optarg, &tmp, 0);
845                         switch (*tmp) {
846                         case 's':
847                                 tmp++;
848                                 break;
849                         case '\0':
850                         case 'd':
851                         case 'D': /* days */
852                                 interval *= 86400;
853                                 if (*tmp != '\0')
854                                         tmp++;
855                                 break;
856                         case 'm':
857                         case 'M': /* months! */
858                                 interval *= 86400 * 30;
859                                 tmp++;
860                                 break;
861                         case 'w':
862                         case 'W': /* weeks */
863                                 interval *= 86400 * 7;
864                                 tmp++;
865                                 break;
866                         }
867                         if (*tmp) {
868                                 com_err(program_name, 0,
869                                         _("bad interval - %s"), optarg);
870                                 usage();
871                         }
872                         i_flag = 1;
873                         open_flag = EXT2_FLAG_RW;
874                         break;
875                 case 'j':
876                         if (!journal_size)
877                                 journal_size = -1;
878                         open_flag = EXT2_FLAG_RW;
879                         break;
880                 case 'J':
881                         parse_journal_opts(optarg);
882                         open_flag = EXT2_FLAG_RW;
883                         break;
884                 case 'l':
885                         l_flag = 1;
886                         break;
887                 case 'L':
888                         new_label = optarg;
889                         L_flag = 1;
890                         open_flag |= EXT2_FLAG_RW |
891                                 EXT2_FLAG_JOURNAL_DEV_OK;
892                         break;
893                 case 'm':
894                         reserved_ratio = strtod(optarg, &tmp);
895                         if (*tmp || reserved_ratio > 50 ||
896                             reserved_ratio < 0) {
897                                 com_err(program_name, 0,
898                                         _("bad reserved block ratio - %s"),
899                                         optarg);
900                                 usage();
901                         }
902                         m_flag = 1;
903                         open_flag = EXT2_FLAG_RW;
904                         break;
905                 case 'M':
906                         new_last_mounted = optarg;
907                         M_flag = 1;
908                         open_flag = EXT2_FLAG_RW;
909                         break;
910                 case 'o':
911                         if (mntopts_cmd) {
912                                 com_err(program_name, 0,
913                                         _("-o may only be specified once"));
914                                 usage();
915                         }
916                         mntopts_cmd = optarg;
917                         open_flag = EXT2_FLAG_RW;
918                         break;
919                         
920                 case 'O':
921                         if (features_cmd) {
922                                 com_err(program_name, 0,
923                                         _("-O may only be specified once"));
924                                 usage();
925                         }
926                         features_cmd = optarg;
927                         open_flag = EXT2_FLAG_RW;
928                         break;
929                 case 'Q':
930                         Q_flag = 1;
931                         parse_quota_opts(optarg);
932                         open_flag = EXT2_FLAG_RW;
933                         break;
934                 case 'r':
935                         reserved_blocks = strtoul(optarg, &tmp, 0);
936                         if (*tmp) {
937                                 com_err(program_name, 0,
938                                         _("bad reserved blocks count - %s"),
939                                         optarg);
940                                 usage();
941                         }
942                         r_flag = 1;
943                         open_flag = EXT2_FLAG_RW;
944                         break;
945                 case 's': /* Deprecated */
946                         s_flag = atoi(optarg);
947                         open_flag = EXT2_FLAG_RW;
948                         break;
949                 case 'T':
950                         T_flag = 1;
951                         last_check_time = parse_time(optarg);
952                         open_flag = EXT2_FLAG_RW;
953                         break;
954                 case 'u':
955                                 resuid = strtoul(optarg, &tmp, 0);
956                                 if (*tmp) {
957                                         pw = getpwnam(optarg);
958                                         if (pw == NULL)
959                                                 tmp = optarg;
960                                         else {
961                                                 resuid = pw->pw_uid;
962                                                 *tmp = 0;
963                                         }
964                                 }
965                                 if (*tmp) {
966                                         com_err(program_name, 0,
967                                                 _("bad uid/user name - %s"),
968                                                 optarg);
969                                         usage();
970                                 }
971                                 u_flag = 1;
972                                 open_flag = EXT2_FLAG_RW;
973                                 break;
974                 case 'U':
975                         new_UUID = optarg;
976                         U_flag = 1;
977                         open_flag = EXT2_FLAG_RW |
978                                 EXT2_FLAG_JOURNAL_DEV_OK;
979                         break;
980                 case 'I':
981                         new_inode_size = strtoul(optarg, &tmp, 0);
982                         if (*tmp) {
983                                 com_err(program_name, 0,
984                                         _("bad inode size - %s"),
985                                         optarg);
986                                 usage();
987                         }
988                         if (!((new_inode_size &
989                                (new_inode_size - 1)) == 0)) {
990                                 com_err(program_name, 0,
991                                         _("Inode size must be a "
992                                           "power of two- %s"),
993                                         optarg);
994                                 usage();
995                         }
996                         open_flag = EXT2_FLAG_RW;
997                         I_flag = 1;
998                         break;
999                 default:
1000                         usage();
1001                 }
1002         if (optind < argc - 1 || optind == argc)
1003                 usage();
1004         if (!open_flag && !l_flag)
1005                 usage();
1006         io_options = strchr(argv[optind], '?');
1007         if (io_options)
1008                 *io_options++ = 0;
1009         device_name = blkid_get_devname(NULL, argv[optind], NULL);
1010         if (!device_name) {
1011                 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
1012                         argv[optind]);
1013                 exit(1);
1014         }
1015 }
1016
1017 #ifdef CONFIG_BUILD_FINDFS
1018 void do_findfs(int argc, char **argv)
1019 {
1020         char    *dev;
1021
1022         if ((argc != 2) ||
1023             (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1024                 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1025                 exit(2);
1026         }
1027         dev = blkid_get_devname(NULL, argv[1], NULL);
1028         if (!dev) {
1029                 com_err("findfs", 0, _("Unable to resolve '%s'"),
1030                         argv[1]);
1031                 exit(1);
1032         }
1033         puts(dev);
1034         exit(0);
1035 }
1036 #endif
1037
1038 static void parse_extended_opts(ext2_filsys fs, const char *opts)
1039 {
1040         char    *buf, *token, *next, *p, *arg;
1041         int     len, hash_alg;
1042         int     r_usage = 0;
1043
1044         len = strlen(opts);
1045         buf = malloc(len+1);
1046         if (!buf) {
1047                 fprintf(stderr,
1048                         _("Couldn't allocate memory to parse options!\n"));
1049                 exit(1);
1050         }
1051         strcpy(buf, opts);
1052         for (token = buf; token && *token; token = next) {
1053                 p = strchr(token, ',');
1054                 next = 0;
1055                 if (p) {
1056                         *p = 0;
1057                         next = p+1;
1058                 }
1059                 arg = strchr(token, '=');
1060                 if (arg) {
1061                         *arg = 0;
1062                         arg++;
1063                 }
1064                 if (!strcmp(token, "test_fs")) {
1065                         fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1066                         printf("Setting test filesystem flag\n");
1067                         ext2fs_mark_super_dirty(fs);
1068                 } else if (!strcmp(token, "^test_fs")) {
1069                         fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1070                         printf("Clearing test filesystem flag\n");
1071                         ext2fs_mark_super_dirty(fs);
1072                 } else if (strcmp(token, "stride") == 0) {
1073                         if (!arg) {
1074                                 r_usage++;
1075                                 continue;
1076                         }
1077                         stride = strtoul(arg, &p, 0);
1078                         if (*p) {
1079                                 fprintf(stderr,
1080                                        _("Invalid RAID stride: %s\n"),
1081                                         arg);
1082                                 r_usage++;
1083                                 continue;
1084                         }
1085                         stride_set = 1;
1086                 } else if (strcmp(token, "stripe-width") == 0 ||
1087                            strcmp(token, "stripe_width") == 0) {
1088                         if (!arg) {
1089                                 r_usage++;
1090                                 continue;
1091                         }
1092                         stripe_width = strtoul(arg, &p, 0);
1093                         if (*p) {
1094                                 fprintf(stderr,
1095                                         _("Invalid RAID stripe-width: %s\n"),
1096                                         arg);
1097                                 r_usage++;
1098                                 continue;
1099                         }
1100                         stripe_width_set = 1;
1101                 } else if (strcmp(token, "hash_alg") == 0 ||
1102                            strcmp(token, "hash-alg") == 0) {
1103                         if (!arg) {
1104                                 r_usage++;
1105                                 continue;
1106                         }
1107                         hash_alg = e2p_string2hash(arg);
1108                         if (hash_alg < 0) {
1109                                 fprintf(stderr,
1110                                         _("Invalid hash algorithm: %s\n"),
1111                                         arg);
1112                                 r_usage++;
1113                                 continue;
1114                         }
1115                         fs->super->s_def_hash_version = hash_alg;
1116                         printf(_("Setting default hash algorithm "
1117                                  "to %s (%d)\n"),
1118                                arg, hash_alg);
1119                         ext2fs_mark_super_dirty(fs);
1120                 } else if (!strcmp(token, "mount_opts")) {
1121                         if (!arg) {
1122                                 r_usage++;
1123                                 continue;
1124                         }
1125                         if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1126                                 fprintf(stderr,
1127                                         "Extended mount options too long\n");
1128                                 continue;
1129                         }
1130                         ext_mount_opts = strdup(arg);
1131                 } else
1132                         r_usage++;
1133         }
1134         if (r_usage) {
1135                 fprintf(stderr, _("\nBad options specified.\n\n"
1136                         "Extended options are separated by commas, "
1137                         "and may take an argument which\n"
1138                         "\tis set off by an equals ('=') sign.\n\n"
1139                         "Valid extended options are:\n"
1140                         "\thash_alg=<hash algorithm>\n"
1141                         "\tmount_opts=<extended default mount options>\n"
1142                         "\tstride=<RAID per-disk chunk size in blocks>\n"
1143                         "\tstripe_width=<RAID stride*data disks in blocks>\n"
1144                         "\ttest_fs\n"
1145                         "\t^test_fs\n"));
1146                 free(buf);
1147                 exit(1);
1148         }
1149         free(buf);
1150 }
1151
1152 /*
1153  * Fill in the block bitmap bmap with the information regarding the
1154  * blocks to be moved
1155  */
1156 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1157                             ext2fs_block_bitmap bmap)
1158 {
1159         dgrp_t i;
1160         int retval;
1161         ext2_badblocks_list bb_list = 0;
1162         blk64_t j, needed_blocks = 0;
1163         blk64_t start_blk, end_blk;
1164
1165         retval = ext2fs_read_bb_inode(fs, &bb_list);
1166         if (retval)
1167                 return retval;
1168
1169         for (i = 0; i < fs->group_desc_count; i++) {
1170                 start_blk = ext2fs_inode_table_loc(fs, i) +
1171                                         fs->inode_blocks_per_group;
1172
1173                 end_blk = ext2fs_inode_table_loc(fs, i) +
1174                                         new_ino_blks_per_grp;
1175
1176                 for (j = start_blk; j < end_blk; j++) {
1177                         if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1178                                 /*
1179                                  * IF the block is a bad block we fail
1180                                  */
1181                                 if (ext2fs_badblocks_list_test(bb_list, j)) {
1182                                         ext2fs_badblocks_list_free(bb_list);
1183                                         return ENOSPC;
1184                                 }
1185
1186                                 ext2fs_mark_block_bitmap2(bmap, j);
1187                         } else {
1188                                 /*
1189                                  * We are going to use this block for
1190                                  * inode table. So mark them used.
1191                                  */
1192                                 ext2fs_mark_block_bitmap2(fs->block_map, j);
1193                         }
1194                 }
1195                 needed_blocks += end_blk - start_blk;
1196         }
1197
1198         ext2fs_badblocks_list_free(bb_list);
1199         if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1200                 return ENOSPC;
1201
1202         return 0;
1203 }
1204
1205 static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1206 {
1207         dgrp_t group;
1208         group = ext2fs_group_of_blk(fs, blk);
1209         if (ext2fs_block_bitmap_loc(fs, group) == blk)
1210                 return 1;
1211         if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1212                 return 1;
1213         return 0;
1214 }
1215
1216 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1217 {
1218         blk_t start_blk, end_blk;
1219         start_blk = fs->super->s_first_data_block +
1220                         EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1221         /*
1222          * We cannot get new block beyond end_blk for for the last block group
1223          * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1224          */
1225         end_blk   = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1226         if (blk >= start_blk && blk <= end_blk)
1227                 return 1;
1228         return 0;
1229 }
1230
1231 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1232 {
1233
1234         char *buf;
1235         dgrp_t group = 0;
1236         errcode_t retval;
1237         int meta_data = 0;
1238         blk64_t blk, new_blk, goal;
1239         struct blk_move *bmv;
1240
1241         retval = ext2fs_get_mem(fs->blocksize, &buf);
1242         if (retval)
1243                 return retval;
1244
1245         for (new_blk = blk = fs->super->s_first_data_block;
1246              blk < ext2fs_blocks_count(fs->super); blk++) {
1247                 if (!ext2fs_test_block_bitmap2(bmap, blk))
1248                         continue;
1249
1250                 if (ext2fs_is_meta_block(fs, blk)) {
1251                         /*
1252                          * If the block is mapping a fs meta data block
1253                          * like group desc/block bitmap/inode bitmap. We
1254                          * should find a block in the same group and fix
1255                          * the respective fs metadata pointers. Otherwise
1256                          * fail
1257                          */
1258                         group = ext2fs_group_of_blk(fs, blk);
1259                         goal = ext2fs_group_first_block2(fs, group);
1260                         meta_data = 1;
1261
1262                 } else {
1263                         goal = new_blk;
1264                 }
1265                 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1266                 if (retval)
1267                         goto err_out;
1268
1269                 /* new fs meta data block should be in the same group */
1270                 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1271                         retval = ENOSPC;
1272                         goto err_out;
1273                 }
1274
1275                 /* Mark this block as allocated */
1276                 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1277
1278                 /* Add it to block move list */
1279                 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1280                 if (retval)
1281                         goto err_out;
1282
1283                 bmv->old_loc = blk;
1284                 bmv->new_loc = new_blk;
1285
1286                 list_add(&(bmv->list), &blk_move_list);
1287
1288                 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1289                 if (retval)
1290                         goto err_out;
1291
1292                 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1293                 if (retval)
1294                         goto err_out;
1295         }
1296
1297 err_out:
1298         ext2fs_free_mem(&buf);
1299         return retval;
1300 }
1301
1302 static blk64_t translate_block(blk64_t blk)
1303 {
1304         struct list_head *entry;
1305         struct blk_move *bmv;
1306
1307         list_for_each(entry, &blk_move_list) {
1308                 bmv = list_entry(entry, struct blk_move, list);
1309                 if (bmv->old_loc == blk)
1310                         return bmv->new_loc;
1311         }
1312
1313         return 0;
1314 }
1315
1316 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1317                          blk64_t *block_nr,
1318                          e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1319                          blk64_t ref_block EXT2FS_ATTR((unused)),
1320                          int ref_offset EXT2FS_ATTR((unused)),
1321                          void *priv_data)
1322 {
1323         int ret = 0;
1324         blk64_t new_blk;
1325         ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1326
1327         if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1328                 return 0;
1329         new_blk = translate_block(*block_nr);
1330         if (new_blk) {
1331                 *block_nr = new_blk;
1332                 /*
1333                  * This will force the ext2fs_write_inode in the iterator
1334                  */
1335                 ret |= BLOCK_CHANGED;
1336         }
1337
1338         return ret;
1339 }
1340
1341 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1342 {
1343         errcode_t retval = 0;
1344         ext2_ino_t ino;
1345         blk64_t blk;
1346         char *block_buf = 0;
1347         struct ext2_inode inode;
1348         ext2_inode_scan scan = NULL;
1349
1350         retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1351         if (retval)
1352                 return retval;
1353
1354         retval = ext2fs_open_inode_scan(fs, 0, &scan);
1355         if (retval)
1356                 goto err_out;
1357
1358         while (1) {
1359                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1360                 if (retval)
1361                         goto err_out;
1362
1363                 if (!ino)
1364                         break;
1365
1366                 if (inode.i_links_count == 0)
1367                         continue; /* inode not in use */
1368
1369                 /* FIXME!!
1370                  * If we end up modifying the journal inode
1371                  * the sb->s_jnl_blocks will differ. But a
1372                  * subsequent e2fsck fixes that.
1373                  * Do we need to fix this ??
1374                  */
1375
1376                 if (ext2fs_file_acl_block(&inode) &&
1377                     ext2fs_test_block_bitmap2(bmap,
1378                                               ext2fs_file_acl_block(&inode))) {
1379                         blk = translate_block(ext2fs_file_acl_block(&inode));
1380                         if (!blk)
1381                                 continue;
1382
1383                         ext2fs_file_acl_block_set(&inode, blk);
1384
1385                         /*
1386                          * Write the inode to disk so that inode table
1387                          * resizing can work
1388                          */
1389                         retval = ext2fs_write_inode(fs, ino, &inode);
1390                         if (retval)
1391                                 goto err_out;
1392                 }
1393
1394                 if (!ext2fs_inode_has_valid_blocks(&inode))
1395                         continue;
1396
1397                 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1398                                                process_block, bmap);
1399                 if (retval)
1400                         goto err_out;
1401
1402         }
1403
1404 err_out:
1405         ext2fs_free_mem(&block_buf);
1406
1407         return retval;
1408 }
1409
1410 /*
1411  * We need to scan for inode and block bitmaps that may need to be
1412  * moved.  This can take place if the filesystem was formatted for
1413  * RAID arrays using the mke2fs's extended option "stride".
1414  */
1415 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1416 {
1417         dgrp_t i;
1418         blk_t blk, new_blk;
1419
1420         for (i = 0; i < fs->group_desc_count; i++) {
1421                 blk = ext2fs_block_bitmap_loc(fs, i);
1422                 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1423                         new_blk = translate_block(blk);
1424                         if (!new_blk)
1425                                 continue;
1426                         ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1427                 }
1428
1429                 blk = ext2fs_inode_bitmap_loc(fs, i);
1430                 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1431                         new_blk = translate_block(blk);
1432                         if (!new_blk)
1433                                 continue;
1434                         ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1435                 }
1436         }
1437         return 0;
1438 }
1439
1440 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1441 {
1442         dgrp_t i;
1443         blk64_t blk;
1444         errcode_t retval;
1445         int new_ino_blks_per_grp;
1446         unsigned int j;
1447         char *old_itable = NULL, *new_itable = NULL;
1448         char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1449         unsigned long old_ino_size;
1450         int old_itable_size, new_itable_size;
1451
1452         old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1453         old_ino_size = EXT2_INODE_SIZE(fs->super);
1454
1455         new_ino_blks_per_grp = ext2fs_div_ceil(
1456                                         EXT2_INODES_PER_GROUP(fs->super) *
1457                                         new_ino_size,
1458                                         fs->blocksize);
1459
1460         new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1461
1462         retval = ext2fs_get_mem(old_itable_size, &old_itable);
1463         if (retval)
1464                 return retval;
1465
1466         retval = ext2fs_get_mem(new_itable_size, &new_itable);
1467         if (retval)
1468                 goto err_out;
1469
1470         tmp_old_itable = old_itable;
1471         tmp_new_itable = new_itable;
1472
1473         for (i = 0; i < fs->group_desc_count; i++) {
1474                 blk = ext2fs_inode_table_loc(fs, i);
1475                 retval = io_channel_read_blk64(fs->io, blk,
1476                                 fs->inode_blocks_per_group, old_itable);
1477                 if (retval)
1478                         goto err_out;
1479
1480                 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1481                         memcpy(new_itable, old_itable, old_ino_size);
1482
1483                         memset(new_itable+old_ino_size, 0,
1484                                         new_ino_size - old_ino_size);
1485
1486                         new_itable += new_ino_size;
1487                         old_itable += old_ino_size;
1488                 }
1489
1490                 /* reset the pointer */
1491                 old_itable = tmp_old_itable;
1492                 new_itable = tmp_new_itable;
1493
1494                 retval = io_channel_write_blk64(fs->io, blk,
1495                                         new_ino_blks_per_grp, new_itable);
1496                 if (retval)
1497                         goto err_out;
1498         }
1499
1500         /* Update the meta data */
1501         fs->inode_blocks_per_group = new_ino_blks_per_grp;
1502         fs->super->s_inode_size = new_ino_size;
1503
1504 err_out:
1505         if (old_itable)
1506                 ext2fs_free_mem(&old_itable);
1507
1508         if (new_itable)
1509                 ext2fs_free_mem(&new_itable);
1510
1511         return retval;
1512 }
1513
1514 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1515 {
1516         blk64_t         blk;
1517         ext2_ino_t      ino;
1518         unsigned int    group = 0;
1519         unsigned int    count = 0;
1520         int             total_free = 0;
1521         int             group_free = 0;
1522
1523         /*
1524          * First calculate the block statistics
1525          */
1526         for (blk = fs->super->s_first_data_block;
1527              blk < ext2fs_blocks_count(fs->super); blk++) {
1528                 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1529                         group_free++;
1530                         total_free++;
1531                 }
1532                 count++;
1533                 if ((count == fs->super->s_blocks_per_group) ||
1534                     (blk == ext2fs_blocks_count(fs->super)-1)) {
1535                         ext2fs_bg_free_blocks_count_set(fs, group++,
1536                                                         group_free);
1537                         count = 0;
1538                         group_free = 0;
1539                 }
1540         }
1541         total_free = EXT2FS_C2B(fs, total_free);
1542         ext2fs_free_blocks_count_set(fs->super, total_free);
1543
1544         /*
1545          * Next, calculate the inode statistics
1546          */
1547         group_free = 0;
1548         total_free = 0;
1549         count = 0;
1550         group = 0;
1551
1552         /* Protect loop from wrap-around if s_inodes_count maxed */
1553         for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1554                 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1555                         group_free++;
1556                         total_free++;
1557                 }
1558                 count++;
1559                 if ((count == fs->super->s_inodes_per_group) ||
1560                     (ino == fs->super->s_inodes_count)) {
1561                         ext2fs_bg_free_inodes_count_set(fs, group++,
1562                                                         group_free);
1563                         count = 0;
1564                         group_free = 0;
1565                 }
1566         }
1567         fs->super->s_free_inodes_count = total_free;
1568         ext2fs_mark_super_dirty(fs);
1569         return 0;
1570 }
1571
1572 #define list_for_each_safe(pos, pnext, head) \
1573         for (pos = (head)->next, pnext = pos->next; pos != (head); \
1574              pos = pnext, pnext = pos->next)
1575
1576 static void free_blk_move_list(void)
1577 {
1578         struct list_head *entry, *tmp;
1579         struct blk_move *bmv;
1580
1581         list_for_each_safe(entry, tmp, &blk_move_list) {
1582                 bmv = list_entry(entry, struct blk_move, list);
1583                 list_del(entry);
1584                 ext2fs_free_mem(&bmv);
1585         }
1586         return;
1587 }
1588
1589 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1590 {
1591         errcode_t retval;
1592         int new_ino_blks_per_grp;
1593         ext2fs_block_bitmap bmap;
1594
1595         retval = ext2fs_read_inode_bitmap(fs);
1596         if (retval) {
1597                 fputs(_("Failed to read inode bitmap\n"), stderr);
1598                 return retval;
1599         }
1600         retval = ext2fs_read_block_bitmap(fs);
1601         if (retval) {
1602                 fputs(_("Failed to read blockbitmap\n"), stderr);
1603                 return retval;
1604         }
1605         INIT_LIST_HEAD(&blk_move_list);
1606
1607
1608         new_ino_blks_per_grp = ext2fs_div_ceil(
1609                                         EXT2_INODES_PER_GROUP(fs->super)*
1610                                         new_size,
1611                                         fs->blocksize);
1612
1613         /* We may change the file system.
1614          * Mark the file system as invalid so that
1615          * the user is prompted to run fsck.
1616          */
1617         fs->super->s_state &= ~EXT2_VALID_FS;
1618
1619         retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1620                                                 &bmap);
1621         if (retval) {
1622                 fputs(_("Failed to allocate block bitmap when "
1623                                 "increasing inode size\n"), stderr);
1624                 return retval;
1625         }
1626         retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1627         if (retval) {
1628                 fputs(_("Not enough space to increase inode size \n"), stderr);
1629                 goto err_out;
1630         }
1631         retval = move_block(fs, bmap);
1632         if (retval) {
1633                 fputs(_("Failed to relocate blocks during inode resize \n"),
1634                       stderr);
1635                 goto err_out;
1636         }
1637         retval = inode_scan_and_fix(fs, bmap);
1638         if (retval)
1639                 goto err_out_undo;
1640
1641         retval = group_desc_scan_and_fix(fs, bmap);
1642         if (retval)
1643                 goto err_out_undo;
1644
1645         retval = expand_inode_table(fs, new_size);
1646         if (retval)
1647                 goto err_out_undo;
1648
1649         ext2fs_calculate_summary_stats(fs);
1650
1651         fs->super->s_state |= EXT2_VALID_FS;
1652         /* mark super block and block bitmap as dirty */
1653         ext2fs_mark_super_dirty(fs);
1654         ext2fs_mark_bb_dirty(fs);
1655
1656 err_out:
1657         free_blk_move_list();
1658         ext2fs_free_block_bitmap(bmap);
1659
1660         return retval;
1661
1662 err_out_undo:
1663         free_blk_move_list();
1664         ext2fs_free_block_bitmap(bmap);
1665         fputs(_("Error in resizing the inode size.\n"
1666                         "Run e2undo to undo the "
1667                         "file system changes. \n"), stderr);
1668
1669         return retval;
1670 }
1671
1672 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1673 {
1674         errcode_t retval = 0;
1675         const char *tdb_dir;
1676         char *tdb_file;
1677         char *dev_name, *tmp_name;
1678
1679 #if 0 /* FIXME!! */
1680         /*
1681          * Configuration via a conf file would be
1682          * nice
1683          */
1684         profile_get_string(profile, "scratch_files",
1685                                         "directory", 0, 0,
1686                                         &tdb_dir);
1687 #endif
1688         tmp_name = strdup(name);
1689         if (!tmp_name) {
1690         alloc_fn_fail:
1691                 com_err(program_name, ENOMEM, 
1692                         _("Couldn't allocate memory for tdb filename\n"));
1693                 return ENOMEM;
1694         }
1695         dev_name = basename(tmp_name);
1696
1697         tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1698         if (!tdb_dir)
1699                 tdb_dir = "/var/lib/e2fsprogs";
1700
1701         if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1702             access(tdb_dir, W_OK))
1703                 return 0;
1704
1705         tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1706         if (!tdb_file)
1707                 goto alloc_fn_fail;
1708         sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1709
1710         if (!access(tdb_file, F_OK)) {
1711                 if (unlink(tdb_file) < 0) {
1712                         retval = errno;
1713                         com_err(program_name, retval,
1714                                 _("while trying to delete %s"),
1715                                 tdb_file);
1716                         free(tdb_file);
1717                         return retval;
1718                 }
1719         }
1720
1721         set_undo_io_backing_manager(*io_ptr);
1722         *io_ptr = undo_io_manager;
1723         set_undo_io_backup_file(tdb_file);
1724         printf(_("To undo the tune2fs operation please run "
1725                  "the command\n    e2undo %s %s\n\n"),
1726                  tdb_file, name);
1727         free(tdb_file);
1728         free(tmp_name);
1729         return retval;
1730 }
1731
1732 int main(int argc, char **argv)
1733 {
1734         errcode_t retval;
1735         ext2_filsys fs;
1736         struct ext2_super_block *sb;
1737         io_manager io_ptr, io_ptr_orig = NULL;
1738
1739 #ifdef ENABLE_NLS
1740         setlocale(LC_MESSAGES, "");
1741         setlocale(LC_CTYPE, "");
1742         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1743         textdomain(NLS_CAT_NAME);
1744 #endif
1745         if (argc && *argv)
1746                 program_name = *argv;
1747         add_error_table(&et_ext2_error_table);
1748
1749 #ifdef CONFIG_BUILD_FINDFS
1750         if (strcmp(get_progname(argv[0]), "findfs") == 0)
1751                 do_findfs(argc, argv);
1752 #endif
1753         if (strcmp(get_progname(argv[0]), "e2label") == 0)
1754                 parse_e2label_options(argc, argv);
1755         else
1756                 parse_tune2fs_options(argc, argv);
1757
1758 #ifdef CONFIG_TESTIO_DEBUG
1759         if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1760                 io_ptr = test_io_manager;
1761                 test_io_backing_manager = unix_io_manager;
1762         } else
1763 #endif
1764                 io_ptr = unix_io_manager;
1765
1766 retry_open:
1767         retval = ext2fs_open2(device_name, io_options, open_flag,
1768                               0, 0, io_ptr, &fs);
1769         if (retval) {
1770                         com_err(program_name, retval,
1771                                 _("while trying to open %s"),
1772                         device_name);
1773                 fprintf(stderr,
1774                         _("Couldn't find valid filesystem superblock.\n"));
1775                 exit(1);
1776         }
1777
1778         if (I_flag && !io_ptr_orig) {
1779                 /*
1780                  * Check the inode size is right so we can issue an
1781                  * error message and bail before setting up the tdb
1782                  * file.
1783                  */
1784                 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1785                         fprintf(stderr, _("The inode size is already %lu\n"),
1786                                 new_inode_size);
1787                         exit(1);
1788                 }
1789                 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1790                         fprintf(stderr, _("Shrinking the inode size is "
1791                                           "not supported\n"));
1792                         exit(1);
1793                 }
1794
1795                 /*
1796                  * If inode resize is requested use the
1797                  * Undo I/O manager
1798                  */
1799                 io_ptr_orig = io_ptr;
1800                 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1801                 if (retval)
1802                         exit(1);
1803                 if (io_ptr != io_ptr_orig) {
1804                         ext2fs_close(fs);
1805                         goto retry_open;
1806                 }
1807         }
1808
1809         sb = fs->super;
1810         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1811
1812         if (print_label) {
1813                 /* For e2label emulation */
1814                 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1815                        sb->s_volume_name);
1816                 remove_error_table(&et_ext2_error_table);
1817                 exit(0);
1818         }
1819
1820         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1821         if (retval) {
1822                 com_err("ext2fs_check_if_mount", retval,
1823                         _("while determining whether %s is mounted."),
1824                         device_name);
1825                 exit(1);
1826         }
1827         /* Normally we only need to write out the superblock */
1828         fs->flags |= EXT2_FLAG_SUPER_ONLY;
1829
1830         if (c_flag) {
1831                 sb->s_max_mnt_count = max_mount_count;
1832                 ext2fs_mark_super_dirty(fs);
1833                 printf(_("Setting maximal mount count to %d\n"),
1834                        max_mount_count);
1835         }
1836         if (C_flag) {
1837                 sb->s_mnt_count = mount_count;
1838                 ext2fs_mark_super_dirty(fs);
1839                 printf(_("Setting current mount count to %d\n"), mount_count);
1840         }
1841         if (e_flag) {
1842                 sb->s_errors = errors;
1843                 ext2fs_mark_super_dirty(fs);
1844                 printf(_("Setting error behavior to %d\n"), errors);
1845         }
1846         if (g_flag) {
1847                 sb->s_def_resgid = resgid;
1848                 ext2fs_mark_super_dirty(fs);
1849                 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
1850         }
1851         if (i_flag) {
1852                 if (interval >= (1ULL << 32)) {
1853                         com_err(program_name, 0,
1854                                 _("interval between checks is too big (%lu)"),
1855                                 interval);
1856                         exit(1);
1857                 }
1858                 sb->s_checkinterval = interval;
1859                 ext2fs_mark_super_dirty(fs);
1860                 printf(_("Setting interval between checks to %lu seconds\n"),
1861                        interval);
1862         }
1863         if (m_flag) {
1864                 ext2fs_r_blocks_count_set(sb, reserved_ratio *
1865                                           ext2fs_blocks_count(sb) / 100.0);
1866                 ext2fs_mark_super_dirty(fs);
1867                 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
1868                         reserved_ratio, ext2fs_r_blocks_count(sb));
1869         }
1870         if (r_flag) {
1871                 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
1872                         com_err(program_name, 0,
1873                                 _("reserved blocks count is too big (%llu)"),
1874                                 reserved_blocks);
1875                         exit(1);
1876                 }
1877                 ext2fs_r_blocks_count_set(sb, reserved_blocks);
1878                 ext2fs_mark_super_dirty(fs);
1879                 printf(_("Setting reserved blocks count to %llu\n"),
1880                        reserved_blocks);
1881         }
1882         if (s_flag == 1) {
1883                 if (sb->s_feature_ro_compat &
1884                     EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1885                         fputs(_("\nThe filesystem already has sparse "
1886                                 "superblocks.\n"), stderr);
1887                 else {
1888                         sb->s_feature_ro_compat |=
1889                                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1890                         sb->s_state &= ~EXT2_VALID_FS;
1891                         ext2fs_mark_super_dirty(fs);
1892                         printf(_("\nSparse superblock flag set.  %s"),
1893                                _(please_fsck));
1894                 }
1895         }
1896         if (s_flag == 0) {
1897                 fputs(_("\nClearing the sparse superflag not supported.\n"),
1898                       stderr);
1899                 exit(1);
1900         }
1901         if (T_flag) {
1902                 sb->s_lastcheck = last_check_time;
1903                 ext2fs_mark_super_dirty(fs);
1904                 printf(_("Setting time filesystem last checked to %s\n"),
1905                        ctime(&last_check_time));
1906         }
1907         if (u_flag) {
1908                 sb->s_def_resuid = resuid;
1909                 ext2fs_mark_super_dirty(fs);
1910                 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
1911         }
1912         if (L_flag) {
1913                 if (strlen(new_label) > sizeof(sb->s_volume_name))
1914                         fputs(_("Warning: label too long, truncating.\n"),
1915                               stderr);
1916                 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1917                 strncpy(sb->s_volume_name, new_label,
1918                         sizeof(sb->s_volume_name));
1919                 ext2fs_mark_super_dirty(fs);
1920         }
1921         if (M_flag) {
1922                 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1923                 strncpy(sb->s_last_mounted, new_last_mounted,
1924                         sizeof(sb->s_last_mounted));
1925                 ext2fs_mark_super_dirty(fs);
1926         }
1927         if (mntopts_cmd)
1928                 update_mntopts(fs, mntopts_cmd);
1929         if (features_cmd)
1930                 update_feature_set(fs, features_cmd);
1931         if (extended_cmd)
1932                 parse_extended_opts(fs, extended_cmd);
1933         if (journal_size || journal_device)
1934                 add_journal(fs);
1935
1936         if (Q_flag) {
1937                 if (mount_flags & EXT2_MF_MOUNTED) {
1938                         fputs(_("The quota feature may only be changed when "
1939                                 "the filesystem is unmounted.\n"), stderr);
1940                         exit(1);
1941                 }
1942                 handle_quota_options(fs);
1943         }
1944
1945         if (U_flag) {
1946                 int set_csum = 0;
1947                 dgrp_t i;
1948
1949                 if (sb->s_feature_ro_compat &
1950                     EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
1951                         /*
1952                          * Determine if the block group checksums are
1953                          * correct so we know whether or not to set
1954                          * them later on.
1955                          */
1956                         for (i = 0; i < fs->group_desc_count; i++)
1957                                 if (!ext2fs_group_desc_csum_verify(fs, i))
1958                                         break;
1959                         if (i >= fs->group_desc_count)
1960                                 set_csum = 1;
1961                 }
1962                 if ((strcasecmp(new_UUID, "null") == 0) ||
1963                     (strcasecmp(new_UUID, "clear") == 0)) {
1964                         uuid_clear(sb->s_uuid);
1965                 } else if (strcasecmp(new_UUID, "time") == 0) {
1966                         uuid_generate_time(sb->s_uuid);
1967                 } else if (strcasecmp(new_UUID, "random") == 0) {
1968                         uuid_generate(sb->s_uuid);
1969                 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1970                         com_err(program_name, 0, _("Invalid UUID format\n"));
1971                         exit(1);
1972                 }
1973                 if (set_csum) {
1974                         for (i = 0; i < fs->group_desc_count; i++)
1975                                 ext2fs_group_desc_csum_set(fs, i);
1976                         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1977                 }
1978                 ext2fs_mark_super_dirty(fs);
1979         }
1980         if (I_flag) {
1981                 if (mount_flags & EXT2_MF_MOUNTED) {
1982                         fputs(_("The inode size may only be "
1983                                 "changed when the filesystem is "
1984                                 "unmounted.\n"), stderr);
1985                         exit(1);
1986                 }
1987                 if (fs->super->s_feature_incompat &
1988                     EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1989                         fputs(_("Changing the inode size not supported for "
1990                                 "filesystems with the flex_bg\n"
1991                                 "feature enabled.\n"),
1992                               stderr);
1993                         exit(1);
1994                 }
1995                 /*
1996                  * We want to update group descriptor also
1997                  * with the new free inode count
1998                  */
1999                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2000                 if (resize_inode(fs, new_inode_size) == 0) {
2001                         printf(_("Setting inode size %lu\n"),
2002                                                         new_inode_size);
2003                 } else {
2004                         printf(_("Failed to change inode size\n"));
2005                         exit(1);
2006                 }
2007         }
2008
2009         if (l_flag)
2010                 list_super(sb);
2011         if (stride_set) {
2012                 sb->s_raid_stride = stride;
2013                 ext2fs_mark_super_dirty(fs);
2014                 printf(_("Setting stride size to %d\n"), stride);
2015         }
2016         if (stripe_width_set) {
2017                 sb->s_raid_stripe_width = stripe_width;
2018                 ext2fs_mark_super_dirty(fs);
2019                 printf(_("Setting stripe width to %d\n"), stripe_width);
2020         }
2021         if (ext_mount_opts) {
2022                 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2023                         sizeof(fs->super->s_mount_opts));
2024                 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2025                 ext2fs_mark_super_dirty(fs);
2026                 printf(_("Setting extended default mount options to '%s'\n"),
2027                        ext_mount_opts);
2028                 free(ext_mount_opts);
2029         }
2030         free(device_name);
2031         remove_error_table(&et_ext2_error_table);
2032         return (ext2fs_close(fs) ? 1 : 0);
2033 }