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