]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/tune2fs.c
tune2fs: rewrite block group checksums when changing checksumming feature flags
[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 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
708 fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
709 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
710 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
711 fs->super->s_checksum_type = EXT2_CRC32C_CHKSUM;
712 else
713 fs->super->s_checksum_type = 0;
714 ext2fs_mark_super_dirty(fs);
715 }
716
717 static void enable_uninit_bg(ext2_filsys fs)
718 {
719 struct ext2_group_desc *gd;
720 int i;
721
722 for (i = 0; i < fs->group_desc_count; i++) {
723 gd = ext2fs_group_desc(fs, fs->group_desc, i);
724 gd->bg_itable_unused = 0;
725 gd->bg_flags = EXT2_BG_INODE_ZEROED;
726 ext2fs_group_desc_csum_set(fs, i);
727 }
728 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
729 }
730
731 static void disable_uninit_bg(ext2_filsys fs, __u32 csum_feature_flag)
732 {
733 struct ext2_group_desc *gd;
734 int i;
735
736 /* Load bitmaps to ensure that the uninit ones get written out */
737 fs->super->s_feature_ro_compat |= csum_feature_flag;
738 ext2fs_read_bitmaps(fs);
739 ext2fs_mark_ib_dirty(fs);
740 ext2fs_mark_bb_dirty(fs);
741 fs->super->s_feature_ro_compat &= ~csum_feature_flag;
742
743 for (i = 0; i < fs->group_desc_count; i++) {
744 gd = ext2fs_group_desc(fs, fs->group_desc, i);
745 if ((gd->bg_flags & EXT2_BG_INODE_ZEROED) == 0) {
746 /*
747 * XXX what we really should do is zap
748 * uninitialized inode tables instead.
749 */
750 request_fsck_afterwards(fs);
751 break;
752 }
753 gd->bg_itable_unused = 0;
754 gd->bg_flags = 0;
755 ext2fs_group_desc_csum_set(fs, i);
756 }
757 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
758 }
759
760 /*
761 * Update the feature set as provided by the user.
762 */
763 static int update_feature_set(ext2_filsys fs, char *features)
764 {
765 struct ext2_super_block *sb = fs->super;
766 struct ext2_group_desc *gd;
767 __u32 old_features[3];
768 int i, type_err;
769 unsigned int mask_err;
770
771 #define FEATURE_ON(type, mask) (!(old_features[(type)] & (mask)) && \
772 ((&sb->s_feature_compat)[(type)] & (mask)))
773 #define FEATURE_OFF(type, mask) ((old_features[(type)] & (mask)) && \
774 !((&sb->s_feature_compat)[(type)] & (mask)))
775 #define FEATURE_CHANGED(type, mask) ((mask) & \
776 (old_features[(type)] ^ (&sb->s_feature_compat)[(type)]))
777
778 old_features[E2P_FEATURE_COMPAT] = sb->s_feature_compat;
779 old_features[E2P_FEATURE_INCOMPAT] = sb->s_feature_incompat;
780 old_features[E2P_FEATURE_RO_INCOMPAT] = sb->s_feature_ro_compat;
781
782 if (e2p_edit_feature2(features, &sb->s_feature_compat,
783 ok_features, clear_ok_features,
784 &type_err, &mask_err)) {
785 if (!mask_err)
786 fprintf(stderr,
787 _("Invalid filesystem option set: %s\n"),
788 features);
789 else if (type_err & E2P_FEATURE_NEGATE_FLAG)
790 fprintf(stderr, _("Clearing filesystem feature '%s' "
791 "not supported.\n"),
792 e2p_feature2string(type_err &
793 E2P_FEATURE_TYPE_MASK,
794 mask_err));
795 else
796 fprintf(stderr, _("Setting filesystem feature '%s' "
797 "not supported.\n"),
798 e2p_feature2string(type_err, mask_err));
799 return 1;
800 }
801
802 if (FEATURE_OFF(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
803 if ((mount_flags & EXT2_MF_MOUNTED) &&
804 !(mount_flags & EXT2_MF_READONLY)) {
805 fputs(_("The has_journal feature may only be "
806 "cleared when the filesystem is\n"
807 "unmounted or mounted "
808 "read-only.\n"), stderr);
809 return 1;
810 }
811 if (sb->s_feature_incompat &
812 EXT3_FEATURE_INCOMPAT_RECOVER) {
813 fputs(_("The needs_recovery flag is set. "
814 "Please run e2fsck before clearing\n"
815 "the has_journal flag.\n"), stderr);
816 return 1;
817 }
818 if (sb->s_journal_inum) {
819 if (remove_journal_inode(fs))
820 return 1;
821 }
822 if (sb->s_journal_dev) {
823 if (remove_journal_device(fs))
824 return 1;
825 }
826 }
827 if (FEATURE_ON(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
828 int error;
829
830 if ((mount_flags & EXT2_MF_MOUNTED) ||
831 (mount_flags & EXT2_MF_READONLY)) {
832 fputs(_("The multiple mount protection feature can't\n"
833 "be set if the filesystem is mounted or\n"
834 "read-only.\n"), stderr);
835 return 1;
836 }
837
838 error = ext2fs_mmp_init(fs);
839 if (error) {
840 fputs(_("\nError while enabling multiple mount "
841 "protection feature."), stderr);
842 return 1;
843 }
844
845 /*
846 * We want to update group desc with the new free blocks count
847 */
848 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
849
850 printf(_("Multiple mount protection has been enabled "
851 "with update interval %ds.\n"),
852 sb->s_mmp_update_interval);
853 }
854
855 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP)) {
856 int error;
857
858 if (mount_flags & EXT2_MF_READONLY) {
859 fputs(_("The multiple mount protection feature cannot\n"
860 "be disabled if the filesystem is readonly.\n"),
861 stderr);
862 return 1;
863 }
864
865 error = ext2fs_read_bitmaps(fs);
866 if (error) {
867 fputs(_("Error while reading bitmaps\n"), stderr);
868 return 1;
869 }
870
871 error = ext2fs_mmp_read(fs, sb->s_mmp_block, NULL);
872 if (error) {
873 struct mmp_struct *mmp_cmp = fs->mmp_cmp;
874
875 if (error == EXT2_ET_MMP_MAGIC_INVALID)
876 printf(_("Magic number in MMP block does not "
877 "match. expected: %x, actual: %x\n"),
878 EXT4_MMP_MAGIC, mmp_cmp->mmp_magic);
879 else
880 com_err(program_name, error,
881 _("while reading MMP block."));
882 goto mmp_error;
883 }
884
885 /* We need to force out the group descriptors as well */
886 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
887 ext2fs_block_alloc_stats(fs, sb->s_mmp_block, -1);
888 mmp_error:
889 sb->s_mmp_block = 0;
890 sb->s_mmp_update_interval = 0;
891 }
892
893 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
894 /*
895 * If adding a journal flag, let the create journal
896 * code below handle setting the flag and creating the
897 * journal. We supply a default size if necessary.
898 */
899 if (!journal_size)
900 journal_size = -1;
901 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
902 }
903
904 if (FEATURE_ON(E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX)) {
905 if (!sb->s_def_hash_version)
906 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
907 if (uuid_is_null((unsigned char *) sb->s_hash_seed))
908 uuid_generate((unsigned char *) sb->s_hash_seed);
909 }
910
911 if (FEATURE_OFF(E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
912 if (ext2fs_check_desc(fs)) {
913 fputs(_("Clearing the flex_bg flag would "
914 "cause the the filesystem to be\n"
915 "inconsistent.\n"), stderr);
916 return 1;
917 }
918 }
919
920 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
921 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
922 if ((mount_flags & EXT2_MF_MOUNTED) &&
923 !(mount_flags & EXT2_MF_READONLY)) {
924 fputs(_("The huge_file feature may only be "
925 "cleared when the filesystem is\n"
926 "unmounted or mounted "
927 "read-only.\n"), stderr);
928 return 1;
929 }
930 }
931
932 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
933 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
934 if (check_fsck_needed(fs))
935 exit(1);
936 rewrite_checksums = 1;
937 /* metadata_csum supersedes uninit_bg */
938 fs->super->s_feature_ro_compat &=
939 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
940
941 /* if uninit_bg was previously off, rewrite group desc */
942 if (!(old_features[E2P_FEATURE_RO_INCOMPAT] &
943 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
944 enable_uninit_bg(fs);
945
946 /*
947 * Since metadata_csum supersedes uninit_bg, pretend like
948 * uninit_bg has been off all along.
949 */
950 old_features[E2P_FEATURE_RO_INCOMPAT] &=
951 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
952 }
953
954 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
955 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
956 if (check_fsck_needed(fs))
957 exit(1);
958 rewrite_checksums = 1;
959 /*
960 * If we're turning off metadata_csum and not turning on
961 * uninit_bg, rewrite group desc.
962 */
963 if (!(fs->super->s_feature_ro_compat &
964 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
965 disable_uninit_bg(fs,
966 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
967 else
968 /*
969 * metadata_csum previously provided uninit_bg, so if
970 * we're also setting the uninit_bg feature bit,
971 * pretend like it was previously enabled. Checksums
972 * will be rewritten with crc16 later.
973 */
974 old_features[E2P_FEATURE_RO_INCOMPAT] |=
975 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
976 }
977
978 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
979 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
980 /* Do not enable uninit_bg when metadata_csum enabled */
981 if (fs->super->s_feature_ro_compat &
982 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)
983 fs->super->s_feature_ro_compat &=
984 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
985 else
986 enable_uninit_bg(fs);
987 }
988
989 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
990 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
991 disable_uninit_bg(fs,
992 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
993
994 if (FEATURE_ON(E2P_FEATURE_RO_INCOMPAT,
995 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
996 /*
997 * Set the Q_flag here and handle the quota options in the code
998 * below.
999 */
1000 if (!Q_flag) {
1001 Q_flag = 1;
1002 /* Enable both user quota and group quota by default */
1003 usrquota = QOPT_ENABLE;
1004 grpquota = QOPT_ENABLE;
1005 }
1006 sb->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1007 }
1008
1009 if (FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1010 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1011 /*
1012 * Set the Q_flag here and handle the quota options in the code
1013 * below.
1014 */
1015 if (Q_flag)
1016 fputs(_("\nWarning: '^quota' option overrides '-Q'"
1017 "arguments.\n"), stderr);
1018 Q_flag = 1;
1019 /* Disable both user quota and group quota by default */
1020 usrquota = QOPT_DISABLE;
1021 grpquota = QOPT_DISABLE;
1022 }
1023
1024 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
1025 (sb->s_feature_compat || sb->s_feature_ro_compat ||
1026 sb->s_feature_incompat))
1027 ext2fs_update_dynamic_rev(fs);
1028
1029 if (FEATURE_CHANGED(E2P_FEATURE_RO_INCOMPAT,
1030 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER) ||
1031 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1032 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
1033 FEATURE_CHANGED(E2P_FEATURE_INCOMPAT,
1034 EXT2_FEATURE_INCOMPAT_FILETYPE) ||
1035 FEATURE_CHANGED(E2P_FEATURE_COMPAT,
1036 EXT2_FEATURE_COMPAT_RESIZE_INODE) ||
1037 FEATURE_OFF(E2P_FEATURE_RO_INCOMPAT,
1038 EXT2_FEATURE_RO_COMPAT_LARGE_FILE))
1039 request_fsck_afterwards(fs);
1040
1041 if ((old_features[E2P_FEATURE_COMPAT] != sb->s_feature_compat) ||
1042 (old_features[E2P_FEATURE_INCOMPAT] != sb->s_feature_incompat) ||
1043 (old_features[E2P_FEATURE_RO_INCOMPAT] != sb->s_feature_ro_compat))
1044 ext2fs_mark_super_dirty(fs);
1045
1046 return 0;
1047 }
1048
1049 /*
1050 * Add a journal to the filesystem.
1051 */
1052 static int add_journal(ext2_filsys fs)
1053 {
1054 unsigned long journal_blocks;
1055 errcode_t retval;
1056 ext2_filsys jfs;
1057 io_manager io_ptr;
1058
1059 if (fs->super->s_feature_compat &
1060 EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
1061 fputs(_("The filesystem already has a journal.\n"), stderr);
1062 goto err;
1063 }
1064 if (journal_device) {
1065 check_plausibility(journal_device);
1066 check_mount(journal_device, 0, _("journal"));
1067 #ifdef CONFIG_TESTIO_DEBUG
1068 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1069 io_ptr = test_io_manager;
1070 test_io_backing_manager = unix_io_manager;
1071 } else
1072 #endif
1073 io_ptr = unix_io_manager;
1074 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1075 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1076 fs->blocksize, io_ptr, &jfs);
1077 if (retval) {
1078 com_err(program_name, retval,
1079 _("\n\twhile trying to open journal on %s\n"),
1080 journal_device);
1081 goto err;
1082 }
1083 printf(_("Creating journal on device %s: "),
1084 journal_device);
1085 fflush(stdout);
1086
1087 retval = ext2fs_add_journal_device(fs, jfs);
1088 ext2fs_close(jfs);
1089 if (retval) {
1090 com_err(program_name, retval,
1091 _("while adding filesystem to journal on %s"),
1092 journal_device);
1093 goto err;
1094 }
1095 fputs(_("done\n"), stdout);
1096 } else if (journal_size) {
1097 fputs(_("Creating journal inode: "), stdout);
1098 fflush(stdout);
1099 journal_blocks = figure_journal_size(journal_size, fs);
1100
1101 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1102 journal_flags);
1103 if (retval) {
1104 fprintf(stderr, "\n");
1105 com_err(program_name, retval,
1106 _("\n\twhile trying to create journal file"));
1107 return retval;
1108 } else
1109 fputs(_("done\n"), stdout);
1110 /*
1111 * If the filesystem wasn't mounted, we need to force
1112 * the block group descriptors out.
1113 */
1114 if ((mount_flags & EXT2_MF_MOUNTED) == 0)
1115 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
1116 }
1117 print_check_message(fs->super->s_max_mnt_count,
1118 fs->super->s_checkinterval);
1119 return 0;
1120
1121 err:
1122 free(journal_device);
1123 return 1;
1124 }
1125
1126 void handle_quota_options(ext2_filsys fs)
1127 {
1128 quota_ctx_t qctx;
1129 ext2_ino_t qf_ino;
1130
1131 if (!usrquota && !grpquota)
1132 /* Nothing to do. */
1133 return;
1134
1135 quota_init_context(&qctx, fs, -1);
1136
1137 if (usrquota == QOPT_ENABLE || grpquota == QOPT_ENABLE)
1138 quota_compute_usage(qctx);
1139
1140 if (usrquota == QOPT_ENABLE && !fs->super->s_usr_quota_inum) {
1141 if ((qf_ino = quota_file_exists(fs, USRQUOTA,
1142 QFMT_VFS_V1)) > 0)
1143 quota_update_limits(qctx, qf_ino, USRQUOTA);
1144 quota_write_inode(qctx, USRQUOTA);
1145 } else if (usrquota == QOPT_DISABLE) {
1146 quota_remove_inode(fs, USRQUOTA);
1147 }
1148
1149 if (grpquota == QOPT_ENABLE && !fs->super->s_grp_quota_inum) {
1150 if ((qf_ino = quota_file_exists(fs, GRPQUOTA,
1151 QFMT_VFS_V1)) > 0)
1152 quota_update_limits(qctx, qf_ino, GRPQUOTA);
1153 quota_write_inode(qctx, GRPQUOTA);
1154 } else if (grpquota == QOPT_DISABLE) {
1155 quota_remove_inode(fs, GRPQUOTA);
1156 }
1157
1158 quota_release_context(&qctx);
1159
1160 if ((usrquota == QOPT_ENABLE) || (grpquota == QOPT_ENABLE)) {
1161 fs->super->s_feature_ro_compat |= EXT4_FEATURE_RO_COMPAT_QUOTA;
1162 ext2fs_mark_super_dirty(fs);
1163 } else if (!fs->super->s_usr_quota_inum &&
1164 !fs->super->s_grp_quota_inum) {
1165 fs->super->s_feature_ro_compat &= ~EXT4_FEATURE_RO_COMPAT_QUOTA;
1166 ext2fs_mark_super_dirty(fs);
1167 }
1168
1169 return;
1170 }
1171
1172 void parse_quota_opts(const char *opts)
1173 {
1174 char *buf, *token, *next, *p;
1175 int len;
1176
1177 len = strlen(opts);
1178 buf = malloc(len+1);
1179 if (!buf) {
1180 fputs(_("Couldn't allocate memory to parse quota "
1181 "options!\n"), stderr);
1182 exit(1);
1183 }
1184 strcpy(buf, opts);
1185 for (token = buf; token && *token; token = next) {
1186 p = strchr(token, ',');
1187 next = 0;
1188 if (p) {
1189 *p = 0;
1190 next = p+1;
1191 }
1192
1193 if (strcmp(token, "usrquota") == 0) {
1194 usrquota = QOPT_ENABLE;
1195 } else if (strcmp(token, "^usrquota") == 0) {
1196 usrquota = QOPT_DISABLE;
1197 } else if (strcmp(token, "grpquota") == 0) {
1198 grpquota = QOPT_ENABLE;
1199 } else if (strcmp(token, "^grpquota") == 0) {
1200 grpquota = QOPT_DISABLE;
1201 } else {
1202 fputs(_("\nBad quota options specified.\n\n"
1203 "Following valid quota options are available "
1204 "(pass by separating with comma):\n"
1205 "\t[^]usrquota\n"
1206 "\t[^]grpquota\n"
1207 "\n\n"), stderr);
1208 free(buf);
1209 exit(1);
1210 }
1211 }
1212 free(buf);
1213 }
1214
1215
1216
1217 static void parse_e2label_options(int argc, char ** argv)
1218 {
1219 if ((argc < 2) || (argc > 3)) {
1220 fputs(_("Usage: e2label device [newlabel]\n"), stderr);
1221 exit(1);
1222 }
1223 io_options = strchr(argv[1], '?');
1224 if (io_options)
1225 *io_options++ = 0;
1226 device_name = blkid_get_devname(NULL, argv[1], NULL);
1227 if (!device_name) {
1228 com_err("e2label", 0, _("Unable to resolve '%s'"),
1229 argv[1]);
1230 exit(1);
1231 }
1232 open_flag = EXT2_FLAG_JOURNAL_DEV_OK;
1233 if (argc == 3) {
1234 open_flag |= EXT2_FLAG_RW;
1235 L_flag = 1;
1236 new_label = argv[2];
1237 } else
1238 print_label++;
1239 }
1240
1241 static time_t parse_time(char *str)
1242 {
1243 struct tm ts;
1244
1245 if (strcmp(str, "now") == 0) {
1246 return (time(0));
1247 }
1248 memset(&ts, 0, sizeof(ts));
1249 #ifdef HAVE_STRPTIME
1250 strptime(str, "%Y%m%d%H%M%S", &ts);
1251 #else
1252 sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
1253 &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
1254 ts.tm_year -= 1900;
1255 ts.tm_mon -= 1;
1256 if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
1257 ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
1258 ts.tm_min > 59 || ts.tm_sec > 61)
1259 ts.tm_mday = 0;
1260 #endif
1261 if (ts.tm_mday == 0) {
1262 com_err(program_name, 0,
1263 _("Couldn't parse date/time specifier: %s"),
1264 str);
1265 usage();
1266 }
1267 ts.tm_isdst = -1;
1268 return (mktime(&ts));
1269 }
1270
1271 static void parse_tune2fs_options(int argc, char **argv)
1272 {
1273 int c;
1274 char *tmp;
1275 struct group *gr;
1276 struct passwd *pw;
1277
1278 open_flag = 0;
1279
1280 printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1281 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)
1282 switch (c) {
1283 case 'c':
1284 max_mount_count = strtol(optarg, &tmp, 0);
1285 if (*tmp || max_mount_count > 16000) {
1286 com_err(program_name, 0,
1287 _("bad mounts count - %s"),
1288 optarg);
1289 usage();
1290 }
1291 if (max_mount_count == 0)
1292 max_mount_count = -1;
1293 c_flag = 1;
1294 open_flag = EXT2_FLAG_RW;
1295 break;
1296 case 'C':
1297 mount_count = strtoul(optarg, &tmp, 0);
1298 if (*tmp || mount_count > 16000) {
1299 com_err(program_name, 0,
1300 _("bad mounts count - %s"),
1301 optarg);
1302 usage();
1303 }
1304 C_flag = 1;
1305 open_flag = EXT2_FLAG_RW;
1306 break;
1307 case 'e':
1308 if (strcmp(optarg, "continue") == 0)
1309 errors = EXT2_ERRORS_CONTINUE;
1310 else if (strcmp(optarg, "remount-ro") == 0)
1311 errors = EXT2_ERRORS_RO;
1312 else if (strcmp(optarg, "panic") == 0)
1313 errors = EXT2_ERRORS_PANIC;
1314 else {
1315 com_err(program_name, 0,
1316 _("bad error behavior - %s"),
1317 optarg);
1318 usage();
1319 }
1320 e_flag = 1;
1321 open_flag = EXT2_FLAG_RW;
1322 break;
1323 case 'E':
1324 extended_cmd = optarg;
1325 open_flag |= EXT2_FLAG_RW;
1326 break;
1327 case 'f': /* Force */
1328 f_flag = 1;
1329 break;
1330 case 'g':
1331 resgid = strtoul(optarg, &tmp, 0);
1332 if (*tmp) {
1333 gr = getgrnam(optarg);
1334 if (gr == NULL)
1335 tmp = optarg;
1336 else {
1337 resgid = gr->gr_gid;
1338 *tmp = 0;
1339 }
1340 }
1341 if (*tmp) {
1342 com_err(program_name, 0,
1343 _("bad gid/group name - %s"),
1344 optarg);
1345 usage();
1346 }
1347 g_flag = 1;
1348 open_flag = EXT2_FLAG_RW;
1349 break;
1350 case 'i':
1351 interval = strtoul(optarg, &tmp, 0);
1352 switch (*tmp) {
1353 case 's':
1354 tmp++;
1355 break;
1356 case '\0':
1357 case 'd':
1358 case 'D': /* days */
1359 interval *= 86400;
1360 if (*tmp != '\0')
1361 tmp++;
1362 break;
1363 case 'm':
1364 case 'M': /* months! */
1365 interval *= 86400 * 30;
1366 tmp++;
1367 break;
1368 case 'w':
1369 case 'W': /* weeks */
1370 interval *= 86400 * 7;
1371 tmp++;
1372 break;
1373 }
1374 if (*tmp) {
1375 com_err(program_name, 0,
1376 _("bad interval - %s"), optarg);
1377 usage();
1378 }
1379 i_flag = 1;
1380 open_flag = EXT2_FLAG_RW;
1381 break;
1382 case 'j':
1383 if (!journal_size)
1384 journal_size = -1;
1385 open_flag = EXT2_FLAG_RW;
1386 break;
1387 case 'J':
1388 parse_journal_opts(optarg);
1389 open_flag = EXT2_FLAG_RW;
1390 break;
1391 case 'l':
1392 l_flag = 1;
1393 break;
1394 case 'L':
1395 new_label = optarg;
1396 L_flag = 1;
1397 open_flag |= EXT2_FLAG_RW |
1398 EXT2_FLAG_JOURNAL_DEV_OK;
1399 break;
1400 case 'm':
1401 reserved_ratio = strtod(optarg, &tmp);
1402 if (*tmp || reserved_ratio > 50 ||
1403 reserved_ratio < 0) {
1404 com_err(program_name, 0,
1405 _("bad reserved block ratio - %s"),
1406 optarg);
1407 usage();
1408 }
1409 m_flag = 1;
1410 open_flag = EXT2_FLAG_RW;
1411 break;
1412 case 'M':
1413 new_last_mounted = optarg;
1414 M_flag = 1;
1415 open_flag = EXT2_FLAG_RW;
1416 break;
1417 case 'o':
1418 if (mntopts_cmd) {
1419 com_err(program_name, 0,
1420 _("-o may only be specified once"));
1421 usage();
1422 }
1423 mntopts_cmd = optarg;
1424 open_flag = EXT2_FLAG_RW;
1425 break;
1426 case 'O':
1427 if (features_cmd) {
1428 com_err(program_name, 0,
1429 _("-O may only be specified once"));
1430 usage();
1431 }
1432 features_cmd = optarg;
1433 open_flag = EXT2_FLAG_RW;
1434 break;
1435 case 'Q':
1436 Q_flag = 1;
1437 parse_quota_opts(optarg);
1438 open_flag = EXT2_FLAG_RW;
1439 break;
1440 case 'r':
1441 reserved_blocks = strtoul(optarg, &tmp, 0);
1442 if (*tmp) {
1443 com_err(program_name, 0,
1444 _("bad reserved blocks count - %s"),
1445 optarg);
1446 usage();
1447 }
1448 r_flag = 1;
1449 open_flag = EXT2_FLAG_RW;
1450 break;
1451 case 's': /* Deprecated */
1452 s_flag = atoi(optarg);
1453 open_flag = EXT2_FLAG_RW;
1454 break;
1455 case 'T':
1456 T_flag = 1;
1457 last_check_time = parse_time(optarg);
1458 open_flag = EXT2_FLAG_RW;
1459 break;
1460 case 'u':
1461 resuid = strtoul(optarg, &tmp, 0);
1462 if (*tmp) {
1463 pw = getpwnam(optarg);
1464 if (pw == NULL)
1465 tmp = optarg;
1466 else {
1467 resuid = pw->pw_uid;
1468 *tmp = 0;
1469 }
1470 }
1471 if (*tmp) {
1472 com_err(program_name, 0,
1473 _("bad uid/user name - %s"),
1474 optarg);
1475 usage();
1476 }
1477 u_flag = 1;
1478 open_flag = EXT2_FLAG_RW;
1479 break;
1480 case 'U':
1481 new_UUID = optarg;
1482 U_flag = 1;
1483 open_flag = EXT2_FLAG_RW |
1484 EXT2_FLAG_JOURNAL_DEV_OK;
1485 break;
1486 case 'I':
1487 new_inode_size = strtoul(optarg, &tmp, 0);
1488 if (*tmp) {
1489 com_err(program_name, 0,
1490 _("bad inode size - %s"),
1491 optarg);
1492 usage();
1493 }
1494 if (!((new_inode_size &
1495 (new_inode_size - 1)) == 0)) {
1496 com_err(program_name, 0,
1497 _("Inode size must be a "
1498 "power of two- %s"),
1499 optarg);
1500 usage();
1501 }
1502 open_flag = EXT2_FLAG_RW;
1503 I_flag = 1;
1504 break;
1505 default:
1506 usage();
1507 }
1508 if (optind < argc - 1 || optind == argc)
1509 usage();
1510 if (!open_flag && !l_flag)
1511 usage();
1512 io_options = strchr(argv[optind], '?');
1513 if (io_options)
1514 *io_options++ = 0;
1515 device_name = blkid_get_devname(NULL, argv[optind], NULL);
1516 if (!device_name) {
1517 com_err("tune2fs", 0, _("Unable to resolve '%s'"),
1518 argv[optind]);
1519 exit(1);
1520 }
1521 }
1522
1523 #ifdef CONFIG_BUILD_FINDFS
1524 void do_findfs(int argc, char **argv)
1525 {
1526 char *dev;
1527
1528 if ((argc != 2) ||
1529 (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5))) {
1530 fprintf(stderr, "Usage: findfs LABEL=<label>|UUID=<uuid>\n");
1531 exit(2);
1532 }
1533 dev = blkid_get_devname(NULL, argv[1], NULL);
1534 if (!dev) {
1535 com_err("findfs", 0, _("Unable to resolve '%s'"),
1536 argv[1]);
1537 exit(1);
1538 }
1539 puts(dev);
1540 exit(0);
1541 }
1542 #endif
1543
1544 static int parse_extended_opts(ext2_filsys fs, const char *opts)
1545 {
1546 char *buf, *token, *next, *p, *arg;
1547 int len, hash_alg;
1548 int r_usage = 0;
1549
1550 len = strlen(opts);
1551 buf = malloc(len+1);
1552 if (!buf) {
1553 fprintf(stderr,
1554 _("Couldn't allocate memory to parse options!\n"));
1555 return 1;
1556 }
1557 strcpy(buf, opts);
1558 for (token = buf; token && *token; token = next) {
1559 p = strchr(token, ',');
1560 next = 0;
1561 if (p) {
1562 *p = 0;
1563 next = p+1;
1564 }
1565 arg = strchr(token, '=');
1566 if (arg) {
1567 *arg = 0;
1568 arg++;
1569 }
1570 if (strcmp(token, "clear-mmp") == 0 ||
1571 strcmp(token, "clear_mmp") == 0) {
1572 clear_mmp = 1;
1573 } else if (strcmp(token, "mmp_update_interval") == 0) {
1574 unsigned long interval;
1575 if (!arg) {
1576 r_usage++;
1577 continue;
1578 }
1579 interval = strtoul(arg, &p, 0);
1580 if (*p) {
1581 fprintf(stderr,
1582 _("Invalid mmp_update_interval: %s\n"),
1583 arg);
1584 r_usage++;
1585 continue;
1586 }
1587 if (interval == 0) {
1588 interval = EXT4_MMP_UPDATE_INTERVAL;
1589 } else if (interval > EXT4_MMP_MAX_UPDATE_INTERVAL) {
1590 fprintf(stderr,
1591 _("mmp_update_interval too big: %lu\n"),
1592 interval);
1593 r_usage++;
1594 continue;
1595 }
1596 printf(P_("Setting multiple mount protection update "
1597 "interval to %lu second\n",
1598 "Setting multiple mount protection update "
1599 "interval to %lu seconds\n", interval),
1600 interval);
1601 fs->super->s_mmp_update_interval = interval;
1602 ext2fs_mark_super_dirty(fs);
1603 } else if (!strcmp(token, "test_fs")) {
1604 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1605 printf("Setting test filesystem flag\n");
1606 ext2fs_mark_super_dirty(fs);
1607 } else if (!strcmp(token, "^test_fs")) {
1608 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
1609 printf("Clearing test filesystem flag\n");
1610 ext2fs_mark_super_dirty(fs);
1611 } else if (strcmp(token, "stride") == 0) {
1612 if (!arg) {
1613 r_usage++;
1614 continue;
1615 }
1616 stride = strtoul(arg, &p, 0);
1617 if (*p) {
1618 fprintf(stderr,
1619 _("Invalid RAID stride: %s\n"),
1620 arg);
1621 r_usage++;
1622 continue;
1623 }
1624 stride_set = 1;
1625 } else if (strcmp(token, "stripe-width") == 0 ||
1626 strcmp(token, "stripe_width") == 0) {
1627 if (!arg) {
1628 r_usage++;
1629 continue;
1630 }
1631 stripe_width = strtoul(arg, &p, 0);
1632 if (*p) {
1633 fprintf(stderr,
1634 _("Invalid RAID stripe-width: %s\n"),
1635 arg);
1636 r_usage++;
1637 continue;
1638 }
1639 stripe_width_set = 1;
1640 } else if (strcmp(token, "hash_alg") == 0 ||
1641 strcmp(token, "hash-alg") == 0) {
1642 if (!arg) {
1643 r_usage++;
1644 continue;
1645 }
1646 hash_alg = e2p_string2hash(arg);
1647 if (hash_alg < 0) {
1648 fprintf(stderr,
1649 _("Invalid hash algorithm: %s\n"),
1650 arg);
1651 r_usage++;
1652 continue;
1653 }
1654 fs->super->s_def_hash_version = hash_alg;
1655 printf(_("Setting default hash algorithm "
1656 "to %s (%d)\n"),
1657 arg, hash_alg);
1658 ext2fs_mark_super_dirty(fs);
1659 } else if (!strcmp(token, "mount_opts")) {
1660 if (!arg) {
1661 r_usage++;
1662 continue;
1663 }
1664 if (strlen(arg) >= sizeof(fs->super->s_mount_opts)) {
1665 fprintf(stderr,
1666 "Extended mount options too long\n");
1667 continue;
1668 }
1669 ext_mount_opts = strdup(arg);
1670 } else
1671 r_usage++;
1672 }
1673 if (r_usage) {
1674 fprintf(stderr, _("\nBad options specified.\n\n"
1675 "Extended options are separated by commas, "
1676 "and may take an argument which\n"
1677 "\tis set off by an equals ('=') sign.\n\n"
1678 "Valid extended options are:\n"
1679 "\tclear_mmp\n"
1680 "\thash_alg=<hash algorithm>\n"
1681 "\tmount_opts=<extended default mount options>\n"
1682 "\tstride=<RAID per-disk chunk size in blocks>\n"
1683 "\tstripe_width=<RAID stride*data disks in blocks>\n"
1684 "\ttest_fs\n"
1685 "\t^test_fs\n"));
1686 free(buf);
1687 return 1;
1688 }
1689 free(buf);
1690
1691 return 0;
1692 }
1693
1694 /*
1695 * Fill in the block bitmap bmap with the information regarding the
1696 * blocks to be moved
1697 */
1698 static int get_move_bitmaps(ext2_filsys fs, int new_ino_blks_per_grp,
1699 ext2fs_block_bitmap bmap)
1700 {
1701 dgrp_t i;
1702 int retval;
1703 ext2_badblocks_list bb_list = 0;
1704 blk64_t j, needed_blocks = 0;
1705 blk64_t start_blk, end_blk;
1706
1707 retval = ext2fs_read_bb_inode(fs, &bb_list);
1708 if (retval)
1709 return retval;
1710
1711 for (i = 0; i < fs->group_desc_count; i++) {
1712 start_blk = ext2fs_inode_table_loc(fs, i) +
1713 fs->inode_blocks_per_group;
1714
1715 end_blk = ext2fs_inode_table_loc(fs, i) +
1716 new_ino_blks_per_grp;
1717
1718 for (j = start_blk; j < end_blk; j++) {
1719 if (ext2fs_test_block_bitmap2(fs->block_map, j)) {
1720 /*
1721 * IF the block is a bad block we fail
1722 */
1723 if (ext2fs_badblocks_list_test(bb_list, j)) {
1724 ext2fs_badblocks_list_free(bb_list);
1725 return ENOSPC;
1726 }
1727
1728 ext2fs_mark_block_bitmap2(bmap, j);
1729 } else {
1730 /*
1731 * We are going to use this block for
1732 * inode table. So mark them used.
1733 */
1734 ext2fs_mark_block_bitmap2(fs->block_map, j);
1735 }
1736 }
1737 needed_blocks += end_blk - start_blk;
1738 }
1739
1740 ext2fs_badblocks_list_free(bb_list);
1741 if (needed_blocks > ext2fs_free_blocks_count(fs->super))
1742 return ENOSPC;
1743
1744 return 0;
1745 }
1746
1747 static int ext2fs_is_meta_block(ext2_filsys fs, blk_t blk)
1748 {
1749 dgrp_t group;
1750 group = ext2fs_group_of_blk(fs, blk);
1751 if (ext2fs_block_bitmap_loc(fs, group) == blk)
1752 return 1;
1753 if (ext2fs_inode_bitmap_loc(fs, group) == blk)
1754 return 1;
1755 return 0;
1756 }
1757
1758 static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk_t blk)
1759 {
1760 blk_t start_blk, end_blk;
1761 start_blk = fs->super->s_first_data_block +
1762 EXT2_BLOCKS_PER_GROUP(fs->super) * group;
1763 /*
1764 * We cannot get new block beyond end_blk for for the last block group
1765 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
1766 */
1767 end_blk = start_blk + EXT2_BLOCKS_PER_GROUP(fs->super);
1768 if (blk >= start_blk && blk <= end_blk)
1769 return 1;
1770 return 0;
1771 }
1772
1773 static int move_block(ext2_filsys fs, ext2fs_block_bitmap bmap)
1774 {
1775
1776 char *buf;
1777 dgrp_t group = 0;
1778 errcode_t retval;
1779 int meta_data = 0;
1780 blk64_t blk, new_blk, goal;
1781 struct blk_move *bmv;
1782
1783 retval = ext2fs_get_mem(fs->blocksize, &buf);
1784 if (retval)
1785 return retval;
1786
1787 for (new_blk = blk = fs->super->s_first_data_block;
1788 blk < ext2fs_blocks_count(fs->super); blk++) {
1789 if (!ext2fs_test_block_bitmap2(bmap, blk))
1790 continue;
1791
1792 if (ext2fs_is_meta_block(fs, blk)) {
1793 /*
1794 * If the block is mapping a fs meta data block
1795 * like group desc/block bitmap/inode bitmap. We
1796 * should find a block in the same group and fix
1797 * the respective fs metadata pointers. Otherwise
1798 * fail
1799 */
1800 group = ext2fs_group_of_blk(fs, blk);
1801 goal = ext2fs_group_first_block2(fs, group);
1802 meta_data = 1;
1803
1804 } else {
1805 goal = new_blk;
1806 }
1807 retval = ext2fs_new_block2(fs, goal, NULL, &new_blk);
1808 if (retval)
1809 goto err_out;
1810
1811 /* new fs meta data block should be in the same group */
1812 if (meta_data && !ext2fs_is_block_in_group(fs, group, new_blk)) {
1813 retval = ENOSPC;
1814 goto err_out;
1815 }
1816
1817 /* Mark this block as allocated */
1818 ext2fs_mark_block_bitmap2(fs->block_map, new_blk);
1819
1820 /* Add it to block move list */
1821 retval = ext2fs_get_mem(sizeof(struct blk_move), &bmv);
1822 if (retval)
1823 goto err_out;
1824
1825 bmv->old_loc = blk;
1826 bmv->new_loc = new_blk;
1827
1828 list_add(&(bmv->list), &blk_move_list);
1829
1830 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1831 if (retval)
1832 goto err_out;
1833
1834 retval = io_channel_write_blk64(fs->io, new_blk, 1, buf);
1835 if (retval)
1836 goto err_out;
1837 }
1838
1839 err_out:
1840 ext2fs_free_mem(&buf);
1841 return retval;
1842 }
1843
1844 static blk64_t translate_block(blk64_t blk)
1845 {
1846 struct list_head *entry;
1847 struct blk_move *bmv;
1848
1849 list_for_each(entry, &blk_move_list) {
1850 bmv = list_entry(entry, struct blk_move, list);
1851 if (bmv->old_loc == blk)
1852 return bmv->new_loc;
1853 }
1854
1855 return 0;
1856 }
1857
1858 static int process_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1859 blk64_t *block_nr,
1860 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1861 blk64_t ref_block EXT2FS_ATTR((unused)),
1862 int ref_offset EXT2FS_ATTR((unused)),
1863 void *priv_data)
1864 {
1865 int ret = 0;
1866 blk64_t new_blk;
1867 ext2fs_block_bitmap bmap = (ext2fs_block_bitmap) priv_data;
1868
1869 if (!ext2fs_test_block_bitmap2(bmap, *block_nr))
1870 return 0;
1871 new_blk = translate_block(*block_nr);
1872 if (new_blk) {
1873 *block_nr = new_blk;
1874 /*
1875 * This will force the ext2fs_write_inode in the iterator
1876 */
1877 ret |= BLOCK_CHANGED;
1878 }
1879
1880 return ret;
1881 }
1882
1883 static int inode_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1884 {
1885 errcode_t retval = 0;
1886 ext2_ino_t ino;
1887 blk64_t blk;
1888 char *block_buf = 0;
1889 struct ext2_inode inode;
1890 ext2_inode_scan scan = NULL;
1891
1892 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1893 if (retval)
1894 return retval;
1895
1896 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1897 if (retval)
1898 goto err_out;
1899
1900 while (1) {
1901 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1902 if (retval)
1903 goto err_out;
1904
1905 if (!ino)
1906 break;
1907
1908 if (inode.i_links_count == 0)
1909 continue; /* inode not in use */
1910
1911 /* FIXME!!
1912 * If we end up modifying the journal inode
1913 * the sb->s_jnl_blocks will differ. But a
1914 * subsequent e2fsck fixes that.
1915 * Do we need to fix this ??
1916 */
1917
1918 if (ext2fs_file_acl_block(fs, &inode) &&
1919 ext2fs_test_block_bitmap2(bmap,
1920 ext2fs_file_acl_block(fs, &inode))) {
1921 blk = translate_block(ext2fs_file_acl_block(fs,
1922 &inode));
1923 if (!blk)
1924 continue;
1925
1926 ext2fs_file_acl_block_set(fs, &inode, blk);
1927
1928 /*
1929 * Write the inode to disk so that inode table
1930 * resizing can work
1931 */
1932 retval = ext2fs_write_inode(fs, ino, &inode);
1933 if (retval)
1934 goto err_out;
1935 }
1936
1937 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1938 continue;
1939
1940 retval = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1941 process_block, bmap);
1942 if (retval)
1943 goto err_out;
1944
1945 }
1946
1947 err_out:
1948 ext2fs_free_mem(&block_buf);
1949
1950 return retval;
1951 }
1952
1953 /*
1954 * We need to scan for inode and block bitmaps that may need to be
1955 * moved. This can take place if the filesystem was formatted for
1956 * RAID arrays using the mke2fs's extended option "stride".
1957 */
1958 static int group_desc_scan_and_fix(ext2_filsys fs, ext2fs_block_bitmap bmap)
1959 {
1960 dgrp_t i;
1961 blk_t blk, new_blk;
1962
1963 for (i = 0; i < fs->group_desc_count; i++) {
1964 blk = ext2fs_block_bitmap_loc(fs, i);
1965 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1966 new_blk = translate_block(blk);
1967 if (!new_blk)
1968 continue;
1969 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
1970 }
1971
1972 blk = ext2fs_inode_bitmap_loc(fs, i);
1973 if (ext2fs_test_block_bitmap2(bmap, blk)) {
1974 new_blk = translate_block(blk);
1975 if (!new_blk)
1976 continue;
1977 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
1978 }
1979 }
1980 return 0;
1981 }
1982
1983 static int expand_inode_table(ext2_filsys fs, unsigned long new_ino_size)
1984 {
1985 dgrp_t i;
1986 blk64_t blk;
1987 errcode_t retval;
1988 int new_ino_blks_per_grp;
1989 unsigned int j;
1990 char *old_itable = NULL, *new_itable = NULL;
1991 char *tmp_old_itable = NULL, *tmp_new_itable = NULL;
1992 unsigned long old_ino_size;
1993 int old_itable_size, new_itable_size;
1994
1995 old_itable_size = fs->inode_blocks_per_group * fs->blocksize;
1996 old_ino_size = EXT2_INODE_SIZE(fs->super);
1997
1998 new_ino_blks_per_grp = ext2fs_div_ceil(
1999 EXT2_INODES_PER_GROUP(fs->super) *
2000 new_ino_size,
2001 fs->blocksize);
2002
2003 new_itable_size = new_ino_blks_per_grp * fs->blocksize;
2004
2005 retval = ext2fs_get_mem(old_itable_size, &old_itable);
2006 if (retval)
2007 return retval;
2008
2009 retval = ext2fs_get_mem(new_itable_size, &new_itable);
2010 if (retval)
2011 goto err_out;
2012
2013 tmp_old_itable = old_itable;
2014 tmp_new_itable = new_itable;
2015
2016 for (i = 0; i < fs->group_desc_count; i++) {
2017 blk = ext2fs_inode_table_loc(fs, i);
2018 retval = io_channel_read_blk64(fs->io, blk,
2019 fs->inode_blocks_per_group, old_itable);
2020 if (retval)
2021 goto err_out;
2022
2023 for (j = 0; j < EXT2_INODES_PER_GROUP(fs->super); j++) {
2024 memcpy(new_itable, old_itable, old_ino_size);
2025
2026 memset(new_itable+old_ino_size, 0,
2027 new_ino_size - old_ino_size);
2028
2029 new_itable += new_ino_size;
2030 old_itable += old_ino_size;
2031 }
2032
2033 /* reset the pointer */
2034 old_itable = tmp_old_itable;
2035 new_itable = tmp_new_itable;
2036
2037 retval = io_channel_write_blk64(fs->io, blk,
2038 new_ino_blks_per_grp, new_itable);
2039 if (retval)
2040 goto err_out;
2041 }
2042
2043 /* Update the meta data */
2044 fs->inode_blocks_per_group = new_ino_blks_per_grp;
2045 fs->super->s_inode_size = new_ino_size;
2046
2047 err_out:
2048 if (old_itable)
2049 ext2fs_free_mem(&old_itable);
2050
2051 if (new_itable)
2052 ext2fs_free_mem(&new_itable);
2053
2054 return retval;
2055 }
2056
2057 static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
2058 {
2059 blk64_t blk;
2060 ext2_ino_t ino;
2061 unsigned int group = 0;
2062 unsigned int count = 0;
2063 int total_free = 0;
2064 int group_free = 0;
2065
2066 /*
2067 * First calculate the block statistics
2068 */
2069 for (blk = fs->super->s_first_data_block;
2070 blk < ext2fs_blocks_count(fs->super); blk++) {
2071 if (!ext2fs_fast_test_block_bitmap2(fs->block_map, blk)) {
2072 group_free++;
2073 total_free++;
2074 }
2075 count++;
2076 if ((count == fs->super->s_blocks_per_group) ||
2077 (blk == ext2fs_blocks_count(fs->super)-1)) {
2078 ext2fs_bg_free_blocks_count_set(fs, group++,
2079 group_free);
2080 count = 0;
2081 group_free = 0;
2082 }
2083 }
2084 total_free = EXT2FS_C2B(fs, total_free);
2085 ext2fs_free_blocks_count_set(fs->super, total_free);
2086
2087 /*
2088 * Next, calculate the inode statistics
2089 */
2090 group_free = 0;
2091 total_free = 0;
2092 count = 0;
2093 group = 0;
2094
2095 /* Protect loop from wrap-around if s_inodes_count maxed */
2096 for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
2097 if (!ext2fs_fast_test_inode_bitmap2(fs->inode_map, ino)) {
2098 group_free++;
2099 total_free++;
2100 }
2101 count++;
2102 if ((count == fs->super->s_inodes_per_group) ||
2103 (ino == fs->super->s_inodes_count)) {
2104 ext2fs_bg_free_inodes_count_set(fs, group++,
2105 group_free);
2106 count = 0;
2107 group_free = 0;
2108 }
2109 }
2110 fs->super->s_free_inodes_count = total_free;
2111 ext2fs_mark_super_dirty(fs);
2112 return 0;
2113 }
2114
2115 #define list_for_each_safe(pos, pnext, head) \
2116 for (pos = (head)->next, pnext = pos->next; pos != (head); \
2117 pos = pnext, pnext = pos->next)
2118
2119 static void free_blk_move_list(void)
2120 {
2121 struct list_head *entry, *tmp;
2122 struct blk_move *bmv;
2123
2124 list_for_each_safe(entry, tmp, &blk_move_list) {
2125 bmv = list_entry(entry, struct blk_move, list);
2126 list_del(entry);
2127 ext2fs_free_mem(&bmv);
2128 }
2129 return;
2130 }
2131
2132 static int resize_inode(ext2_filsys fs, unsigned long new_size)
2133 {
2134 errcode_t retval;
2135 int new_ino_blks_per_grp;
2136 ext2fs_block_bitmap bmap;
2137
2138 retval = ext2fs_read_inode_bitmap(fs);
2139 if (retval) {
2140 fputs(_("Failed to read inode bitmap\n"), stderr);
2141 return retval;
2142 }
2143 retval = ext2fs_read_block_bitmap(fs);
2144 if (retval) {
2145 fputs(_("Failed to read block bitmap\n"), stderr);
2146 return retval;
2147 }
2148 INIT_LIST_HEAD(&blk_move_list);
2149
2150
2151 new_ino_blks_per_grp = ext2fs_div_ceil(
2152 EXT2_INODES_PER_GROUP(fs->super)*
2153 new_size,
2154 fs->blocksize);
2155
2156 /* We may change the file system.
2157 * Mark the file system as invalid so that
2158 * the user is prompted to run fsck.
2159 */
2160 fs->super->s_state &= ~EXT2_VALID_FS;
2161
2162 retval = ext2fs_allocate_block_bitmap(fs, _("blocks to be moved"),
2163 &bmap);
2164 if (retval) {
2165 fputs(_("Failed to allocate block bitmap when "
2166 "increasing inode size\n"), stderr);
2167 return retval;
2168 }
2169 retval = get_move_bitmaps(fs, new_ino_blks_per_grp, bmap);
2170 if (retval) {
2171 fputs(_("Not enough space to increase inode size \n"), stderr);
2172 goto err_out;
2173 }
2174 retval = move_block(fs, bmap);
2175 if (retval) {
2176 fputs(_("Failed to relocate blocks during inode resize \n"),
2177 stderr);
2178 goto err_out;
2179 }
2180 retval = inode_scan_and_fix(fs, bmap);
2181 if (retval)
2182 goto err_out_undo;
2183
2184 retval = group_desc_scan_and_fix(fs, bmap);
2185 if (retval)
2186 goto err_out_undo;
2187
2188 retval = expand_inode_table(fs, new_size);
2189 if (retval)
2190 goto err_out_undo;
2191
2192 ext2fs_calculate_summary_stats(fs);
2193
2194 fs->super->s_state |= EXT2_VALID_FS;
2195 /* mark super block and block bitmap as dirty */
2196 ext2fs_mark_super_dirty(fs);
2197 ext2fs_mark_bb_dirty(fs);
2198
2199 err_out:
2200 free_blk_move_list();
2201 ext2fs_free_block_bitmap(bmap);
2202
2203 return retval;
2204
2205 err_out_undo:
2206 free_blk_move_list();
2207 ext2fs_free_block_bitmap(bmap);
2208 fputs(_("Error in resizing the inode size.\n"
2209 "Run e2undo to undo the "
2210 "file system changes. \n"), stderr);
2211
2212 return retval;
2213 }
2214
2215 static int tune2fs_setup_tdb(const char *name, io_manager *io_ptr)
2216 {
2217 errcode_t retval = 0;
2218 const char *tdb_dir;
2219 char *tdb_file;
2220 char *dev_name, *tmp_name;
2221
2222 #if 0 /* FIXME!! */
2223 /*
2224 * Configuration via a conf file would be
2225 * nice
2226 */
2227 profile_get_string(profile, "scratch_files",
2228 "directory", 0, 0,
2229 &tdb_dir);
2230 #endif
2231 tmp_name = strdup(name);
2232 if (!tmp_name) {
2233 alloc_fn_fail:
2234 com_err(program_name, ENOMEM,
2235 _("Couldn't allocate memory for tdb filename\n"));
2236 return ENOMEM;
2237 }
2238 dev_name = basename(tmp_name);
2239
2240 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2241 if (!tdb_dir)
2242 tdb_dir = "/var/lib/e2fsprogs";
2243
2244 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2245 access(tdb_dir, W_OK))
2246 return 0;
2247
2248 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
2249 if (!tdb_file)
2250 goto alloc_fn_fail;
2251 sprintf(tdb_file, "%s/tune2fs-%s.e2undo", tdb_dir, dev_name);
2252
2253 if (!access(tdb_file, F_OK)) {
2254 if (unlink(tdb_file) < 0) {
2255 retval = errno;
2256 com_err(program_name, retval,
2257 _("while trying to delete %s"),
2258 tdb_file);
2259 free(tdb_file);
2260 return retval;
2261 }
2262 }
2263
2264 set_undo_io_backing_manager(*io_ptr);
2265 *io_ptr = undo_io_manager;
2266 set_undo_io_backup_file(tdb_file);
2267 printf(_("To undo the tune2fs operation please run "
2268 "the command\n e2undo %s %s\n\n"),
2269 tdb_file, name);
2270 free(tdb_file);
2271 free(tmp_name);
2272 return retval;
2273 }
2274
2275 int main(int argc, char **argv)
2276 {
2277 errcode_t retval;
2278 ext2_filsys fs;
2279 struct ext2_super_block *sb;
2280 io_manager io_ptr, io_ptr_orig = NULL;
2281 int rc = 0;
2282
2283 #ifdef ENABLE_NLS
2284 setlocale(LC_MESSAGES, "");
2285 setlocale(LC_CTYPE, "");
2286 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2287 textdomain(NLS_CAT_NAME);
2288 set_com_err_gettext(gettext);
2289 #endif
2290 if (argc && *argv)
2291 program_name = *argv;
2292 add_error_table(&et_ext2_error_table);
2293
2294 #ifdef CONFIG_BUILD_FINDFS
2295 if (strcmp(get_progname(argv[0]), "findfs") == 0)
2296 do_findfs(argc, argv);
2297 #endif
2298 if (strcmp(get_progname(argv[0]), "e2label") == 0)
2299 parse_e2label_options(argc, argv);
2300 else
2301 parse_tune2fs_options(argc, argv);
2302
2303 #ifdef CONFIG_TESTIO_DEBUG
2304 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_DEBUG")) {
2305 io_ptr = test_io_manager;
2306 test_io_backing_manager = unix_io_manager;
2307 } else
2308 #endif
2309 io_ptr = unix_io_manager;
2310
2311 retry_open:
2312 if ((open_flag & EXT2_FLAG_RW) == 0 || f_flag)
2313 open_flag |= EXT2_FLAG_SKIP_MMP;
2314
2315 open_flag |= EXT2_FLAG_64BITS;
2316
2317 /* keep the filesystem struct around to dump MMP data */
2318 open_flag |= EXT2_FLAG_NOFREE_ON_ERROR;
2319
2320 retval = ext2fs_open2(device_name, io_options, open_flag,
2321 0, 0, io_ptr, &fs);
2322 if (retval) {
2323 com_err(program_name, retval,
2324 _("while trying to open %s"),
2325 device_name);
2326 if (retval == EXT2_ET_MMP_FSCK_ON ||
2327 retval == EXT2_ET_MMP_UNKNOWN_SEQ)
2328 dump_mmp_msg(fs->mmp_buf,
2329 _("If you are sure the filesystem "
2330 "is not in use on any node, run:\n"
2331 "'tune2fs -f -E clear_mmp {device}'\n"));
2332 else if (retval == EXT2_ET_MMP_FAILED)
2333 dump_mmp_msg(fs->mmp_buf, NULL);
2334 else if (retval == EXT2_ET_MMP_MAGIC_INVALID)
2335 fprintf(stderr,
2336 _("MMP block magic is bad. Try to fix it by "
2337 "running:\n'e2fsck -f %s'\n"), device_name);
2338 else if (retval != EXT2_ET_MMP_FAILED)
2339 fprintf(stderr,
2340 _("Couldn't find valid filesystem superblock.\n"));
2341
2342 ext2fs_free(fs);
2343 exit(1);
2344 }
2345 fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
2346
2347 if (I_flag && !io_ptr_orig) {
2348 /*
2349 * Check the inode size is right so we can issue an
2350 * error message and bail before setting up the tdb
2351 * file.
2352 */
2353 if (new_inode_size == EXT2_INODE_SIZE(fs->super)) {
2354 fprintf(stderr, _("The inode size is already %lu\n"),
2355 new_inode_size);
2356 rc = 1;
2357 goto closefs;
2358 }
2359 if (new_inode_size < EXT2_INODE_SIZE(fs->super)) {
2360 fprintf(stderr, _("Shrinking the inode size is "
2361 "not supported\n"));
2362 rc = 1;
2363 goto closefs;
2364 }
2365
2366 /*
2367 * If inode resize is requested use the
2368 * Undo I/O manager
2369 */
2370 io_ptr_orig = io_ptr;
2371 retval = tune2fs_setup_tdb(device_name, &io_ptr);
2372 if (retval) {
2373 rc = 1;
2374 goto closefs;
2375 }
2376 if (io_ptr != io_ptr_orig) {
2377 ext2fs_close(fs);
2378 goto retry_open;
2379 }
2380 }
2381
2382 sb = fs->super;
2383 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2384
2385 if (print_label) {
2386 /* For e2label emulation */
2387 printf("%.*s\n", (int) sizeof(sb->s_volume_name),
2388 sb->s_volume_name);
2389 remove_error_table(&et_ext2_error_table);
2390 goto closefs;
2391 }
2392
2393 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
2394 if (retval) {
2395 com_err("ext2fs_check_if_mount", retval,
2396 _("while determining whether %s is mounted."),
2397 device_name);
2398 rc = 1;
2399 goto closefs;
2400 }
2401 /* Normally we only need to write out the superblock */
2402 fs->flags |= EXT2_FLAG_SUPER_ONLY;
2403
2404 if (c_flag) {
2405 sb->s_max_mnt_count = max_mount_count;
2406 ext2fs_mark_super_dirty(fs);
2407 printf(_("Setting maximal mount count to %d\n"),
2408 max_mount_count);
2409 }
2410 if (C_flag) {
2411 sb->s_mnt_count = mount_count;
2412 ext2fs_mark_super_dirty(fs);
2413 printf(_("Setting current mount count to %d\n"), mount_count);
2414 }
2415 if (e_flag) {
2416 sb->s_errors = errors;
2417 ext2fs_mark_super_dirty(fs);
2418 printf(_("Setting error behavior to %d\n"), errors);
2419 }
2420 if (g_flag) {
2421 sb->s_def_resgid = resgid;
2422 ext2fs_mark_super_dirty(fs);
2423 printf(_("Setting reserved blocks gid to %lu\n"), resgid);
2424 }
2425 if (i_flag) {
2426 if (interval >= (1ULL << 32)) {
2427 com_err(program_name, 0,
2428 _("interval between checks is too big (%lu)"),
2429 interval);
2430 rc = 1;
2431 goto closefs;
2432 }
2433 sb->s_checkinterval = interval;
2434 ext2fs_mark_super_dirty(fs);
2435 printf(_("Setting interval between checks to %lu seconds\n"),
2436 interval);
2437 }
2438 if (m_flag) {
2439 ext2fs_r_blocks_count_set(sb, reserved_ratio *
2440 ext2fs_blocks_count(sb) / 100.0);
2441 ext2fs_mark_super_dirty(fs);
2442 printf (_("Setting reserved blocks percentage to %g%% (%llu blocks)\n"),
2443 reserved_ratio, ext2fs_r_blocks_count(sb));
2444 }
2445 if (r_flag) {
2446 if (reserved_blocks > ext2fs_blocks_count(sb)/2) {
2447 com_err(program_name, 0,
2448 _("reserved blocks count is too big (%llu)"),
2449 reserved_blocks);
2450 rc = 1;
2451 goto closefs;
2452 }
2453 ext2fs_r_blocks_count_set(sb, reserved_blocks);
2454 ext2fs_mark_super_dirty(fs);
2455 printf(_("Setting reserved blocks count to %llu\n"),
2456 reserved_blocks);
2457 }
2458 if (s_flag == 1) {
2459 if (sb->s_feature_ro_compat &
2460 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
2461 fputs(_("\nThe filesystem already has sparse "
2462 "superblocks.\n"), stderr);
2463 else {
2464 sb->s_feature_ro_compat |=
2465 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2466 sb->s_state &= ~EXT2_VALID_FS;
2467 ext2fs_mark_super_dirty(fs);
2468 printf(_("\nSparse superblock flag set. %s"),
2469 _(please_fsck));
2470 }
2471 }
2472 if (s_flag == 0) {
2473 fputs(_("\nClearing the sparse superflag not supported.\n"),
2474 stderr);
2475 rc = 1;
2476 goto closefs;
2477 }
2478 if (T_flag) {
2479 sb->s_lastcheck = last_check_time;
2480 ext2fs_mark_super_dirty(fs);
2481 printf(_("Setting time filesystem last checked to %s\n"),
2482 ctime(&last_check_time));
2483 }
2484 if (u_flag) {
2485 sb->s_def_resuid = resuid;
2486 ext2fs_mark_super_dirty(fs);
2487 printf(_("Setting reserved blocks uid to %lu\n"), resuid);
2488 }
2489 if (L_flag) {
2490 if (strlen(new_label) > sizeof(sb->s_volume_name))
2491 fputs(_("Warning: label too long, truncating.\n"),
2492 stderr);
2493 memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
2494 strncpy(sb->s_volume_name, new_label,
2495 sizeof(sb->s_volume_name));
2496 ext2fs_mark_super_dirty(fs);
2497 }
2498 if (M_flag) {
2499 memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
2500 strncpy(sb->s_last_mounted, new_last_mounted,
2501 sizeof(sb->s_last_mounted));
2502 ext2fs_mark_super_dirty(fs);
2503 }
2504 if (mntopts_cmd) {
2505 rc = update_mntopts(fs, mntopts_cmd);
2506 if (rc)
2507 goto closefs;
2508 }
2509 if (features_cmd) {
2510 rc = update_feature_set(fs, features_cmd);
2511 if (rc)
2512 goto closefs;
2513 }
2514 if (extended_cmd) {
2515 rc = parse_extended_opts(fs, extended_cmd);
2516 if (rc)
2517 goto closefs;
2518 if (clear_mmp && !f_flag) {
2519 fputs(_("Error in using clear_mmp. "
2520 "It must be used with -f\n"),
2521 stderr);
2522 goto closefs;
2523 }
2524 }
2525 if (clear_mmp) {
2526 rc = ext2fs_mmp_clear(fs);
2527 goto closefs;
2528 }
2529 if (journal_size || journal_device) {
2530 rc = add_journal(fs);
2531 if (rc)
2532 goto closefs;
2533 }
2534
2535 if (Q_flag) {
2536 if (mount_flags & EXT2_MF_MOUNTED) {
2537 fputs(_("The quota feature may only be changed when "
2538 "the filesystem is unmounted.\n"), stderr);
2539 rc = 1;
2540 goto closefs;
2541 }
2542 handle_quota_options(fs);
2543 }
2544
2545 if (U_flag) {
2546 int set_csum = 0;
2547 dgrp_t i;
2548
2549 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2550 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
2551 /*
2552 * Changing the UUID requires rewriting all metadata,
2553 * which can race with a mounted fs. Don't allow that.
2554 */
2555 if (mount_flags & EXT2_MF_MOUNTED) {
2556 fputs(_("The UUID may only be "
2557 "changed when the filesystem is "
2558 "unmounted.\n"), stderr);
2559 exit(1);
2560 }
2561
2562 if (check_fsck_needed(fs))
2563 exit(1);
2564 }
2565
2566 if (ext2fs_has_group_desc_csum(fs)) {
2567 /*
2568 * Determine if the block group checksums are
2569 * correct so we know whether or not to set
2570 * them later on.
2571 */
2572 for (i = 0; i < fs->group_desc_count; i++)
2573 if (!ext2fs_group_desc_csum_verify(fs, i))
2574 break;
2575 if (i >= fs->group_desc_count)
2576 set_csum = 1;
2577 }
2578 if ((strcasecmp(new_UUID, "null") == 0) ||
2579 (strcasecmp(new_UUID, "clear") == 0)) {
2580 uuid_clear(sb->s_uuid);
2581 } else if (strcasecmp(new_UUID, "time") == 0) {
2582 uuid_generate_time(sb->s_uuid);
2583 } else if (strcasecmp(new_UUID, "random") == 0) {
2584 uuid_generate(sb->s_uuid);
2585 } else if (uuid_parse(new_UUID, sb->s_uuid)) {
2586 com_err(program_name, 0, _("Invalid UUID format\n"));
2587 rc = 1;
2588 goto closefs;
2589 }
2590 ext2fs_init_csum_seed(fs);
2591 if (set_csum) {
2592 for (i = 0; i < fs->group_desc_count; i++)
2593 ext2fs_group_desc_csum_set(fs, i);
2594 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2595 }
2596 ext2fs_mark_super_dirty(fs);
2597 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
2598 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2599 rewrite_checksums = 1;
2600 }
2601 if (rewrite_checksums)
2602 rewrite_metadata_checksums(fs);
2603 if (I_flag) {
2604 if (mount_flags & EXT2_MF_MOUNTED) {
2605 fputs(_("The inode size may only be "
2606 "changed when the filesystem is "
2607 "unmounted.\n"), stderr);
2608 rc = 1;
2609 goto closefs;
2610 }
2611 if (fs->super->s_feature_incompat &
2612 EXT4_FEATURE_INCOMPAT_FLEX_BG) {
2613 fputs(_("Changing the inode size not supported for "
2614 "filesystems with the flex_bg\n"
2615 "feature enabled.\n"),
2616 stderr);
2617 rc = 1;
2618 goto closefs;
2619 }
2620 /*
2621 * We want to update group descriptor also
2622 * with the new free inode count
2623 */
2624 fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
2625 if (resize_inode(fs, new_inode_size) == 0) {
2626 printf(_("Setting inode size %lu\n"),
2627 new_inode_size);
2628 } else {
2629 printf(_("Failed to change inode size\n"));
2630 rc = 1;
2631 goto closefs;
2632 }
2633 }
2634
2635 if (l_flag)
2636 list_super(sb);
2637 if (stride_set) {
2638 sb->s_raid_stride = stride;
2639 ext2fs_mark_super_dirty(fs);
2640 printf(_("Setting stride size to %d\n"), stride);
2641 }
2642 if (stripe_width_set) {
2643 sb->s_raid_stripe_width = stripe_width;
2644 ext2fs_mark_super_dirty(fs);
2645 printf(_("Setting stripe width to %d\n"), stripe_width);
2646 }
2647 if (ext_mount_opts) {
2648 strncpy((char *)(fs->super->s_mount_opts), ext_mount_opts,
2649 sizeof(fs->super->s_mount_opts));
2650 fs->super->s_mount_opts[sizeof(fs->super->s_mount_opts)-1] = 0;
2651 ext2fs_mark_super_dirty(fs);
2652 printf(_("Setting extended default mount options to '%s'\n"),
2653 ext_mount_opts);
2654 free(ext_mount_opts);
2655 }
2656 free(device_name);
2657 remove_error_table(&et_ext2_error_table);
2658
2659 closefs:
2660 if (rc) {
2661 ext2fs_mmp_stop(fs);
2662 exit(1);
2663 }
2664
2665 return (ext2fs_close(fs) ? 1 : 0);
2666 }