]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - resize/online.c
e2fsck,resize2fs: fix gcc -Wall nits
[thirdparty/e2fsprogs.git] / resize / online.c
index d73b9f6a3ccb8d3e1f4a0fb42f65166382206ade..9a4d828c2b292469f9500bc8f1f845198802bba2 100644 (file)
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include "resize2fs.h"
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 
 extern char *program_name;
 
+#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
+
+#ifdef __linux__
+static int parse_version_number(const char *s)
+{
+       int     major, minor, rev;
+       char    *endptr;
+       const char *cp = s;
+
+       if (!s)
+               return 0;
+       major = strtol(cp, &endptr, 10);
+       if (cp == endptr || *endptr != '.')
+               return 0;
+       cp = endptr + 1;
+       minor = strtol(cp, &endptr, 10);
+       if (cp == endptr || *endptr != '.')
+               return 0;
+       cp = endptr + 1;
+       rev = strtol(cp, &endptr, 10);
+       if (cp == endptr)
+               return 0;
+       return ((((major * 256) + minor) * 256) + rev);
+}
+
+#define VERSION_CODE(a,b,c) (((a) << 16) + ((b) << 8) + (c))
+
+#endif
+
 errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
-                          blk_t *new_size, int flags EXT2FS_ATTR((unused)))
+                          blk64_t *new_size, int flags EXT2FS_ATTR((unused)))
 {
 #ifdef __linux__
        struct ext2_new_group_input input;
@@ -33,13 +63,31 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
        blk_t                   size;
        int                     fd, overhead;
        int                     use_old_ioctl = 1;
+       int                     no_meta_bg_resize = 0;
+       int                     no_resize_ioctl = 0;
+
+       if (getenv("RESIZE2FS_KERNEL_VERSION")) {
+               char *version_to_emulate = getenv("RESIZE2FS_KERNEL_VERSION");
+               int kvers = parse_version_number(version_to_emulate);
+
+               if (kvers < VERSION_CODE(3, 7, 0))
+                       no_meta_bg_resize = 1;
+               if (kvers < VERSION_CODE(3, 3, 0))
+                       no_resize_ioctl = 1;
+       }
+
+       if (ext2fs_has_feature_sparse_super2(fs->super) &&
+           (access("/sys/fs/ext4/features/sparse_super2", R_OK) != 0)) {
+               com_err(program_name, 0, _("kernel does not support online "
+                                          "resize with sparse_super2"));
+               exit(1);
+       }
 
        printf(_("Filesystem at %s is mounted on %s; "
                 "on-line resizing required\n"), fs->device_name, mtpt);
 
        if (*new_size < ext2fs_blocks_count(sb)) {
-               printf(_("On-line shrinking from %llu to %u not supported.\n"),
-                      ext2fs_blocks_count(sb), *new_size);
+               com_err(program_name, 0, _("On-line shrinking not supported"));
                exit(1);
        }
 
@@ -48,18 +96,39 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
         * the on-line resizing inode must be present.
         */
        new_desc_blocks = ext2fs_div_ceil(
-               ext2fs_div_ceil(*new_size -
-                               fs->super->s_first_data_block,
-                               EXT2_BLOCKS_PER_GROUP(fs->super)),
+               ext2fs_div64_ceil(*new_size -
+                                 fs->super->s_first_data_block,
+                                 EXT2_BLOCKS_PER_GROUP(fs->super)),
                EXT2_DESC_PER_BLOCK(fs->super));
-       printf("old desc_blocks = %lu, new_desc_blocks = %lu\n",
+       printf("old_desc_blocks = %lu, new_desc_blocks = %lu\n",
               fs->desc_blocks, new_desc_blocks);
-       if (!(fs->super->s_feature_compat &
-             EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
-           new_desc_blocks != fs->desc_blocks) {
-               com_err(program_name, 0,
-                       _("Filesystem does not support online resizing"));
-               exit(1);
+
+       /*
+        * Do error checking to make sure the resize will be successful.
+        */
+       if ((access("/sys/fs/ext4/features/meta_bg_resize", R_OK) != 0) ||
+           no_meta_bg_resize) {
+               if (!ext2fs_has_feature_resize_inode(fs->super) &&
+                   (new_desc_blocks != fs->desc_blocks)) {
+                       com_err(program_name, 0,
+                               _("Filesystem does not support online resizing"));
+                       exit(1);
+               }
+
+               if (ext2fs_has_feature_resize_inode(fs->super) &&
+                   new_desc_blocks > (fs->desc_blocks +
+                                      fs->super->s_reserved_gdt_blocks)) {
+                       com_err(program_name, 0,
+                               _("Not enough reserved gdt blocks for resizing"));
+                       exit(1);
+               }
+
+               if ((ext2fs_blocks_count(sb) > MAX_32_NUM) ||
+                   (*new_size > MAX_32_NUM)) {
+                       com_err(program_name, 0,
+                               _("Kernel does not support resizing a file system this large"));
+                       exit(1);
+               }
        }
 
        fd = open(mtpt, O_RDONLY);
@@ -69,7 +138,40 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
                exit(1);
        }
 
-       size=ext2fs_blocks_count(sb);
+       if (no_resize_ioctl) {
+               printf(_("Old resize interface requested.\n"));
+       } else if (ioctl(fd, EXT4_IOC_RESIZE_FS, new_size)) {
+               /*
+                * If kernel does not support EXT4_IOC_RESIZE_FS, use the
+                * old online resize. Note that the old approach does not
+                * handle >32 bit file systems
+                *
+                * Sigh, if we are running a 32-bit binary on a 64-bit
+                * kernel (which happens all the time on the MIPS
+                * architecture in Debian, but can happen on other CPU
+                * architectures as well) we will get EINVAL returned
+                * when an ioctl doesn't exist, at least up to Linux
+                * 3.1.  See compat_sys_ioctl() in fs/compat_ioctl.c
+                * in the kernel sources.  This is probably a kernel
+                * bug, but work around it here.
+                */
+               if ((errno != ENOTTY) && (errno != EINVAL)) {
+                       if (errno == EPERM)
+                               com_err(program_name, 0,
+                               _("Permission denied to resize filesystem"));
+                       else
+                               com_err(program_name, errno,
+                               _("While checking for on-line resizing "
+                                 "support"));
+                       exit(1);
+               }
+       } else {
+               close(fd);
+               return 0;
+       }
+
+       size = ext2fs_blocks_count(sb);
+
        if (ioctl(fd, EXT2_IOC_GROUP_EXTEND, &size)) {
                if (errno == EPERM)
                        com_err(program_name, 0,
@@ -87,12 +189,16 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
                ext2fs_blocks_count(sb);
 
        retval = ext2fs_read_bitmaps(fs);
-       if (retval)
+       if (retval) {
+               close(fd);
                return retval;
+       }
 
        retval = ext2fs_dup_handle(fs, &new_fs);
-       if (retval)
+       if (retval) {
+               close(fd);
                return retval;
+       }
 
        /* The current method of adding one block group at a time to a
         * mounted filesystem means it is impossible to accomodate the
@@ -104,12 +210,14 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
         * the layout advantages of flex_bg in the new block groups,
         * but at least it allows on-line resizing to function.
         */
-       new_fs->super->s_feature_incompat &= ~EXT4_FEATURE_INCOMPAT_FLEX_BG;
+       ext2fs_clear_feature_flex_bg(new_fs->super);
        retval = adjust_fs_info(new_fs, fs, 0, *new_size);
-       if (retval)
+       if (retval) {
+               close(fd);
                return retval;
+       }
 
-       printf(_("Performing an on-line resize of %s to %u (%dk) blocks.\n"),
+       printf(_("Performing an on-line resize of %s to %llu (%dk) blocks.\n"),
               fs->device_name, *new_size, fs->blocksize / 1024);
 
        size = fs->group_desc_count * sb->s_blocks_per_group +
@@ -136,12 +244,7 @@ errcode_t online_resize_fs(ext2_filsys fs, const char *mtpt,
                input.block_bitmap = ext2fs_block_bitmap_loc(new_fs, i);
                input.inode_bitmap = ext2fs_inode_bitmap_loc(new_fs, i);
                input.inode_table = ext2fs_inode_table_loc(new_fs, i);
-               input.blocks_count = sb->s_blocks_per_group;
-               if (i == new_fs->group_desc_count-1) {
-                       input.blocks_count = ext2fs_blocks_count(new_fs->super) -
-                               sb->s_first_data_block -
-                               (i * sb->s_blocks_per_group);
-               }
+               input.blocks_count = ext2fs_group_blocks_count(new_fs, i);
                input.reserved_blocks = (blk_t) (percent * input.blocks_count
                                                 / 100.0);