Whamcloud - gitweb
tune2fs: Add support for turning on quota feature
[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);
611         return;
612
613 err:
614         free(journal_device);
615         exit(1);
616 }
617
618 void handle_quota_options(ext2_filsys fs)
619 {
620         quota_ctx_t qctx;
621         errcode_t retval;
622         ext2_ino_t qf_ino;
623
624         if (!usrquota && !grpquota)
625                 /* Nothing to do. */
626                 return;
627
628         init_quota_context(&qctx, fs, -1);
629
630         if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
631                 if ((qf_ino = quota_file_exists(fs, USRQUOTA, QFMT_VFS_V1)) > 0)
632                         set_sb_quota_inum(fs, qf_ino, USRQUOTA);
633                 else
634                         write_quota_inode(qctx, USRQUOTA);
635         } else if (usrquota == QOPT_DISABLE) {
636                 remove_quota_inode(fs, USRQUOTA);
637         }
638
639         if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
640                 if ((qf_ino = quota_file_exists(fs, GRPQUOTA, QFMT_VFS_V1)) > 0)
641                         set_sb_quota_inum(fs, qf_ino, GRPQUOTA);
642                 else
643                         write_quota_inode(qctx, GRPQUOTA);
644         } else if (grpquota == QOPT_DISABLE) {
645                 remove_quota_inode(fs, GRPQUOTA);
646         }
647
648         release_quota_context(&qctx);
649
650         if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
651                 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
652                 ext2fs_mark_super_dirty(fs);
653         } else if ((usrquota == QOPT_DISABLE) && (grpquota == QOPT_DISABLE)) {
654                 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
655                 ext2fs_mark_super_dirty(fs);
656         }
657
658         return;
659 }
660
661 void parse_quota_opts(const char *opts)
662 {
663         char    *buf, *token, *next, *p, *arg;
664         int     len;
665
666         len = strlen(opts);
667         buf = malloc(len+1);
668         if (!buf) {
669                 fputs(_("Couldn't allocate memory to parse quota "
670                         "options!\n"), stderr);
671                 exit(1);
672         }
673         strcpy(buf, opts);
674         for (token = buf; token && *token; token = next) {
675                 p = strchr(token, ',');
676                 next = 0;
677                 if (p) {
678                         *p = 0;
679                         next = p+1;
680                 }
681
682                 if (strcmp(token, "usrquota") == 0) {
683                         usrquota = QOPT_ENABLE;
684                 } else if (strcmp(token, "^usrquota") == 0) {
685                         usrquota = QOPT_DISABLE;
686                 } else if (strcmp(token, "grpquota") == 0) {
687                         grpquota = QOPT_ENABLE;
688                 } else if (strcmp(token, "^grpquota") == 0) {
689                         grpquota = QOPT_DISABLE;
690                 } else {
691                         fputs(_("\nBad quota options specified.\n\n"
692                                 "Following valid quota options are available "
693                                 "(pass by separating with comma):\n"
694                                 "\t[^]usrquota\n"
695                                 "\t[^]grpquota\n"
696                                 "\n\n"), stderr);
697                         free(buf);
698                         exit(1);
699                 }
700         }
701         free(buf);
702 }
703
704
705
706 static void parse_e2label_options(int argc, char ** argv)
707 {
708         if ((argc < 2) || (argc > 3)) {
709                 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
710                 exit(1);
711         }
712         io_options = strchr(argv[1], '?');
713         if (io_options)
714                 *io_options++ = 0;
715         device_name = blkid_get_devname(NULL, argv[1], NULL);
716         if (!device_name) {
717                 com_err("e2label", 0, _("Unable to resolve '%s'"),
718                         argv[1]);
719                 exit(1);
720         }
721         open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
722         if (argc == 3) {
723                 open_flag |= EXT2_FLAG_RW;
724                 L_flag = 1;
725                 new_label = argv[2];
726         } else
727                 print_label++;
728 }
729
730 static time_t parse_time(char *str)
731 {
732         struct  tm      ts;
733
734         if (strcmp(str, "now") == 0) {
735                 return (time(0));
736         }
737         memset(&ts, 0, sizeof(ts));
738 #ifdef HAVE_STRPTIME
739         strptime(str, "%Y%m%d%H%M%S", &ts);
740 #else
741         sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
742                &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
743         ts.tm_year -= 1900;
744         ts.tm_mon -= 1;
745         if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
746             ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
747             ts.tm_min > 59 || ts.tm_sec > 61)
748                 ts.tm_mday = 0;
749 #endif
750         if (ts.tm_mday == 0) {
751                 com_err(program_name, 0,
752                         _("Couldn't parse date/time specifier: %s"),
753                         str);
754                 usage();
755         }
756         ts.tm_isdst = -1;
757         return (mktime(&ts));
758 }
759
760 static void parse_tune2fs_options(int argc, char **argv)
761 {
762         int c;
763         char *tmp;
764         struct group *gr;
765         struct passwd *pw;
766
767         open_flag = 0;
768
769         printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
770         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)
771                 switch (c) {
772                 case 'c':
773                         max_mount_count = strtol(optarg, &tmp, 0);
774                         if (*tmp || max_mount_count > 16000) {
775                                 com_err(program_name, 0,
776                                         _("bad mounts count - %s"),
777                                         optarg);
778                                 usage();
779                         }
780                         if (max_mount_count == 0)
781                                 max_mount_count = -1;
782                         c_flag = 1;
783                         open_flag = EXT2_FLAG_RW;
784                         break;
785                 case 'C':
786                         mount_count = strtoul(optarg, &tmp, 0);
787                         if (*tmp || mount_count > 16000) {
788                                 com_err(program_name, 0,
789                                         _("bad mounts count - %s"),
790                                         optarg);
791                                 usage();
792                         }
793                         C_flag = 1;
794                         open_flag = EXT2_FLAG_RW;
795                         break;
796                 case 'e':
797                         if (strcmp(optarg, "continue") == 0)
798                                 errors = EXT2_ERRORS_CONTINUE;
799                         else if (strcmp(optarg, "remount-ro") == 0)
800                                 errors = EXT2_ERRORS_RO;
801                         else if (strcmp(optarg, "panic") == 0)
802                                 errors = EXT2_ERRORS_PANIC;
803                         else {
804                                 com_err(program_name, 0,
805                                         _("bad error behavior - %s"),
806                                         optarg);
807                                 usage();
808                         }
809                         e_flag = 1;
810                         open_flag = EXT2_FLAG_RW;
811                         break;
812                 case 'E':
813                         extended_cmd = optarg;
814                         open_flag |= EXT2_FLAG_RW;
815                         break;
816                 case 'f': /* Force */
817                         f_flag = 1;
818                         break;
819                 case 'g':
820                         resgid = strtoul(optarg, &tmp, 0);
821                         if (*tmp) {
822                                 gr = getgrnam(optarg);
823                                 if (gr == NULL)
824                                         tmp = optarg;
825                                 else {
826                                         resgid = gr->gr_gid;
827                                         *tmp = 0;
828                                 }
829                         }
830                         if (*tmp) {
831                                 com_err(program_name, 0,
832                                         _("bad gid/group name - %s"),
833                                         optarg);
834                                 usage();
835                         }
836                         g_flag = 1;
837                         open_flag = EXT2_FLAG_RW;
838                         break;
839                 case 'i':
840                         interval = strtoul(optarg, &tmp, 0);
841                         switch (*tmp) {
842                         case 's':
843                                 tmp++;
844                                 break;
845                         case '\0':
846                         case 'd':
847                         case 'D': /* days */
848                                 interval *= 86400;
849                                 if (*tmp != '\0')
850                                         tmp++;
851                                 break;
852                         case 'm':
853                         case 'M': /* months! */
854                                 interval *= 86400 * 30;
855                                 tmp++;
856                                 break;
857                         case 'w':
858                         case 'W': /* weeks */
859                                 interval *= 86400 * 7;
860                                 tmp++;
861                                 break;
862                         }
863                         if (*tmp) {
864                                 com_err(program_name, 0,
865                                         _("bad interval - %s"), optarg);
866                                 usage();
867                         }
868                         i_flag = 1;
869                         open_flag = EXT2_FLAG_RW;
870                         break;
871                 case 'j':
872                         if (!journal_size)
873                                 journal_size = -1;
874                         open_flag = EXT2_FLAG_RW;
875                         break;
876                 case 'J':
877                         parse_journal_opts(optarg);
878                         open_flag = EXT2_FLAG_RW;
879                         break;
880                 case 'l':
881                         l_flag = 1;
882                         break;
883                 case 'L':
884                         new_label = optarg;
885                         L_flag = 1;
886                         open_flag |= EXT2_FLAG_RW |
887                                 EXT2_FLAG_JOURNAL_DEV_OK;
888                         break;
889                 case 'm':
890                         reserved_ratio = strtod(optarg, &tmp);
891                         if (*tmp || reserved_ratio > 50 ||
892                             reserved_ratio < 0) {
893                                 com_err(program_name, 0,
894                                         _("bad reserved block ratio - %s"),
895                                         optarg);
896                                 usage();
897                         }
898                         m_flag = 1;
899                         open_flag = EXT2_FLAG_RW;
900                         break;
901                 case 'M':
902                         new_last_mounted = optarg;
903                         M_flag = 1;
904                         open_flag = EXT2_FLAG_RW;
905                         break;
906                 case 'o':
907                         if (mntopts_cmd) {
908                                 com_err(program_name, 0,
909                                         _("-o may only be specified once"));
910                                 usage();
911                         }
912                         mntopts_cmd = optarg;
913                         open_flag = EXT2_FLAG_RW;
914                         break;
915                         
916                 case 'O':
917                         if (features_cmd) {
918                                 com_err(program_name, 0,
919                                         _("-O may only be specified once"));
920                                 usage();
921                         }
922                         features_cmd = optarg;
923                         open_flag = EXT2_FLAG_RW;
924                         break;
925                 case 'Q':
926                         Q_flag = 1;
927                         parse_quota_opts(optarg);
928                         open_flag = EXT2_FLAG_RW;
929                         break;
930                 case 'r':
931                         reserved_blocks = strtoul(optarg, &tmp, 0);
932                         if (*tmp) {
933                                 com_err(program_name, 0,
934                                         _("bad reserved blocks count - %s"),
935                                         optarg);
936                                 usage();
937                         }
938                         r_flag = 1;
939                         open_flag = EXT2_FLAG_RW;
940                         break;
941                 case 's': /* Deprecated */
942                         s_flag = atoi(optarg);
943                         open_flag = EXT2_FLAG_RW;
944                         break;
945                 case 'T':
946                         T_flag = 1;
947                         last_check_time = parse_time(optarg);
948                         open_flag = EXT2_FLAG_RW;
949                         break;
950                 case 'u':
951                                 resuid = strtoul(optarg, &tmp, 0);
952                                 if (*tmp) {
953                                         pw = getpwnam(optarg);
954                                         if (pw == NULL)
955                                                 tmp = optarg;
956                                         else {
957                                                 resuid = pw->pw_uid;
958                                                 *tmp = 0;
959                                         }
960                                 }
961                                 if (*tmp) {
962                                         com_err(program_name, 0,
963                                                 _("bad uid/user name - %s"),
964                                                 optarg);
965                                         usage();
966                                 }
967                                 u_flag = 1;
968                                 open_flag = EXT2_FLAG_RW;
969                                 break;
970                 case 'U':
971                         new_UUID = optarg;
972                         U_flag = 1;
973                         open_flag = EXT2_FLAG_RW |
974                                 EXT2_FLAG_JOURNAL_DEV_OK;
975                         break;
976                 case 'I':
977                         new_inode_size = strtoul(optarg, &tmp, 0);
978                         if (*tmp) {
979                                 com_err(program_name, 0,
980                                         _("bad inode size - %s"),
981                                         optarg);
982                                 usage();
983                         }
984                         if (!((new_inode_size &
985                                (new_inode_size - 1)) == 0)) {
986                                 com_err(program_name, 0,
987                                         _("Inode size must be a "
988                                           "power of two- %s"),
989                                         optarg);
990                                 usage();
991                         }
992                         open_flag = EXT2_FLAG_RW;
993                         I_flag = 1;
994                         break;
995                 default:
996                         usage();
997                 }
998         if (optind < argc - 1 || optind == argc)
999                 usage();
1000         if (!open_flag && !l_flag)
1001                 usage();
1002         io_options = strchr(argv[optind], '?');
1003         if (io_options)
1004                 *io_options++ = 0;
1005         device_name = blkid_get_devname(NULL, argv[optind], NULL);
1006         if (!device_name) {
1007                 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
1008                         argv[optind]);
1009                 exit(1);
1010         }
1011 }
1012
1013 #ifdef CONFIG_BUILD_FINDFS
1014 void do_findfs(int argc, char **argv)
1015 {
1016         char    *dev;
1017
1018         if ((argc != 2) ||
1019             (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1020                 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1021                 exit(2);
1022         }
1023         dev = blkid_get_devname(NULL, argv[1], NULL);
1024         if (!dev) {
1025                 com_err("findfs", 0, _("Unable to resolve '%s'"),
1026                         argv[1]);
1027                 exit(1);
1028         }
1029         puts(dev);
1030         exit(0);
1031 }
1032 #endif
1033
1034 static void parse_extended_opts(ext2_filsys fs, const char *opts)
1035 {
1036         char    *buf, *token, *next, *p, *arg;
1037         int     len, hash_alg;
1038         int     r_usage = 0;
1039
1040         len = strlen(opts);
1041         buf = malloc(len+1);
1042         if (!buf) {
1043                 fprintf(stderr,
1044                         _("Couldn't allocate memory to parse options!\n"));
1045                 exit(1);
1046         }
1047         strcpy(buf, opts);
1048         for (token = buf; token && *token; token = next) {
1049                 p = strchr(token, ',');
1050                 next = 0;
1051                 if (p) {
1052                         *p = 0;
1053                         next = p+1;
1054                 }
1055                 arg = strchr(token, '=');
1056                 if (arg) {
1057                         *arg = 0;
1058                         arg++;
1059                 }
1060                 if (!strcmp(token, "test_fs")) {
1061                         fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1062                         printf("Setting test filesystem flag\n");
1063                         ext2fs_mark_super_dirty(fs);
1064                 } else if (!strcmp(token, "^test_fs")) {
1065                         fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1066                         printf("Clearing test filesystem flag\n");
1067                         ext2fs_mark_super_dirty(fs);
1068                 } else if (strcmp(token, "stride") == 0) {
1069                         if (!arg) {
1070                                 r_usage++;
1071                                 continue;
1072                         }
1073                         stride = strtoul(arg, &p, 0);
1074                         if (*p) {
1075                                 fprintf(stderr,
1076                                        _("Invalid RAID stride: %s\n"),
1077                                         arg);
1078                                 r_usage++;
1079                                 continue;
1080                         }
1081                         stride_set = 1;
1082                 } else if (strcmp(token, "stripe-width") == 0 ||
1083                            strcmp(token, "stripe_width") == 0) {
1084                         if (!arg) {
1085                                 r_usage++;
1086                                 continue;
1087                         }
1088                         stripe_width = strtoul(arg, &p, 0);
1089                         if (*p) {
1090                                 fprintf(stderr,
1091                                         _("Invalid RAID stripe-width: %s\n"),
1092                                         arg);
1093                                 r_usage++;
1094                                 continue;
1095                         }
1096                         stripe_width_set = 1;
1097                 } else if (strcmp(token, "hash_alg") == 0 ||
1098                            strcmp(token, "hash-alg") == 0) {
1099                         if (!arg) {
1100                                 r_usage++;
1101                                 continue;
1102                         }
1103                         hash_alg = e2p_string2hash(arg);
1104                         if (hash_alg < 0) {
1105                                 fprintf(stderr,
1106                                         _("Invalid hash algorithm: %s\n"),
1107                                         arg);
1108                                 r_usage++;
1109                                 continue;
1110                         }
1111                         fs->super->s_def_hash_version = hash_alg;
1112                         printf(_("Setting default hash algorithm "
1113                                  "to %s (%d)\n"),
1114                                arg, hash_alg);
1115                         ext2fs_mark_super_dirty(fs);
1116                 } else if (!strcmp(token, "mount_opts")) {
1117                         if (!arg) {
1118                                 r_usage++;
1119                                 continue;
1120                         }
1121                         if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1122                                 fprintf(stderr,
1123                                         "Extended mount options too long\n");
1124                                 continue;
1125                         }
1126                         ext_mount_opts = strdup(arg);
1127                 } else
1128                         r_usage++;
1129         }
1130         if (r_usage) {
1131                 fprintf(stderr, _("\nBad options specified.\n\n"
1132                         "Extended options are separated by commas, "
1133                         "and may take an argument which\n"
1134                         "\tis set off by an equals ('=') sign.\n\n"
1135                         "Valid extended options are:\n"
1136                         "\thash_alg=<hash algorithm>\n"
1137                         "\tmount_opts=<extended default mount options>\n"
1138                         "\tstride=<RAID per-disk chunk size in blocks>\n"
1139                         "\tstripe_width=<RAID stride*data disks in blocks>\n"
1140                         "\ttest_fs\n"
1141                         "\t^test_fs\n"));
1142                 free(buf);
1143                 exit(1);
1144         }
1145         free(buf);
1146 }
1147
1148 /*
1149  * Fill in the block bitmap bmap with the information regarding the
1150  * blocks to be moved
1151  */
1152 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1153                             ext2fs_block_bitmap bmap)
1154 {
1155         dgrp_t i;
1156         int retval;
1157         ext2_badblocks_list bb_list = 0;
1158         blk64_t j, needed_blocks = 0;
1159         blk64_t start_blk, end_blk;
1160
1161         retval = ext2fs_read_bb_inode(fs, &bb_list);
1162         if (retval)
1163                 return retval;
1164
1165         for (i = 0; i < fs->group_desc_count; i++) {
1166                 start_blk = ext2fs_inode_table_loc(fs, i) +
1167                                         fs->inode_blocks_per_group;
1168
1169                 end_blk = ext2fs_inode_table_loc(fs, i) +
1170                                         new_ino_blks_per_grp;
1171
1172                 for (j = start_blk; j < end_blk; j++) {
1173                         if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1174                                 /*
1175                                  * IF the block is a bad block we fail
1176                                  */
1177                                 if (ext2fs_badblocks_list_test(bb_list, j)) {
1178                                         ext2fs_badblocks_list_free(bb_list);
1179                                         return ENOSPC;
1180                                 }
1181
1182                                 ext2fs_mark_block_bitmap2(bmap, j);
1183                         } else {
1184                                 /*
1185                                  * We are going to use this block for
1186                                  * inode table. So mark them used.
1187                                  */
1188                                 ext2fs_mark_block_bitmap2(fs->block_map, j);
1189                         }
1190                 }
1191                 needed_blocks += end_blk - start_blk;
1192         }
1193
1194         ext2fs_badblocks_list_free(bb_list);
1195         if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1196                 return ENOSPC;
1197
1198         return 0;
1199 }
1200
1201 static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1202 {
1203         dgrp_t group;
1204         group = ext2fs_group_of_blk(fs, blk);
1205         if (ext2fs_block_bitmap_loc(fs, group) == blk)
1206                 return 1;
1207         if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1208                 return 1;
1209         return 0;
1210 }
1211
1212 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1213 {
1214         blk_t start_blk, end_blk;
1215         start_blk = fs->super->s_first_data_block +
1216                         EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1217         /*
1218          * We cannot get new block beyond end_blk for for the last block group
1219          * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1220          */
1221         end_blk   = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1222         if (blk >= start_blk && blk <= end_blk)
1223                 return 1;
1224         return 0;
1225 }
1226
1227 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1228 {
1229
1230         char *buf;
1231         dgrp_t group = 0;
1232         errcode_t retval;
1233         int meta_data = 0;
1234         blk64_t blk, new_blk, goal;
1235         struct blk_move *bmv;
1236
1237         retval = ext2fs_get_mem(fs->blocksize, &buf);
1238         if (retval)
1239                 return retval;
1240
1241         for (new_blk = blk = fs->super->s_first_data_block;
1242              blk < ext2fs_blocks_count(fs->super); blk++) {
1243                 if (!ext2fs_test_block_bitmap2(bmap, blk))
1244                         continue;
1245
1246                 if (ext2fs_is_meta_block(fs, blk)) {
1247                         /*
1248                          * If the block is mapping a fs meta data block
1249                          * like group desc/block bitmap/inode bitmap. We
1250                          * should find a block in the same group and fix
1251                          * the respective fs metadata pointers. Otherwise
1252                          * fail
1253                          */
1254                         group = ext2fs_group_of_blk(fs, blk);
1255                         goal = ext2fs_group_first_block2(fs, group);
1256                         meta_data = 1;
1257
1258                 } else {
1259                         goal = new_blk;
1260                 }
1261                 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1262                 if (retval)
1263                         goto err_out;
1264
1265                 /* new fs meta data block should be in the same group */
1266                 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1267                         retval = ENOSPC;
1268                         goto err_out;
1269                 }
1270
1271                 /* Mark this block as allocated */
1272                 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1273
1274                 /* Add it to block move list */
1275                 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1276                 if (retval)
1277                         goto err_out;
1278
1279                 bmv->old_loc = blk;
1280                 bmv->new_loc = new_blk;
1281
1282                 list_add(&(bmv->list), &blk_move_list);
1283
1284                 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1285                 if (retval)
1286                         goto err_out;
1287
1288                 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1289                 if (retval)
1290                         goto err_out;
1291         }
1292
1293 err_out:
1294         ext2fs_free_mem(&buf);
1295         return retval;
1296 }
1297
1298 static blk64_t translate_block(blk64_t blk)
1299 {
1300         struct list_head *entry;
1301         struct blk_move *bmv;
1302
1303         list_for_each(entry, &blk_move_list) {
1304                 bmv = list_entry(entry, struct blk_move, list);
1305                 if (bmv->old_loc == blk)
1306                         return bmv->new_loc;
1307         }
1308
1309         return 0;
1310 }
1311
1312 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1313                          blk64_t *block_nr,
1314                          e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1315                          blk64_t ref_block EXT2FS_ATTR((unused)),
1316                          int ref_offset EXT2FS_ATTR((unused)),
1317                          void *priv_data)
1318 {
1319         int ret = 0;
1320         blk64_t new_blk;
1321         ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1322
1323         if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1324                 return 0;
1325         new_blk = translate_block(*block_nr);
1326         if (new_blk) {
1327                 *block_nr = new_blk;
1328                 /*
1329                  * This will force the ext2fs_write_inode in the iterator
1330                  */
1331                 ret |= BLOCK_CHANGED;
1332         }
1333
1334         return ret;
1335 }
1336
1337 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1338 {
1339         errcode_t retval = 0;
1340         ext2_ino_t ino;
1341         blk64_t blk;
1342         char *block_buf = 0;
1343         struct ext2_inode inode;
1344         ext2_inode_scan scan = NULL;
1345
1346         retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1347         if (retval)
1348                 return retval;
1349
1350         retval = ext2fs_open_inode_scan(fs, 0, &scan);
1351         if (retval)
1352                 goto err_out;
1353
1354         while (1) {
1355                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1356                 if (retval)
1357                         goto err_out;
1358
1359                 if (!ino)
1360                         break;
1361
1362                 if (inode.i_links_count == 0)
1363                         continue; /* inode not in use */
1364
1365                 /* FIXME!!
1366                  * If we end up modifying the journal inode
1367                  * the sb->s_jnl_blocks will differ. But a
1368                  * subsequent e2fsck fixes that.
1369                  * Do we need to fix this ??
1370                  */
1371
1372                 if (ext2fs_file_acl_block(&inode) &&
1373                     ext2fs_test_block_bitmap2(bmap,
1374                                               ext2fs_file_acl_block(&inode))) {
1375                         blk = translate_block(ext2fs_file_acl_block(&inode));
1376                         if (!blk)
1377                                 continue;
1378
1379                         ext2fs_file_acl_block_set(&inode, blk);
1380
1381                         /*
1382                          * Write the inode to disk so that inode table
1383                          * resizing can work
1384                          */
1385                         retval = ext2fs_write_inode(fs, ino, &inode);
1386                         if (retval)
1387                                 goto err_out;
1388                 }
1389
1390                 if (!ext2fs_inode_has_valid_blocks(&inode))
1391                         continue;
1392
1393                 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1394                                                process_block, bmap);
1395                 if (retval)
1396                         goto err_out;
1397
1398         }
1399
1400 err_out:
1401         ext2fs_free_mem(&block_buf);
1402
1403         return retval;
1404 }
1405
1406 /*
1407  * We need to scan for inode and block bitmaps that may need to be
1408  * moved.  This can take place if the filesystem was formatted for
1409  * RAID arrays using the mke2fs's extended option "stride".
1410  */
1411 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1412 {
1413         dgrp_t i;
1414         blk_t blk, new_blk;
1415
1416         for (i = 0; i < fs->group_desc_count; i++) {
1417                 blk = ext2fs_block_bitmap_loc(fs, i);
1418                 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1419                         new_blk = translate_block(blk);
1420                         if (!new_blk)
1421                                 continue;
1422                         ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1423                 }
1424
1425                 blk = ext2fs_inode_bitmap_loc(fs, i);
1426                 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1427                         new_blk = translate_block(blk);
1428                         if (!new_blk)
1429                                 continue;
1430                         ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1431                 }
1432         }
1433         return 0;
1434 }
1435
1436 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1437 {
1438         dgrp_t i;
1439         blk64_t blk;
1440         errcode_t retval;
1441         int new_ino_blks_per_grp;
1442         unsigned int j;
1443         char *old_itable = NULL, *new_itable = NULL;
1444         char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1445         unsigned long old_ino_size;
1446         int old_itable_size, new_itable_size;
1447
1448         old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1449         old_ino_size = EXT2_INODE_SIZE(fs->super);
1450
1451         new_ino_blks_per_grp = ext2fs_div_ceil(
1452                                         EXT2_INODES_PER_GROUP(fs->super) *
1453                                         new_ino_size,
1454                                         fs->blocksize);
1455
1456         new_itable_size = new_ino_blks_per_grp * fs->blocksize;
1457
1458         retval = ext2fs_get_mem(old_itable_size, &old_itable);
1459         if (retval)
1460                 return retval;
1461
1462         retval = ext2fs_get_mem(new_itable_size, &new_itable);
1463         if (retval)
1464                 goto err_out;
1465
1466         tmp_old_itable = old_itable;
1467         tmp_new_itable = new_itable;
1468
1469         for (i = 0; i < fs->group_desc_count; i++) {
1470                 blk = ext2fs_inode_table_loc(fs, i);
1471                 retval = io_channel_read_blk64(fs->io, blk,
1472                                 fs->inode_blocks_per_group, old_itable);
1473                 if (retval)
1474                         goto err_out;
1475
1476                 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
1477                         memcpy(new_itable, old_itable, old_ino_size);
1478
1479                         memset(new_itable+old_ino_size, 0,
1480                                         new_ino_size - old_ino_size);
1481
1482                         new_itable += new_ino_size;
1483                         old_itable += old_ino_size;
1484                 }
1485
1486                 /* reset the pointer */
1487                 old_itable = tmp_old_itable;
1488                 new_itable = tmp_new_itable;
1489
1490                 retval = io_channel_write_blk64(fs->io, blk,
1491                                         new_ino_blks_per_grp, new_itable);
1492                 if (retval)
1493                         goto err_out;
1494         }
1495
1496         /* Update the meta data */
1497         fs->inode_blocks_per_group = new_ino_blks_per_grp;
1498         fs->super->s_inode_size = new_ino_size;
1499
1500 err_out:
1501         if (old_itable)
1502                 ext2fs_free_mem(&old_itable);
1503
1504         if (new_itable)
1505                 ext2fs_free_mem(&new_itable);
1506
1507         return retval;
1508 }
1509
1510 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
1511 {
1512         blk64_t         blk;
1513         ext2_ino_t      ino;
1514         unsigned int    group = 0;
1515         unsigned int    count = 0;
1516         int             total_free = 0;
1517         int             group_free = 0;
1518
1519         /*
1520          * First calculate the block statistics
1521          */
1522         for (blk = fs->super->s_first_data_block;
1523              blk < ext2fs_blocks_count(fs->super); blk++) {
1524                 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
1525                         group_free++;
1526                         total_free++;
1527                 }
1528                 count++;
1529                 if ((count == fs->super->s_blocks_per_group) ||
1530                     (blk == ext2fs_blocks_count(fs->super)-1)) {
1531                         ext2fs_bg_free_blocks_count_set(fs, group++,
1532                                                         group_free);
1533                         count = 0;
1534                         group_free = 0;
1535                 }
1536         }
1537         total_free = EXT2FS_C2B(fs, total_free);
1538         ext2fs_free_blocks_count_set(fs->super, total_free);
1539
1540         /*
1541          * Next, calculate the inode statistics
1542          */
1543         group_free = 0;
1544         total_free = 0;
1545         count = 0;
1546         group = 0;
1547
1548         /* Protect loop from wrap-around if s_inodes_count maxed */
1549         for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
1550                 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
1551                         group_free++;
1552                         total_free++;
1553                 }
1554                 count++;
1555                 if ((count == fs->super->s_inodes_per_group) ||
1556                     (ino == fs->super->s_inodes_count)) {
1557                         ext2fs_bg_free_inodes_count_set(fs, group++,
1558                                                         group_free);
1559                         count = 0;
1560                         group_free = 0;
1561                 }
1562         }
1563         fs->super->s_free_inodes_count = total_free;
1564         ext2fs_mark_super_dirty(fs);
1565         return 0;
1566 }
1567
1568 #define list_for_each_safe(pos, pnext, head) \
1569         for (pos = (head)->next, pnext = pos->next; pos != (head); \
1570              pos = pnext, pnext = pos->next)
1571
1572 static void free_blk_move_list(void)
1573 {
1574         struct list_head *entry, *tmp;
1575         struct blk_move *bmv;
1576
1577         list_for_each_safe(entry, tmp, &blk_move_list) {
1578                 bmv = list_entry(entry, struct blk_move, list);
1579                 list_del(entry);
1580                 ext2fs_free_mem(&bmv);
1581         }
1582         return;
1583 }
1584
1585 static int resize_inode(ext2_filsys fs, unsigned long new_size)
1586 {
1587         errcode_t retval;
1588         int new_ino_blks_per_grp;
1589         ext2fs_block_bitmap bmap;
1590
1591         ext2fs_read_inode_bitmap(fs);
1592         ext2fs_read_block_bitmap(fs);
1593         INIT_LIST_HEAD(&blk_move_list);
1594
1595
1596         new_ino_blks_per_grp = ext2fs_div_ceil(
1597                                         EXT2_INODES_PER_GROUP(fs->super)*
1598                                         new_size,
1599                                         fs->blocksize);
1600
1601         /* We may change the file system.
1602          * Mark the file system as invalid so that
1603          * the user is prompted to run fsck.
1604          */
1605         fs->super->s_state &= ~EXT2_VALID_FS;
1606
1607         retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
1608                                                 &bmap);
1609         if (retval) {
1610                 fputs(_("Failed to allocate block bitmap when "
1611                                 "increasing inode size\n"), stderr);
1612                 return retval;
1613         }
1614         retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
1615         if (retval) {
1616                 fputs(_("Not enough space to increase inode size \n"), stderr);
1617                 goto err_out;
1618         }
1619         retval = move_block(fs, bmap);
1620         if (retval) {
1621                 fputs(_("Failed to relocate blocks during inode resize \n"),
1622                       stderr);
1623                 goto err_out;
1624         }
1625         retval = inode_scan_and_fix(fs, bmap);
1626         if (retval)
1627                 goto err_out_undo;
1628
1629         retval = group_desc_scan_and_fix(fs, bmap);
1630         if (retval)
1631                 goto err_out_undo;
1632
1633         retval = expand_inode_table(fs, new_size);
1634         if (retval)
1635                 goto err_out_undo;
1636
1637         ext2fs_calculate_summary_stats(fs);
1638
1639         fs->super->s_state |= EXT2_VALID_FS;
1640         /* mark super block and block bitmap as dirty */
1641         ext2fs_mark_super_dirty(fs);
1642         ext2fs_mark_bb_dirty(fs);
1643
1644 err_out:
1645         free_blk_move_list();
1646         ext2fs_free_block_bitmap(bmap);
1647
1648         return retval;
1649
1650 err_out_undo:
1651         free_blk_move_list();
1652         ext2fs_free_block_bitmap(bmap);
1653         fputs(_("Error in resizing the inode size.\n"
1654                         "Run e2undo to undo the "
1655                         "file system changes. \n"), stderr);
1656
1657         return retval;
1658 }
1659
1660 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
1661 {
1662         errcode_t retval = 0;
1663         const char *tdb_dir;
1664         char *tdb_file;
1665         char *dev_name, *tmp_name;
1666
1667 #if 0 /* FIXME!! */
1668         /*
1669          * Configuration via a conf file would be
1670          * nice
1671          */
1672         profile_get_string(profile, "scratch_files",
1673                                         "directory", 0, 0,
1674                                         &tdb_dir);
1675 #endif
1676         tmp_name = strdup(name);
1677         if (!tmp_name) {
1678         alloc_fn_fail:
1679                 com_err(program_name, ENOMEM, 
1680                         _("Couldn't allocate memory for tdb filename\n"));
1681                 return ENOMEM;
1682         }
1683         dev_name = basename(tmp_name);
1684
1685         tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1686         if (!tdb_dir)
1687                 tdb_dir = "/var/lib/e2fsprogs";
1688
1689         if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1690             access(tdb_dir, W_OK))
1691                 return 0;
1692
1693         tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
1694         if (!tdb_file)
1695                 goto alloc_fn_fail;
1696         sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
1697
1698         if (!access(tdb_file, F_OK)) {
1699                 if (unlink(tdb_file) < 0) {
1700                         retval = errno;
1701                         com_err(program_name, retval,
1702                                 _("while trying to delete %s"),
1703                                 tdb_file);
1704                         free(tdb_file);
1705                         return retval;
1706                 }
1707         }
1708
1709         set_undo_io_backing_manager(*io_ptr);
1710         *io_ptr = undo_io_manager;
1711         set_undo_io_backup_file(tdb_file);
1712         printf(_("To undo the tune2fs operation please run "
1713                  "the command\n    e2undo %s %s\n\n"),
1714                  tdb_file, name);
1715         free(tdb_file);
1716         free(tmp_name);
1717         return retval;
1718 }
1719
1720 int main(int argc, char **argv)
1721 {
1722         errcode_t retval;
1723         ext2_filsys fs;
1724         struct ext2_super_block *sb;
1725         io_manager io_ptr, io_ptr_orig = NULL;
1726
1727 #ifdef ENABLE_NLS
1728         setlocale(LC_MESSAGES, "");
1729         setlocale(LC_CTYPE, "");
1730         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1731         textdomain(NLS_CAT_NAME);
1732 #endif
1733         if (argc && *argv)
1734                 program_name = *argv;
1735         add_error_table(&et_ext2_error_table);
1736
1737 #ifdef CONFIG_BUILD_FINDFS
1738         if (strcmp(get_progname(argv[0]), "findfs") == 0)
1739                 do_findfs(argc, argv);
1740 #endif
1741         if (strcmp(get_progname(argv[0]), "e2label") == 0)
1742                 parse_e2label_options(argc, argv);
1743         else
1744                 parse_tune2fs_options(argc, argv);
1745
1746 #ifdef CONFIG_TESTIO_DEBUG
1747         if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
1748                 io_ptr = test_io_manager;
1749                 test_io_backing_manager = unix_io_manager;
1750         } else
1751 #endif
1752                 io_ptr = unix_io_manager;
1753
1754 retry_open:
1755         retval = ext2fs_open2(device_name, io_options, open_flag,
1756                               0, 0, io_ptr, &fs);
1757         if (retval) {
1758                         com_err(program_name, retval,
1759                                 _("while trying to open %s"),
1760                         device_name);
1761                 fprintf(stderr,
1762                         _("Couldn't find valid filesystem superblock.\n"));
1763                 exit(1);
1764         }
1765
1766         if (I_flag && !io_ptr_orig) {
1767                 /*
1768                  * Check the inode size is right so we can issue an
1769                  * error message and bail before setting up the tdb
1770                  * file.
1771                  */
1772                 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
1773                         fprintf(stderr, _("The inode size is already %lu\n"),
1774                                 new_inode_size);
1775                         exit(1);
1776                 }
1777                 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
1778                         fprintf(stderr, _("Shrinking the inode size is "
1779                                           "not supported\n"));
1780                         exit(1);
1781                 }
1782
1783                 /*
1784                  * If inode resize is requested use the
1785                  * Undo I/O manager
1786                  */
1787                 io_ptr_orig = io_ptr;
1788                 retval = tune2fs_setup_tdb(device_name, &io_ptr);
1789                 if (retval)
1790                         exit(1);
1791                 if (io_ptr != io_ptr_orig) {
1792                         ext2fs_close(fs);
1793                         goto retry_open;
1794                 }
1795         }
1796
1797         sb = fs->super;
1798         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1799
1800         if (print_label) {
1801                 /* For e2label emulation */
1802                 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
1803                        sb->s_volume_name);
1804                 remove_error_table(&et_ext2_error_table);
1805                 exit(0);
1806         }
1807
1808         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1809         if (retval) {
1810                 com_err("ext2fs_check_if_mount", retval,
1811                         _("while determining whether %s is mounted."),
1812                         device_name);
1813                 exit(1);
1814         }
1815         /* Normally we only need to write out the superblock */
1816         fs->flags |= EXT2_FLAG_SUPER_ONLY;
1817
1818         if (c_flag) {
1819                 sb->s_max_mnt_count = max_mount_count;
1820                 ext2fs_mark_super_dirty(fs);
1821                 printf(_("Setting maximal mount count to %d\n"),
1822                        max_mount_count);
1823         }
1824         if (C_flag) {
1825                 sb->s_mnt_count = mount_count;
1826                 ext2fs_mark_super_dirty(fs);
1827                 printf(_("Setting current mount count to %d\n"), mount_count);
1828         }
1829         if (e_flag) {
1830                 sb->s_errors = errors;
1831                 ext2fs_mark_super_dirty(fs);
1832                 printf(_("Setting error behavior to %d\n"), errors);
1833         }
1834         if (g_flag) {
1835                 sb->s_def_resgid = resgid;
1836                 ext2fs_mark_super_dirty(fs);
1837                 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
1838         }
1839         if (i_flag) {
1840                 if (interval >= (1ULL << 32)) {
1841                         com_err(program_name, 0,
1842                                 _("interval between checks is too big (%lu)"),
1843                                 interval);
1844                         exit(1);
1845                 }
1846                 sb->s_checkinterval = interval;
1847                 ext2fs_mark_super_dirty(fs);
1848                 printf(_("Setting interval between checks to %lu seconds\n"),
1849                        interval);
1850         }
1851         if (m_flag) {
1852                 ext2fs_r_blocks_count_set(sb, reserved_ratio *
1853                                           ext2fs_blocks_count(sb) / 100.0);
1854                 ext2fs_mark_super_dirty(fs);
1855                 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
1856                         reserved_ratio, ext2fs_r_blocks_count(sb));
1857         }
1858         if (r_flag) {
1859                 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
1860                         com_err(program_name, 0,
1861                                 _("reserved blocks count is too big (%llu)"),
1862                                 reserved_blocks);
1863                         exit(1);
1864                 }
1865                 ext2fs_r_blocks_count_set(sb, reserved_blocks);
1866                 ext2fs_mark_super_dirty(fs);
1867                 printf(_("Setting reserved blocks count to %llu\n"),
1868                        reserved_blocks);
1869         }
1870         if (s_flag == 1) {
1871                 if (sb->s_feature_ro_compat &
1872                     EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
1873                         fputs(_("\nThe filesystem already has sparse "
1874                                 "superblocks.\n"), stderr);
1875                 else {
1876                         sb->s_feature_ro_compat |=
1877                                 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1878                         sb->s_state &= ~EXT2_VALID_FS;
1879                         ext2fs_mark_super_dirty(fs);
1880                         printf(_("\nSparse superblock flag set.  %s"),
1881                                _(please_fsck));
1882                 }
1883         }
1884         if (s_flag == 0) {
1885                 fputs(_("\nClearing the sparse superflag not supported.\n"),
1886                       stderr);
1887                 exit(1);
1888         }
1889         if (T_flag) {
1890                 sb->s_lastcheck = last_check_time;
1891                 ext2fs_mark_super_dirty(fs);
1892                 printf(_("Setting time filesystem last checked to %s\n"),
1893                        ctime(&last_check_time));
1894         }
1895         if (u_flag) {
1896                 sb->s_def_resuid = resuid;
1897                 ext2fs_mark_super_dirty(fs);
1898                 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
1899         }
1900         if (L_flag) {
1901                 if (strlen(new_label) > sizeof(sb->s_volume_name))
1902                         fputs(_("Warning: label too long, truncating.\n"),
1903                               stderr);
1904                 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
1905                 strncpy(sb->s_volume_name, new_label,
1906                         sizeof(sb->s_volume_name));
1907                 ext2fs_mark_super_dirty(fs);
1908         }
1909         if (M_flag) {
1910                 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
1911                 strncpy(sb->s_last_mounted, new_last_mounted,
1912                         sizeof(sb->s_last_mounted));
1913                 ext2fs_mark_super_dirty(fs);
1914         }
1915         if (mntopts_cmd)
1916                 update_mntopts(fs, mntopts_cmd);
1917         if (features_cmd)
1918                 update_feature_set(fs, features_cmd);
1919         if (extended_cmd)
1920                 parse_extended_opts(fs, extended_cmd);
1921         if (journal_size || journal_device)
1922                 add_journal(fs);
1923
1924         if (Q_flag) {
1925                 if (mount_flags & EXT2_MF_MOUNTED) {
1926                         fputs(_("The quota feature may only be changed when "
1927                                 "the filesystem is unmounted.\n"), stderr);
1928                         exit(1);
1929                 }
1930                 handle_quota_options(fs);
1931         }
1932
1933         if (U_flag) {
1934                 int set_csum = 0;
1935                 dgrp_t i;
1936
1937                 if (sb->s_feature_ro_compat &
1938                     EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
1939                         /*
1940                          * Determine if the block group checksums are
1941                          * correct so we know whether or not to set
1942                          * them later on.
1943                          */
1944                         for (i = 0; i < fs->group_desc_count; i++)
1945                                 if (!ext2fs_group_desc_csum_verify(fs, i))
1946                                         break;
1947                         if (i >= fs->group_desc_count)
1948                                 set_csum = 1;
1949                 }
1950                 if ((strcasecmp(new_UUID, "null") == 0) ||
1951                     (strcasecmp(new_UUID, "clear") == 0)) {
1952                         uuid_clear(sb->s_uuid);
1953                 } else if (strcasecmp(new_UUID, "time") == 0) {
1954                         uuid_generate_time(sb->s_uuid);
1955                 } else if (strcasecmp(new_UUID, "random") == 0) {
1956                         uuid_generate(sb->s_uuid);
1957                 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
1958                         com_err(program_name, 0, _("Invalid UUID format\n"));
1959                         exit(1);
1960                 }
1961                 if (set_csum) {
1962                         for (i = 0; i < fs->group_desc_count; i++)
1963                                 ext2fs_group_desc_csum_set(fs, i);
1964                         fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1965                 }
1966                 ext2fs_mark_super_dirty(fs);
1967         }
1968         if (I_flag) {
1969                 if (mount_flags & EXT2_MF_MOUNTED) {
1970                         fputs(_("The inode size may only be "
1971                                 "changed when the filesystem is "
1972                                 "unmounted.\n"), stderr);
1973                         exit(1);
1974                 }
1975                 if (fs->super->s_feature_incompat &
1976                     EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1977                         fputs(_("Changing the inode size not supported for "
1978                                 "filesystems with the flex_bg\n"
1979                                 "feature enabled.\n"),
1980                               stderr);
1981                         exit(1);
1982                 }
1983                 /*
1984                  * We want to update group descriptor also
1985                  * with the new free inode count
1986                  */
1987                 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1988                 if (resize_inode(fs, new_inode_size) == 0) {
1989                         printf(_("Setting inode size %lu\n"),
1990                                                         new_inode_size);
1991                 }
1992         }
1993
1994         if (l_flag)
1995                 list_super(sb);
1996         if (stride_set) {
1997                 sb->s_raid_stride = stride;
1998                 ext2fs_mark_super_dirty(fs);
1999                 printf(_("Setting stride size to %d\n"), stride);
2000         }
2001         if (stripe_width_set) {
2002                 sb->s_raid_stripe_width = stripe_width;
2003                 ext2fs_mark_super_dirty(fs);
2004                 printf(_("Setting stripe width to %d\n"), stripe_width);
2005         }
2006         if (ext_mount_opts) {
2007                 strncpy(fs->super->s_mount_opts, ext_mount_opts,
2008                         sizeof(fs->super->s_mount_opts));
2009                 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2010                 ext2fs_mark_super_dirty(fs);
2011                 printf(_("Setting extended default mount options to '%s'\n"),
2012                        ext_mount_opts);
2013                 free(ext_mount_opts);
2014         }
2015         free(device_name);
2016         remove_error_table(&et_ext2_error_table);
2017         return (ext2fs_close(fs) ? 1 : 0);
2018 }