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