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