]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - resize/resize2fs.c
resize2fs: update checksums in the extent tree's relocated block
[thirdparty/e2fsprogs.git] / resize / resize2fs.c
index d0a525b20ded62fb159b3f019b30feaa8ed3ee2b..38032e5c310964d6376c38b130a0159fc28fc30f 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 1997, 1998 by Theodore Ts'o and
  *     PowerQuest, Inc.
  *
- * Copyright (C) 1999, 2000 by Theosore Ts'o
+ * Copyright (C) 1999, 2000 by Theodore Ts'o
  *
  * %Begin-Header%
  * This file may be redistributed under the terms of the GNU Public
@@ -73,14 +73,14 @@ static inline int is_inode_bm(ext2_filsys fs, unsigned int grp, blk64_t blk)
        return blk == ext2fs_inode_bitmap_loc(fs, grp);
 }
 
-static inline int is_inode_tb(ext2_filsys fs, unsigned int grp, blk64_t blk)
+static int is_inode_tb(ext2_filsys fs, unsigned int grp, blk64_t blk)
 {
        return blk >= ext2fs_inode_table_loc(fs, grp) &&
               blk < (ext2fs_inode_table_loc(fs, grp) +
                      fs->inode_blocks_per_group);
 }
 
-/* Some bigalloc helper macros which are more succint... */
+/* Some bigalloc helper macros which are more succinct... */
 #define B2C(x) EXT2FS_B2C(fs, (x))
 #define C2B(x) EXT2FS_C2B(fs, (x))
 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
@@ -206,6 +206,10 @@ errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
                goto errout;
        print_resource_track(rfs, &rtrack, fs->io);
 
+       retval = clear_sparse_super2_last_group(rfs);
+       if (retval)
+               goto errout;
+
        init_resource_track(&rtrack, "calculate_summary_stats", fs->io);
        retval = ext2fs_calculate_summary_stats(rfs->new_fs);
        if (retval)
@@ -224,10 +228,6 @@ errcode_t resize_fs(ext2_filsys fs, blk64_t *new_size, int flags,
                goto errout;
        print_resource_track(rfs, &rtrack, fs->io);
 
-       retval = clear_sparse_super2_last_group(rfs);
-       if (retval)
-               goto errout;
-
        retval = ext2fs_set_gdt_csum(rfs->new_fs);
        if (retval)
                goto errout;
@@ -329,8 +329,8 @@ static errcode_t resize_group_descriptors(ext2_resize_t rfs, blk64_t new_size)
                copy_size = EXT2_DESC_SIZE(rfs->new_fs->super);
        for (i = 0; i < rfs->old_fs->group_desc_count; i++) {
                memcpy(n, o, copy_size);
-               n += EXT2_DESC_SIZE(rfs->new_fs->super);
-               o += EXT2_DESC_SIZE(rfs->old_fs->super);
+               n = (char *)n + EXT2_DESC_SIZE(rfs->new_fs->super);
+               o = (char *)o + EXT2_DESC_SIZE(rfs->old_fs->super);
        }
 
        ext2fs_free_mem(&rfs->new_fs->group_desc);
@@ -521,7 +521,7 @@ static errcode_t zero_high_bits_in_inodes(ext2_resize_t rfs)
        if (!(rfs->flags & (RESIZE_DISABLE_64BIT | RESIZE_ENABLE_64BIT)))
                return 0;
 
-       if (fs->super->s_creator_os != EXT2_OS_LINUX)
+       if (fs->super->s_creator_os == EXT2_OS_HURD)
                return 0;
 
        retval = ext2fs_open_inode_scan(fs, 0, &scan);
@@ -578,13 +578,13 @@ out:
 }
 
 /*
- * Clean up the bitmaps for unitialized bitmaps
+ * Clean up the bitmaps for uninitialized bitmaps
  */
 static void fix_uninit_block_bitmaps(ext2_filsys fs)
 {
        blk64_t         blk, lblk;
        dgrp_t          g;
-       int             i;
+       unsigned int    i;
 
        if (!ext2fs_has_group_desc_csum(fs))
                return;
@@ -604,7 +604,7 @@ static void fix_uninit_block_bitmaps(ext2_filsys fs)
                ext2fs_mark_block_bitmap2(fs->block_map,
                                          ext2fs_inode_bitmap_loc(fs, g));
                for (i = 0, blk = ext2fs_inode_table_loc(fs, g);
-                    i < (unsigned int) fs->inode_blocks_per_group;
+                    i < fs->inode_blocks_per_group;
                     i++, blk++)
                        ext2fs_mark_block_bitmap2(fs->block_map, blk);
        }
