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