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