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