@@ -631,12 +631,12 @@ static errcode_t free_gdp_blocks(ext2_filsys fs,
                                 ext2_filsys old_fs,
                                 dgrp_t group)
 {
-       blk64_t blk;
-       int     j;
-       dgrp_t  i;
+       blk64_t         blk;
+       unsigned int    j;
+       dgrp_t          i;
        ext2fs_block_bitmap bg_map = NULL;
-       errcode_t retval = 0;
-       dgrp_t count = old_fs->group_desc_count - fs->group_desc_count;
+       errcode_t       retval = 0;
+       dgrp_t          count = old_fs->group_desc_count - fs->group_desc_count;
 
        /* If bigalloc, don't free metadata living in the same cluster */
        if (EXT2FS_CLUSTER_RATIO(fs) > 1) {
@@ -698,7 +698,7 @@ errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs,
        blk64_t         old_numblocks, numblocks, adjblocks;
        unsigned long   i, j, old_desc_blocks;
        unsigned int    meta_bg, meta_bg_size;
-       int             has_super, csum_flag;
+       int             has_super, csum_flag, has_bg;
        unsigned long long new_inodes;  /* u64 to check for overflow */
        double          percent;
 
@@ -721,7 +721,19 @@ retry:
         */
        overhead = (int) (2 + fs->inode_blocks_per_group);
 
-       if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
+       has_bg = 0;
+       if (ext2fs_has_feature_sparse_super2(fs->super)) {
+               /*
+                * We have to do this manually since
+                * super->s_backup_bgs hasn't been set up yet.
+                */
+               if (fs->group_desc_count == 2)
+                       has_bg = fs->super->s_backup_bgs[0] != 0;
+               else
+                       has_bg = fs->super->s_backup_bgs[1] != 0;
+       } else
+               has_bg = ext2fs_bg_has_super(fs, fs->group_desc_count - 1);
+       if (has_bg)
                overhead += 1 + fs->desc_blocks +
                        fs->super->s_reserved_gdt_blocks;
 
@@ -744,7 +756,7 @@ retry:
         */
        new_inodes =(unsigned long long) fs->super->s_inodes_per_group * fs->group_desc_count;
        if (new_inodes > ~0U) {
-               fprintf(stderr, _("inodes (%llu) must be less than %u"),
+               fprintf(stderr, _("inodes (%llu) must be less than %u\n"),
                                   new_inodes, ~0U);
                return EXT2_ET_TOO_MANY_INODES;
        }
@@ -843,10 +855,9 @@ retry:
                if (last_bg > old_last_bg) {
                        if (old_fs->group_desc_count == 1)
                                fs->super->s_backup_bgs[0] = 1;
-                       if (old_fs->group_desc_count == 1 &&
-                           fs->super->s_backup_bgs[0])
-                               fs->super->s_backup_bgs[0] = last_bg;
-                       else if (fs->super->s_backup_bgs[1])
+                       if ((old_fs->group_desc_count < 3 &&
+                            fs->group_desc_count > 2) ||
+                           fs->super->s_backup_bgs[1])
                                fs->super->s_backup_bgs[1] = last_bg;
                } else if (last_bg < old_last_bg) {
                        if (fs->super->s_backup_bgs[0] > last_bg)
@@ -909,8 +920,9 @@ retry:
        group_block = ext2fs_group_first_block2(fs,
                                                old_fs->group_desc_count);
        csum_flag = ext2fs_has_group_desc_csum(fs);
-       if (!getenv("RESIZE2FS_FORCE_ITABLE_INIT") &&
-           access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0)
+       if (getenv("RESIZE2FS_FORCE_LAZY_ITABLE_INIT") ||
+           (!getenv("RESIZE2FS_FORCE_ITABLE_INIT") &&
+            access("/sys/fs/ext4/features/lazy_itable_init", F_OK) == 0))
                lazy_itable_init = 1;
        if (ext2fs_has_feature_meta_bg(fs->super))
                old_desc_blocks = fs->super->s_first_meta_bg;
@@ -921,7 +933,7 @@ retry:
        /*
         * If we changed the number of block_group descriptor blocks,
         * we need to make sure they are all marked as reserved in the
-        * file systems's block allocation map.
+        * filesystem's block allocation map.
         */
        for (i = 0; i < old_fs->group_desc_count; i++)
                ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
@@ -1245,7 +1257,8 @@ static void mark_fs_metablock(ext2_resize_t rfs,
  */
 static errcode_t blocks_to_move(ext2_resize_t rfs)
 {
-       int             j, has_super;
+       unsigned int    j;
+       int             has_super;
        dgrp_t          i, max_groups, g;
        blk64_t         blk, group_blk;
        blk64_t         old_blocks, new_blocks, group_end, cluster_freed;
@@ -1491,7 +1504,7 @@ static errcode_t blocks_to_move(ext2_resize_t rfs)
 
                /*
                 * For those structures that have changed, we need to
-                * do bookkeepping.
+                * do bookkeeping.
                 */
                if (ext2fs_block_bitmap_loc(old_fs, i) !=
                    (blk = ext2fs_block_bitmap_loc(fs, i))) {
@@ -1622,6 +1635,7 @@ static errcode_t resize2fs_get_alloc_block(ext2_filsys fs,
 {
        ext2_resize_t rfs = (ext2_resize_t) fs->priv_data;
        blk64_t blk;
+       int group;
 
        blk = get_new_block(rfs);
        if (!blk)
@@ -1634,6 +1648,12 @@ static errcode_t resize2fs_get_alloc_block(ext2_filsys fs,
 
        ext2fs_mark_block_bitmap2(rfs->old_fs->block_map, blk);
        ext2fs_mark_block_bitmap2(rfs->new_fs->block_map, blk);
+
+       group = ext2fs_group_of_blk2(rfs->old_fs, blk);
+       ext2fs_clear_block_uninit(rfs->old_fs, group);
+       group = ext2fs_group_of_blk2(rfs->new_fs, blk);
+       ext2fs_clear_block_uninit(rfs->new_fs, group);
+
        *ret = (blk64_t) blk;
        return 0;
 }
@@ -1644,8 +1664,7 @@ static errcode_t block_mover(ext2_resize_t rfs)
        ext2_filsys             fs = rfs->new_fs;
        ext2_filsys             old_fs = rfs->old_fs;
        errcode_t               retval;
-       __u64                   size;
-       int                     c;
+       __u64                   c, size;
        int                     to_move, moved;
        ext2_badblocks_list     badblock_list = 0;
        int                     bb_modified = 0;
@@ -1877,7 +1896,7 @@ static errcode_t migrate_ea_block(ext2_resize_t rfs, ext2_ino_t ino,
        errcode_t err = 0;
 
        /* No EA block or no remapping?  Quit early. */
-       if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 && !rfs->bmap)
+       if (ext2fs_file_acl_block(rfs->old_fs, inode) == 0 || !rfs->bmap)
                return 0;
        new_block = extent_translate(rfs->old_fs, rfs->bmap,
                ext2fs_file_acl_block(rfs->old_fs, inode));
@@ -1968,6 +1987,146 @@ static void quiet_com_err_proc(const char *whoami EXT2FS_ATTR((unused)),
 {
 }
 
+static int fix_ea_entries(ext2_extent imap, struct ext2_ext_attr_entry *entry,
+                         struct ext2_ext_attr_entry *end, ext2_ino_t last_ino)
+{
+       int modified = 0;
+       ext2_ino_t new_ino;
+
+       while (entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry)) {
+               if (entry->e_value_inum > last_ino) {
+                       new_ino = ext2fs_extent_translate(imap,
+                                                         entry->e_value_inum);
+                       entry->e_value_inum = new_ino;
+                       modified = 1;
+               }
+               entry = EXT2_EXT_ATTR_NEXT(entry);
+       }
+       return modified;
+}
+
+static int fix_ea_ibody_entries(ext2_extent imap,
+                               struct ext2_inode_large *inode, int inode_size,
+                               ext2_ino_t last_ino)
+{
+       struct ext2_ext_attr_entry *start, *end;
+       __u32 *ea_magic;
+
+       if (inode->i_extra_isize == 0)
+               return 0;
+
+       ea_magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
+                               inode->i_extra_isize);
+       if (*ea_magic != EXT2_EXT_ATTR_MAGIC)
+               return 0;
+
+       start = (struct ext2_ext_attr_entry *)(ea_magic + 1);
+       end = (struct ext2_ext_attr_entry *)((char *)inode + inode_size);
+
+       return fix_ea_entries(imap, start, end, last_ino);
+}
+
+static int fix_ea_block_entries(ext2_extent imap, char *block_buf,
+                               unsigned int blocksize, ext2_ino_t last_ino)
+{
+       struct ext2_ext_attr_header *header;
+       struct ext2_ext_attr_entry *start, *end;
+
+       header = (struct ext2_ext_attr_header *)block_buf;
+       start = (struct ext2_ext_attr_entry *)(header+1);
+       end = (struct ext2_ext_attr_entry *)(block_buf + blocksize);
+
+       return fix_ea_entries(imap, start, end, last_ino);
+}
+
+/* A simple LRU cache to check recently processed blocks. */
+struct blk_cache {
+       int cursor;
+       blk64_t blks[4];
+};
+
+#define BLK_IN_CACHE(b,c) ((b) == (c).blks[0] || (b) == (c).blks[1] || \
+                          (b) == (c).blks[2] || (b) == (c).blks[3])
+#define BLK_ADD_CACHE(b,c) {                   \
+       (c).blks[(c).cursor] = (b);             \
+       (c).cursor = ((c).cursor + 1) % 4;      \
+}
+
+static errcode_t fix_ea_inode_refs(ext2_resize_t rfs, struct ext2_inode *inode,
+                                  char *block_buf, ext2_ino_t last_ino)
+{
+       ext2_filsys     fs = rfs->new_fs;
+       ext2_inode_scan scan = NULL;
+       ext2_ino_t      ino;
+       int             inode_size = EXT2_INODE_SIZE(fs->super);
+       blk64_t         blk;
+       int             modified;
+       struct blk_cache blk_cache;
+       struct ext2_ext_attr_header *header;
+       errcode_t               retval;
+
+       memset(&blk_cache, 0, sizeof(blk_cache));
+
+       header = (struct ext2_ext_attr_header *)block_buf;
+
+       retval = ext2fs_open_inode_scan(fs, 0, &scan);
+       if (retval)
+               goto out;
+
+       while (1) {
+               retval = ext2fs_get_next_inode_full(scan, &ino, inode,
+                                                   inode_size);
+               if (retval)
+                       goto out;
+               if (!ino)
+                       break;
+
+               if (inode->i_links_count == 0 && ino != EXT2_RESIZE_INO)
+                       continue; /* inode not in use */
+
+               if (inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
+                       modified = fix_ea_ibody_entries(rfs->imap,
+                                       (struct ext2_inode_large *)inode,
+                                       inode_size, last_ino);
+                       if (modified) {
+                               retval = ext2fs_write_inode_full(fs, ino, inode,
+                                                                inode_size);
+                               if (retval)
+                                       goto out;
+                       }
+               }
+
+               blk = ext2fs_file_acl_block(fs, inode);
+               if (blk && !BLK_IN_CACHE(blk, blk_cache)) {
+                       retval = ext2fs_read_ext_attr3(fs, blk, block_buf, ino);
+                       if (retval)
+                               goto out;
+
+                       modified = fix_ea_block_entries(rfs->imap, block_buf,
+                                                       fs->blocksize,
+                                                       last_ino);
+                       if (modified) {
+                               retval = ext2fs_write_ext_attr3(fs, blk,
+                                                               block_buf, ino);
+                               if (retval)
+                                       goto out;
+                               /*
+                                * If refcount is greater than 1, we might see
+                                * the same block referenced by other inodes
+                                * later.
+                                */
+                               if (header->h_refcount > 1)
+                                       BLK_ADD_CACHE(blk, blk_cache);
+                       }
+               }
+       }
+       retval = 0;
+out:
+       if (scan)
+               ext2fs_close_inode_scan(scan);
+       return retval;
+
+}
 static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
 {
        struct process_block_struct     pb;
@@ -1978,6 +2137,7 @@ static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
        char                    *block_buf = 0;
        ext2_ino_t              start_to_move;
        int                     inode_size;
+       int                     update_ea_inode_refs = 0;
 
        if ((rfs->old_fs->group_desc_count <=
             rfs->new_fs->group_desc_count) &&
@@ -2050,7 +2210,15 @@ static errcode_t inode_scan_and_fix(ext2_resize_t rfs)
 
                ext2fs_inode_alloc_stats2(rfs->new_fs, new_inode, +1,
                                          pb.is_dir);
-               inode->i_ctime = time(0);
+               /*
+                * i_ctime field in xattr inodes contain a portion of the ref
+                * count, do not overwrite.
+                */
+               if (inode->i_flags & EXT4_EA_INODE_FL)
+                       update_ea_inode_refs = 1;
+               else
+                       inode->i_ctime = time(0);
+
                retval = ext2fs_write_inode_full(rfs->old_fs, new_inode,
                                                inode, inode_size);
                if (retval)
@@ -2076,31 +2244,20 @@ remap_blocks:
                if (retval)
                        goto errout;
 
-               /* Rewrite extent block checksums with new inode number */
-               if (ext2fs_has_feature_metadata_csum(rfs->old_fs->super) &&
-                   (inode->i_flags & EXT4_EXTENTS_FL)) {
-                       rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
-                       retval = rewrite_extents(rfs->old_fs, new_inode);
-                       rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
-                       if (retval)
-                               goto errout;
-               }
-
                /*
                 * Update inodes to point to new blocks; schedule directory
                 * blocks for inode remapping.  Need to write out dir blocks
                 * with new inode numbers if we have metadata_csum enabled.
                 */
+               rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
                if (ext2fs_inode_has_valid_blocks2(rfs->old_fs, inode) &&
                    (rfs->bmap || pb.is_dir)) {
                        pb.ino = new_inode;
                        pb.old_ino = ino;
                        pb.has_extents = inode->i_flags & EXT4_EXTENTS_FL;
-                       rfs->old_fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
                        retval = ext2fs_block_iterate3(rfs->old_fs,
                                                       new_inode, 0, block_buf,
                                                       process_block, &pb);
-                       rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
                        if (retval)
                                goto errout;
                        if (pb.error) {
@@ -2115,11 +2272,28 @@ remap_blocks:
                        if (retval)
                                goto errout;
                }
+
+               /* Fix up extent block checksums with the new inode number */
+               if (ext2fs_has_feature_metadata_csum(rfs->old_fs->super) &&
+                   (inode->i_flags & EXT4_EXTENTS_FL)) {
+                       retval = rewrite_extents(rfs->old_fs, new_inode);
+                       if (retval)
+                               goto errout;
+               }
+       }
+
+       if (update_ea_inode_refs &&
+           ext2fs_has_feature_ea_inode(rfs->new_fs->super)) {
+               retval = fix_ea_inode_refs(rfs, inode, block_buf,
+                                          start_to_move);
+               if (retval)
+                       goto errout;
        }
        io_channel_flush(rfs->old_fs->io);
 
 errout:
        reset_com_err_hook();
+       rfs->old_fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
        if (rfs->bmap) {
                ext2fs_free_extent_table(rfs->bmap);
                rfs->bmap = 0;
@@ -2280,7 +2454,8 @@ static errcode_t move_itables(ext2_resize_t rfs)
        char            *cp;
        blk64_t         old_blk, new_blk, blk, cluster_freed;
        errcode_t       retval;
-       int             j, to_move, moved;
+       int             to_move, moved;
+       unsigned int    j;
        ext2fs_block_bitmap     new_bmap = NULL;
 
        max_groups = fs->group_desc_count;
@@ -2459,6 +2634,9 @@ static errcode_t clear_sparse_super2_last_group(ext2_resize_t rfs)
            fs->super->s_backup_bgs[1] == old_last_bg)
                return 0;
 
+       if (old_last_bg == 0)
+               return 0;
+
        retval = ext2fs_super_and_bgd_loc2(rfs->old_fs, old_last_bg,
                                           &sb, &old_desc, NULL, &num);
        if (retval)
@@ -2761,7 +2939,7 @@ static int calc_group_overhead(ext2_filsys fs, blk64_t grp,
 
 
 /*
- * calcluate the minimum number of blocks the given fs can be resized to
+ * calculate the minimum number of blocks the given fs can be resized to
  */
 blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
 {
@@ -2854,7 +3032,7 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
 #endif
 
        /*
-        * if we need more group descriptors in order to accomodate our data
+        * if we need more group descriptors in order to accommodate our data
         * then we need to add them here
         */
        blks_needed = data_needed;