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